- #include <reg51.h>
- #include "intrins.h" //_nop_();延时函数用
- #define uchar unsigned char
- #define uint unsigned int
- #define Lcd_Port P3 //定义数据端口
- #define KEY_IO P0 //键盘接口
- sbit RS = P2^1; //定义和LCM的连接端口
- sbit RW = P2^2;
- sbit E = P2^5;
- sbit Busy = P3^7;
- sbit speaker=P1^0;
- uchar data keytemp,key;
- uchar L=0;
- uchar k=0;
- uchar c = 0;
- uchar b=0;
- uchar j=0;
- uchar e=1;
- uchar d = 0;
- uchar hour =24;
- uchar hour1=0;
- uchar min = 11;
- uchar sec = 59; //赋初值
- uchar data flag=0x00;
- bit flag_key=0;
- uchar code Lcddata[] ={"0123456789:"};
- //uchar codeLcddata[12]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff,0xbf};
- /******************************************************************************************
- * 函数名称 :Timer0Init
- * 功能描述 :定时器0初始化
- ******************************************************************************************/
- void Timer0Init(void)
- {
- TMOD=0x11;
- TH0 = 0x3c;
- TL0 = 0xb0;
- TH1=-10300/256;
- TL1=-10300%256; //25MS定时初值(T1计时用)
- ET0=1;
- ET1=1;
- TR0=1;
- TR1=1;
- EA=1;
- }
- /******************************************************************************************
- * 函数名称 :IsrTimer0
- * 功能描述 :T0 50mS中断程序
- ******************************************************************************************/
- void IsrTimer0(void) interrupt 1 using 1//T0 50mS中断程序
- {
- static uchar count = 0;
- TH0 = 0x3c;
- TL0 = 0xb0;
- count++;
- if(count ==20) //定时1s时间到
- {
- count = 0;
- sec--;
- if(sec == 0) //1分钟时间到
- {
- if(min == 0) //1小时时间到
- {
- min = 0;
- EA=0;
- speaker=~speaker;
- }
- else
- {
- sec= 59;
- min--;
- }
- }
- }
- }
- void IsrTimer1(void) interrupt 3 using3//T1 50mS中断程序
- {uchar i=0,m=0,a=0;
- static uchar ta20ms= 0;
- TH1=-10300/256;
- TL1=-10300%256;
- ta20ms++;
- if(ta20ms== 1 ) //定时1s时间到
- {
- ta20ms=0;
- hour1--;
- if(hour1 ==-1) //1分钟时间到
- {
- hour--;hour1 =99;
- if(hour==0)
- {
- hour=24;hour1=0;
- k=1;
- //EA=0;
- L=1;
- speaker=~speaker;
- while(a<=2){
- while(m<=225){for(i=0;i<=225;i++)
- a++;m++;}}
- speaker=1;}
- }
- }
- }
- /******************************************************************************************
- * 函数名称 :Delay
- * 功能描述 :延时子程序,延时 (1MS*t) S
- ******************************************************************************************/
- void Delay(uchar t)
- {
- uchar a;
- while(t-- != 0)
- {
- for(a = 0; a < 125; a++);
- }
- }
- /******************************************************************************************
- * 函数名称 :Read_Busy
- * 功能描述 :读忙信号判断
- ******************************************************************************************/
- void Read_Busy(void)
- {
- uchar i=50;
- Lcd_Port=0xff;
- RS= 0;
- RW= 1;
- E= 1;
- while((i--)&&Busy);
- E=0;
- }
- /******************************************************************************************
- * 函数名称 :Write_Comm
- * 功能描述 :写指令函数 ok
- ******************************************************************************************/
- void Write_Comm(uchar lcdcomm)
- {
- Read_Busy();
- RS = 0;
- RW = 0;
- E = 1;
- Lcd_Port=lcdcomm;
- E = 0;
- }
- /******************************************************************************************
- * 函数名称 :Write_Char
- * 功能描述 :写字符函数 ok
- ******************************************************************************************/
- void Write_Char(uint num)//写字符函数
- {
- Read_Busy();
- RS = 1;
- RW = 0;
- E = 1;
- Lcd_Port = Lcddata[num];
- E = 0;
- }
- /******************************************************************************************
- * 函数名称 :Write_Data
- * 功能描述 :写数据函数 ok
- ******************************************************************************************/
- void Write_Data(uchar lcddata)
- {
- Read_Busy();
- RS = 1;
- RW = 0;
- E = 1;
- Lcd_Port = lcddata;
- E = 0;
- }
- /******************************************************************************************
- * 函数名称 :Init_LCD
- * 功能描述 :初始化LCD
- ******************************************************************************************/
- void Init_LCD(void)
- {
- //Delay(400); //稍微延时,等待LCM进入工作状态
- Write_Comm(0x38); //8位2行5*8
- Write_Comm(0x0c); //显示开/关,光标开闪烁开
- Write_Comm(0x01); //清显示
- Write_Comm(0x06); //文字不动,光标右移
- Write_Comm(0x02); //光标归位
- }
- /******************************************************************************************
- * 函数名称 :Show_Time
- * 功能描述 :LCD上显示当前时间
- ******************************************************************************************/
- void Show_Time(void)
- {
- Write_Comm(0x80+0x08); //显示首地址
- Write_Char( hour / 10 );//显示小时
- Write_Char( hour % 10 );
- Write_Char( 10 ); //显示:
- Write_Char( hour1 / 10 );//显示小时
- Write_Char( hour1 % 10 );
- Write_Comm(0x80+0x4b);
- Write_Char( min / 10 ); //显示分钟
- Write_Char( min % 10 );
- Write_Char( 10 ); //显示:
- Write_Char( sec / 10 ); //显示秒
- Write_Char( sec % 10 );
- Write_Comm(0x80+0x45);
- Write_Char( c / 10 );
- Write_Char( c % 10 );
- Write_Comm(0x85);
- Write_Char(d / 10 );
- Write_Char(d % 10 );
- }
- //***************按键扫描函数/*************
- void keyscan()
- {
- uchar i,t=0xef,temp=0; //1110 1111
- KEY_IO=0x0f;
- keytemp=(~KEY_IO)&0x0e;
- if(keytemp!=0) //0000 0000 ;0000 1000; 00000100; 0000 0010; 0000 0001;
- {
- Delay(20);
- for(i=0;i<4;i++)
- {
- KEY_IO=t;
- keytemp=(~KEY_IO)&0x0e;
- if(keytemp!=0&flag_key==0)
- {
- flag_key=1;
- switch(keytemp)
- {
- case 0x08:key=0*3+i;break;
- case 0x04:key=1*4+i;break;
- case 0x02:key=2*4+i;break;
- default:break;
- }
- switch(key)
- {
- case0:{c++; j=1;if(c>=99){ Write_Comm(0x84);Write_Data('1');d=0;}b=0;}break;
- case 1:{c++;c++; j=1;if(c>=99){Write_Comm(0x84);Write_Data('1');d=0;}b=0;}break;
- case 2:{c++;c++;c++;if(c>=99){Write_Comm(0x84);Write_Data('1');d=0;} j=1;b=0;}break;
- case 4:{d++;if(d>=99){Write_Comm(0x80+0x44);Write_Data('1');c=0;};b=1;j=0;}break;
- case 5:{d++;d++;if(d>=99){Write_Comm(0x80+0x44);Write_Data('1');c=0;}b=1;j=0;}break;
- case 6:{d++;d++;d++;if(d>=99){Write_Comm(0x80+0x44);Write_Data('1');c=0;}b=1;j=0;}break;
- case 8:{if(b==1){d--;if (d==0)d=0;};if(j==1) c--;}break;
- case 9:if(k==1)hour=24;EA=~EA;speaker=1;L=0;k=0;break;
- case 10: e++;if(e==5)e=1;Write_Comm(0x80+0x0d);Write_Char(e%10); break;
- case 3:{ Write_Comm(0x80);
- Write_Data('C');
- Write_Data('B');
- Write_Data('A');
- Write_Data(':');
- Write_Comm(0x80+0x0d);
- Write_Char(e%10);
- Write_Data('S');
- Write_Data('t');
- Write_Comm(0x80+0x40);
- Write_Data('N');
- Write_Data('B');
- Write_Data('A');
- Write_Data(':');
- temp=c;
- c=d;
- d=temp;
- }break;
- case 7:{ Write_Comm(0x80);
- Write_Data('N');
- Write_Data('B');
- Write_Data('A');
- Write_Data(':');
- Write_Comm(0x80+0x0d);
- Write_Char(e%10);
- Write_Data('S');
- Write_Data('t');
- Write_Comm(0x80+0x40);
- Write_Data('C');
- Write_Data('B');
- Write_Data('A');
- Write_Data(':');
- temp=c;
- c=d;
- d=temp;}break;
- default:break;
- }
- }
- t=(t<<1)|0x01;
- }
- }
- else flag_key=0;
- }
- /******************************************************************************************
- * 函数名称 :main
- * 功能描述 :主函数
- ******************************************************************************************/
- void main(void)
- {
- Init_LCD(); //初始化 LCM
- Timer0Init(); //初始化定时器
- Write_Comm(0x80);
- Write_Data('N');
- Write_Data('B');
- Write_Data('A');
- Write_Data(':');
- Write_Comm(0x80+0x0d);
- Write_Char(e%10);
- Write_Data('S');
- Write_Data('t');
- Write_Comm(0x80+0x40);
- Write_Data('C');
- Write_Data('B');
- Write_Data('A');
- Write_Data(':');
- while(1)
- {
- Show_Time(); //显示时间
- keyscan();
- }
- }
复制代码
|