#include #define uint unsigned int #define uchar unsigned char sbit key1=P1^0; sbit key2=P1^1; sbit key3=P1^2; sbit key4=P1^3; sbit lcden=P2^2; sbit lcdrs=P2^0; sbit lcdrw=P2^1; sbit led=P2^4; sbit sound=P2^5; uint a,b,i,min,hour,minge,minshi,hourge,hourshi,amin,ahour,aminge,aminshi,ahourge,ahourshi,sec; //unsigned char code x[]={0xFF,0xC7,0x83,0x01,0x00,0x00,0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF,0xFF}; //unsigned char code z[]={0xFF,0xE3,0xC1,0x80,0x00,0x00,0x00,0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0x7F,0xFF,0xFF}; uchar code table1[]={'0','1','2','3','4','5','6','7','8','9'}; uchar code table2[]="Time:00:00 " ; uchar code Alarm_1[]="Alarm:00:00 "; uchar code nul[]=" "; uchar num1,num2; void delay(unsigned int xms) //???? { uint i,j; for(i=xms;i>0;i--) for(j=124;j>0;j--); } void write_com(uchar com) //?LCD命令控制 { delay(5) ; lcdrs=0; P0=com; delay(5); lcden=1; delay(5); lcden=0; } void write_data(uchar date) //?LCD数据控制 { delay(5); lcdrs=1; P0=date; delay(5); lcden=1; delay(5); lcden=0; } void time() { while(1) { if(key1==0) { delay(10); if(key1==0) { if(hour==23) // 设置时间 hour=0; else hour++; hourge=hour%10; hourshi=hour/10; write_com(0x0f); delay(2); write_com(0x80+6); write_data(table1[hourge]); delay(5); write_com(0x80+5); delay(2); write_data(table1[hourshi]); while(!key1); //delay(1); } } if(key2==0) { delay(10); if(key2==0) { if(min==59) //分钟设置 min=0; else min++; minge=min%10; minshi=min/10; write_com(0x0f); write_com(0x80+9); write_data(table1[minge]); delay(1); write_com(0x80+8); write_data(table1[minshi]); delay(1); while(!key2); } } if(key3==0) { delay(10); if(key3==0) { write_com(0x0c); TR1=1; while(!key3); break; } } } } void alarm() { while(1) { if(key1==0) { delay(10); if(key1==0) { if(ahour==24) ahour=0; else ahour++; ahourge=ahour%10; ahourshi=ahour/10; write_com(0x0f); //delay(2); write_com(0x80+0x40+8); write_data(':'); write_com(0x80+0x40+7); write_data(table1[ahourge]); delay(1); write_com(0x80+0x40+6); //delay(2); write_data(table1[ahourshi]); delay(1); while(!key1); } } if(key2==0) { delay(10); if(key2==0) { if(amin==59) amin=0; else amin++; aminge=amin%10; aminshi=amin/10; write_com(0x0f); //delay(2); write_com(0x80+0x40+10); write_data(table1[aminge]); delay(1); write_com(0x80+0x40+9); // delay(2); write_data(table1[aminshi]); delay(1); while(!key2); } } if(key3==0) { delay(10); if(key3==0) { write_com(0x0c); write_com(0x80+0x40); for(i=0;i<11;i++) write_data(nul); while(!key3); break; } } } } void keyscan() { if(key1==0) { delay(10); if(key1==0) { while(!key1); time(); } } else if(key2==0) { delay(10); if(key2==0) { while(!key2) { ahourge=ahour%10; ahourshi=ahour/10; write_com(0x80+0x40+7); write_data(table1[ahourge]); delay(5); write_com(0x80+0x40+6); delay(2); write_data(table1[ahourshi]); aminge=amin%10; aminshi=amin/10; write_com(0x80+0x40+10); write_data(table1[aminge]); delay(5); write_com(0x80+0x40+9); delay(2); write_data(table1[aminshi]); write_com(0x80+0x40); for(i=0;i<11;++i) write_data(Alarm_1); } write_com(0x80+0x40); for(i=0;i<11;i++) write_data(nul); } } else if(key3==0) { delay(10); if(key3==0) { while(!key3); ahourge=ahour%10; ahourshi=ahour/10; write_com(0x80+0x40+7); write_data(table1[ahourge]); delay(5); write_com(0x80+0x40+6); delay(2); write_data(table1[ahourshi]); aminge=amin%10; aminshi=amin/10; write_com(0x80+0x40+10); write_data(table1[aminge]); delay(5); write_com(0x80+0x40+9); delay(2); write_data(table1[aminshi]); write_com(0x80+0x40); for(i=0;i<11;++i) write_data(Alarm_1); alarm(); } } else if(key4==0) delay(5); if(key4==0) { while(!key4); a=a+1; if(a%2==0) { for(b=0;b<6;b++) { sound=~sound; delay(200); } sound=1; } else if(a%2==1) { sound=0; delay(300); sound=1; delay(300); } sound=1; } } } void lcd_init() lcden=0; lcdrw=0; write_com(0x38); write_com(0x0c); write_com(0x06); write_com(0x01); delay(2); write_com(0x80); for(i=0;i<16;i++) { write_data(table2); delay(1); } } void init() key1=1;key2=1;key3=1;key4=1;sec=0;a=0;sound=1;amin=1; //led=1; TMOD=0x11; TH1=0x3C; TL1=0xB0; EA=1; ET1=1; TR1=1; } void led1() interrupt 3 { TH1=0x3C; TL1=0xB0; num1++; num2++; if(num1==10) { num1=0; led=~led; } if(amin==min&&ahour==hour&&a%2==0) sound=0; } void main() { lcd_init(); init(); while(1) { keyscan(); if(num2==20) { num2=0; if(hour==23&&min==59&&sec==59) { hour=0;min=0;sec=0; } else if(sec==59&&min==59) { sec=0;min=0;hour++; } else if(sec==59) { sec=0;min++; } else sec++; minge=min%10; minshi=min/10; write_com(0x80+9); write_data(table1[minge]); delay(5); write_com(0x80+8); // delay(2); write_data(table1[minshi]); hourge=hour%10; hourshi=hour/10; write_com(0x80+6); write_data(table1[hourge]); delay(5); write_com(0x80+5); // delay(2); write_data(table1[hourshi]); write_com(0x80+7); write_data(':'); } } }
学友们,老师们,能给看看什么地方错了吗?给改下!
|