经典12864和DS18B20温度显示程序

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
本程序说明: 该程序应用在 STC89C52 单片机,连接 12864 (带字库)液晶 和 DS18B20 温度传感器;只要连接好硬件电路后,并且下载此程序, 即可运行使用。
/******************************************************************/ #include <reg52.h> /******************************************************************/ #define uchar unsigned char #define uint unsigned int /******************************************************************/ uchar code table1[]={" 我的大学 "}; uchar code table2[]={"1234567890123456"}; uchar code table3[]={"当前温度为 :Temp "}; uchar code table4[]={" 摄氏度 "}; /******************************************************************/ sbit DQ=P3^3; //ds18b20 数据口 sbit rs=P3^5; //12864 rs 端口 sbit rw=P3^6; //12864 rw 端口 sbit e=P3^4; //12864 e 端口 sbit psb=P3^7; //12864 psb 端口 uchar m; uint temp; uchar flag_get,num,sign; uchar st[6]; /******************************************************************/ void delay(uint i) //延时函数 { while(i--); } void Init_DS18B20() //ds18b20 初始化 { uchar x=0; DQ = 1; delay(8); DQ = 0;
delay_50us(10); e=0; delay_50us(2); } /******************************************************************/ void init_12864() { delay_1ms(100); write_12864_com(0x30); delay_50us(4); write_12864_com(0x30); delay_50us(4); write_12864_com(0x0c); delay_50us(4); write_12864_com(0x01); delay_50us(240); write_12864_com(0x06); delay_50us(10); } ////来自百度文库////////////////////////////////////////////////////////// void main() { uchar TempH,dot,i; init_12864(); write_12864_com(0x80); for(i=0;i<16;i++) { write_12864_dat(table1[i]); delay_50us(1); } write_12864_com(0x90); for(i=0;i<16;i++) { write_12864_dat(table2[i]); delay_50us(1); } write_12864_com(0x88); for(i=0;i<16;i++){ write_12864_dat(table3[i]); delay_50us(1); } write_12864_com(0x98); for(i=0;i<16;i++)
delay(200); Init_DS18B20(); WriteOneChar(0xCC); WriteOneChar(0xBE); a=ReadOneChar(); b=ReadOneChar(); b<<=8; t=a+b; return(t); }
//////////////////////////////////////////////////////////////// void delay_1ms(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void delay_50us(uint t) { uchar j; for(;t>0;t--) for(j=19;j>0;j--); } /******************************************************************/ void write_12864_com(uchar com) { rw=0; rs=0; delay_50us(1); P0=com; e=1; delay_50us(10); e=0; delay_50us(2); } /******************************************************************/ void write_12864_dat(uchar dat) { rw=0; rs=1; delay_50us(1); P0=dat;e=1;
{ write_12864_dat(table4[i]); delay_50us(1);
} TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; IE=0x82;EA=1;ET0=1; TR0=1; while(1) { //st[1]=TempH/100+0x30; // 百位温度 st[2]=(TempH%100)/10+0x30; // 十位温度 st[3]=(TempH%100)%10+0x30; // 个位温度 st[4]='.'; st[5]=dot+0x30; // 小数位 write_12864_com(0x98+1); for(i=0;i<6;i++) { write_12864_dat(st[i]); delay_50us(1); } if(flag_get==1) { temp=ReadTemperature(); if(temp&0x8000) { st[0]=0x2d; temp=~temp; temp +=1; } else st[0]=0x2b; TempH=temp>>4; dot=temp&0x0F; dot=dot*6/10;flag_get=0; } } ET0=0; TR0=0; write_12864_com(0x01); }
/******************************************************************/ void time0(void) interrupt 1 using 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; num++; if (num==15) {num=0; flag_get=1; } }
delay(80); DQ = 1; delay(10); x=DQ; delay(5); } uchar ReadOneChar()//ds18b20 读取温度值 { uchar i=0; uchar dat = 0; for (i=8;i>0;i--) { DQ = 0; dat>>=1; DQ = 1; if(DQ) dat|=0x80; delay(5); } return(dat); } /******************************************************************/ void WriteOneChar(uchar dat) {uchar i=0; for (i=8; i>0; i--) { DQ = 0; DQ = dat&0x01; delay(5); DQ = 1; dat>>=1; } delay(5); } /******************************************************************/ uint ReadTemperature()//ds18b20 读取温度值 { uchar a=0; uint b=0; uint t=0; Init_DS18B20(); WriteOneChar(0xCC); WriteOneChar(0x44);
相关文档
最新文档