简易加法计算器 编译通过,数码管显示0,但4*4按键都没反应,请高手们检查哪有不对,检查快一下午了,没发现不对,编译通过,数码管显示0,但4*4按键都没反应。 注:接口等定义均无误
- #include<reg51.h>
- sbit KEYOUT_1 = P1^4;
- sbit KEYOUT_2 = P1^5;
- sbit KEYOUT_3 = P1^6;
- sbit KEYOUT_4 = P1^7;
- sbit KEYIN_1 = P1^0;
- sbit KEYIN_2 = P1^1;
- sbit KEYIN_3 = P1^2;
- sbit KEYIN_4 = P1^3;
- unsigned char code LedChar[] = {
- 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8,
- 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e
- };
- unsigned char LedBuff[6] = {
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
- };
- unsigned char 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、回车、向右
- };
- unsigned char KeySta[4][4] = {
- {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
- };
- void Keydrive();
- void ShowNumber(unsigned long num);
- void main()
- {
- EA = 1;
- TMOD = 0x01;
- ET0 = 1;
- TH0 = 0xFC;
- TL0 = 0x67;
- TR0 = 1;
- ShowNumber(0);
- while(1)
- {
- Keydrive();
- }
- }
- void ShowNumber(unsigned long num)
- {
- char i;
- unsigned char ledbuf[6];
- for(i=0; i<6; i++)
- {
- ledbuf[i] = num %10;
- num = num /10;
- }
- for(i=5; i>=1; i–)
- {
- if(ledbuf[i] == 0)
- LedBuff[i] = 0xFF;
- else
- break;
- }
- for( ; i>=0; i–)
- LedBuff[i] = LedChar[ledbuf[i]];
- }
- void KeyAction(unsigned char keycode)
- {
- static unsigned long result = 0;
- static unsigned long addend = 0;
- 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)
- {
- result = 0;
- addend = 0;
- ShowNumber(addend);
- }
- }
- void Keydrive()
- {
- unsigned char i, j;
- static unsigned char 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(KeySta[i][j] != backup[i][j])
- {
- if(backup[i][j] == 0)
- KeyAction(KeyCodeMap[i][j]);
- KeySta[i][j] = backup[i][j];
- }
- }
- }
- }
- void KeyScan()
- {
- unsigned char i;
- static unsigned char keyout = 0;
- unsigned char keybuf[4][4];
- keybuf[keyout][0] = (keybuf[keyout][0] << 1) | KEYIN_1;
- keybuf[keyout][1] = (keybuf[keyout][1] << 1) | KEYIN_2;
- keybuf[keyout][2] = (keybuf[keyout][2] << 1) | KEYIN_3;
- keybuf[keyout][3] = (keybuf[keyout][3] << 1) | KEYIN_4;
- 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: KEYOUT_4 = 1; KEYOUT_1 = 0; break;
- case 1: KEYOUT_1 = 1; KEYOUT_2 = 0; break;
- case 2: KEYOUT_2 = 1; KEYOUT_3 = 0; break;
- case 3: KEYOUT_3 = 1; KEYOUT_4 = 0; break;
- default: break;
- }
- }
- void LedRefresh()
- {
- static unsigned char i = 0;
- P0 = 0xFF;
- switch(i)
- {
- case 0: P2 = 0xFE; i++; P0 = LedBuff[0]; break;
- case 1: P2 = 0xFD; i++; P0 = LedBuff[1]; break;
- case 2: P2 = 0xFB; i++; P0 = LedBuff[2]; break;
- case 3: P2 = 0xF7; i++; P0 = LedBuff[3]; break;
- case 4: P2 = 0xEF; i++; P0 = LedBuff[4]; break;
- case 5: P2 = 0xDF; i=0; P0 = LedBuff[5]; break;
- default: break;
- }
- }
- void inttimer0() interrupt 1
- {
- TH0 = 0xFC;
- TL0 = 0x67;
- LedRefresh();
- KeyScan();
- }
复制代码
,自己搞明白了,是KeySta[i][j] = backup[i][j];这句写反了,得反过来,不然总是原来backup[i][j]的值。初学者特别要注意!!!!,void Keydrive()函数有问题,
akszhf 发表于 2017-1-14 16:15
void Keydrive()函数有问题
没问题啊,正常了,请问30、31行什么意思?只写名称、不写函数体?