|
板子没有问题,输入其他显示程序可以正常显示,但是这个程序输入进去,运行却没有完整的显示出来。
实际运行图
原理图
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit BEEP=P2^1;//蜂鸣器接口定义
- sbit DQ=P1^6; //ds18b20数据线接口定义
- sbit RS=P3^0;
- sbit RW=P3^1;
- sbit E=P3^2;
- /*函数定义*/
- void delay(uint k);
- void init_watch(void);
- void lcd_init(void);
- void showchar(void);
- void lcd_write_command(uchar dat);
- void lcd_write_data(uchar dat);
- void display(uchar *p);
- void watch_show(void);
- uint read_temp(void);
- void ds18b20_reset(void);
- uchar ds18b20_readdata(void);
- void ds18b20_writedata(uchar dat);
- void ds18b20_temp_display(void);
- void key_scan(void);
- uchar code table[]={0x10,0x06,0x09,
- 0x08,0x08,0x09,0x06,0x00};//字符℃
- uchar tab[]={0x30,0x31,0x32,0x33,0x34,
- 0x35,0x36,0x37,0x38,0x39};//0~9ASCII码
- /*字符串及变量定义*/
- uchar code str1[]={"TEMP:"};
- uchar code str2[]={"TH:"};
- uchar code str3[]={"TL:"};
- uchar code str4[]={"member1:"};
- uchar code str5[]={"chen feng!"};
- uchar code str6[]={"member2:"};
- uchar code str7[]={"zhang shun!"};
- uchar code str8[]={"member3:"};
- uchar code str9[]={"yang zheng ru!"};
- uchar code str10[]={" Welcome"};
- uchar code str11[]={"To wen kong!"};
- uchar hour1,hour2,min1,min2,sec1,
- sec2,t,hour,min,sec,tflag;
- uchar year1,year2,year3,year4,month,
- month1,month2,day,day1,day2;
- uchar moshi=0;//初始显示界面定义
- uint year,tvalue,temph=30,templ=15;
- unsigned data disdata[5];
- void delay(uint k)
- { while(k--);}
- /*lcd初始化*/
- void lcd_init(void)
- { lcd_write_command(0x30);
- lcd_write_command(0x30);
- lcd_write_command(0x30);
- lcd_write_command(0x38);
- lcd_write_command(0x08);
- lcd_write_command(0x01);
- lcd_write_command(0x06);
- lcd_write_command(0x0c);
- }
- /*始终初始化及中断定义*/
- void init_watch()
- { hour=10;year=2012;month=7;day=5;
- lcd_init();
- TMOD=0x01; //工作方式1
- TH0=(65536-5000)/256;
- TL0=(65536-5000)%256;
- EA=1; //开总中断
- ET0=1; //开定时器中断0
- EX0=1; //开外部中断0
- IT0=1;//外部中断0下降沿触发
- TR0=1;//启动定时器0
- }
- /*显示温度界面提示字符*/
- void showchar(void)
- { lcd_init();
- lcd_write_command(0x80);
- display(str1);//显示字符串str1
- }
- /*lcd写命令子程序*/
- void lcd_write_command(uchar dat)
- {
- delay(5);//操作前短暂延时,保证信号稳定
- P0=dat;
- RS=0;
- RW=0;
- E=1;
- E=0;
- delay(100);
- }
- /*lcd写数据子程序*/
- void lcd_write_data(uchar dat)
- {
- delay(5);//操作前短暂延时,保证信号稳定
- P0=dat;
- RS=1;
- RW=0;
- E=1;
- E=0;
- delay(100);
- }
- /*lcd显示时间*/
- void watch_show()
- { 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;hour1=hour/10;
- hour2=hour%10;min1=min/10;
- min2=min%10; sec1=sec/10;sec2=sec%10;
- lcd_write_command(0x84);
- lcd_write_data(tab[hour1]);//显示时高位
- lcd_write_data(tab[hour2]);//显示时低位
- lcd_write_data(0x3a);
- lcd_write_data(tab[min1]);//显示分高位
- lcd_write_data(tab[min2]);//显示分低位
- lcd_write_data(0x3a);
- lcd_write_data(tab[sec1]);//显示秒高位
- lcd_write_data(tab[sec2]);//显示秒低位
- lcd_write_command(0xc3);//下四行显示年
- lcd_write_data(tab[year1]);
- lcd_write_data(tab[year2]);
- lcd_write_data(tab[year3]);
- lcd_write_data(tab[year4]);
- lcd_write_data('-');//显示-
- lcd_write_data(tab[month1]);//显示月高位
- lcd_write_data(tab[month2]);//显示月低位
- lcd_write_data('-');
- lcd_write_data(tab[day1]);//显示日高位
- lcd_write_data(tab[day2]);//显示日低位
- }
- /*显示字符子程序*/
- void display(uchar *p)
- { while(*p!='\0')
- { lcd_write_data(*p);p++; }
- }
- /*ds18b20复位*/
- void ds18b20_reset(void)
- { DQ=0;
- delay(100);
- DQ=1;
- delay(50);
- }
- /*读ds8b20数据*/
- uchar ds18b20_readdata(void)
- { uchar i,value;
- value=0;
- for(i=8;i>0;i--)
- { DQ=0;
- value>>=1;
- DQ=1;
- if(DQ)
- { value|=0x80;}
- delay(5);
- }
- return(value);
- }
- /*ds18b20写数据*/
- void ds18b20_writedata(uchar dat)
- { uchar i;
- for(i=8;i>0;i--)
- { DQ=0;
- DQ=dat&0x01;
- delay(5);
- DQ=1;
- dat>>=1;
- }
- }
- /*读取温度并转换*/
- uint read_temp(void)
- { uchar a,b;
- ds18b20_reset();
- ds18b20_writedata(0xcc);
- ds18b20_writedata(0x44);
- delay(100);
- ds18b20_reset();
- ds18b20_writedata(0xcc);
- ds18b20_writedata(0xbe);
- delay(100);
- a=ds18b20_readdata();
- b=ds18b20_readdata();
- tvalue=b;
- tvalue<<=8;
- tvalue=tvalue|a;
- if(tvalue<=0x0fff)
- tflag=0;//温度符号位为正
- else
- { tvalue=~tvalue+1;tflag=1; }//符号位为负
- tvalue=tvalue*0.625;
- return(tvalue);
- }
- /*温度显示*/
- void ds18b20_temp_display(void)
- { char flagdat,m=0;
- 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=' ';//温度为正不显示
- else flagdat=0x2d;//温度为负则显示-
- if(disdata[0]==0x30)
- { disdata[0]=0x20;//温度最高位为0不显示
- if(disdata[1]==0x30)
- { disdata[1]=0x20; }
- }
- lcd_write_command(0x89);
- lcd_write_data(flagdat);//显示符号位
- lcd_write_data(disdata[0]);//显示温度
- lcd_write_data(disdata[1]);
- lcd_write_data(disdata[2]);
- lcd_write_data(0x2e);//显示小数点
- lcd_write_data(disdata[3]);//显示温度小数位
- lcd_write_command(0x40); for(m=0;m<8;m++) //CGRAM中写入字符℃
- { lcd_write_data(table[m]); }
- lcd_write_command(0x8f);
- lcd_write_data(0x00);//显示字符℃
- lcd_write_command(0xc0);
- display(str2);
- lcd_write_data((temph/10)+0x30);
- lcd_write_data((temph%10)+0x30);
- lcd_write_command(0xc5);
- lcd_write_data(0x00);
- lcd_write_command(0xc9);
- display(str3);
- lcd_write_data((templ/10)+0x30);
- lcd_write_data((templ%10)+0x30);
- lcd_write_command(0xce);
- lcd_write_data(0x00);
- }
- /*定时器0中断产生时间*/
- void timer0() interrupt 1
- {TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- t++;
- if(t==20)
- { t=0;
- sec++;
- if(sec==60)
- { sec=0;
- min++;
- if(min==60)
- { min=0;
- hour++;
- if(hour==24)
- { hour=0;
- day++;
- if((month==1||month==3||month==5||
- month==7||month==8||month==10||
- month==12)&day==32)
- { day=1;
- month++;
- }
- if((month==4||month==6||month==9||
- month==11)&&day==31)
- { day=1;
- month++;
- }
- if(year%4!=0&&month==2&&day==29)
- { day=1;
- month++;
- }
- if(year%4==0&&month==2&&day==30)
- { day=1;
- month++;
- }
- if(month==13)
- { month=1;
- year++;
- }
- }
- }
- }
- }
- }
- /*外部中断0实现显示界面的转换*/
- void iint0() interrupt 0
- { moshi++;//一次中断则显示界面转换一次
- if(moshi==6) moshi=0;//共6个显示界面
- }
- /*4*4键盘扫描*/
- uchar Keycan(void)
- { uchar rcode, ccode;
- P1=0xF0;
- if((P1&0xF0) != 0xF0)
- { delay(30000);
- if((P1&0xF0) != 0xF0)
- {rcode=0xFE;
- while((rcode&0x10) != 0)
- {P1=rcode;
- if((P1&0xF0) != 0xF0)
- { ccode = (P1&0xF0)|0x0F;
- do{;}while((P1&0xF0)!=0xF0);
- return ((~rcode) + (~ccode));
- }
- else
- rcode = (rcode<<1)|0x01;
- }
- }
- }
- return 0;
- }
- /*根据扫描结果调整报警上下限、日期及时间*/
- void KeyDispaly(uchar key)
- { switch(key)
- {case 0x11: temph++; break;//提高报警上限
- case 0x21: temph--; break;//降低报警上限
- case 0x41: templ++; break;//提高报警下限
- case 0x81: templ--; break;//降低报警下限
- case 0x12: sec++; break;//按键增加秒
- case 0x22: sec--; break;//按键降低秒
- case 0x42: min++; break;//按键增加分
- case 0x82: min--; break;//按键降低分
- case 0x14: hour++; break;//按键增加时
- case 0x24: hour--; break;//按键降低时
- case 0x44: day++; break;//按键增加日
- case 0x84: day--; break;//按键降低日
- case 0x18: month++; break;//按键增加月
- case 0x28: month--; break;//按键降低月
- case 0x48: year++; break;//按键增加年
- case 0x88: year--; break;//按键降低年
- default: break;
- }
- }
- /*主程序*/
- void main(void)
- { uchar Key;
- DQ=1;
- BEEP=1;//初始蜂鸣器不报警
- init_watch();
- while(1)
- { if(moshi==0)//显示界面1
- { lcd_write_command(0x01);
- while(moshi==0)
- { lcd_write_command(0x80);
- display(str10);
- lcd_write_command(0xc1);
- display(str11);
- }
- }
- if(moshi==1)//显示界面2
- { lcd_write_command(0x01);//lcd清屏
- while(moshi==1)
- { lcd_write_command(0x80);
- display(str4);
- lcd_write_command(0xc2);
- display(str5);
- }
- }
- if(moshi==2)//显示界面3
- { lcd_write_command(0x01);
- while(moshi==2)
- { lcd_write_command(0x80);
- display(str6);
- lcd_write_command(0xc3);
- display(str7);
- }
- }
- if(moshi==3)//显示界面4
- { lcd_write_command(0x01);
- while(moshi==3)
- { lcd_write_command(0x80);
- display(str8);
- lcd_write_command(0xc1);
- display(str9);
- }
- }
- if(moshi==4)//显示界面5
- { lcd_write_command(0x01);
- showchar();
- while(moshi==4)
- { read_temp();//读温度
- if(((tvalue/10)%100)>=temph)
- BEEP=0;//温度大于上限时报警
- else if(((tvalue/10)%100)<=templ)
- BEEP=0;//温度小于下限时报警
- else BEEP=1;//否则不报警
- ds18b20_temp_display();
- Key = Keycan();//键盘扫描
- KeyDispaly(Key);//调整报警上下限
- }
- }
- if(moshi==5)//显示界面6
- { lcd_write_command(0x01);
- while(moshi==5)
- { watch_show();//显示日期时间
- Key = Keycan();
- KeyDispaly(Key);
- }
- }
- }
- }
复制代码
|
|