本帖最后由 Anywaydebug 于 2022-10-16 12:00 编辑
很可能是因为你数码管动态扫描用的delay这种占用延时,按键检测去抖动和松手也使用了delay这种占用延时,当执行按键检测程序的时候数码管就无法得到动态扫描,才导致这样的显示效果。
给你提供一种解决方法:数码管的动态扫描放在定时器中断里面去执行
下面这个简单的代码,测试数码管动态显示独立键盘的值
- #include <reg52.h>
-
- #define CFG_SYSFEQ 11059200
- sbit DIGIT = P2^6;//数码管段选
- sbit SEG = P2^7;//数码管位选
- sbit KEY_S2 = P3^0;
- sbit KEY_S3 = P3^1;
- sbit KEY_S4 = P3^2;
- sbit KEY_S5 = P3^3;
-
- static volatile unsigned int idata KEY_CODE;
- /*System ticks*/
- static unsigned int code g_timer0_init_val = 65536 - (CFG_SYSFEQ/12/1000);
- void time_Initialization(void)
- {
- /*Time0 at 16-bit timer mode.*/
- TMOD =(TMOD & 0xf0u) | 0x01u;
- TL0 = g_timer0_init_val & 0xff;
- TH0 = g_timer0_init_val >> 8;
- ET0 = 1; //Enable timer0 interrupt
- TR0 = 1; //Start running
- }
- //毫秒级延时函数定义
- void delay(unsigned int z)
- {
- unsigned int x,y;
- for(x = z; x > 0; x--)
- for(y = 114; y > 0 ; y--);
- }
-
- #define SEG7_DIGIT_NUM 4
- void display(unsigned int val)
- {
- const char code digitMaps[]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
- const unsigned char code segMaps[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef};
- unsigned char q, b, s, g;
- static unsigned char segment;
- q = val / 1000;
- b = val % 1000 / 100;
- s = val % 100 / 10;
- g = val % 10;
- P0 = 0x00;//清除断码
- switch(segment)
- {
- case 0: DIGIT = 1; P0 = digitMaps[q]; DIGIT = 0; break;
- case 1: DIGIT = 1; P0 = digitMaps[b]; DIGIT = 0; break;
- case 2: DIGIT = 1; P0 = digitMaps[s]; DIGIT = 0; break;
- case 3: DIGIT = 1; P0 = digitMaps[g]; DIGIT = 0; break;
- }
- P0 = 0xff;//清除位码
- SEG = 1;//打开位选锁存器
- P0 = segMaps[segment];
- SEG = 0;//锁存位选数据
- segment++;
- if(segment == SEG7_DIGIT_NUM)
- segment = 0;
- }
- #define KEYPORT P3
- void scanKeyCode(void)
- {
- /*stand-alone keys*/
- KEYPORT = 0xffu;
- if(KEY_S2 == 0)//判断S2是否被按下
- {
- delay(20);//按键消抖
- if(KEY_S2 == 0)
- {
- while(!KEY_S2);//松手检测
- KEY_CODE = 1;
- }
- }
- else if(KEY_S3 == 0)//判断S3是否被按下
- {
- delay(20);//按键消抖
- if(KEY_S3 == 0)
- {
- while(!KEY_S3);//松手检测
- KEY_CODE = 10;
- }
- }
- else if(KEY_S4 == 0)//判断S4是否被按下
- {
- delay(20);//按键消抖
- if(KEY_S4 == 0)
- {
- while(!KEY_S4);//松手检测
- KEY_CODE = 100;
- }
- }
- else if(KEY_S5 == 0)//判断S5是否被按下
- {
- delay(20);//按键消抖
- if(KEY_S5 == 0)
- {
- while(!KEY_S5);//松手检测
- KEY_CODE = 1000;
- }
- }
-
- }
- void main(void)
- {
- time_Initialization();
- EA = 1;
- while(1)
- {
- scanKeyCode();
- }
- }
-
- /*Timer0 as system tick timer. 1KHZ*/
- void timer0_ISR(void) interrupt 1
- {
- TL0 = g_timer0_init_val & 0xff;
- TH0 = g_timer0_init_val >> 8;
- display(KEY_CODE);
- }
复制代码
|