简介:第一行显示,距离系统爆炸时间第二行显示,还剩第三行显示,00时,00分,00秒第四行显示,周林按下旋钮开关,开始设定秒,秒编辑位闪动。左右旋转设定数值,在按下开关,秒确定,设定分,分编辑位闪动左右调数值,再按下,分确定,小时位闪动。左右转调小时,再按一下。时间确定,开始倒计时。到达定时时间后停止,并闪动。#includereg52.h#includeintrins.h/*********************定义********************************/#defineLCD_dataP0//数据口sbitLCD_RS=P2^0;//寄存器选择输入sbitLCD_RW=P2^1;//液晶读/写控制sbitLCD_EN=P2^2;//液晶使能控制sbitLCD_PSB=P2^3;//串/并方式控制sbitLCD_RST=P2^5;//液晶复位端口sbitsset=P3^4;//设置按钮sbitspk=P1^4;sbitsadd=P2^7;sbitsdel=P2^6;signedcharsecond=0,minute=0,hour=0,count=0,keycount=0;signedcharkey=0;unsignedcharcodeDIS1[]={距离系统爆炸时间};unsignedcharcodeDIS2[]={还剩:};unsignedcharcodeDIS31[]={时};unsignedcharcodeDIS32[]={分};unsignedcharcodeDIS33[]={秒};unsignedcharcodeDIS4[]={周林};unsignedcharcodetable2[]=0123456789;charcodereserve[3]_at_0x3b;//保留0x3b开始的3个字节/*********************延时子函数**************************/voiddelayNOP()//us延时子函数(4.34us){_nop_();_nop_();_nop_();_nop_();}voiddelayms(unsignedintms)//ms延时子函数{unsignedchary;while(ms--){for(y=0;y114;y++);}}//******************检查LCD忙状态**************************bitlcd_busy()//检查LCD忙状态1忙,0闲{bitresult;LCD_RS=0;LCD_RW=1;LCD_EN=1;delayNOP();result=(bit)(P0&0x80);LCD_EN=0;return(result);}//***********************写指令数据***********************voidlcd_wcmd(unsignedcharcmd)//写指令数据到LCD{while(lcd_busy()){;}LCD_RS=0;LCD_RW=0;LCD_EN=1;P0=cmd;delayNOP();LCD_EN=0;}voidlcd_wdat(unsignedchardat)//写显示数据到LCD{while(lcd_busy());LCD_RS=1;LCD_RW=0;LCD_EN=1;P0=dat;delayNOP();LCD_EN=0;}voidwritesmh(unsignedcharadd,unsignedchardate)//显示个位和十位数据{unsignedcharshi,ge;shi=date/10;ge=date%10;lcd_wcmd(0x80+add);delayms(5);lcd_wdat(table2[shi]);delayms(5);lcd_wdat(table2[ge]);delayms(5);}//*********************初始化设定****************************voidlcd_init(){LCD_PSB=1;//并口方式LCD_RST=0;//液晶复位delayms(5);//液晶复位LCD_RST=1;//液晶复位delayms(5);//液晶复位lcd_wcmd(0x34);//扩充功能设定,扩充指令,选8位数据delayms(5);lcd_wcmd(0x30);//功能设定,基本指令操作,选8位数据delayms(5);lcd_wcmd(0x0C);//显示开,关光标,关光标反白允许。delayms(5);lcd_wcmd(0x04);//光标的移动方向delayms(5);lcd_wcmd(0x01);//清除LCD的显示内容delayms(5);TMOD=0x01;TH0=0x3C;TL0=0xB0;EA=1;ET0=1;TR0=0;EX0=0;//外部中断0允许IT0=1;//外部中断0为边沿触发方式}//*********************设定显示位置子函数**********************voidlcd_pos(unsignedcharX,unsignedcharY)//设定显示位置{unsignedcharpos;if(X==1){X=0x80;}elseif(X==2){X=0x90;}elseif(X==3){X=0x88;}elseif(X==4){X=0x98;}pos=X+Y;//确定显示地址lcd_wcmd(pos);//写显示地址}//*****************清屏子函数***************************voidclr_screen()//清屏子函数{lcd_wcmd(0x34);//扩充指令操作delayms(5);lcd_wcmd(0x30);//基本指令操作delayms(5);lcd_wcmd(0x01);//清屏delayms(5);}//******************闪烁子函数**************************voidlcdflag(){lcd_wcmd(0x08);//关闭显示delayms(500);lcd_wcmd(0x0c);//开启显示delayms(500);lcd_wcmd(0x08);delayms(500);lcd_wcmd(0x0c);delayms(500);lcd_wcmd(0x08);delayms(500);lcd_wcmd(0x0c);delayms(500);}//******************写字符串子函数*************************voidwr_string(unsignedcharstr[])//写字符串子函数{unsignedcharnum=0;while(str[num]){lcd_wdat(str[num++]);//写数据}}//********************按键设置时间*************************voidkeyscan()//按键设置时间{if(sset==0){delayms(10);if(sset==0){EX0=1;keycount++;while(sset==0){;}if(keycount==1){TR0=0;//停止计时key=1;keycount=2;lcd_wcmd(0x8e);lcd_wcmd(0x0d);//秒位闪烁}if(keycount==2){;}if(keycount==3){keycount=4;lcd_wcmd(0x8c);lcd_wcmd(0x0d);//分位闪烁//lcd_wcmd(0x04);光标左移一位}if(keycount==4){;}if(keycount==5){keycount=6;lcd_wcmd(0x8a);lcd_wcmd(0x0d);//时位闪烁}if(keycount==6){;}if(keycount==7){keycount=0;lcd_wcmd(0x0c);//停止闪烁TR0=1;//开始计时EX0=1;}}}if(keycount!=0){if(keycount==2)//编辑秒{if(sadd==0)//数值加1{delayms(10);if(sadd==0){second++;if(second==60){second=0;}sadd=1;}writesmh(0x0e,second);lcd_wcmd(0x8e);lcd_wcmd(0x0d);//秒位闪烁}if(sdel==0)//数值减1{delayms(10);if(sdel==0){if(second==0){second=60;}second--;sdel=1;}writesmh(0x0e,second);lcd_wcmd(0x8e);lcd_wcmd(0x0d);//秒位闪烁}}if(keycount==4)//编辑分钟{if(sadd==0)//数值加1{delayms(10);;if(sadd==0){minute++;if(minute==60){minute=0;}sadd=1;}writesmh(0x0c,minute);lcd_wcmd(0x8c);lcd_wcmd(0x0d);//分位闪烁}if(sdel==0)//数值减1{delayms(10);if(sdel==0){if(minute==0){minute=60;}minute--;sdel=1;}writesmh(0x0c,minute);lcd_wcmd(0x8c);lcd_wcmd(0x0d);//分位闪烁}}if(keycount==6)//编辑小时{if(sadd==0)//数值加1{delayms(10);if(sadd==0){hour++;if(hour==24){minute=0;}sadd=1;}writesmh(0x0a,hour);lcd_wcmd(0x8a);lcd_wcmd(0x0d);//时位闪烁}if(sdel==0)//数值减1{delayms(10);if(sdel==0){if(hour==0){hour=24;}hour--;sdel=1;}writesmh(0x0a,hour);lcd_wcmd(0x8a);lcd_wcmd(0x0d);//时位闪烁}}}}//主函数主函数主函数主函数主函数主函数主函数主函数主函数主函数主函数主函数主函数主函数voidmain(){delayms(100);//上电,等待稳定lcd_init();//初始化LCDlcd_pos(1,0);//设置显示位置为第一行wr_string(DIS1);lcd_pos(2,0);//设置显示位置为第二行wr_string(DIS2);lcd_pos(3,3);//设置显示位置为第四行wr_string(DIS31);lcd_pos(3,5);//设置显示位置为第四行wr_string(DIS32);lcd_pos(3,7);//设置显示位置为第四行wr_string(DIS33);lcd_pos(4,6);//设置显示位置为第四行wr_string(DIS4);writesmh(0x0e,second);delayms(5);writesmh(0x0c,minute);delayms(5);writesmh(0x0a,hour);delayms(5);while(1){keyscan();}}//**********************中断***************************voidtime0()interrupt1using0//倒计时中断{TH0=0x3C;TL0=0xB0;count++;if(count==20){count=0;second--;if(second0){second=59;minute--;if(minute0){minute=59;hour--;if(ho