- #include <reg52.h>
- #include<intrins.h>
- #include <stdio.h>
- #include <math.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit Key1 = P2^3;
- sbit Key2 = P2^2;
- sbit Key3 = P2^1;
- sbit Key4 = P2^0;
- sbit BELL = P3^6;
- sbit CONNECT = P3^7;
- unsigned int Key1_flag = 0;
- unsigned int Key2_flag = 0;
- unsigned int Key3_flag = 0;
- unsigned int Key4_flag = 0;
- unsigned char b;
- unsigned char code Num[21]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
- 0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,
- 0x10,0x89};
- unsigned char code Disdigit[4] = {0x7F,0xBF,0xDF,0xEF};
- unsigned char Disbuf[4];
- void delayms(uint t)
- {
- uint i;
- while(t--)
- {
- /* 对于11.0592M时钟,约延时1ms */
- for (i=0;i<125;i++)
- {}
- }
- }
-
- //-----------------------------------------------------
- void SendData(uchar Dat)
- {
- uchar i=0;
- SBUF = Dat;
- while (1)
- {
- if(TI)
- {
- TI=0;
- break;
- }
- }
- }
- void ScanKey()
- {
- if(Key1 == 0)
- {
- delayms(100);
- if(Key1 == 0)
- {
- Key1_flag = 1;
- Key2_flag = 0;
- Key3_flag = 0;
- Key4_flag = 0;
- Key1 = 1;
- }
- else;
- }
- if(Key2 == 0)
- {
- delayms(100);
- if(Key2 == 0)
- {
- Key2_flag = 1;
- Key1_flag = 0;
- Key3_flag = 0;
- Key4_flag = 0;
- Key2 = 1;
- }
- else;
- }
- if(Key3 == 0)
- {
- delayms(50);
- if(Key3 == 0)
- {
- Key3_flag = 1;
- Key1_flag = 0;
- Key2_flag = 0;
- Key4_flag = 0;
- Key3 = 1;
- }
- else;
- }
- if(Key4 == 0)
- {
- delayms(50);
- if(Key4 == 0)
- {
- Key4_flag = 1;
- Key1_flag = 0;
- Key2_flag = 0;
- Key3_flag = 0;
- Key4 = 1;
- }
- else;
- }
- else;
- }
- void KeyProc()
- {
- if(Key1_flag)
- {
- TR1 = 1;
- SendData(0x55);
- Key1_flag = 0;
- }
- else if(Key2_flag)
- {
- TR1 = 1;
- SendData(0x11);
- Key2_flag = 0;
- }
- else if(Key3_flag)
- {
- P1=0xff;
- BELL = 0;
- CONNECT = 1;
- Key3_flag = 0;
- }
- else if(Key4_flag)
- {
- CONNECT = 0;
- BELL = 1;
- Key4_flag = 0;
- }
- else;
- }
- void Initdisplay(void)
- {
- Disbuf[0] = 1;
- Disbuf[1] = 2;
- Disbuf[2] = 3;
- Disbuf[3] = 4;
- }
- void Display() //显示
- {
- unsigned int i = 0;
- unsigned int temp,count;
- temp = Disdigit[count];
- P2 =temp;
- temp = Disbuf[count];
- temp = Num[temp];
- P0 =temp;
- count++;
- if (count==4)
- count=0;
- }
-
- void time0() interrupt 1 using 2
- {
- Display();
- TH0 = (65535 - 2000)/256;
- TL0 = (65535 - 2000)%256;
- }
-
- void main()
- {
- Initdisplay();
- TMOD = 0x21;
- TH0 = (65535 - 2000)/256;
- TL0 = (65535 - 2000)%256;
- TR0 = 1;
- ET0 = 1;
- TH1 = 0xFD; //11.0592M
- TL1 = 0xFD;
- PCON&=0x80;
- TR1 = 1;
- ET1 = 1;
- SCON = 0x40; //串口方式
- REN = 1;
- PT1 = 0;
- PT0 = 1;
- EA = 1;
- while(1)
- {
- ScanKey();
- KeyProc();
-
- if(RI)
- {
- Disbuf[0] = 0;
- Disbuf[1] = 20;
- Disbuf[2] = SBUF>>4;
- Disbuf[3] = SBUF&0x0f;
- RI = 0;
- }
- else;
- }
- }
复制代码 |