|
串口發(fā)送放在按鍵松手后
- #include <reg52.h>
- typedef unsigned char uchar;
- typedef unsigned int uint;
- uchar code duan[] = {0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- uchar KeyValue;
- void delay(uint z)
- {
- uint y;
- for (;z>0;z--)
- for (y = 0; y < 110; y++);
- }
- void chuankouInit()
- {
- TMOD = 0x20;
- TH1 = 243;
- TL1 = 243;
- TR1 = 1;
- SCON = 0x50;
- PCON = 0x80; //倍波特率
- ES = 1;
- EA = 1;
- }
- void keypros()
- {
- P3 = 0xf0;//0xff不行,一定要控制變量先行后列或者先列后行否則要列出所有情況
- if (P3!=0xf0)
- {
- delay(10);
- if (P3!= 0xf0)
- {
- switch (P3)
- {
- case (0x70): KeyValue = 3;break;
- case (0xb0): KeyValue = 2;break;
- case (0xd0): KeyValue = 1;break;
- case (0xe0): KeyValue = 0;break;
- }
- P3 = 0x0f;
- switch (P3)
- {
- case (0x07): KeyValue += 12;break;
- case (0x0b): KeyValue += 8;break;
- case (0x0d): KeyValue += 4;break;
- case (0x0e): KeyValue += 0;break;
- }
- while(P3 != 0x0f);
- SBUF = KeyValue;
- while (!TI);
- TI = 0; //發(fā)完
- }
- }
- }
- void main()
- {
- chuankouInit();
- while (1)
- {
- keypros();
- P0 = ~duan[KeyValue];
- }
- }
- void chuankou() interrupt 4
- {
- uchar receiveData;
- receiveData = KeyValue;
- RI = 0; //收完
- }
復(fù)制代碼 |
|