基于51单片机的1602液晶显示温度和时间的C程序

合集下载

基于51单片机的1602液晶显示温度和时间的C程序

基于51单片机的1602液晶显示温度和时间的C程序

//液晶显示温度#include "AT89X52.H"#define Ddata P0sbit RS=P2^7; //命令数据控制端sbit RW=P2^6; //读写选择端sbit LCDE=P2^5; //液晶使能端sbit DQ=P2^0; //ds18b20与单片机连接口#define uchar unsigned char#define uint unsigned intunsigned char hour=0,min=0,sec=0; //定义初值unsigned int count=0;unsigned char line1[16]={" temp: "}; //16个字符unsigned char line2[16]={" time: 00:00:00"}; //16个字符unsigned char tab[]={'0','1','2','3','4','5','6','7','8','9'}; //数组uchar data disdata[5];uint tvalue; //温度值uchar tflag; //温度正负标志void time();/*************************lcd1602程序**************************/ void delay1ms(unsigned int ms)//延时1毫秒(不够精确的){unsigned int i,j;for(i=0;i<ms;i++)for(j=0;j<110;j++);}void delay5ms()//延时5毫秒(不够精确的){unsigned int i;for (i=0;i<1000;i++);}void delay50us(){register int i;for (i=0;i<20;i++);}void delay(){unsigned char m,n;for(m=255;m>0;m--)for(n=255;n>0;n--);}void wr_com(unsigned char comm) //********写控制字符程序E=1 RS=0RW=0 **********//{LCDE=0; //使能端RS=0; //********RS寄存器选择输入端,当RS=0;当进行写模块操作,指向指令寄存器。

51单片机万年历1602显示时间温度按键调时(焊接前备份)

51单片机万年历1602显示时间温度按键调时(焊接前备份)
hmin_fen++;
}
if(hmin_fen==6)
{
hmin_fen=0;
//nmin_shi++;
}
ZuiZhong[13]=l_tmpdate[1]%16+0x30;
ZuiZhong[14]=':';
ZuiZhong[15]=l_tmpdate[0]/16+0x30;
ZuiZhong[16]=l_tmpdate[0]%16+0x30;
ZuiZhong[17]=' ';
/* ZuiZhong[18]=Temperature%1000/100+0x30;
ZuiZhong[19]=Temperature%100/10+0x30;
ZuiZhong[20]=Temperature%10+0x30;
ZuiZhong[21]=0xdf;
ZuiZhong[22]='C'; */
write_lcd1602(0x80+0x43,0); // 20
for(i=23;i<=24;i++)
write_lcd1602( ZuiZhong[i], 1 );
if(K1==0)
#include <reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^6; // 【1602显示引脚定义】
sbit rw=P2^5; //【【【【【7-6-5顺序

51单片机c语言编写的1602液晶显示闹钟

51单片机c语言编写的1602液晶显示闹钟

#include<reg52.h>#define uchar unsigned char#define uint unsigned intuchar code table[]=" 2012-08-17 MON"; //设定开始显示的时间uchar code table1[]=" 12:30:00";uchar code table2[7][3]= {{"MON"},{"TUE"},{"WED"},{"THU"},{"FRI"},{"SAT"},{"SUN"}};//定义二维字符数组sbit wr=P3^6; // 1602液晶的读/写端sbit dula=P2^6; // 数码管段选sbit wela=P2^7; // 数码管位选sbit lcden=P3^4; // 1602液晶使能端sbit lcdrs=P3^5; // 1602液晶命令/数据端sbit key1=P3^0; // 声明s4按键sbit key2=P3^1; // 声明s8按键sbit key3=P3^2; // 声明s12按键sbit key4=P3^3; // 声明s16按键sbit beep=P2^3; // 声明蜂鸣器sbit rd=P3^7; // 声明4个按键的公共端uchar num1,shi=12,fen=30,miao,yue=8,ri=17,shi1,fen1,miao1,keynum1,week; //定义时分秒变量uint nian=2012; //定义年变量void delayms(uint x){uint i,j;for(i=x;i>0;i--) //定义带参数延时函数for(j=110;j>0;j--);}void write_com(uchar com) //1602液晶写指令函数{ wr=0;lcdrs=0;P0=com;delayms(1);lcden=1;delayms(1);lcden=0;}void write_date(uchar date) //1602液晶写数据函数{ wr=0;lcdrs=1;P0=date;delayms(1);lcden=1;delayms(1);lcden=0;}void write_addate1(uchar add,uint date) //定义显示年的函数{ uint qian;uchar bai,shi,ge;qian=date/1000;bai=date%1000/100;shi=date%1000%100/10;ge=date%10;write_com(add+0x80);write_date(0x30+qian);write_date(0x30+bai);write_date(0x30+shi);write_date(0x30+ge);}void write_addate2(uchar add,uchar date) //定义显示月、日、时、分、秒函数{uchar shi,ge;shi=date/10;ge=date%10;write_com(add+0x80);write_date(0x30+shi);write_date(0x30+ge);}void write_rq(uchar i) //定义显示星期的函数{ uchar x;for(x=0;x<3;x++){ write_date(table2[i][x]);delayms(1);}}void init () //定义初始化函数{dula=0; // 关闭数码管段、位选端wela=0;TMOD=0x01; // 定时器0工作方式为16位TH0=(65536-45872)/256; // 装初值TL0=(65536-45872)%256;EA=1; // 开总中断ET0=1; // 开定时器0中断write_com(0x38); // 设置16*2显示,5*7点阵,8位数据口write_com(0x06); // 写一个字符后地址指针加1write_com(0x0c); // 设置开显示,不显示光标write_com(0x01); // 显示清0,数据清0write_com(0x80); // 使指针指向第1行第1列}void keyscan() // 定义按键函数{uchar keynum;rd=0; //4个按键公共端为低电平if(key1==0) //s4按键被按下{ delayms(3); //去抖延时if(key1==0) //确认s4按键被按下,进入调时间模式{keynum++;while(!key1); //松手检测}if(keynum==1) //s4按下一次{ TR0=0; //关闭定时器0write_com(0x81); //指针指向第1行2列write_com(0x0f); //开启光标显示,光标闪烁}if(keynum==2){write_com(0x86); //光标跳到月份}if(keynum==3){write_com(0x89); //光标跳到日}if(keynum==4){write_com(0x8c); //光标跳到星期}if(keynum==5){write_com(0xc4); //光标跳到时}if(keynum==6){write_com(0xc7); //光标跳到分}if(keynum==7){write_com(0xca); //光标跳到秒}if(keynum==8){ keynum=0; //退出调时间模式TR0=1; //开启定时器0write_com(0x0c); //关闭光标显示}}if(keynum) //当进入调时间模式时{if(key2==0) //s8按键按下{delayms(3); //去抖延时if(key2==0){if(keynum==1){nian++; //s8每按下一次年份加1if(nian==10000) // 加满清0nian=0;write_addate1(1,nian); // 显示年份write_com(0x81); //写完数据指针指向年份原来位置}if(keynum==2){yue++; //s8每按下一次月份加1if(yue==13)yue=0; //加满清0write_addate2(6,yue); //显示月份write_com(0x86); //写完数据指针指向月份原来位置}if(keynum==3){ ri++;if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12) //1,3,5,7,8,10,12月有31天if(ri==32)//加满还原ri=1;if(yue==4||yue==6||yue==9||yue==11) //4,6,9,11月有30天if(ri==31)ri=1;if(yue==2&&((nian%4==0&&nian%100!=0)||nian%400==0)) //闰年2月有30天if(ri==30)ri=1;if(yue==2&&(nian%4!=0)||(nian%100==0&&nian%400!=0)) //非闰年2月有31天if(ri==29)ri=1;write_addate2(9,ri); //显示日write_com(0x89); //写完数据指针指向日原来位置}if(keynum==4){week++;//s8每按下一次星期加1if(week==7)//加满还原week=0;write_rq(week); //显示星期write_com(0x8c); //写完数据指针指向星期原来位置}if(keynum==5){shi++;//s8每按下一次时加1if(shi==24)shi=0;//加满还原write_addate2(0x44,shi); //显示时write_com(0xc4); //写完数据指针指向时原来位置}if(keynum==6){fen++;//s8每按下一次分加1if(fen==60)fen=0;//加满还原write_addate2(0x47,fen); //显示分write_com(0xc7);//写完数据指针指向分原来位置}if(keynum==7){miao++;//s8每按下一次秒加1if(miao==60)miao=0;//加满还原write_addate2(0x4a,miao);//显示秒write_com(0xca);//写完数据指针指向秒原来位置}while(!key2); //松手检测}}if(key3==0){delayms(3);if(key3==0){ //s12按下时减1if(keynum==1){nian--;if(nian==-1)nian=9999;write_addate1(1,nian);write_com(0x81);}if(keynum==2){ yue--;if(yue==-1)yue=12;write_addate2(6,yue);write_com(0x86);}if(keynum==3){ri--;if(ri==0){if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)ri=31;if(yue==4||yue==6||yue==9||yue==11)ri=30;if(yue==2&&((nian%4==0&&nian%100!=0)||nian%400==0))ri=29;if(yue==2&&(nian%4!=0)||(nian%100==0&&nian%400!=0))ri=28;}write_addate2(9,ri);write_com(0x89);}if(keynum==4){week--;if(week==-1)week=6;write_rq(week);write_com(0x8c);}if(keynum==5){shi--;if(shi==-1)shi=23;write_addate2(0x44,shi);write_com(0xc4);}if(keynum==6){fen--;if(fen==-1)fen=59;write_addate2(0x47,fen);write_com(0xc7);}if(keynum==7){miao--;if(miao==-1)miao=59;write_addate2(0x4a,miao);write_com(0xca);}while(!key3); //松手检测}}}if(key4==0) //s16被按下进入调闹钟模式{ delayms(3);if(key4==0){keynum1++;while(!key4);}if(keynum1==1){ write_com(0x01); //清屏,指针清0write_com(0xc6); //在时分之间显示“:”write_date(':');write_com(0xc9); //在分秒之间显示“:”write_date(':');write_com(0x0f); //开启光标write_com(0xc4); //光标指向时}if(keynum1==2){write_com(0xc7);//光标指向分}if(keynum1==3){write_com(0xca); //光标指向秒}if(keynum1==4){ keynum1=0;write_com(0x0c); //关闭光标}}if(keynum1){if(key2==0) //s8按下加1{delayms(3);if(key2==0){if(keynum1==1){shi1++;if(shi1==24)shi1=0;write_addate2(0x44,shi1);write_com(0xc4);}if(keynum1==2){fen1++;if(fen1==60)fen1=0;write_addate2(0x47,fen1);write_com(0xc7);}if(keynum1==3){miao1++;if(miao1==60)miao1=0;write_addate2(0x4a,miao1);write_com(0xca);}while(!key2);}}if(key3==0) //s12每按一次减1{delayms(3);if(key3==0){if(keynum1==1){shi1--;if(shi1==-1)shi1=23;write_addate2(0x44,shi1);write_com(0xc4);}if(keynum1==2){fen1--;if(fen1==-1)fen1=59;write_addate2(0x47,fen1);write_com(0xc7);}if(keynum1==3){miao1--;if(miao1==-1)miao1=59;write_addate2(0x4a,miao1);write_com(0xca);}while(!key3);//松手检测}}}}void main(){ uchar num;init();for(num=0;num<15;num++){ write_date(table[num]);delayms(5);} //显示初始时间write_com(0xc0);for(num=0;num<12;num++){ write_date(table1[num]);delayms(5);}TR0=1; //开启定时器0while(1){ if(shi==shi1&&fen==fen1&&miao==miao1) //闹钟到了蜂鸣器响beep=0;keyscan(); //扫描键盘}}void time() interrupt 1 //中断函数{TH0=(65536-45872)/256;TL0=(65536-45872)%256; //进入中断装初值num1++;if(num1==20) //一秒到了{ num1=0; //清0miao++;if(miao==60) //一分到了{ miao=0; fen++; }if(!keynum1) write_addate2(0x4a,miao); //当调闹钟模式按下时不显示当前时间if(fen==60){ fen=0;shi++;}if(!keynum1) write_addate2(0x47,fen); //当调闹钟模式按下时不显示当前时间if(shi==24){shi=0;ri++;week++;}if(!keynum1) write_addate2(0x44,shi); //当调闹钟模式按下时不显示当前时间if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)if(ri==32){ ri=1; y ue++; }if(yue==4||yue==6||yue==9||yue==11)if(ri==31){ ri=1; yue++; }if(yue==2&&((nian%4==0&&nian%100!=0)||nian%400==0))if(ri==30){ ri=1; yue++; }if(yue==2&&(nian%4!=0)||(nian%100==0&&nian%400!=0))if(ri==29){ ri=1; yue++; }if(!keynum1) write_addate2(9,ri);//当调闹钟模式按下时不显示当前时间if(week==7)week=0;if(!keynum1)//当调闹钟模式按下时不显示当前时间{ write_com(0x8c);write_rq(week);}if(yue==13){ yue=0;nian++;}if(!keynum1)//当调闹钟模式按下时不显示当前时间{write_addate2(6,yue);write_com(0x88);write_date('-');}if(nian==10000)nian=0;if(!keynum1){ write_addate1(1,nian);//当调闹钟模式按下时不显示当前时间write_com(0x85);write_date('-');}}}。

51单片机控制1602LCD显示程序

51单片机控制1602LCD显示程序

LCD显示电路#include<reg51.h>sbit RS=P3^7; //寄存器选择位,将RS位定义为P2.0引脚sbit RW=P3^6; //读写选择位,将RW位定义为P2.1引脚sbit E=P2^7; //使能信号位,将E位定义为P2.2引脚sbit BF=P0^7; //忙碌标志位,将BF位定义为P0.7引脚#define Lcd_Data P0#include <string.h>#include<intrins.h> //包含_nop_()函数定义的头文件unsigned char code string1[ ]={0x77,0x75,0x20,0x79,0x61,0x6E,0x67,0x20,0x79,0x61,0x6E,0x67,0x20,0x20,0x20,0x20}; //第一行显示的字符void Lcd_delay1ms() // 函数功能:延时1ms//注:不同单片机不同晶振需要对此函数进行修改{ unsigned char i,j;for(i=0;i<90;i++)for(j=0;j<33;j++);}void Lcd_delay(unsigned int n) // 函数功能:延时若干毫秒,入口参数:n{ unsigned int i;for(i=0;i<n;i++)Lcd_delay1ms();}/*****************************************************函数功能:判断液晶模块的忙碌状态返回值:result。

result=1,忙碌;result=0,不忙***************************************************/bit Lcd_BusyTest(void)bit result;RS=0; //根据规定,RS为低电平,RW为高电平时,可以读状态RW=1;E=1; //E=1,才允许读写_nop_(); //空操作_nop_();_nop_();_nop_(); //空操作四个机器周期,给硬件反应时间result=BF; //将忙碌标志电平赋给resultE=0;return result;}/*****************************************************函数功能:将模式设置指令或显示地址写入液晶模块入口参数:dictate***************************************************/void Lcd_WriteCom (unsigned char dictate){ while(Lcd_BusyTest()==1); //如果忙就等待RS=0; //根据规定,RS和R/W同时为低电平时,可以写入指令RW=0;E=0; //E置低电平(写指令时就是让E从0到1发生正跳变,所以应先置ぜ? _nop_();_nop_(); //空操作两个机器周期,给硬件反应时间Lcd_Data=dictate; //将数据送入P0口,即写入指令或地址_nop_();_nop_();_nop_();_nop_(); //空操作四个机器周期,给硬件反应时间E=1; //E置高电平_nop_();_nop_();_nop_();_nop_(); //空操作四个机器周期,给硬件反应时间E=0; //当E由高电平跳变成低电平时,液晶模块开始执行命令}/*****************************************************函数功能:指定字符显示的实际地址x入口参数:注:此函数已经加上了0x80,故只需写上实际地址就行***************************************************/void Lcd_WriteAddress(unsigned char x){ Lcd_WriteCom(x|0x80); //显示位置的确定方法规定为80H+地址码x/*****************************************************函数功能:将数据(字符的标准ASCII码)写入液晶模块入口参数:y(为字符常量)***************************************************/void Lcd_WriteData(unsigned char y){while(Lcd_BusyTest()==1);RS=1; //RS为高电平,RW为低电平时,可以写入数据RW=0;E=0; //E置低电平(写指令时就是让E从0到1发生正跳变所以应先置ぜ?Lcd_Data=y; //将数据送入P0口,即将数据写入液晶模块_nop_();_nop_();_nop_();_nop_(); //空操作四个机器周期,给硬件反应时间E=1; //E置高电平_nop_();_nop_();_nop_();_nop_(); //空操作四个机器周期,给硬件反应时间E=0; //当E由高电平跳变成低电平时,液晶模块开始执行命令}/*****************************************************函数功能:对LCD的显示模式进行初始化设置***************************************************/void Lcd_Int(void){Lcd_delay(15); //延时15ms,首次写指令时应给LCD一段较长的反应时间Lcd_WriteCom(0x38); //显示模式设置:16×2显示,5×7点阵,8位数据接口Lcd_delay(5); //延时5msLcd_WriteCom(0x38); //9次写设置模式Lcd_delay(5);Lcd_WriteCom(0x38); //9次写设置模式Lcd_delay(5);Lcd_WriteCom(0x38); //9次写设置模式Lcd_delay(5);Lcd_WriteCom(0x38); //9次写设置模式Lcd_delay(5);设置模式次写9// Lcd_WriteCom(0x38);Lcd_delay(5);Lcd_WriteCom(0x38); //9次写设置模式Lcd_delay(5);Lcd_WriteCom(0x38); //9次写设置模式Lcd_delay(5);Lcd_WriteCom(0x38); //9次写设置模式Lcd_delay(5);Lcd_WriteCom(0x38); //9次写设置模式Lcd_delay(5);Lcd_WriteCom(0x38); //9次写设置模式Lcd_delay(5);Lcd_WriteCom(0x38); //9次写设置模式Lcd_delay(5);Lcd_WriteCom(0x0C); //显示模式设置:显示开,有光标,光标闪烁Lcd_delay(5);Lcd_WriteCom(0x06); //显示模式设置:光标右移,字符不移Lcd_delay(5);Lcd_WriteCom(0x01); //清屏幕指令,将以前的显示内容清零Lcd_delay(5); }void hanying_show(void){unsigned char Lcd_i;Lcd_WriteCom(0x01);//清显示:清屏幕指令Lcd_delay(2);Lcd_WriteAddress(0x00); // 设置显示位置为最左侧Lcd_delay(2);Lcd_i=0;while(string1[Lcd_i]!='\0') //'\0'是数组结束标志需先将字符存入{Lcd_WriteData(string1[Lcd_i]); // 显示字符Lcd_i++;Lcd_delay(4);}}void main(){Lcd_Int(); //1602初始化while(1){hanying_show();}}。

基于51单片机的1602液晶屏的时钟显示完整程序

基于51单片机的1602液晶屏的时钟显示完整程序

//基于51单片机的1602液晶屏的时钟显示完整程序#include <reg52.h>#define uchar unsigned char#define uint unsigned intsbit en=P2^6;sbit rs=P2^4;sbit rw=P2^5;uchar count,shi,fen,miao;void delay(unsigned int);void init();void write_com(unsigned char);void write_date(unsigned char);void write_sfm(uchar,uchar);uchar code table[]="2013-2-15";uchar code table1[]="23:59:55";void main(){init();while(1){if(count==20){count=0;miao++;if(miao==60){miao=0;fen++;if(fen==60){fen=0;shi++;if(shi==24){shi=0;}write_sfm(1,shi);}write_sfm(3,fen);}write_sfm(6,miao);}}// while(1);}void delay(unsigned int z) // 延时函数{unsigned int x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void init(){unsigned char num;en=0;rw=0;write_com(0x38); //设置点阵write_com(0x0c); //设置光标不闪烁write_com(0x06); //设置光标地址后移write_com(0x01); //清屏write_com(0x80); // 表示从第一行开始显示for(num=0;num<9;num++){write_date(table[num]);delay(20);}write_com(0x80+0x40);for(num=0;num<8;num++){write_date(table1[num]);delay(20);}TMOD=0x01;TH0=(65536-50000)/256;TL0=(65535-50000)%256;EA=1;ET0=1;TR0=1;}void write_com(unsigned char com) //送指令{rs=0;rw=0;en=0;P0=com;delay(5);en=1;delay(5);en=0;}void write_date(unsigned char date) //送数据{rs=1;en=0;rw=0;P0=date;delay(5);en=1;delay(5);en=0;}void time0() interrupt 1{TH0=(65536-50000)/256;TL0=(65535-50000)%256;count++;}void write_sfm(uchar add,uchar date) //将数据分离{unsigned char shi,ge;shi=date/10;ge=date%10;write_com(0x80+0x40+add);write_date(0x30+shi); //将数值转化为十六进制0是30。

51单片机计算器1602显示程序

51单片机计算器1602显示程序
{
delay_jsq(5);
if(P2!=0xfb)
{
if(P2!=0xfb)
{
temp=P2&0xf0;
switch(temp)
{
case 0xe0:num=8;
break;
case 0xd0:num=9;
break;
case 0xb0:num=10;
break;
case 0x70:num=11;
case 0x70:num=7;
break;
}
}
while(P2!=0xfd);
if(j!=0)
{
write_lcd1602(0x01,0);
delay(1);
j=0;
}
if(num==4||num==5||num==6&&num!=7)//如果按下的是'4','5'或'6'
{
if(flag1==0)//没有按过符号键
break;
}
}
}
}
void main()
{
ini_lcd1602();
while(1)
{
keyscan_4_4();
}
}
write_lcd1602(0x06,0);
delay(1);
write_lcd1602(0x01,0);
delay(1);
num_1=0;
i=0;
j=0;
a=0; //第一个参与运算的数
b=0; //第二个参与运算的数
c=0;
flag1=0; //flag1表示是否有符号键按下,
fuhao=0; // fuhao表征按下的是哪个符号

基于51单片机的1602液晶时钟设计的C程序(闰年补偿,月份调整)

基于51单片机的1602液晶时钟设计的C程序(闰年补偿,月份调整)

#include<reg52.h>#define uchar unsigned char#define uint unsigned intuchar code table[]=" 2012-02-29 MON";uchar code table1[]=" 23:59:55";uchar num,count,shi,fen,miao,nian,yue,ri,xingqi,s1num,flag; sbit lcden=P2^5;sbit lcdrs=P1^0;sbit lcdrw=P1^1;sbit s1=P3^0;sbit s2=P3^1;sbit s3=P3^2;sbit s4=P3^3;sbit feng=P2^3;/***************延迟函数***************/void delay(uint z){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}/**********写命令***************/void write_com(uchar com){lcdrs=0;lcdrw=0;P0=com;lcden=1;delay(5);lcden=0;}/**************写数据****************/void write_data(uchar date){lcdrs=1;lcdrw=0;P0=date;lcden=1;delay(5);lcden=0;}/*************时分秒调节函数********************/void write_sfm(uchar add,uchar date){uchar shi,ge;shi=date/10;ge=date%10;write_com(0x80+0x40+add);write_data(0x30+shi);write_data(0x30+ge);}/****************年月日调节函数***************/void write_nyr(uchar ad,uchar dat){uchar shi,ge;shi=dat/10;ge=dat%10;write_com(0x80+0x00+ad);write_data(0x30+shi);write_data(0x30+ge);}/**************************星期显示函数*****************/void display_week(){switch (xingqi){case 1: write_data('M');write_data('O');write_data('N');write_com(0x80+0x00+14);break;case 2: write_data('T');write_data('U');write_data('E');write_com(0x80+0x00+14);break;case 3: write_data('W');write_data('E');write_data('D');write_com(0x80+0x00+14);break;case 4: write_data('T');write_data('H');write_data('U');write_com(0x80+0x00+14);break;case 5: write_data('F');write_data('R');write_data('I');write_com(0x80+0x00+14);break;case 6: write_data('S');write_data('A');write_data('T');write_com(0x80+0x00+14);break;case 7: write_data('S');write_data('U');write_data('N');write_com(0x80+0x00+14);break;}}void keycan(){/****************光标左移***************/if(s1==0){delay(5);if(s1==0){s1num++;while(!s1);if(s1num==1){TR0=0;write_com(0x80+0x40+11);write_data('<');write_com(0x80+0x40+10);//光标重新回到闪烁点,写光标地址同样加一write_com(0x0f); //光标闪烁}if(s1num==2){write_com(0x80+0x40+11);write_data(' ');write_com(0x80+0x40+8);write_data('<');write_com(0x80+0x40+7);}if(s1num==3){write_com(0x80+0x40+8);write_data(':');write_com(0x80+0x40+5);write_data('<');write_com(0x80+0x40+4);}if(s1num==4){write_com(0x80+0x40+5);write_data(':');write_com(0x80+15);write_data('<');write_com(0x80+14);}if(s1num==5){write_com(0x80+15);write_data(' ');write_com(0x80+11);write_data('<');write_com(0x80+10);}if(s1num==6){write_com(0x80+11);write_data(' ');write_com(0x80+8);write_data('<');write_com(0x80+7); }if(s1num==7){write_com(0x80+8);write_data('-');write_com(0x80+5);write_data('<');write_com(0x80+4); }if(s1num==8){s1num=0;write_com(0x80+5);write_data('-');write_com(0x0c);TR0=1;}}}/**************光标右移***************//* if(s2==0){delay(5);if(s2==0){s1num--;while(!s2);if(s1num==1){TR0=0;write_com(0x80+0x40+11);write_data('<');write_com(0x80+0x40+11);//光标重新回到闪烁点,写光标地址同样加一write_com(0x0f); //光标闪烁}if(s1num==2){write_com(0x80+0x40+11);write_data(' ');write_com(0x80+0x40+8);write_data('<');write_com(0x80+0x40+8);}if(s1num==3){write_com(0x80+0x40+8);write_data(':');write_com(0x80+0x40+5);write_data('<');write_com(0x80+0x40+5);}if(s1num==4){write_com(0x80+0x40+5);write_data(':');write_com(0x80+15);write_data('<');write_com(0x80+15);}if(s1num==5){write_com(0x80+15);write_data(' ');write_com(0x80+11);write_data('<');write_com(0x80+11);}if(s1num==6){write_com(0x80+11);write_data(' ');write_com(0x80+8);write_data('<');write_com(0x80+8);}if(s1num==7){write_com(0x80+8);write_data('-');write_com(0x80+5);write_data('<');write_com(0x80+5);}if(s1num==8){s1num=0;write_com(0x80+5);write_data('-');write_com(0x0c);TR0=1;}}} *//***************************按键加*******************************/if(s1num!=0){if(s3==0){delay(5);if(s3==0){while(!s3);if(s1num==1){miao++;if(miao==60)miao=0;write_sfm(9,miao);write_com(0x80+0x40+10);}if(s1num==2){fen++;if(fen==60)fen=0;write_sfm(6,fen);write_com(0x80+0x40+7);}if(s1num==3){shi++;if(shi==24)shi=0;write_sfm(3,shi);write_com(0x80+0x40+4);}if(s1num==4){xingqi++;if(xingqi==8)xingqi=1;write_com(0x80+0x00+12);display_week();}if(s1num==5){ri++;if(ri==32&&(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)){ri=1;yue++;if(yue==13){yue=1;nian++;write_nyr(3,nian);}write_nyr(6,yue);}if(ri==31&&(yue==4||yue==6||yue==9||yue==11)){ri=1;yue++;write_nyr(6,yue);}if(flag==1){if(ri==30&&yue==2){ri=1;yue++;write_nyr(6,yue);}}if(flag==0){if(ri==29&&yue==2){ri=1;yue++;write_nyr(6,yue);}}write_nyr(9,ri);write_com(0x80+0x00+10);}if(s1num==6){yue++;if(yue==2&&ri>29&&flag==1) //当为闰年时,如果ri=30,但是月份已经加到2,这种情况ri必须等于29{ri=29;write_nyr(9,ri);}if(yue==2&&ri>28&&flag==0) //当为平年时,如果ri=30,但是月份已经加到2,这种情况ri必须等于28{ri=28;write_nyr(9,ri);}if((yue==4||yue==6||yue==9||yue==11)&&(ri==31||ri==29||ri==28)){ri=1;write_nyr(9,ri);}if(yue==13){yue=1;nian++;if(nian%4>0){write_nyr(9,28);}write_nyr(3,nian);}write_nyr(6,yue);write_com(0x80+0x00+7);}if(s1num==7){nian++;if(nian==100)nian=0;flag=0;if(nian%4==0)flag=1;write_nyr(3,nian);write_com(0x80+0x00+4);}}}}/***************************按键减*********************/if(s4==0){delay(5);if(s4==0){while(!s4);if(s1num==1){miao--;if(miao==-1)miao=59;write_sfm(9,miao);write_com(0x80+0x40+10);}if(s1num==2){fen--;if(fen==-1)fen=59;write_sfm(6,fen);write_com(0x80+0x40+7);}if(s1num==3){shi--;if(shi==-1)shi=23;write_sfm(3,shi);write_com(0x80+0x40+4);}if(s1num==4){xingqi--;if(xingqi==0)xingqi=7;write_com(0x80+0x00+12);display_week();}if(s1num==5){ri--;if(ri==0&&(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)){ri=30; //防止当yue=12时,当yue减到11时,ri任然等于31时这种不允许情况出现yue--;if(yue==2){if(flag==1)ri=29;elseri=28;}write_nyr(6,yue);if(yue==0){yue=12;ri=31;write_nyr(9,ri);write_nyr(6,yue);}}if(ri==0&&(yue==4||yue==6||yue==9||yue==11)){ri=31;//按下减小按钮,yue 减到3或5或8或10,为大月yue--;write_nyr(6,yue);}if(flag==1) //闰年标志位{if(ri==0&&yue==2){ri=29;yue--;write_nyr(6,yue);}}if(flag==0) //平年标志位{if(ri==0&&yue==2){ri=28;yue--;write_nyr(6,yue);}}write_nyr(9,ri);write_com(0x80+0x00+10);}if(s1num==6){yue--;if(yue==2&&ri>29&&flag==1){ri=29;write_nyr(9,ri);}if(yue==2&&ri>28&&flag==0){ri=28;write_nyr(9,ri);}if(yue==0){yue=12;nian--;write_nyr(3,nian);}write_nyr(6,yue);if((yue==4||yue==6||yue==9||yue==11)&&(ri==31||ri==29||ri==28)){ri=1;write_nyr(9,ri);}write_com(0x80+0x00+7);}if(s1num==7){nian--;if(nian==-1)nian=99;flag=0; //清除标志位if(nian%4==0)flag=1;write_nyr(3,nian);write_com(0x80+0x00+4);}}}}/************初始化**************/void init(){write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);write_com(0x80+0x00);for(num=0;num<15;num++){write_data(table[num]);delay(5);}write_com(0x80+0x40);for(num=0;num<11;num++){write_data(table1[num]);delay(5);}shi=23;fen=59;miao=55;xingqi=1;ri=29;yue=2;nian=12;if(nian%4==0)flag=1;TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;}/***********主函数*************/ void main(){init(); //初始化while(1){keycan();}}/******启动定时器*************/void timer() interrupt 1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;count++;if(count==18){count=0;miao++;if(miao==60){miao=0;fen++;if(fen==60){fen=0;feng=0;delay(500);feng=1;shi++;if(shi==24){shi=0;ri++;xingqi++;if(xingqi==8)xingqi=1;write_com(0x80+0x00+12);display_week();if(ri==32&&(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)){ri=1;yue++;if(yue==13){yue=1;nian++;if(nian==99)nian=0;if(nian%4==0)flag=1;flag=0;write_nyr(3,nian);}write_nyr(6,yue);}if(ri==31&&(yue==4||yue==6||yue==9||yue==11)){ri=1;yue++;write_nyr(6,yue);}if(flag==1){if(ri==30&&yue==2){ri=1;yue++;write_nyr(6,yue);}}if(flag==0){if(ri==29&&yue==2).专业整理..学习帮手.{ri=1;yue++;write_nyr(6,yue);}}write_nyr(9,ri);}write_sfm(3,shi);}write_sfm(6,fen);}write_sfm(9,miao);}}。

我的51单片机之 液晶1602 的C语言和汇编编程

我的51单片机之 液晶1602 的C语言和汇编编程

//三次显示模式设置,不检测忙信号
//显示模式设置,开始要求每次检测忙信号 //关闭显示 //显示清屏 // 显示光标移动设置 // 显示开及光标设置
//按指定位置显示一个字符
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
#include<reg51.h>
#include<intrins.h>
#define DBPort P1 //液晶数据口
sbit RW=P0^1; //寄存器选择,高电平时选择数据寄存器、低电平时选择指令寄存器
sbit RS=P0^0; //读写信号线,高电平时进行读操作,低电平时进行写操作。
sbit E=P0^2;
void main(void) {
Delay400Ms(); Init(); //LCM 初始化 DisplayListChar(0, 1, uctech); DisplayListChar(0, 0, net); while(1); }
//第二行第一列开始 //第一行第一列开始
//写数据 void WriteData(unsigned char WD) {
ReadStatus(); //检测忙 DBPort = WD; RS = 1; RW = 0; E = 0; //可加小延时 delay(); E = 1; }
//写指令 void WriteCommand(unsigned char WC,BuysC) {
if (BuysC) ReadStatus(); DBPort = WC; RS = 0; RW = 0; E = 0; E = 1; }

1602液晶显示时间与温度程序

1602液晶显示时间与温度程序

#include<reg52.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intsbit lcden=P2^7;sbit lcdrs=P2^5;sbit rw=P2^6;sbit DQ=P3^5;sbit key1=P3^0;sbit key2=P3^1;sbit key3=P3^2;sbit key4=P3^7;uchar count,key1num,key2num,key3num,shi,fen,miao; uchar num;uchar code table[]= {" 00:00:00 "};uchar code table1[]={" 000.0 C "};uchar code table2[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; uchar SegBuffer[4]={0};void Delay_15us(uint del){for(del;del>0;del--){_nop_();_nop_();_nop_();_nop_();}_nop_();_nop_();}bit RST_DS18B20(){bit ret=1;DQ=0;Delay_15us(39);DQ=1;Delay_15us(4);ret=DQ;Delay_15us(27);DQ=1;return ret;}void WBIT_DS18B20(bit data_bit){DQ=0;Delay_15us(0);DQ=data_bit;Delay_15us(2);DQ=1;}void WBYTE_DS18B20(uchar data_byte){uchar i;for(i=0;i<8;i++){WBIT_DS18B20((bit)(data_byte & 0x01));data_byte>>=1;}}bit RBIT_DS18B20(){bit data_bit;DQ=0;_nop_();_nop_();_nop_();DQ=1;_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();data_bit=DQ;Delay_15us(3);DQ=1;return data_bit;}uchar RBYTE_DS18B20(){uchar data_byte=0;uchar i,j;for(i=0;i<8;i++){j=RBIT_DS18B20();j=j<<i;data_byte=data_byte|j;}return data_byte;}void START_DS18B20(){DQ=1;_nop_();_nop_();RST_DS18B20();WBYTE_DS18B20(0xcc);WBYTE_DS18B20(0x44);}uint READ_TEM(){uint tem=0;DQ=1;_nop_();_nop_();RST_DS18B20();WBYTE_DS18B20(0xcc);WBYTE_DS18B20(0xbe);tem=RBYTE_DS18B20();tem=(RBYTE_DS18B20()<<8)|tem;return tem;}void Temperature_4(uint data_tem) {uint dat=0;float temp;if(data_tem&0x800){data_tem=~data_tem+1;temp=data_tem*0.0625;temp=temp*10+0.5;dat=temp;SegBuffer[0]=dat%10;SegBuffer[1]=dat/10%10;SegBuffer[2]=dat/100%10;;}else{temp=data_tem*0.0625;temp=temp*10+0.5;dat=temp ;SegBuffer[0]=dat%10;SegBuffer[1]=dat/10%10;SegBuffer[2]=dat/100%10;SegBuffer[3]=dat/1000%10;}}void delay_1ms(uint del){uint i,j;for(i=0;i<del;i++)for(j=0;j<148;j++);}void write_com(uchar com){lcdrs=0;P0=com;delay_1ms(5);lcden=1;delay_1ms(5);lcden=0;}void write_date(uchar date){lcdrs=1;P0=date;delay_1ms(5);lcden=1;delay_1ms(5);lcden=0;}void display(){write_date(0x30+SegBuffer[0]); write_com(0x80+0x40+7);write_date(0x30+SegBuffer[1]); write_com(0x80+0x40+6);write_date(0x30+SegBuffer[2]); write_com(0x80+0x40+5);write_date(0x30+SegBuffer[3]);}void init(){rw=0;lcden=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);write_com(0x80);for(num=0;num<16;num++){write_date(table[num]);delay_1ms(5);}write_com(0x80+0x40);for(num=0;num<16;num++){write_date(table1[num]);delay_1ms(5);}TMOD=0x01;TH0=(65536-45872)/256;TL0=(65536-45872)%256;IE=0x82;TR0=1;}void write_sfm(uchar add,uchar date) {uchar shi1,ge;shi1=date/10;ge=date%10;write_com(0x80+add);write_date(0x30+shi1);}void keyscan(){key4=0;if(key1==0){delay_1ms(1);if(key1==0){key1num++;while(!key1);if(key1num==1){TR0=0;write_com(0x80+11);write_com(0x0f);}if(key1num==2){write_com(0x80+8);}if(key1num==3){write_com(0x80+5);}if(key1num==4){key1num=0;write_com(0x0c);TR0=1;}}}if(key1num!=0){if(key2==0){delay_1ms(1);if(key2==0){while(!key2);if(key1num==1){miao++;if(miao==60)miao=0;write_sfm(10,miao);write_com(0x80+10);}if(key1num==2){fen++;if(fen==60)fen=0;write_sfm(7,fen);write_com(0x80+7);}if(key1num==3){shi++;if(shi==24)shi=0;write_sfm(4,shi);write_com(0x80+4);}}}if(key3==0){delay_1ms(1);if(key3==0){while(!key3);if(key1num==1){miao--;if(miao==-1)miao=59;write_sfm(10,miao);write_com(0x80+10);}if(key1num==2){fen--;if(fen==-1)fen=59;write_sfm(7,fen);write_com(0x80+7);}if(key1num==3){shi--;if(shi==-1)shi=23;write_sfm(4,shi);write_com(0x80+4);}}}}}void main(){uint temp=0;init();while(1){START_DS18B20();temp=READ_TEM();Temperature_4(temp);display();keyscan();}}void T0_time() interrupt 1{TH0=(65536-45872)/256;TL0=(65536-45872)%256;count++;if(count==20){count=0;miao++;if(miao==60){miao=0;fen++;if(fen==60){fen=0;shi++;if(shi==24){shi=0;}write_sfm(4,shi);}write_sfm(7,fen);}write_sfm(10,miao);}}。

1602液晶显示电子钟时间C程序

1602液晶显示电子钟时间C程序

#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit RS=P2^0;sbit RW=P2^1;sbit E=P2^2;sbit key1=P2^3;sbit key2=P2^4;sbit key3=P2^5;void delay(void);void init(void);void wc51r(uchar i);void wc51ddr(uchar i);void fbusy(void);void LCD_xianshi(void);void delay_zs(uint z);void tiaoshi(void);uchar hour,hour1,min,min1,sec,sec1,aa,bb,num1,num2,num3,flag;uchar year1,year2,year3,year4,month,month1,month2,day,day1,day2; unsigned int year;uchar tab[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};void main(){init();while(1){LCD_xianshi();}}void init(){// key1=1;key2=1;key3=1;num1=12;year=2010;month=11;day=15;// wc51r(0x01);wc51r(0x38);wc51r(0x0c);wc51r(0x06);TMOD=0x01; //工作方式1TH0=(65536-5000)/256; //定时器高位装初值计数溢出一次为50ms TL0=(65536-5000)%256; //定时器低位装初值EA=1; //CPU开中断ET0=1; //允许T0中断// ET1=1;TR0=1; //启动T0工作// TR1=1;}void fbusy(){RW=1;RS=0;E=1;while(!P1&0x80);E=0;delay();}void wc51r(uchar j){fbusy();E=0;RS=0;RW=0;E=1;P1=j;E=0;delay();}void wc51ddr(uchar j){fbusy();E=0;RS=1;RW=0;E=1;P1=j;E=0;delay();}void delay(){uchar i,j;for(j=0;j<10;j++)for(i=0;i<10;i++);}/*void delay_zs(uint z){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}*/void LCD_xianshi(){year1=year/1000;year2=year/100%10;year3=year%100/10;year4=year%10;month1=month/10;month2=month%10;day1=day/10;day2=day%10;hour=num1/10;hour1=num1%10;min=num2/10;min1=num2%10;sec=num3/10;sec1=num3%10;wc51r(0x84); //起始地址为第1行第1列,0x85为第1行第6列;0xc0为第2行第1列wc51ddr(tab[hour]); //显示1wc51ddr(tab[hour1]); //显示2wc51ddr(0x3a); //显示:wc51ddr(tab[min]); //显示0wc51ddr(tab[min1]); //显示0wc51ddr(0x3a); //显示:wc51ddr(tab[sec]);wc51ddr(tab[sec1]);wc51r(0xc3);wc51ddr(tab[year1]);wc51ddr(tab[year2]);wc51ddr(tab[year3]);wc51ddr(tab[year4]);wc51ddr('-');wc51ddr(tab[month1]);wc51ddr(tab[month2]);wc51ddr('-');wc51ddr(tab[day1]);wc51ddr(tab[day2]);}/*void tiaoshi(void){while(key1){TR0=0;if(key2==0){LCD_xianshi;num3++;if(num3==60){num3=0;num2++;}}else if(key3==0)num2++;}// while(key1);// if(key1==0){ET0=1;TR0=1;flag=0;}}*///************************定时器中断程序**************************// void timer0() interrupt 1{TH0=(65536-50000)/256; //重装计数初值TL0=(65536-50000)%256;aa++;if(aa==20) //计数20次后恰为20x50ms==1s{aa=0;num3++; //秒加1if(num3==60) //秒计数到60后清零{num3=0;num2++;if(num2==60){num2=0;num1++;if(num1==24){num1=0;day++;if(month%2==0){if(day==31){day=0;month++;if(month==13){month=0;year++;}}}else if(month%2==1){if(day==32){day=0;month++;if(month==13){month=0;year++;}}}}}}}}//************************定时器中断程序**************************// /*void timer1() interrupt 3{TH1=(65536-50000)/256; //重装计数初值TL1=(65536-50000)%256;bb++;if(bb%20==0){if(key1==0){ET0=0;TR0=0;flag=1;}}}*/#include <msp430g2452.h>unsigned char table[] ={"0123456789"};unsigned char hanzi1[8]={0x04,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02};//年 unsigned char hanzi2[8]={0x0f,0x09,0x0f,0x09,0x0f,0x09,0x13,0x11};//月 unsigned char hanzi3[8]={0x1f,0x11,0x11,0x1f,0x11,0x11,0x1f,0x00};//日unsigned char num;void delay_1ms(void){unsigned int i;for (i=0;i<1140;i++);}void delay_nms(unsigned int n){unsigned int i=0;for (i=0;i<n;i++)delay_1ms();}void write_com(unsigned int com){//lcdrs = 0;P2OUT&= 0xfd; P1OUT = com; delay_nms(5); //lcden = 1;P2OUT|= 0x01; delay_nms(5); P2OUT&= 0xfe; }void write_dat(unsigned int dat) {//lcdrs = 1;P2OUT |= 0x02; P1OUT = dat; delay_nms(5); //lcden=1;P2OUT|= 0x01; delay_nms(5); // lcden=0;P2OUT&= 0xfe; }void init() {write_com(0x38);// write_com(0x0f);// write_com(0x01);// write_com(0x06);// // write_com(0x07);//整屏移动 delay_nms(10); }void CGRAM_LCD(unsigned char *hz,unsigned char temp) {unsigned char s; for(s=0;s<8;s++) {write_com(temp+s);write_dat(*(hz+s)); } }void main(void) {WDTCTL = WDTPW + WDTHOLD; // Stop WDT P1DIR |= 0xff; // P1.2 output P2DIR |= 0xff; init();CGRAM_LCD(hanzi1,0x40); CGRAM_LCD(hanzi2,0x48); CGRAM_LCD(ha nzi3,0x50);write_com(0x80); write_dat(table[2]); delay_nms(100);write_com(0x80+0x01); write_dat(table[0]); delay_nms(100);write_com(0x80+0x02); write_dat(table[1]); delay_nms(100);write_com(0x80+0x03); write_dat(table[2]);delay_nms(100);write_com(0x80+0x04); write_dat(0); delay_nms(100);write_com(0x80+0x06); write_dat(table[6]); delay_nms(100);write_com(0x80+0x07); write_dat(1); delay_nms(100);write_com(0x80+0x09); write_dat(table[2]); delay_nms(100);write_com(0x80+0x0a); write_dat(2); while(1); }。

单片机的LCM1602液晶显示温度与万年历显控制

单片机的LCM1602液晶显示温度与万年历显控制

毕业设计报告(论文)报告(论文)题目:基于单片机的LCM1602液晶控制——温度与万年历显示设计作者所在系部:电子工程系作者所在专业:作者所在班级:作者姓名:作者学号:指导教师姓名:完成时间: 2011年 6 月 9 日院教务处制电子工程系毕业设计(论文)任务书指导教师:教研室主任:系主任:摘要论文的研究工作是以液晶屏显示技术为背景展开的,并且详细介绍了通过MCS-51单片机控制LCM1602液晶的显示情况,以软件形式对系统进行控制,使得系统控制更具灵活与方便。

本文在深入分析LCD显示技术的基础上,重点解析了LCM显示的单片机控制技术,以及LCD显示在各种电子显示中的优势,同时阐述了其在日常显示系统中的应用;并且以Proteus与Keil uVision4软件为基础,编写了MCS-51单片机对LCM1602显示控制的软件,绘制其原理图,并使用Proteus软件与Keil uVision4软件建立联合仿真。

论文主要论述了原理图各个模块的作用,控制软件的各个模块的编程。

关键词液晶显示技术LCM1602 MCS-51单片机Proteus Keil uVision4目录第1章绪论 (1)1.1课题背景及主要技术国内外研究概况 (1)1.2LCM1602显示控制系统简介 (2)1.3课题的建立以及本文完成的主要工作 (3)第2章开发工具软件简介 (4)2.1K EIL U V ISION4软件简介 (4)2.2P ROTEUS软件简介 (4)2.3K EIL U V ISION4与P ROTEUS软件联合仿真 (5)2.4小结 (5)第3章 LCD显示控制技术 (6)3.1LCD显示技术的发展 (6)3.2LCM1602显示控制技术及其体系结构 (7)3.2.1 LCM1602模块简介 (8)3.2.2 LCM1602模块内部结构 (9)3.2.3 LCM1602控制指令 (10)3.3小结 (12)第4章系统硬件概况 (13)4.1系统概况 (13)4.2功能模块 (13)4.2.1 MCS-51单片机最小系统模块 (14)4.2.2 温度采集模块 (14)4.2.3 蜂鸣器报警模块 (15)4.2.4 万年历调节设置模块 (16)4.2.5 LCM1602显示模块 (16)4.2.6 电源模块 (17)3.3小结 (17)第5章软件控制系统概况 (18)5.1程序流程概况 (18)5.2万年历显示控制模块 (18)5.2.1 流程图 (18)5.2.2 源程序代码 (19)5.3温度显示控制模块 (19)5.3.1 程序流程 (19)5.3.2 源程序代码 (19)5.3.3 主程序 (19)5.4小结 (20)第6章课题特点 (21)6.1LCM模块的应用 (21)6.2程序结构化与模块化设计 (21)6.3抗干扰技术 (21)第7章结论 (23)7.1调试联合仿真 (23)7.2仿真结果 (23)7.3小结 (23)致谢 (24)参考文献 (25)附录 (26)基于单片机的LCM1602液晶控制——温度与万年历显示设计第1章绪论1.1 课题背景及主要技术国内外研究概况自20世纪80年代起,显示设备经历着传统工艺的改良、新工艺的发展、成熟的优胜劣汰。

基于51单片机定时器的1602液晶显示器时钟显示(带年月日)

基于51单片机定时器的1602液晶显示器时钟显示(带年月日)

基于51单⽚机定时器的1602液晶显⽰器时钟显⽰(带年⽉⽇)#include#define uchar unsigned char#define uint unsigned intuchar a,count,S1num,xqnum;char yue,ri,shi,fen,miao;int nian;sbit lcdrs=P2^5;sbit lcdrw=P2^4;sbit lcden=P2^3;sbit S1=P3^0; //定义键——进⼊设置sbit S2=P3^1; //定义键——增⼤sbit S3=P3^2; //定义键——减⼩sbit S4=P3^3; //定义键——退出设置uchar code table0[]="2014-08-13 WED";uchar code table1[]="00:00:00";uchar code xingqi[][3]={"MON","TUE","WED","THU","FRI","SAT","SUN"};void delay(uint z){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}/**************************************************1602液晶显⽰器模块sbit lcdrs=P2^5;sbit lcdrw=P2^4;sbit lcden=P2^3;*************************************************/void write_com(uchar com) //液晶写指令{lcdrw=0;P0=com;delay(5);lcden=1;delay(5);lcden=0;}void write_data(uchar dat) //液晶写数据{lcdrw=0;lcdrs=1;P0=dat;delay(5);lcden=1;delay(5);lcden=0;}/********************************************年⽉⽇写⼊1602函数********************************************/void write_sfm(uchar add,uchar dat) //写时分秒函数{ uchar shi,ge;shi=dat/10;ge=dat%10;write_com(0x80+0x40+add);write_data(0x30+shi);write_data(0x30+ge);}void write_yr(uchar add,uchar dat) //写⽉⽇函数{uchar shi,ge;shi=dat/10;ge=dat%10;write_com(0x80+add);write_data(0x30+shi);}void write_nian(uchar add,uint dat) //写年函数{uint qian,bai,shi,ge;qian=dat/1000;bai=dat%1000/100;shi=dat%100/10;ge=dat%10;write_com(0x80+add);write_data(0x30+qian);write_data(0x30+bai);write_data(0x30+shi);write_data(0x30+ge);/***************************************************初始化***************************************************/void init_lcd() //液晶初始化{lcden=0;nian=2014;yue=8;ri=13;shi=0; //初始shi、fen、miaofen=0;miao=0;write_com(0x38); //设置16x2显⽰,5x7点阵,8位数据⼝write_com(0x0c); //设置开显⽰,不显⽰光标write_com(0x06); //写⼀个字符后地址指针加1write_com(0x01); //显⽰清0,数据指针清0}void init() //初始化函数{init_lcd(); //液晶初始化write_com(0x80); //设置显⽰初始坐标for(a=0;a<14;a++) //显⽰年⽉⽇初始值write_data(table0[a]);delay(5);}write_com(0x80+0x40); //设置显⽰初始坐标for(a=0;a<8;a++) //显⽰时分秒初始值{write_data(table1[a]);delay(5);}write_nian(0,nian);write_sfm(6,miao); //分别将shi、fen、miao送去液晶显⽰write_sfm(3,fen);write_sfm(0,shi);count=0;xqnum=0;S1num=0; //初始化全局变量countTMOD=0x01; //设置定时器0⼯作模式1 TH0=(65535-50000)/256; //定时器装初始值TL0=(65535-50000)%256;EA=1; //开总中断ET0=1; //开定时器0中断TR0=1; //启动定时器0}/**************************************************独⽴键盘sbit S1=P3^0; //定义键——进⼊设置sbit S2=P3^1; //定义键——增⼤sbit S3=P3^2; //定义键——减⼩sbit S4=P3^3; //定义键——退出设置独⽴键盘已接地*************************************************/void keyscan(){if(S1==0){delay(5); //确认定义键被按下{S1num++; //定义键S1按下次数记录while(!S1); //释放按键确认if(S1num==1) //S1按下⼀次时{TR0=0; //关闭定时器write_com(0xc0+7); //光标定位到秒位置write_com(0x0f); //光标闪烁}if(S1num==2) //S1按下两次时{write_com(0xc0+4); //光标定位到分位置}if(S1num==3) //S1按下三次时{write_com(0xc0+1); //光标定位到时位置}if(S1num==4) //S1按下四次时{write_com(0x80+13); //光标定位到星期位置}if(S1num==5) //S1按下五次时{write_com(0x80+9); //光标定位到⽇位置}if(S1num==6) //S1按下六次时{write_com(0x80+6); //光标定位到⽉位置}if(S1num==7) //S1按下七次时{write_com(0x80+3); //光标定位到年位置}if(S1num==8) //S1按下⼋次时S1num=0; //S1记录按键次数清零TR0=1; //开启定时器write_com(0x0c); //取消光标闪烁}}}if(S1num!=0) //只有定义键按下后S2、S3、S4才有效{ if(S2==0){delay(5); //防抖if(S2==0) //确认按键被按下{while(!S2); //释放按键确认if(S1num==1) //S1按下⼀次时{miao++; //调整秒加1if(miao==60) //满60清零miao=0;write_sfm(6,miao); //每调节⼀次送液晶显⽰⼀次write_com(0x80+0x40+6); //显⽰位置重新回到调节处} if(S1num==2) //S1按下两次时{fen++; //调整分加1if(fen==60)fen=0;write_sfm(3,fen);write_com(0x80+0x40+3);}if(S1num==3) //S1按下三次时{shi++; //调整时加1if(shi==24)shi=0;write_com(0x80+0x40);}if(S1num==4) //星期加调整{ xqnum++;if(xqnum==7)xqnum=0;write_com(0x80+0x0b);for(a=0;a<3;a++){write_data(xingqi[xqnum][a]); delay(5);}}if(S1num==5) //⽇加调整{ri++;if(yue==2){if(nian%400==0){if(ri==30){ri=1;}}if(nian%400!=0){if(ri==29){ri=1;}}}else if(yue<=7){if(yue%2==0&yue!=2){ri=1;}}else if(yue%2!=0&yue!=2) {if(ri==32){ri=1;}}}else if(yue>=8){if(yue%2==0){if(ri==32){ri=1;}}else if(yue%2!=0){if(ri==31){ri=1;}}}write_yr(8,ri);}if(S1num==6) //⽉加调整{ yue++;write_yr(5,yue);}if(S1num==7) //年加调整{nian++;if(nian==2019)nian=2014;write_nian(0,nian);}}}if(S3==0){delay(5);if(S3==0) //确认按键被按下{while(!S3);if(S1num==1){miao--; //调整秒减1if(miao==-1) //减到00后再减重新设置为59 miao=59;write_sfm(6,miao);write_com(0x80+0x40+6);}if(S1num==2){fen--; //调整分减1if(fen==-1)fen=59;write_sfm(3,fen);write_com(0x80+0x40+3);}if(S1num==3)shi--; //调整时减1if(shi==-1)shi=23;write_sfm(0,shi);write_com(0x80+0x40);}if(S1num==4){xqnum--; //调整星期减⼀if(xqnum==-1)xqnum=6;write_com(0x80+0x0b);for(a=0;a<3;a++){write_data(xingqi[xqnum][a]); delay(5);}}if(S1num==5) //调整⽇{ri--;if(yue==2){if(nian%400==0){if(ri==0){ri=29;}}if(nian%400!=0){if(ri==0){ri=28;}else if(yue<=7){if(yue%2==0&yue!=2){if(ri==0){ri=30;}}else if(yue%2!=0&yue!=2) {if(ri==0){ri=31;}}}else if(yue>=8){if(yue%2==0){if(ri==0){ri=31;}}else if(yue%2!=0){if(ri==0){ri=30;}}write_yr(8,ri);}if(S1num==6) //调整⽉{yue--;if(yue==0)yue=12;write_yr(5,yue);}if(S1num==7) //调整年{nian--;if(nian==2013)nian=2018;write_nian(0,nian);}}}if(S4==0) //退出设置、开启中断{delay(5);if(S4==0){S1num=0;TR0=1;write_com(0x0c);}}}}/**************************************************定时器0*************************************************/ void timer0() interrupt 1 //定时器0中断服务程序TH0=(65535-50000)/256; //重装定时器初始值TL0=(65535-50000)%256; count++; //中断次数累加if(count==20) //20次50毫秒即⼀秒{count=0;miao++;if(miao==60) //秒加到60时分进位{miao=0;fen++;if(fen==60) //分加到60时时进位{fen=0;shi++;if(shi==24) //时加到24时清0{shi=0;xqnum++;ri++;if(yue==2) //如果是⼆⽉{if(nian%400==0) //闰年判断{if(ri==30){ri=1;yue++;write_yr(5,yue);}}if(nian%400!=0) //平年判断{if(ri==29){ri=1;yue++;write_yr(5,yue);}}}else if(yue<=7&yue!=2) //⼀⽉到七⽉{if(yue%2==0) //偶数⽉(除⼆⽉){if(ri==31){ri=1;yue++;}}else if(yue%2!=0) //奇数⽉{if(ri==32){ri=1;yue++;}}write_yr(5,yue);}else if(yue>=8) //⼋⽉到12⽉{if(yue%2==0) //偶数⽉(除⼆⽉){if(ri==32){ri=1;yue++;if(yue==13) //如果判断⽉份为12⽉,则加⼀后重新定义⽉份为1{yue=1;nian++;write_nian(0,nian);}write_yr(5,yue);}}else if(yue%2!=0) //奇数⽉{if(ri==31){ri=1;yue++;write_yr(5,yue);}}}write_yr(8,ri);if(xqnum==7) //星期写⼊xqnum=0;write_com(0x80+0x0b);for(a=0;a<3;a++){write_data(xingqi[xqnum][a]); delay(5);}}write_sfm(0,shi); //重新写⼊数据}write_sfm(3,fen); //重新写⼊数据}write_sfm(6,miao); //重新写⼊数据}}/*************************************************主函数*************************************************/ void main(){init();while(1){keyscan(); //不停的检测按键是否被按下} }。

(完整word版)1602液晶时钟程序(51单片机)

(完整word版)1602液晶时钟程序(51单片机)

1602液晶时钟程序程序按郭天袢《新概念51单片机C语言教程》第十四章扩展显示年、月、日、星期调节功能。

1)时间显示在1602液晶上,并按秒实时更新2)能调节按键调整时、分、秒、年、月、日和星期3)每次按键按下时有短滴声响声4)断电后下次上电会接着上次断电前的时间数继续运行原理图如图//原代码如下:#include<reg52.h>//#include"24c02.h"#define uchar unsigned char#define uint unsigned int#include"24c02.h"sbit dula=P2^6;sbit wela=P2^7;sbit LCDEN=P3^4;sbit LCDRS=P3^5;sbit s1=P3^0;//定义按键--功能键sbit s2=P3^1;//定义按键--增大键sbit s3=P3^2;//定义按键--减小键sbit rd=P3^7;//按键公共脚sbit beep=P2^3;//定义蜂鸣器脚uchar count,s1_num;//定义中断次数计数变量功能键按键次数变量uchar week,day,month,year;//定义变量:星期日月年char miao,shi,fen;//定义秒分时变量uchar code table[]=" 2012-11-28 Wed";//void write_week(char we);//显示星期几函数void delay(uint z){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void di()//蜂鸣器响一声函数{beep=0;delay(100);beep=1;}void write_com(uchar com) //液晶写命令函数{LCDRS=0;P0=com;delay(5);LCDEN=1;delay(5);LCDEN=0;}void write_date(uchar date) //液晶写数据函数{LCDRS=1;P0=date;delay(5);LCDEN=1;delay(5);LCDEN=0;}void write_week(char we) //写星期几显示函数{write_com(0x80+12);switch(we){case 1: write_date('M');delay(5); //星期一Mondaywrite_date('o');delay(5);write_date('n');delay(5);break;case 2: write_date('T');delay(5); //星期二Tuesdaywrite_date('u');delay(5);write_date('e');delay(5);break;case 3: write_date('W');delay(5); //Wednesdaywrite_date('e');delay(5);write_date('d');delay(5);break;case 4: write_date('T');delay(5); //Thursdaywrite_date('h');delay(5);write_date('u');delay(5);break;case 5: write_date('F');delay(5); //星期五Fridaywrite_date('r');delay(5);write_date('i');delay(5);break;case 6: write_date('S');delay(5); //星期六Saturdaywrite_date('a');delay(5);write_date('t');delay(5);break;case 7: write_date('S');delay(5); //星期日Sundaywrite_date('u');delay(5);write_date('n');delay(5);break;default: break;}}void write_sfm(uchar add,uchar date) //写时分秒函数{uchar shiwei,gewei;shiwei=date/10;gewei=date%10;write_com(0x80+0x40+add);//设置显示位置write_date(0x30+shiwei);//加上0x30是因为数字变成字符串需要加上0x30,看ASCII码值与字符的关系write_date(0x30+gewei);}void write_year_month_day(uchar add,uchar day) //写年月日数函数{uchar shi,ge;shi=day/10;ge=day%10;write_com(0x80+add);//设置显示位置write_date(0x30+shi);//加上0x30是因为数字变成字符串需要加上0x30,看ASCII码值与字符的关系write_date(0x30+ge);}void init() //初始化函数{uchar num;rd=0;beep=1;dula=0;wela=0;LCDEN=0;//将使能端置0以完成高脉冲fen=0;miao=0;shi=0;count=0;//计数初始为0init_24c02();write_com(0x38);//设置16*2显示,5*7点阵,8位数据接口write_com(0x0c);//设置开显示,不显示光标write_com(0x06);//写一个字符后地址指针自动加1write_com(0x01);//显示清0,数据指针清0write_com(0x80); //设置显示初始坐标for(num=0;num<15;num++) //显示年月日{write_date(table[num]);delay(5);}write_com(0x80+0x40+6); //写出时间显示部分的两个冒号write_date(0x3a);delay(5);write_com(0x80+0x40+9);write_date(0x3a);delay(5);miao=read_add(1);//开机上电时读取24c02 IIC的数据赋值fen=read_add(2);shi=read_add(3);week=read_add(4);//读取星期几day=read_add(5);//读取多少日month=read_add(6);year=read_add(7);write_sfm(10,miao);//分别送去液晶显示write_sfm(7,fen);write_sfm(4,shi);write_week(week);//开机从24c02读取的星期几数从新写入液晶显示出来write_year_month_day(9,day);write_year_month_day(6,month);write_year_month_day(3,year);//定时器初始化TMOD=0x01; //设置定时器0工作模式1TH0=(65536-45876)/256; //定时器装初值TL0=(65536-45876)%256;EA=1; //开总中断ET0=1; //打定时器0中断TR0=1; //启动定时器0}void keyscan() //按键扫描函数{if(s1==0){delay(5);if(s1==0){s1_num++; //功能键按下次数记录while(!s1); //按键释放确认di();if(s1_num==1) //只按一次按键时TR0=0; //关闭定时器write_com(0x80+0x40+11);write_com(0x0f); //打开光标闪烁}if(s1_num==2) //第二次按下光标闪烁定位到分钟位置{write_com(0x80+0x40+8);}if(s1_num==3) //第三次按下光标闪烁定位到小时位置{write_com(0x80+0x40+5);}/**************************************if(s1_num==4) //第三四次按下退出{s1_num=0;write_com(0x0c); //取消光标闪栎TR0=1; //启动定时器使时钟开始走}***************************************/if(s1_num==4) //光标处星期{write_com(0x80+14);}if(s1_num==5) //光标处日{write_com(0x80+10);}if(s1_num==6) //光标处月{write_com(0x80+7);}if(s1_num==7) //光标处年{write_com(0x80+4);}if(s1_num==8) //当第八次按功能键时退出光标闪烁及开始计时{s1_num=0;write_com(0x0c); //取消光标闪栎TR0=1; //启动定时器使时钟开始走}}if(s1_num!=0){if(s2==0)//增加键确认按下{delay(5);if(s2==0){while(!s2);di();if(s1_num==1){miao++;if(miao==60)miao=0;write_sfm(10,miao);write_com(0x80+0x40+11);write_add(1,miao);}if(s1_num==2){fen++;if(fen==60)fen=0;write_sfm(7,fen);write_com(0x80+0x40+8);write_add(2,fen);}if(s1_num==3){shi++;if(shi==24)shi=0;write_sfm(4,shi);write_com(0x80+0x40+5);write_add(3,shi);}if(s1_num==4)//按四次功能键后光标至星期几处,可调节星期几{week++;if(week==8)week=1;write_week(week);write_com(0x80+14); //写一个字符后光标会移一位,所以要重新定义光标位置write_add(4,week);}if(s1_num==5) //光标至显示日处,可调节多少日{day++;if(day==32)day=1;write_year_month_day(9,day);write_com(0x80+10);write_add(5,day);}if(s1_num==6) //光标至显示月处调节月份{month++;if(month==13)month=1;write_year_month_day(6,month);write_com(0x80+7);write_add(6,month);}if(s1_num==7){year++;if(year==100)year=00;write_year_month_day(3,year);write_com(0x80+4);write_add(7,year);}}}if(s3==0) //确认减小键被按下{delay(5);//按键防抖延时if(s3==0) //确认减小键被按下{while(!s3);di();if(s1_num==1){miao--;if(miao==-1)miao=59;write_sfm(10,miao);write_com(0x80+0x40+11);write_add(1,miao);}if(s1_num==2){fen--;if(fen==-1)fen=59;write_sfm(7,fen);write_com(0x80+0x40+8);write_add(2,fen);}if(s1_num==3){shi--;if(shi==-1)shi=23;write_sfm(4,shi);write_com(0x80+0x40+5);write_add(3,shi);}if(s1_num==4){week--;if(week==0)week=7;write_week(week);write_com(0x80+14); //写一个字符后光标会移一位,所以要重新定义光标位置write_add(4,week);}if(s1_num==5)//光标至显示日处,可调节多少日{day--;if(day==0)day=31;write_year_month_day(9,day);write_com(0x80+10);//光标位置移回在显示多少日的个位write_add(5,day);}if(s1_num==6) //光标至显示月处调节月份{month--;if(month==0)month=12;write_year_month_day(6,month);write_com(0x80+7);write_add(6,month);}if(s1_num==7){year--;if(year==-1)year=99;write_year_month_day(3,year);write_com(0x80+4);write_add(7,year);}}}}}void main(){init();//首先初始化各数据while(1){keyscan(); //按键扫描函数}}void timer0() interrupt 1 //定时器0中断服务程序{TH0=(65536-45876)/256;TL0=(65536-45876)%256;count++; //中断次数累加,计数if(count==20) //50微秒乘以20等于1秒{count=0;miao++;if(miao==60) //秒加到60则进位分钟{miao=0; //同时秒数清0fen++;if(fen==60) //分钟加到60则进位小时{fen=0; //同时分钟清0shi++;if(shi==24) //小时加到24则小时清0{shi=0;}write_sfm(4,shi);//小时若变化则重新写入write_add(3,shi);//写入24c02存储起来,小时数的写入地址为3 }write_sfm(7,fen);//分钟若变化则重新写入write_add(2,fen);}write_sfm(10,miao);//秒若变化则重新写入write_add(1,miao);}}/***********************************24C02.h头文件,将以下源代码需存为与保存的C代码根目录下。

基于某stc51单片机的LCD1602显示时间_的电子万年历(显示当前温度)

基于某stc51单片机的LCD1602显示时间_的电子万年历(显示当前温度)

1 课设所需软件简介1.1 Keil uVision4的简要介绍2009年2月发布Keil μVision4,Keil μVision4引入灵活的窗口管理系统,使开发人员能够使用多台监视器,并提供了视觉上的表面对窗口位置的完全控制的任何地方。

新的用户界面可以更好地利用屏幕空间和更有效地组织多个窗口,提供一个整洁,高效的环境来开发应用程序。

新版本支持更多最新的ARM芯片,还添加了一些其他新功能。

2011年3月ARM公司发布最新集成开发环境RealView MDK开发工具中集成了最新版本的Keil uVision4,其编译器、调试工具实现与ARM器件的最完美匹配。

Keil C51开发系统基本知识Keil C51开发系统基本知识1. 系统概述Keil C51软件提供丰富的库函数和功能强大的集成开发调试工具,全Windows界面。

另外重要的一点,只要看一下编译后生成的汇编代码,就能体会到Keil C51生成的目标代码效率非常之高,多数语句生成的汇编代码很紧凑,容易理解。

在开发大型软件时更能体现高级语言的优势。

下面详细介绍Keil C51开发系统各部分功能和使用。

2. Keil C51单片机软件开发系统的整体结构C51工具包的整体结构,uVision与Ishell分别是C51 for Windows和for Dos的集成开发环境(IDE),可以完成编辑、编译、连接、调试、仿真等整个开发流程。

开发人员可用IDE本身或其它编辑器编辑C或汇编源文件。

然后分别由C51及C51编译器编译生成目标文件(.OBJ)。

目标文件可由LIB51创建生成库文件,也可以与库文件一起经L51连接定位生成绝对目标文件(.ABS)。

ABS文件由OH51转换成标准的Hex文件,以供调试器dScope51或tScope51使用进行源代码级调试,也可由仿真器使用直接对目标板进行调试,也可以直接写入程序存贮器如EPROM中。

使用独立的Keil仿真器时,注意事项:* 仿真器标配11.0592MHz的晶振,但用户可以在仿真器上的晶振插孔中换插其他频率的晶振。

基于51单片机的用 LCD1602 显示时钟的程序

基于51单片机的用 LCD1602 显示时钟的程序

用LCD1602 显示的时钟2012-04-30 15:04有这样一个题目:求一个为51 单片机编写的LCD 电子时钟的设计,简单就好!希望说一下怎么设计这个时钟,都需要些什么东西,最重要的——把这个设计需要的程序写出来。

设计的任务:以单片机控制的时钟,在LCD 显示器上显示当前的时间。

设计的基本要求:1.使用文字型LCD 显示器显示当前时间。

2.显示格式为“时时:分分:秒秒”。

3.用4个功能键操作来设置当前时间。

各个功能键的功能如下:K1:进入设置现在的时间。

K2:设置小时。

K3:设置分钟。

K4:确认完成设置。

4. 程序执行后工作指示灯LED 闪烁,表示程序开始执行,LCD 显示“00:00:00”,然后开始计时。

题目链接:/question/416705477.html//==================================================提到设计时钟,很多人都想到了时钟芯片DS1302,都说它简单、准确。

其实,这是个误区。

仅仅使用一般的单片机,简单的编程,达到相同DS1302 的准确度,并不是难事。

如果不要求计算平闰年、不要求分清大小月、不要求计算星期几,只是要求一个简单的时钟(及日历),用DS1302,就是自寻烦恼。

大家可以打开题目链接,看看其中的一些答案,就可以看出使用DS1302 是多么的繁琐了,简直就是一场噩梦。

做而论道以前就使用普通的单片机和LCD1602 设计过《时钟与日历》,程序设计的非常合理,时间精度就完全取决于晶振的精度。

设计出来的时钟,几个月都差不上一秒。

针对这个题目,做而论道翻出了以前的设计,删节了一些不需要的功能,设计出了符合题目要求的时钟,用PROTEUS 仿真截图如下:程序用C 语言编写,全部代码如下://---------------------------------------------------#include<reg52.h>#define uchar unsigned char#define uint unsigned int#define KEY_IO P3#define LCD_IO P0sbit LCD_RS = P2^0;sbit LCD_RW = P2^1;sbit LCD_EN = P2^2;sbit SPK = P1^2;sbit LED = P2^4;bit new_s, modify = 0;char t0, sec = 50, min = 59, hour = 23;char code LCD_line1[] = "Designed by ZELD"; char code LCD_line2[] = "Timer: 00:00:00 "; char Timer_buf[] = "23:59:50";//---------------------------------------------------void delay(uint z){uint x, y;for(x = z; x > 0; x--) for(y = 100; y > 0; y--);//---------------------------------------------------void W_LCD_Com(uchar com) //写指令{LCD_RS = 0; LCD_IO = com; // LCD_RS和R/W都为低电平时,写入指令LCD_EN = 1; delay(5); LCD_EN = 0; //用EN输入一个高脉冲}//---------------------------------------------------void W_LCD_Dat(uchar dat) //写数据{LCD_RS = 1; LCD_IO = dat; // LCD_RS为高、R/W为低时,写入数据LCD_EN = 1; delay(5); LCD_EN = 0; //用EN输入一个高脉冲}//---------------------------------------------------void W_LCD_STR(uchar *s) //写字符串{while(*s) W_LCD_Dat(*s++);}//---------------------------------------------------void W_BUFF(void) //填写显示缓冲区{Timer_buf[7] = sec % 10 + 48; Timer_buf[6] = sec / 10 + 48;Timer_buf[4] = min % 10 + 48; Timer_buf[3] = min / 10 + 48;Timer_buf[1] = hour % 10 + 48;Timer_buf[0] = hour / 10 + 48;W_LCD_Com(0xc0 + 7); W_LCD_STR(Timer_buf);}//---------------------------------------------------uchar read_key(void){uchar x1, x2;KEY_IO = 255;x1 = KEY_IO;if (x1 != 255) {delay(100);x2 = KEY_IO;if (x1 != x2) return 255;while(x2 != 255) x2 = KEY_IO;if (x1 == 0x7f) return 0;else if (x1 == 0xbf) return 1;else if (x1 == 0xdf) return 2;else if (x1 == 0xef) return 3;else if (x1 == 0xf7) return 4;}return 255;//---------------------------------------------------void Init(){LCD_RW = 0;W_LCD_Com(0x38); delay(50);W_LCD_Com(0x0c);W_LCD_Com(0x06);W_LCD_Com(0x01);W_LCD_Com(0x80); W_LCD_STR(LCD_line1);W_LCD_Com(0xC0); W_LCD_STR(LCD_line2);TMOD = 0x01; //T0定时方式1TH0 = 0x4c;TR0 = 1; //启动T0PT0 = 1; //高优先级, 以保证定时精度ET0 = 1;EA = 1;}//---------------------------------------------------void main(){uint i, j;uchar Key;Init();while(1) {//-------------------------------if (new_s) { //如果出现了新的一秒, 修改时间new_s = 0; sec++; sec %= 60;if(!sec) { min++; min %= 60;if(!min) { hour++; hour %= 24;}}W_BUFF(); //写显示//-------------------------------if (!sec && !min) { //整点报时for (i = 0; i < 200; i++) {SPK = 0; for (j = 0; j < 100; j++);SPK = 1; for (j = 0; j < 100; j++);} }}//-------------------------------Key = read_key(); //读出按键switch(Key) { //分别处理四个按键case 0: modify = 1; break;case 1: if(modify) {min++; min %= 60; W_BUFF(); break;}case 2: if(modify) {hour++; hour %= 24; W_BUFF(); break;}case 3: modify = 0; break;} }}//---------------------------------------------------void timer0(void) interrupt 1 //T0中断函数, 50ms执行一次{TH0 = 0x4c;t0++; t0 %= 20; //20, 一秒钟if(t0 == 0) {new_s = 1; LED = ~LED;}if(modify) LED = 0;}//===================================================呵呵,全部程序,也不过120 行左右。

基于51单片机的1602液晶时钟设计的C程序(闰年补偿,月份调整)

基于51单片机的1602液晶时钟设计的C程序(闰年补偿,月份调整)

#include<reg52.h>#define uchar unsigned char#define uint unsigned intuchar code table[]=" 2012-02-29 MON";uchar code table1[]=" 23:59:55";uchar num,count,shi,fen,miao,nian,yue,ri,xingqi,s1num,flag; sbit lcden=P2^5;sbit lcdrs=P1^0;sbit lcdrw=P1^1;sbit s1=P3^0;sbit s2=P3^1;sbit s3=P3^2;sbit s4=P3^3;sbit feng=P2^3;/***************延迟函数***************/void delay(uint z){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}/**********写命令***************/void write_com(uchar com){lcdrs=0;lcdrw=0;P0=com;lcden=1;delay(5);lcden=0;}/**************写数据****************/void write_data(uchar date){lcdrs=1;lcdrw=0;P0=date;lcden=1;delay(5);lcden=0;}/*************时分秒调节函数********************/void write_sfm(uchar add,uchar date){uchar shi,ge;shi=date/10;ge=date%10;write_com(0x80+0x40+add);write_data(0x30+shi);write_data(0x30+ge);}/****************年月日调节函数***************/void write_nyr(uchar ad,uchar dat){uchar shi,ge;shi=dat/10;ge=dat%10;write_com(0x80+0x00+ad);write_data(0x30+shi);write_data(0x30+ge);}/**************************星期显示函数*****************/void display_week(){switch (xingqi){case 1: write_data('M');write_data('O');write_data('N');write_com(0x80+0x00+14);break;case 2: write_data('T');write_data('U');write_data('E');write_com(0x80+0x00+14);break;case 3: write_data('W');write_data('E');write_data('D');write_com(0x80+0x00+14);break;case 4: write_data('T');write_data('H');write_data('U');write_com(0x80+0x00+14);break;case 5: write_data('F');write_data('R');write_data('I');write_com(0x80+0x00+14);break;case 6: write_data('S');write_data('A');write_data('T');write_com(0x80+0x00+14);break;case 7: write_data('S');write_data('U');write_data('N');write_com(0x80+0x00+14);break;}}void keycan(){/****************光标左移***************/if(s1==0){delay(5);if(s1==0){s1num++;while(!s1);if(s1num==1){TR0=0;write_com(0x80+0x40+11);write_data('<');write_com(0x80+0x40+10);//光标重新回到闪烁点,写光标地址同样加一write_com(0x0f); //光标闪烁}if(s1num==2){write_com(0x80+0x40+11);write_data(' ');write_com(0x80+0x40+8);write_data('<');write_com(0x80+0x40+7);}if(s1num==3){write_com(0x80+0x40+8);write_data(':');write_com(0x80+0x40+5);write_data('<');write_com(0x80+0x40+4);}if(s1num==4){write_com(0x80+0x40+5);write_data(':');write_com(0x80+15);write_data('<');write_com(0x80+14);}if(s1num==5){write_com(0x80+15);write_data(' ');write_com(0x80+11);write_data('<');write_com(0x80+10);}if(s1num==6){write_com(0x80+11);write_data(' ');write_com(0x80+8);write_data('<');write_com(0x80+7); }if(s1num==7){write_com(0x80+8);write_data('-');write_com(0x80+5);write_data('<');write_com(0x80+4); }if(s1num==8){s1num=0;write_com(0x80+5);write_data('-');write_com(0x0c);TR0=1;}}}/**************光标右移***************//* if(s2==0){delay(5);if(s2==0){s1num--;while(!s2);if(s1num==1){TR0=0;write_com(0x80+0x40+11);write_data('<');write_com(0x80+0x40+11);//光标重新回到闪烁点,写光标地址同样加一write_com(0x0f); //光标闪烁}if(s1num==2){write_com(0x80+0x40+11);write_data(' ');write_com(0x80+0x40+8);write_data('<');write_com(0x80+0x40+8);}if(s1num==3){write_com(0x80+0x40+8);write_data(':');write_com(0x80+0x40+5);write_data('<');write_com(0x80+0x40+5);}if(s1num==4){write_com(0x80+0x40+5);write_data(':');write_com(0x80+15);write_data('<');write_com(0x80+15);}if(s1num==5){write_com(0x80+15);write_data(' ');write_com(0x80+11);write_data('<');write_com(0x80+11);}if(s1num==6){write_com(0x80+11);write_data(' ');write_com(0x80+8);write_data('<');write_com(0x80+8);}if(s1num==7){write_com(0x80+8);write_data('-');write_com(0x80+5);write_data('<');write_com(0x80+5);}if(s1num==8){s1num=0;write_com(0x80+5);write_data('-');write_com(0x0c);TR0=1;}}} *//***************************按键加*******************************/if(s1num!=0){if(s3==0){delay(5);if(s3==0){while(!s3);if(s1num==1){miao++;if(miao==60)miao=0;write_sfm(9,miao);write_com(0x80+0x40+10);}if(s1num==2){fen++;if(fen==60)fen=0;write_sfm(6,fen);write_com(0x80+0x40+7);}if(s1num==3){shi++;if(shi==24)shi=0;write_sfm(3,shi);write_com(0x80+0x40+4);}if(s1num==4){xingqi++;if(xingqi==8)xingqi=1;write_com(0x80+0x00+12);display_week();}if(s1num==5){ri++;if(ri==32&&(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)){ri=1;yue++;if(yue==13){yue=1;nian++;write_nyr(3,nian);}write_nyr(6,yue);}if(ri==31&&(yue==4||yue==6||yue==9||yue==11)){ri=1;yue++;write_nyr(6,yue);}if(flag==1){if(ri==30&&yue==2){ri=1;yue++;write_nyr(6,yue);}}if(flag==0){if(ri==29&&yue==2){ri=1;yue++;write_nyr(6,yue);}}write_nyr(9,ri);write_com(0x80+0x00+10);}if(s1num==6){yue++;if(yue==2&&ri>29&&flag==1) //当为闰年时,如果ri=30,但是月份已经加到2,这种情况ri必须等于29{ri=29;write_nyr(9,ri);}if(yue==2&&ri>28&&flag==0) //当为平年时,如果ri=30,但是月份已经加到2,这种情况ri必须等于28{ri=28;write_nyr(9,ri);}if((yue==4||yue==6||yue==9||yue==11)&&(ri==31||ri==29||ri==28)){ri=1;write_nyr(9,ri);}if(yue==13){yue=1;nian++;if(nian%4>0){write_nyr(9,28);}write_nyr(3,nian);}write_nyr(6,yue);write_com(0x80+0x00+7);}if(s1num==7){nian++;if(nian==100)nian=0;flag=0;if(nian%4==0)flag=1;write_nyr(3,nian);write_com(0x80+0x00+4);}}}}/***************************按键减*********************/if(s4==0){delay(5);if(s4==0){while(!s4);if(s1num==1){miao--;if(miao==-1)miao=59;write_sfm(9,miao);write_com(0x80+0x40+10);}if(s1num==2){fen--;if(fen==-1)fen=59;write_sfm(6,fen);write_com(0x80+0x40+7);}if(s1num==3){shi--;if(shi==-1)shi=23;write_sfm(3,shi);write_com(0x80+0x40+4);}if(s1num==4){xingqi--;if(xingqi==0)xingqi=7;write_com(0x80+0x00+12);display_week();}if(s1num==5){ri--;if(ri==0&&(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)){ri=30; //防止当yue=12时,当yue减到11时,ri任然等于31时这种不允许情况出现yue--;if(yue==2){if(flag==1)ri=29;elseri=28;}write_nyr(6,yue);if(yue==0){yue=12;ri=31;write_nyr(9,ri);write_nyr(6,yue);}}if(ri==0&&(yue==4||yue==6||yue==9||yue==11)){ri=31;//按下减小按钮,yue 减到3或5或8或10,为大月yue--;write_nyr(6,yue);}if(flag==1) //闰年标志位{if(ri==0&&yue==2){ri=29;yue--;write_nyr(6,yue);}}if(flag==0) //平年标志位{if(ri==0&&yue==2){ri=28;yue--;write_nyr(6,yue);}}write_nyr(9,ri);write_com(0x80+0x00+10);}if(s1num==6){yue--;if(yue==2&&ri>29&&flag==1){ri=29;write_nyr(9,ri);}if(yue==2&&ri>28&&flag==0){ri=28;write_nyr(9,ri);}if(yue==0){yue=12;nian--;write_nyr(3,nian);}write_nyr(6,yue);if((yue==4||yue==6||yue==9||yue==11)&&(ri==31||ri==29||ri==28)){ri=1;write_nyr(9,ri);}write_com(0x80+0x00+7);}if(s1num==7){nian--;if(nian==-1)nian=99;flag=0; //清除标志位if(nian%4==0)flag=1;write_nyr(3,nian);write_com(0x80+0x00+4);}}}}/************初始化**************/void init(){write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);write_com(0x80+0x00);for(num=0;num<15;num++){write_data(table[num]);delay(5);}write_com(0x80+0x40);for(num=0;num<11;num++){write_data(table1[num]);delay(5);}shi=23;fen=59;miao=55;xingqi=1;ri=29;yue=2;nian=12;if(nian%4==0)flag=1;TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;}/***********主函数*************/ void main(){init(); //初始化while(1){keycan();}}/******启动定时器*************/void timer() interrupt 1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;count++;if(count==18){count=0;miao++;if(miao==60){miao=0;fen++;if(fen==60){fen=0;feng=0;delay(500);feng=1;shi++;if(shi==24){shi=0;ri++;xingqi++;if(xingqi==8)xingqi=1;write_com(0x80+0x00+12);display_week();if(ri==32&&(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)){ri=1;yue++;if(yue==13){yue=1;nian++;if(nian==99)nian=0;if(nian%4==0)flag=1;flag=0;write_nyr(3,nian);}write_nyr(6,yue);}if(ri==31&&(yue==4||yue==6||yue==9||yue==11)){ri=1;yue++;write_nyr(6,yue);}if(flag==1){if(ri==30&&yue==2){ri=1;yue++;write_nyr(6,yue);}}if(flag==0){if(ri==29&&yue==2).专业整理..学习帮手.{ri=1;yue++;write_nyr(6,yue);}}write_nyr(9,ri);}write_sfm(3,shi);}write_sfm(6,fen);}write_sfm(9,miao);}}。

51单片机 lcd1602测试C程序

51单片机 lcd1602测试C程序

51单片机 lcd1602测试C程序///原创,绝对正版...嘻嘻嘻~~~//********************************************************************** //lcd_1602测试C程序//RS接P2^5,RW接P2^6,E接P2^7,P0口为数据口//如果硬件正常,则在液晶屏幕上显示第一行为"Weclome!",第二行显示"Ilove you!"//**********************************************************************//********************************************************************** #include <reg51.h>sbit rs_1602=P2^5;sbit rw_1602=P2^6;sbit en_1602 =P2^7;#define lcd_data P0 //加上拉//********************************************************************** //1602LCD延时Zms函数//********************************************************************** void delay_1602(int z){ int x,y;for(x=0;x<z;x++)for(y=0;y<=111;y++);}//****************************************************************** **** //1602LCD写地址函数//****************************************************************** **** void adr_write(unsigned char adr){delay_1602(5);en_1602=0;rs_1602=0;rw_1602=0;en_1602=1;lcd_data=adr;en_1602=0;}//****************************************************************** **** //1602LCD写数据函数//****************************************************************** **** void ddr_write(unsigned char ddr){delay_1602(5);en_1602=0;rs_1602=1;rw_1602=0;en_1602=1;lcd_data=ddr;en_1602=0;}//****************************************************************** ****//1602LCD初始化函数//****************************************************************** ****void lcd_init(void){en_1602=0;adr_write(0x38); //两行显示,5*7模式adr_write(0x01);//清显示adr_write(0x0c); //整体显示打开,不显示光标adr_write(0x06);adr_write(0x80);//从第一行的第一个位置开始显示}//****************************************************************** ****//1602LCD显示字符串数据函数//****************************************************************** ****void str_write(unsigned char *str){while(*str!='\0'){ddr_write(*str);str++;}}//****************************************************************** ****//1602LCD显示定位函数//****************************************************************** ****void set_point(unsigned char x,y){if(!x)adr_write(0x80+y);elseadr_write(0x80+0x40+y);}void main(void){lcd_init(); //LCD1602初始化while(1){set_point(0,4);str_write("Welcome!");set_point(1,2);str_write("I love you!!"); }}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//液晶显示温度#include "AT89X52.H"#define Ddata P0sbit RS=P2^7;sbit RW=P2^6;sbit LCDE=P2^5;sbit DQ=P2^0;//ds18b20与单片机连接口#define uchar unsigned char#define uint unsigned intunsigned char hour=0,min=0,sec=0;unsigned int count=0;unsigned char line1[16]={" temp: "};unsigned char line2[16]={" time: 00:00:00"};unsigned char tab[]={'0','1','2','3','4','5','6','7','8','9'};uchar data disdata[5];uint tvalue;//温度值uchar tflag;//温度正负标志void time();/*************************lcd1602程序**************************/ void delay1ms(unsigned int ms)//延时1毫秒(不够精确的){unsigned int i,j;for(i=0;i<ms;i++)for(j=0;j<100;j++);}void delay5ms()//延时5毫秒(不够精确的){unsigned int i;for (i=0;i<1000;i++);}void delay50us(){register int i;for (i=0;i<20;i++);}void delay(){unsigned char m,n;for(m=255;m>0;m--)for(n=255;n>0;n--);}void wr_com(unsigned char comm)//********写控制字符程序E=1 RS=0RW=0**********//{LCDE=0;RS=0; //********RS寄存器选择输入端,当RS=0;当进行写模块操作,指向指令寄存器。

RW=0; //********当RS=1,无论是读操作还是写操作,都是指向数据寄存器。

LCDE=1;Ddata=comm;RS=0;RW=0;LCDE=0;}void wr_data(unsigned char dat)//*****当前位置写字符子程序:E=1 RS=1 RW=0{LCDE=0;RS=0; //********RS寄存器选择输入端,当RS=0;当进行写模块操作,指向指令寄存器。

RW=0; //********当RS=1,无论是读操作还是写操作,都是指向数据寄存器。

RS=1;RW=0;LCDE=1;Ddata=dat;LCDE=0;RS=0;RW=0;}unsigned char busycheck()//*****忙状态检查*******//{unsigned char i;LCDE=0;RS=0;RW=0;RS=0;RW=1;LCDE=1;i=Ddata;LCDE=0;return(i);}void init()//**********初始化程序,必须按照产品的资料介绍的过程进行********// {wr_com(0x38); //*****显示模式设置必须三次,不用检测忙信号delay5ms();wr_com(0x38);delay5ms();wr_com(0x38);delay5ms();wr_com(0x01); ///***清屏****delay5ms();busycheck();wr_com(0x08);delay5ms();busycheck();delay5ms();wr_com(0x06); ///****光标移动设置***delay5ms();busycheck();delay50us();wr_com(0x38); ////***显示模式设置****delay50us();busycheck();delay50us();wr_com(0x0c); /////***显示开***delay50us();busycheck();delay50us();wr_com(0x40); //set cgram addressdelay50us();delay50us(); //***这些延时可用可不用***// }void display(){ unsigned char i;wr_com(0x80); //set ram addressdelay50us();busycheck();delay50us();for(i=0;i<6;i++){wr_data(line1[i]); //displaydelay50us();busycheck();}busycheck();delay50us();wr_com(0xc0);delay50us();busycheck();delay50us();for(i=0;i<16;i++){wr_data(line2[i]); //displaydelay50us();busycheck();}}/******************************ds1820程序***************************************/ void delay_18B20(unsigned int i)//延时1微秒while(i--);}void ds1820rst()/*ds1820复位*/{ unsigned char x=0;DQ = 1; //DQ复位delay_18B20(4); //延时DQ = 0; //DQ拉低delay_18B20(100); //精确延时大于480us DQ = 1; //拉高delay_18B20(40);}uchar ds1820rd()/*读数据*/{ unsigned char i=0;unsigned char dat = 0;for (i=8;i>0;i--){ DQ = 0; //给脉冲信号dat>>=1;DQ = 1; //给脉冲信号if(DQ)dat|=0x80;delay_18B20(10);}return(dat);}void ds1820wr(uchar wdata)/*写数据*/ {unsigned char i=0;for (i=8; i>0; i--){ DQ = 0;DQ = wdata&0x01;delay_18B20(10);DQ = 1;wdata>>=1;}}read_temp()/*读取温度值并转换*/{uchar a,b;ds1820rst();ds1820wr(0xcc);//*跳过读序列号*/ds1820wr(0x44);//*启动温度转换*/ds1820rst();ds1820wr(0xcc);//*跳过读序列号*/ds1820wr(0xbe);//*读取温度*/a=ds1820rd();b=ds1820rd();tvalue=b;tvalue<<=8;tvalue=tvalue|a;if(tvalue<0x0fff)tflag=0;else{tvalue=~tvalue+1;tflag=1;}tvalue=tvalue*(0.625);//温度值扩大10倍,精确到1位小数return(tvalue);}/*******************************************************************/ void ds1820disp()//温度值显示{ uchar flagdat;disdata[0]=tvalue/1000+0x30;//百位数disdata[1]=tvalue%1000/100+0x30;//十位数disdata[2]=tvalue%100/10+0x30;//个位数disdata[3]=tvalue%10+0x30;//小数位if(tflag==0)flagdat=0x20;//正温度不显示符号elseflagdat=0x2d;//负温度显示负号:-if(disdata[0]==0x30){disdata[0]=0x20;//如果百位为0,不显示if(disdata[1]==0x30){disdata[1]=0x20;//如果百位为0,十位为0也不显示}}wr_com(0x88);wr_data(flagdat);//显示符号位wr_com(0x89);wr_data(disdata[0]);//显示百位wr_com(0x8a);wr_data(disdata[1]);//显示十位wr_com(0x8b);wr_data(disdata[2]);//显示个位wr_com(0x8c);wr_data(0x2e);//显示小数点wr_com(0x8d);wr_data(disdata[3]);//显示小数位}void main(){TMOD=0X01;EA=1;ET0=1;TR0=1;TH0=(65536-4995)/256;TL0=(65536-4995)%256;init();while(1){read_temp();//读取温度ds1820disp();//显示time();display();}}void t0(void)interrupt 1 using 0 {TH0=(65536-4995)/256;TL0=(65536-4995)%256;count++;if(count==200){count=0;sec++;}if(sec==60){sec=0;min++;}if(min==60){min=0;hour++;}if(hour==24){hour=0;}}void time(){line2[15]=tab[sec%10];line2[14]=tab[sec/10];line2[12]=tab[min%10];line2[11]=tab[min/10];line2[9]=tab[hour%10];line2[8]=tab[hour/10];if(!P1_2){delay();if(!P1_2){delay();sec++;if(sec==60){sec=0;}}}if(!P1_1){delay();if(!P1_1){delay();min++;if(min==60){min=0;}}}if(!P1_0){delay();if(!P1_0){delay();hour++;if(hour==24){hour=0;}}} }。

相关文档
最新文档