#include<reg52.h> #define uchar unsigned char sbit RS=P2^0; //必须有,子程序才可调用sbit RW=P2^1;sbit E=P2^2;sbit led1=P2^3;sbit led2=P2^4;int count,fen,miao,shi,key=0,i,a; voiddelay(unsigned int n) //延时函数{unsigned int x,y; for(x=0;x<n;x++) for(y=0;y<123;y++);} void write_command(char com) //写命令的子函数{ E=0; RS=0; RW=0; P1=com; E=1; E=0; delay(1); } void write_data(char dat) //写数据的子函数{ E=0; RS=1; RW=0; P1=dat; E=1; E=0; delay(1); } void lcd_initial(void) //lcd初始化{ write_command(0x38); write_command(0x0c); write_command(0x06); delay(1); } void string(uchar position,uchar *s) //显示的子函数{ write_command(position); while(*s>0) {write_data(*s++); delay(1);} }/******************************************按键扫描程序单片机:STC89C52P2为扫描按键口S1-4= e7 eb ed eeS5-8= d7 db dd deS9-10= b7 bb bd beS13-16= 77 7b 7d 7e*****************************************/unsigned char keyscand(void){ unsigned char a,b; P3=0xf0; if((P3&0xf0)!=0xf0) { delay(2); if((P3&0xf0)!=0xf0) { a=P3&0xf0; P3=0x0f; b=P3&0x0f; } } return(a+b);} void main(void) { a=0;led1=0;led2=0; lcd_initial();//初始化调用 EA=1; //总开关 ET0=1; //计数器T0允许中断 TMOD=0x01; //T0方式1 TH1=(65536-50000)/256;//设置我要的实践50ms TL1=(65536-50000)%256; TR0=1; write_command(0x81); write_data('A'); write_command(0x82); write_data('3'); write_command(0x83); write_data('3'); write_command(0x84); write_data('a'); write_command(0x86); write_data('B'); write_command(0x87); write_data('3'); write_command(0x88); write_data('3'); write_command(0x89); write_data('b'); write_command(0xce); write_data('6'); write_command(0xcf); write_data('4'); while(1){ key=keyscand(); //读取返回值 switch(key)//判断键值(那一个键按下) { case0xee: i=0; break; //对键值赋值 case0xde: i=1; break; case0xbe: i=2; break; case0x7e: i=3; break; case0xed: i=4; break; case0xdd: i=5; break; case0xbd: i=6; break; case0x7d: i=7; break; case0xeb: i=8; break; case0xdb: i=9; break; case0xbb: i=10; break; case0x7b: i=11; break; case0xe7: i=12; break; case0xd7: i=13; break; case0xb7: i=14; break; case0x77: i=15; break; } if(fen==15&&shi==0||fen==15&&shi==1||fen==15&&shi==2) { led1=1; } if(i==10) {led1=0; delay(10); } if(fen==30&&shi==0||fen==30&&shi==1||fen==30&&shi==2) {led2=1; } if(i==11) {led2=0; delay(10); } } } voidtimer0() interrupt 1 //中断{ TH1=(65536-50000)/256; TL1=(65536-50000)%256; count++; //设置这个数来控制时间为1S write_command(0xC1); //显示shi的十位 write_data(0x30+shi/10); write_command(0xC2); //显示shi的个位 write_data(0x30+shi%10); string(0xC3,":"); write_command(0xC4); //显示fen的十位 write_data(0x30+fen/10); write_command(0xC5); //显示fen的个位 write_data(0x30+fen%10); string(0xC6,":"); write_command(0xC7); write_data(0x30+miao/10); write_command(0xC8); write_data(0x30+miao%10); if(count==1) //20ms*50=1S {count=0; miao++;} //每秒钟miao+1 if(miao==60) {miao=0; fen++;} //每分钟fen+1 if(fen==60) {fen=0; shi++;} if(shi==24) {shi=0;}}
-
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>