抢答器- #include<reg52.h>
- #include<intrins.h>
- sbit RS = P2^0; //定义端口
- sbit RW = P2^1;
- sbit EN = P2^2;
- sbit s = P2^7;
- sbit s0 = P1^0;
- sbit s1 = P1^1;
- sbit s2 = P1^2;
- sbit s3 = P1^3;
- sbit s4 = P1^4;
- sbit s5 = P1^5;
- sbit s6 = P1^6;
- sbit s7 = P1^7;
- sbit beep = P3^7;
- #define RS_CLR RS=0
- #define RS_SET RS=1
- #define RW_CLR RW=0
- #define RW_SET RW=1
- #define EN_CLR EN=0
- #define EN_SET EN=1
- #define DataPort P0
- unsigned char table[10] ={'0','1','2','3','4','5','6','7','8','9'};
- unsigned char shu,key,count=0;
- unsigned char flag=0;
- unsigned char DATA,num;
- //uS 延时函数
- void DelayUs2x(unsigned char t)
- {
- while(--t);
- }
- //mS 延时函数
- void DelayMs(unsigned int t)
- {
- while(t--)
- {
- //延时 1mS
- DelayUs2x(245);
- DelayUs2x(245);
- }
- }
- //判忙函数
- bit LCD_Check_Busy(void)
- {
- DataPort= 0xFF;
- RS_CLR;
- RW_SET;
- EN_CLR;
- _nop_();
- EN_SET;
- return (bit)(DataPort & 0x80);
- }
- //写入命令函数
- void LCD_Write_Com(unsigned char com)
- {
- while(LCD_Check_Busy()); // 忙则等待
- RS_CLR;
- RW_CLR;
- EN_SET;
- DataPort= com;
- _nop_();
- EN_CLR;
- }
- // 写入数据函数
- void LCD_Write_Data(unsigned char Data)
- {
- while(LCD_Check_Busy()); // 忙则等待
- RS_SET;
- RW_CLR;
- EN_SET;
- DataPort= Data;
- _nop_();
- EN_CLR;
- }
- //清屏函数
- void LCD_Clear(void)
- {
- LCD_Write_Com(0x01);
- DelayMs(5);
- }
- //写入字符串函数
- void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x); //表示第一行
- }
- else
- {
- LCD_Write_Com(0xC0 + x); //表示第二行
- }
- while (*s)
- {
- LCD_Write_Data( *s);
- s ++;
- }
- }
- //写入字符函数
- void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x);
- }
- else
- {
- LCD_Write_Com(0xC0 + x);
- }
- LCD_Write_Data( Data);
- }
- //初始化函数
- void LCD_Init(void)
- {
- LCD_Write_Com(0x38); //显示模式设置
- DelayMs(5);
- LCD_Write_Com(0x38);
- DelayMs(5);
- LCD_Write_Com(0x38);
- DelayMs(5);
- LCD_Write_Com(0x38);
- LCD_Write_Com(0x08); //显示关闭
- LCD_Write_Com(0x01); //显示清屏
- LCD_Write_Com(0x06); //显示光标移动设置
- DelayMs(5);
- LCD_Write_Com(0x0C); //显示开及光标设置
- }
- //定时器中断子程序
- void Timer0_isr(void)interrupt 1
- {
- unsigned char temp,temp1,i,j=2;
- TH0=(65536-50000)/256;// 重新赋值 50ms
- TL0=(65536-50000)%256;
- // beep=~beep;
- // DelayMs(100);
- i++;
- if(i==20)
- {
- i=0;
- DATA--;
- if(DATA>0)
- {
- DelayMs(50);
- temp=DATA/10;
- temp1=DATA%10;
- LCD_Write_Char(7,1,table[temp]);
- LCD_Write_Char(8,1,table[temp1]);
- }
- else if(DATA==0)
- {
- P1=0xFF;
- ET0=0;
- DelayMs(50);
- LCD_Write_Char(7,1,'0');
- LCD_Write_Char(8,1,'0');
- for(;j--;j>0)
- {
- beep=0;
- DelayMs(500);
- beep=1;
- }
- }
- }
- }
- void beepon(void)
- {
- unsigned char i;
- for(i=0;i<10;i++)
- {
- beep=0;
- DelayMs(50);
- beep=1;
- }
- }
- unsigned char keyscan(void)
- {
- unsigned char scan1;
- scan1=P1;
- if(scan1!=0xff)
- {
- DelayMs(30);
- if(scan1==P1)
- {
- switch(scan1)
- {
- case 0xff:
- scan1 = 0;
- break;
- case 0xfe:
- scan1 = 1;
- break;
- case 0xfd:
- scan1 = 2;
- break;
- case 0xfb:
- scan1 = 3;
- break;
- case 0xf7:
- scan1 = 4;
- break;
- case 0xef:
- scan1 = 5;
- break;
- case 0xdf:
- scan1 = 6;
- break;
- case 0xbf:
- scan1 = 7;
- break;
- case 0x7f:
- scan1 = 8;
- break;
- }
- }
- }
- else
- scan1=0;
- return(scan1);
- }
- void display(void)
- {
- unsigned char a,b;
- key=keyscan();
- if(key!=0 & flag==0 )
- {
- ET0=0;
- count++;
- DelayMs(200);
- LCD_Write_Char(10,0,table[key]);
- flag=1;
- a=(shu-DATA)/10;
- b=(shu-DATA)%10;
- LCD_Write_Char(7,1,table[a]);
- LCD_Write_Char(8,1,table[b]);
- }
- }
- void main(void)
- {
- unsigned char k;
- unsigned a,b;
- LCD_Init(); //初始化液晶
- DelayMs(20); //延时有助于稳定
- LCD_Clear(); //清屏
- LCD_Write_String(0,1,"time");
- LCD_Write_String(0,0,"xuanshou");
- //Init_Timer0(); //定时器 0 初始化
- while(count==0)
- {
- if(s0==0)
- {
- DelayMs(10);
- if(s0==0)
- {
- count++;
- while(count==1)
- {
- if(s==0)
- {
- DelayMs(10);
- if(s==0)
- {
- while(!s);
- count++;
- }
- }
- if(s1==0)
- {
- DelayMs(10);
- if(s1==0)
- {
- while(!s1);
- {
- DATA++;
- shu=DATA;
- if (DATA==31) {DATA=0;count=0;}
- a=DATA/10;
- b=DATA%10;
- LCD_Write_Char(7,1,table[a]);
- LCD_Write_Char(8,1,table[b]);
- }
- }
- }
- }
- while(count==2)
- {
- TMOD |= 0x01; //使用模式 1,16 位定时器,使用 "|"符号可以在 使用多个定时器时不受影响
- TH0=(65536-50000)/256;// 重新赋值 50ms
- TL0=(65536-50000)%256;
- EA=1; //总中断打开
- ET0=1; //定时器中断打开
- TR0=1; //定时器开关
-
- }
- }
- }
- }
- }
复制代码
|