一、前言
今天做 188 数码管,查看了网上的一些例程,但是没有好多用stm32做的,要么就是vip文章,难受,我自己做出了之后,还是发给大家做个参考学习
坑:STM32直接配置成输出模式不可以,需要先配置成输入再挨个配置输出,因为这个问题卡了一上午
二、简介
数码管是一种常用的电子显示设备,它可以显示数字和一些基本的字符。在您提到的"188数码管"这一表述中,并没有特定的型号或标准定义,这里我将对数码管的基本概念和常见类型进行介绍。
数码管主要由发光二极管(LED)组成,根据段数的不同,通常分为七段数码管和八段数码管。七段数码管由七个LED组成,可以显示数字0到9以及一些字母。八段数码管比七段数码管多一个LED,主要用于显示小数点。
以下是一些关于数码管的关键信息:
- 结构:数码管包含若干个LED,每个LED代表数码管的一个“段”。通过控制这些段的亮与灭,可以组合成不同的数字或字符。
- 类型:
- 七段数码管:能够显示0-9的数字和一些基本的字符(如A-F)。
- 八段数码管:在七段的基础上增加了一个小数点显示功能。
- 连接方式:
- 显示方式:
- 静态显示:每个数码管的每个段都由单独的I/O端口控制,显示稳定,但需要的I/O端口较多。
- 动态显示:通过快速切换显示不同的数码管,由于人眼的视觉暂留效应,看起来像是在同时显示多个数字。
- 应用:数码管广泛应用于各种电子设备中,如计算器、电子钟表、仪表板等,用于显示数字或简单的字符信息。
三、资料获取
关注微信公众号--星之援工作室 发送关键字(188数码管)
使用串口进行控制 提供主要代码 开源,可自行移植
????
四、设备使用
实现效果
stm32驱动188
参考
188数码管驱动程序(简洁)https://blog.csdn.net/weixin_42401119/article/details/120704663?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522a25b647e866ee69cb65cd1ec72c0edf8%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=a25b647e866ee69cb65cd1ec72c0edf8&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-120704663-null-null.142^v102^pc_search_result_base9&utm_term=188%E6%95%B0%E7%A0%81%E7%AE%A1%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F&spm=1018.2226.3001.4187
接线
PA1-PA5 以此类推
四、代码编写
有两种写法,推荐第一种
方法一
188.c
函数实现过程
#include "stm32f10x.h"
#include "delay.h"
#include "188.h"
u8 hundreds = 4; //百位,0不显示,1仅百分比,2百分比+供电,3-百分比+百位,4全部显示
u8 tens = 4; //十位,10-F,11-不显示
u8 unit = 2; //个位,10-F,11-不显示
/*
* 百位,百分比,充电指示显示
* BIT3 BIT2 BIT1 BIT0
* B1 C1 I J
* 0-不显示 0 0 0 0 0x00
* 1-仅百分比 0 0 0 1 0x01
* 2-百分比+充电 0 0 1 1 0x03
* 3-百分比+百位 1 1 0 1 0x0D
* 4-全部显示 1 1 1 1 0x0F
*/
u8 num_hund[5] = {0x00, 0x01, 0x03, 0x0D, 0x0F};
/*
* 十位显示和个位显示
* BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0
* xx A2/3 B2/3 C2/3 D2/3 E2/3 F2/3 G2/3
* 0 1 1 1 1 1 1 0 0x7E
* 1 0 1 1 0 0 0 0 0x60
* 2 1 1 0 1 1 0 1 0x6D
* 3 1 1 1 1 0 0 1 0x79
* 4 0 1 1 0 0 1 1 0x33
* 5 1 0 1 1 0 1 1 0x5B
* 6 1 0 1 1 1 1 1 0x5F
* 7 1 1 1 0 0 0 0 0x70
* 8 1 1 1 1 1 1 1 0x7F
* 9 1 1 1 1 0 1 1 0x7B
* F 1 0 0 0 1 1 1 0x47
* 不显示 0 0 0 0 0 0 0 0x00
*/
u8 nums[12] = {0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, 0x7F, 0x7B, 0x47, 0x00};
void led_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin =GPIO_PIN1|GPIO_PIN2 |GPIO_PIN3|GPIO_PIN4|GPIO_PIN5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
}
void clear()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN1|GPIO_PIN2 |GPIO_PIN3|GPIO_PIN4|GPIO_PIN5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
}
void PIN1_H(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
GPIO_SetBits(GPIOX, GPIO_PIN1);
}
void PIN1_L(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
GPIO_ResetBits(GPIOX, GPIO_PIN1);
}
void PIN2_H(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
GPIO_SetBits(GPIOX, GPIO_PIN2);
}
void PIN2_L(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
GPIO_ResetBits(GPIOX, GPIO_PIN2);
}
void PIN3_H(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
GPIO_SetBits(GPIOX, GPIO_PIN3);
}
void PIN3_L(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
GPIO_ResetBits(GPIOX, GPIO_PIN3);
}
void PIN4_H(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
GPIO_SetBits(GPIOX, GPIO_PIN4);
}
void PIN4_L(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
GPIO_ResetBits(GPIOX, GPIO_PIN4);
}
void PIN5_H(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
GPIO_SetBits(GPIOX, GPIO_PIN5);
}
void PIN5_L(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
GPIO_ResetBits(GPIOX, GPIO_PIN5);
}
/*
* | 1 | 2 | 3 | 4 | 5 拉高
* 1 | | B3 | D3 | F3 | G3
* 2 | A3 | | B2 | D2 | E2
* 3 | C3 | A2 | | C2 | F2
* 4 | E3 | C1 | B1 | | G2
* 5 | | I | J | |
* 拉低
*/
//1脚拉低,可显示B3 D3 F3 G3
void Display_Scan1(void)
{
PIN1_L(); //拉低Pin1
if(nums[unit] & BIT5) //B3
PIN2_H();
if(nums[unit] & BIT3) //D3
PIN3_H();
if(nums[unit] & BIT1) //F3
PIN4_H();
if(nums[unit] & BIT0) //G3
PIN5_H();
}
//2脚拉低,可显示A3 B2 D2 E2
void Display_Scan2(void)
{
PIN2_L();
if(nums[unit] & BIT6) //A3
PIN1_H();
if(nums[tens] & BIT5) //B2
PIN3_H();
if(nums[tens] & BIT3) //D2
PIN4_H();
if(nums[tens] & BIT2) //E2
PIN5_H();
}
//3脚拉低,可显示C3 A2 C2 F2
void Display_Scan3(void)
{
PIN3_L();
if(nums[unit] & BIT4) //C3
PIN1_H();
if(nums[tens] & BIT6) //A2
PIN2_H();
if(nums[tens] & BIT4) //C2
PIN4_H();
if(nums[tens] & BIT1) //F2
PIN5_H();
}
//4脚拉低,可显示E3 C1 B1 G2
void Display_Scan4(void)
{
PIN4_L();
if(nums[unit] & BIT2) //E3
PIN1_H();
if(num_hund[hundreds] & BIT2) //C1
PIN2_H();
if(num_hund[hundreds] & BIT3) //B1
PIN3_H();
if(nums[tens] & BIT0) //G2
PIN5_H();
}
//5脚拉低,可显示 J(百分比) I(充电)
void Display_Scan5(void)
{
PIN5_L();
if(num_hund[hundreds] & BIT1) //I
PIN3_H();
if(num_hund[hundreds] & BIT0) //J
PIN2_H();
}
void Display(int sum)
{
static u8 scan_cnt;//逐行扫描
clear();//消影作用
unit=sum%10;
tens=sum/10%10;
hundreds=sum/100%10;
if(hundreds > 0)
{
hundreds = 4; // 显示数据+图标
}else{
hundreds = 0; // 显示数据
}
switch(scan_cnt)
{
case 0:Display_Scan1();scan_cnt++;break;
case 1:Display_Scan2();scan_cnt++;break;
case 2:Display_Scan3();scan_cnt++;break;
case 3:Display_Scan4();scan_cnt++;break;
case 4:Display_Scan5();scan_cnt=0;break;
default:scan_cnt=0;break;
}
}
188.h
函数定义
#ifndef __LED_H
#define __LED_H
#include "stm32f10x.h"
#define BIT0 0x01
#define BIT1 0x02
#define BIT2 0x04
#define BIT3 0x08
#define BIT4 0x10
#define BIT5 0x20
#define BIT6 0x40
#define BIT7 0x80
#define GPIOX GPIOA
#define GPIO_PIN1 GPIO_Pin_1
#define GPIO_PIN2 GPIO_Pin_2
#define GPIO_PIN3 GPIO_Pin_3
#define GPIO_PIN4 GPIO_Pin_4
#define GPIO_PIN5 GPIO_Pin_5
//#define PIN1_H() GPIO_SetBits(GPIOX, GPIO_PIN1)
//#define PIN1_L() GPIO_ResetBits(GPIOX, GPIO_PIN1)
//#define PIN2_H() GPIO_SetBits(GPIOX, GPIO_PIN2)
//#define PIN2_L() GPIO_ResetBits(GPIOX, GPIO_PIN2)
//#define PIN3_H() GPIO_SetBits(GPIOX, GPIO_PIN3)
//#define PIN3_L() GPIO_ResetBits(GPIOX, GPIO_PIN3)
//#define PIN4_H() GPIO_SetBits(GPIOX, GPIO_PIN4)
//#define PIN4_L() GPIO_ResetBits(GPIOX, GPIO_PIN4)
//
//#define PIN5_H() GPIO_SetBits(GPIOX, GPIO_PIN5)
//#define PIN5_L() GPIO_ResetBits(GPIOX, GPIO_PIN5)
void led_init(void);
void Display_tube(int sum);
void DIGITRON_ShowUnitSegCore( short highPinNum, short lowPinNum );
void clear(void);
void DIGITRON_ShowUnitSeg( short unit, char seg );
void Display_Scan1(void);
void Display_Scan2(void);
void Display_Scan3(void);
void Display_Scan4(void);
void Display_Scan5(void);
void Display(int sum);
void PIN1_H(void);
void PIN2_H(void);
void PIN3_H(void);
void PIN4_H(void);
void PIN5_H(void);
void PIN1_L(void);
void PIN2_L(void);
void PIN3_L(void);
void PIN4_L(void);
void PIN5_L(void);
#endif
方法二
188.c
函数实现过程
#include "stm32f10x.h"
#include "delay.h"
#include "188.h"
short GV_DigitronNumsMapSegs[10] = {0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xE6};
// 初始
void led_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin =GPIO_PIN1|GPIO_PIN2 |GPIO_PIN3|GPIO_PIN4|GPIO_PIN5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
}
// 重置
void clear()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN1|GPIO_PIN2 |GPIO_PIN3|GPIO_PIN4|GPIO_PIN5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
LED1_OFF;
LED2_OFF;
LED3_OFF;
LED4_OFF;
LED5_OFF;
}
void DIGITRON_ShowUnitSegCore( short highPinNum, short lowPinNum )
{
// 重置
clear();
// 初始化为推挽输出
switch(highPinNum)
{
case 1:
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_PIN1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure);
break;}
case 2:{
GPIO_InitTypeDef GPIO_InitStructure1;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure1.GPIO_Pin = GPIO_PIN2;
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure1);
break;}
case 3:{
GPIO_InitTypeDef GPIO_InitStructure2;
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure2.GPIO_Pin = GPIO_PIN3;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure2);
break;}
case 4:{
GPIO_InitTypeDef GPIO_InitStructure3;
GPIO_InitStructure3.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure3.GPIO_Pin = GPIO_PIN4;
GPIO_InitStructure3.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure3);
break;}
case 5:{
GPIO_InitTypeDef GPIO_InitStructure4;
GPIO_InitStructure4.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure4.GPIO_Pin = GPIO_PIN5;
GPIO_InitStructure4.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure4);
break;}
default: break;
}
switch(lowPinNum)
{ case 1:{
GPIO_InitTypeDef GPIO_InitStructure6;
GPIO_InitStructure6.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure6.GPIO_Pin = GPIO_PIN1;
GPIO_InitStructure6.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure6);
break;}
case 2:{
GPIO_InitTypeDef GPIO_InitStructure7;
GPIO_InitStructure7.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure7.GPIO_Pin = GPIO_PIN2;
GPIO_InitStructure7.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure7);
break;}
case 3:{
GPIO_InitTypeDef GPIO_InitStructure8;
GPIO_InitStructure8.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure8.GPIO_Pin = GPIO_PIN3;
GPIO_InitStructure8.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure8);
break;}
case 4:{
GPIO_InitTypeDef GPIO_InitStructure9;
GPIO_InitStructure9.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure9.GPIO_Pin = GPIO_PIN4;
GPIO_InitStructure9.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructure9);
break;}
case 5:{
GPIO_InitTypeDef GPIO_InitStructurea;
GPIO_InitStructurea.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructurea.GPIO_Pin = GPIO_PIN5;
GPIO_InitStructurea.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOX, &GPIO_InitStructurea);
break;}
default: break;
}
// 设置高低电平
switch(lowPinNum)
{
case 1: LED1_ON;
break;
case 2: LED2_ON;
break;
case 3: LED3_ON;
break;
case 4: LED4_ON;
break;
case 5: LED5_ON;
break;
default: break;
}
switch(highPinNum)
{
case 1: LED1_OFF;
break;
case 2: LED2_OFF;
break;
case 3: LED3_OFF;
break;
case 4: LED4_OFF;
break;
case 5: LED5_OFF;
break;
default: break;
}
delay_us(60);
}
void DIGITRON_ShowUnitSeg( short unit, char seg )
{
if( unit == 1 ){
if( seg == 'B' ) DIGITRON_ShowUnitSegCore(4,3);
if( seg == 'C' ) DIGITRON_ShowUnitSegCore(4,2);
}
if( unit == 2 ){
if( seg == 'A' ) DIGITRON_ShowUnitSegCore(3,2);
if( seg == 'B' ) DIGITRON_ShowUnitSegCore(2,3);
if( seg == 'C' ) DIGITRON_ShowUnitSegCore(3,4);
if( seg == 'D' ) DIGITRON_ShowUnitSegCore(2,4);
if( seg == 'E' ) DIGITRON_ShowUnitSegCore(2,5);
if( seg == 'F' ) DIGITRON_ShowUnitSegCore(3,5);
if( seg == 'G' ) DIGITRON_ShowUnitSegCore(4,5);
}
if( unit == 3 ){
if( seg == 'A' ) DIGITRON_ShowUnitSegCore(2,1);
if( seg == 'B' ) DIGITRON_ShowUnitSegCore(1,2);
if( seg == 'C' ) DIGITRON_ShowUnitSegCore(3,1);
if( seg == 'D' ) DIGITRON_ShowUnitSegCore(1,3);
if( seg == 'E' ) DIGITRON_ShowUnitSegCore(4,1);
if( seg == 'F' ) DIGITRON_ShowUnitSegCore(1,4);
if( seg == 'G' ) DIGITRON_ShowUnitSegCore(1,5);
}
}
void DIGITRON_ShowUnitNum( short unit, short number)
{
// 初始化
short numSegs = GV_DigitronNumsMapSegs[number];
// 显示小数点
// 显示数字
if( (numSegs&0x80) == 0x80 ) DIGITRON_ShowUnitSeg(unit, 'A'); // & 1000 0000
if( (numSegs&0x40) == 0x40 ) DIGITRON_ShowUnitSeg(unit, 'B'); // & 0100 0000
if( (numSegs&0x20) == 0x20 ) DIGITRON_ShowUnitSeg(unit, 'C'); // & 0010 0000
if( (numSegs&0x10) == 0x10 ) DIGITRON_ShowUnitSeg(unit, 'D'); // & 0001 0000
if( (numSegs&0x08) == 0x08 ) DIGITRON_ShowUnitSeg(unit, 'F'); // & 0000 0100
if( (numSegs&0x02) == 0x02 ) DIGITRON_ShowUnitSeg(unit, 'E'); // & 0000 1000
if( (numSegs&0x04) == 0x04 ) DIGITRON_ShowUnitSeg(unit, 'G'); // & 0000 0010
}
void Display_tube(int sum)
{
int a,b,c;
clear();
c=sum%10;
b=sum/10%10;
a=sum/100%10;
if(a==1)
{DIGITRON_ShowUnitNum( 1, a);
}
else
{
}
DIGITRON_ShowUnitNum( 2, b);
DIGITRON_ShowUnitNum( 3, c);
}
188.h
函数定义
#ifndef __LED_H
#define __LED_H
#include "stm32f10x.h"
#define GPIOX GPIOA
#define GPIO_PIN1 GPIO_Pin_1
#define GPIO_PIN2 GPIO_Pin_2
#define GPIO_PIN3 GPIO_Pin_3
#define GPIO_PIN4 GPIO_Pin_4
#define GPIO_PIN5 GPIO_Pin_5
#define LED1_ON GPIO_SetBits(GPIOX, GPIO_PIN1)
#define LED1_OFF GPIO_ResetBits(GPIOX, GPIO_PIN1)
#define LED2_ON GPIO_SetBits(GPIOX, GPIO_PIN2)
#define LED2_OFF GPIO_ResetBits(GPIOX, GPIO_PIN2)
#define LED3_ON GPIO_SetBits(GPIOX, GPIO_PIN3)
#define LED3_OFF GPIO_ResetBits(GPIOX, GPIO_PIN3)
#define LED4_ON GPIO_SetBits(GPIOX, GPIO_PIN4)
#define LED4_OFF GPIO_ResetBits(GPIOX, GPIO_PIN4)
#define LED5_ON GPIO_SetBits(GPIOX, GPIO_PIN5)
#define LED5_OFF GPIO_ResetBits(GPIOX, GPIO_PIN5)
void led_init(void);
void Display_tube(int sum);
void DIGITRON_ShowUnitSegCore( short highPinNum, short lowPinNum );
void clear(void);
void DIGITRON_ShowUnitSeg( short unit, char seg );
#endif
main
/************************************************************************************
*************************************************************************************/
#include "stm32f10x.h"
#include "delay.h"
#include "188.h"
#include "usart.h"
u8 unit ; //十位,10-F,11-不显示
u16 time;
int main()
{
delay_init(); // 延时函数初始化
uart_init(115200); // 日志打印串口
// 初始化屏幕
led_init();
while (1) {
time++;
if(time % 50 ==0 ){
if( unit < 100){
unit ++;
}else{
unit= 0;
}
//printf("%dn",unit);
}
Display_tube(unit);
delay_ms(2);
}
}
五、参考
188数码管驱动程序https://blog.csdn.net/qq_57568928/article/details/141928923?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ECtr-3-141928923-blog-120704663.235%5Ev43%5Epc_blog_bottom_relevance_base1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ECtr-3-141928923-blog-120704663.235%5Ev43%5Epc_blog_bottom_relevance_base1&utm_relevant_index=5
联系方式 微信号:13648103287