数码管静态显示0-9999

lemon Lv4

1.任务描述:

使用 STM32F103R6 芯片的 PC0~PC15 ,PB0 ~ PB15引脚分别接两个共阴极 LED 数码管,其中个位数码管接 PC0~PC7,十位数码管接 PC8~PC15 , 百位数码管接 PB0~PB7,千位数码管接 PB8~PB15。采用静态显示方式,编写程序使两位数码管循环显示 0~9999。

2.理论:

①代码理论:

设置一个数字i,从0~9999,依次识别每一位。
千位=i/1000
百位=i/100 - 千位数字×10
十位=i/10 - 百位数字×10 - 千位数字×100
个位=i%1000

②数码管:

a
右边接地:共阴级
左边接电源:共阳极

③字形码(段码):

a

④74L254:

a
a

字母上面有横线的,意思是在低电平下作用,如上图,我们红框部分接的地,那么此原件,就显示字母带横线的,例如“BA”上面有横线,那么左边的B0~B7就是输入端,A0~A7就是输出端

3.代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "stm32f10x.h" 
uint16_t table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uint16_t disp[4];
uint16_t temp,i,temp2,a,b,c;
void Delay(unsigned int count)
{
unsigned int i;
for(;count!=0;count--)
{
i=5000;
while(i--);
}
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = 0xffff;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_Init(GPIOB, &GPIO_InitStructure);
while(1)
{
for(i=0;i<=9999;i++)
{
a=i/1000;
b=i/100-a*10;
c=i/10-a*100-b*10;
disp[3]=table[a];
disp[2]=table[b] ;
disp[1]=table[c];
disp[0]=table[i%10];
temp=(disp[1]<<8)|(disp[0]&0x0ff);
GPIO_Write(GPIOC,temp);
temp2=(disp[3]<<8)|(disp[2]&0x0ff);
GPIO_Write(GPIOB,temp2);
Delay(100);
}
}
}

4.效果图(Proteus):

a

  • 标题: 数码管静态显示0-9999
  • 作者: lemon
  • 创建于 : 2025-04-11 20:55:06
  • 更新于 : 2025-04-11 22:39:49
  • 链接: https://lemon2003.github.io/post/20250411205506.html
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论