问题描述:单片机为清翔单片机,其两个四位共阴极数码管由两片74HC573控制,我做的项目是利用矩阵按键做一个简易加法计算器。我认为大致问题是出在数码管显示ShowNumber这个函数。数码管虽然能够正确显示结果,但是在放着单片机不碰它的时候,数码管只有第三位是常亮的,前两位是不亮的,只能不停地按按键才能让前两位数码管一闪一闪从而隐约地能够看到结果。不知道怎么解决请大家帮个忙?
单片机源程序如下:
- #include <REGX52.H>
- sbit KeyOut1 = P3^0;
- sbit KeyOut2 = P3^1;
- sbit KeyOut3 = P3^2;
- sbit KeyOut4 = P3^3;
- sbit KeyIn1 = P3^4;
- sbit KeyIn2 = P3^5;
- sbit KeyIn3 = P3^6;
- sbit KeyIn4 = P3^7;
- sbit duan = P2^6;
- sbit wei = P2^7;
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- uchar T0RH,T0RL;
- uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
- 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- uchar KeySta[4][4]={
- {1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1},
- };
- uchar code KeyCodeMap[4][4] = { //矩阵按键编号到标准键盘键码的映射表
- { 0x31, 0x32, 0x33, 0x26 }, //数字键 1、数字键 2、数字键 3、向上键
- { 0x34, 0x35, 0x36, 0x25 }, //数字键 4、数字键 5、数字键 6、向左键
- { 0x37, 0x38, 0x39, 0x28 }, //数字键 7、数字键 8、数字键 9、向下键
- { 0x30, 0x1B, 0x0D, 0x27 } //数字键 0、ESC 键、 回车键、 向右键
- };
- uchar we[]={0xFE,0xFD,0xFB,0xF7,0xef,0xdf,0xbf,0x7f};
- unsigned char LedBuff[6] = { //数码管显示缓冲区
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
- };
- void ConfigTimer0(uint ms)
- {
- ulong tmp;
- tmp = 11059200/12;
- tmp = (tmp*ms)/1000;
- tmp = 65536-tmp;
- T0RH = (uchar)(tmp>>8);
- T0RL = (uchar)tmp;
- TMOD &= 0xF0;
- TMOD |= 0x01;
- TH0 = T0RH;
- TL0 = T0RL;
- ET0 = 1;
- TR0 = 1;
- EA = 1;
- }
- void delay(uchar i)
- {
- uchar j,k;
- for(j=i;j>0;j--)
- for(k=125;k>0;k--);
- }
- void ShowNumber(ulong num)
- {
- uchar bai,shi,ge;
- bai = num/100%10;
- shi = num/10%10;
- ge = num/1%10;
- duan = 0;
- P0 = table[bai];
- duan = 1;
- duan = 0;
- wei = 0;
- P0 = we[0];
- wei = 1;
- wei = 0;
- delay(5);
-
- duan = 0;
- P0 = table[shi];
- duan = 1;
- duan = 0;
- wei = 0;
- P0 = we[1];
- wei = 1;
- wei = 0;
- delay(5);
-
- duan = 0;
- P0 = table[ge];
- duan = 1;
- duan = 0;
- wei = 0;
- P0 = we[2];
- wei = 1;
- wei = 0;
- delay(5);
- }
- void KeyAction(uchar keycode)
- {
- static ulong result,addend;
- if((keycode>=0x30)&&(keycode<=0x39))
- {
- addend = (addend*10)+keycode-0x30;
- ShowNumber(addend);
- }
- else if(keycode == 0x26)
- {
- result += addend;
- addend = 0;
- ShowNumber(result);
- }
- else if(keycode == 0x0D)
- {
- result += addend;
- addend = 0;
- ShowNumber(result);
- }
- else if(keycode == 0x1B)
- {
- addend = 0;
- result = 0;
- ShowNumber(addend);
- }
- }
- void KeyDriver()
- {
- uchar i,j;
- static uchar backup[4][4]={
- {1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1},
- };
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- {
- if(backup[i][j]!=KeySta[i][j])
- {
- if(backup[i][j]!=0)
- {
- KeyAction(KeyCodeMap[i][j]);
- }
- backup[i][j] = KeySta[i][j];
- }
- }
- }
- }
- void main()
- {
- ConfigTimer0(2);
- while(1)
- {
- KeyDriver();
- }
- }
- void KeyScan()
- {
- uchar i;
- static uchar KeyOut = 0;
- static uchar KeyBuf[4][4]={
- {0xFF,0xFF,0xFF,0xFF},{0xFF,0xFF,0xFF,0xFF},
- {0xFF,0xFF,0xFF,0xFF},{0xFF,0xFF,0xFF,0xFF}
- };
- KeyBuf[KeyOut][0] = (KeyBuf[KeyOut][0]<<1)|KeyIn1;
- KeyBuf[KeyOut][1] = (KeyBuf[KeyOut][1]<<1)|KeyIn2;
- KeyBuf[KeyOut][2] = (KeyBuf[KeyOut][2]<<1)|KeyIn3;
- KeyBuf[KeyOut][3] = (KeyBuf[KeyOut][3]<<1)|KeyIn4;
- for(i=0;i<4;i++)
- {
- if((KeyBuf[KeyOut][i]&0x0F)==0x00)
- KeySta[KeyOut][i] = 0;
- else if((KeyBuf[KeyOut][i]&0x0F)==0x0F)
- KeySta[KeyOut][i] = 1;
- }
- KeyOut++;
- KeyOut = KeyOut&0x03;
- switch(KeyOut)
- {
- case 0:KeyOut4=1;KeyOut3=1;KeyOut2=1;KeyOut1=0;break;
- case 1:KeyOut4=1;KeyOut3=1;KeyOut2=0;KeyOut1=1;break;
- case 2:KeyOut4=1;KeyOut3=0;KeyOut2=1;KeyOut1=1;break;
- case 3:KeyOut4=0;KeyOut3=1;KeyOut2=1;KeyOut1=1;break;
- default:break;
- }
复制代码
计算器.rar
(33.89 KB, 下载次数: 3)
|