DS18B20温测 12864显示

合集下载

基于Arduino的18B20+LCD12864温度显示

基于Arduino的18B20+LCD12864温度显示

基于Arduino的18B20+LCD12864温度显示本帖最后由 greenfort 于 2012-8-13 01:49 编辑初学Arduino,制作的通过12864显示多个18B20温度的程序接线请参考本论坛中的18B20及LCD12864与Arduino接线,这里不再重复,只是强调一点,18B20的PIN1和PIN3要同时接地,否则会在返回若干值后出现一次错误值(85.00)不知道什么原因鉴于12864的显示大小,程序最多接9个18B20传感器,多了显示不了程序代码如下1. /*2. LCD Arduino3. PIN1 = GND4. PIN2 = 5V5. RS(CS) = 8;6. RW(SID)= 9;7. EN(CLK) = 3;8. PIN15 PSB = GND;9. */10. #include <OneWire.h>11. #include <DallasTemperature.h>12. #include <stdlib.h>13. #define ONE_WIRE_BUS 214. #include "LCD12864RSPI.h"15. #define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )16.17. OneWire oneWire(ONE_WIRE_BUS);18. DallasTemperature sensors(&oneWire);19.20. char str[4]; //定义温度值存储数组,4位,其中3位为数字,1位为小数点21. double temp =0; //定义中间变量,用于将获取的float型温度值转变为unsigned char数组22. unsigned char show[]={23. 0xB5,0xB1,0xC7,0xB0,0xBB,0xB7,0xBE,0xB3,0xCE,0xC2,0xB6,0xC8};//当前环境温度24. int numberOfDevices;25. void setup()26. {27. Serial.begin(9600);28. sensors.begin(); //传感器初始化29. numberOfDevices = sensors.getDeviceCount();30. LCDA.Initialise(); // 屏幕初始化31. LCDA.DisplayString(0,0,show,AR_SIZE(show));//第一行第1格开始,显示文字“当前环境温度”32. }33.34. void loop()35. {36. sensors.requestTemperatures(); //获取温度值37. for(int i=0;i<numberOfDevices; i++) //逐个获取传感器的温度38. {39. temp=sensors.getTempCByIndex(i); //使用索引序号获得摄氏度数值40. dtostrf(temp,4,2,str); //将获取的数值转换为字符型数组41. if (i<=2)42. {43. LCDA.DisplayString(1,i*3,(unsigned char *)str,sizeof(str));。

12864+ds1302+ds18b20数字时钟_温度_日期同步显示

12864+ds1302+ds18b20数字时钟_温度_日期同步显示

毕业(论文)说明书题目:系别:专业:指导老师:学生姓名:学号:理论研究工程设计工程技术研究软件开发2014年7 月18日一毕业设计(论文)内容与要求一、主要研究内容1、8051单片机硬件结构。

2、C语言程序设计基础内容。

3、单片机C语言程序设计的方法。

4、DS18B20温度传感器的使用方法。

5、DS1302时钟芯片的用法。

6、12864LCD液晶屏的编程使用方法。

二、要求1、能够以指针的形式在LCD12864上显示当前时间的小时和分钟和秒。

2、能够以数字加汉字的形式在LCD12864上显示当前时间的小时和分钟和秒。

3、能够以数字加汉字的形式在LCD12864上显示当前年月日。

4、能够以数字加汉字的形式在LCD12864上显示当前星期。

5、时间采用时钟芯片DS1302控制。

6、温度采用DS18B20温度传感器检测当前温度。

7、所有功能在LCD12864当中同步显示。

8、采用AT89S52或者STC89C52RC单片机控制。

三、引言随着人们生活水平的不断提高,单片机控制无疑是人们追求的目标之一,它所给人带来的方便也是不可否定的,要为现代人工作、科研、生活、提供更好的更方便的设施就需要从数单片机技术入手,一切向着数字化控制,智能化控制方向发展。

本设计是数据采集及处理,显示系统与单片机有效结合,本设计是通过在“单片机原理及应用”课堂上学习的知识的综合应用,以及查阅资料,培养一种自学的能力。

并且引导一种创新的思维,把学到的知识应用到日常生活当中。

在设计的过程中,不断的学习,思考和同学间的相互讨论,运用科学的分析问题的方法解决遇到的困难,掌握单片机系统一般的开发流程,学会对常见问题的处理方法,积累设计系统的经验,充分发挥教学与实践的结合。

全能提高个人系统开发的综合能力,开拓了思维,为今后能在相应工作岗位上的工作打下了坚实的基础。

四、方案设计及方案论证1.时钟温度的总体设计思路按照系统的设计功能要求,本时钟温度系统的设计必须采用单片机软件系统实现,用单片机的自动控制能力配合ds1302和ds18b20来控制时钟和温度的调整显示。

基于LCD12864的温度传感器DS18b20的显示程序

基于LCD12864的温度传感器DS18b20的显示程序

/*由于DS18B20对延时的要求很高,所以要根据采用的晶振来确定延迟时间,以下程序待对时间都有明确标注,根据你选用的晶振来调节即可*/#include <reg52.h> //#include <intrins.h>#define uchar unsigned char#define uint unsigned int#define LCD_data P0 //显示屏输出数据口/*液晶控制端口*/sbit LCD_RST = P1^4; //液晶复位端口sbit LCD_RS = P1^5; //寄存器选择输入sbit LCD_RW = P1^6; //液晶读/写控制sbit LCD_EN = P1^7; //液晶使能控制sbit DQ=P1^0;uint a,b,i;uchar str[16]={"--温度:000.0 M--"}; //定义数组uchar k,m;float temp;//延时子函数1void delay5us(uchar z){uchar i;for(i=0;i<z;i++);}void delayNOP(){_nop_();_nop_();_nop_();_nop_();}void delay(uint z) //延时子函数1 ms{uint x,y;for(x=z;x>0;x--)for(y=200;y>0;y--);}bit lcd_busy() //检测液晶忙碌?{bit result;LCD_RS = 0;LCD_RW = 1;LCD_EN = 1;delayNOP();result = (bit)(P0&0x80);LCD_EN = 0;return (result);}void lcd_wcmd(uchar cmd) //液晶屏写指令{while(lcd_busy());LCD_RS = 0;LCD_RW = 0;LCD_EN = 0;_nop_();_nop_();P0 = cmd;delayNOP();LCD_EN = 1;delayNOP();LCD_EN = 0;}void lcd_wdat(uchar dat) //写数据{while(lcd_busy());LCD_RS = 1;LCD_RW = 0;LCD_EN = 0;P0 = dat;delayNOP();LCD_EN = 1;delayNOP();LCD_EN = 0;}void lcd_init() //液晶初始化{LCD_RST = 0; //液晶复位delay(3);LCD_RST = 1;delay(3);lcd_wcmd(0x34); //扩充指令操作delay(5);lcd_wcmd(0x30); //基本指令操作delay(5);lcd_wcmd(0x0C); //显示开,关光标delay(5);lcd_wcmd(0x01); //清除LCD的显示内容delay(5);}uchar ds18_init(){uchar i;DQ = 0;//总线拉低delay5us(100);//500usDQ = 1;//总线拉高delay5us(20);//延时100usi=DQ;delay5us(100);//500usreturn i;}void write_18(uchar value){uchar i,v1=0;v1 = value;for (i =0 ;i<8;i++){DQ = 0;//总线拉低delay5us(2);//延时10usif(v1&0x01)DQ = 1;delay5us(20);//延时100usDQ = 1;v1=v1>>1;}}uchar read_18(){uchar i,v2 = 0;for (i=0;i<8;i++){v2=v2>>1;DQ = 0;delay5us(2);//延时10usDQ = 1;if(DQ)v2|=0x80;delay5us(10);//延时50us}return v2;}void shujuhuansuan() //测温公式{temp = (m*256+k)*0.625;str[8] = (uint)(temp)/100;str[9] = (uint)(temp)%100/10;str[11] = (uint)(temp)%10;}void LCD_xs() //如何在显示屏上显示计算的温度:采用写字符的形式{ uchar s;lcd_wcmd(0x80);for(s = 0;s < 8;s++){lcd_wdat(str[s]);}for(s = 8;s < 10;s++)lcd_wdat(0x30 + str[s]);lcd_wdat(str[10]);lcd_wdat(0x30 + str[11]);for(s = 12;s < 16;s++)lcd_wdat(str[s]);}void main(){delay(100); //上电,等待显示屏稳定lcd_init(); //初始化LCDwhile(1){while(ds18_init()); //当DQ为高电平时,说明存在ROM 继续执行,若不存在就一直在此等write_18(0xcc);write_18(0x44);delay(1000);while(ds18_init());write_18(0xcc);write_18(0xbe);k= read_18();m=read_18();shujuhuansuan();delay(10);LCD_xs();delay(1000);}}。

用DS18B20测温度并用LCD12864液晶屏显示

用DS18B20测温度并用LCD12864液晶屏显示

本程序实现的功能:用DS18B20测温度,并用LCD12864液晶屏显示#include<reg52.h>#include<intrins.h>#define uint unsigned int#define uchar unsigned charsbit rs=P1^1;sbit rw=P1^2;sbit en=P1^3;sbit psb=P1^4;sbit ds=P1^5;uchar code table0[]="Current temp:";uchar code table1[]="当前温度:";uchar code table2[]="摄氏度";uchar table[4];uint temp;float f_temp;void delay(uint tms){uint i,j;for(i=tms;i>0;i--)for(j=110;j>0;j--);}void init_ds(){uint i;ds=0;i=103;while(i>0)ds=1;i=4;while(i>0)i--;}void tempwritebyte(uchar )//由时序图知,向DS18B20写入数据时,写0和写1的时序恰好相反{ //所以如下程序用if和else分别写入0和1;而当从DS18B20读数据时,uint i,j; //读0和读1的时序根本一样,所以读0和读1的程序合二为一,不用分别写出bit bb;for(i=0;i<8;i++){bb=&0x01;//最低位开始写入DS18B20=>>1;if(bb){ds=0;j++;j++;ds=1;j=8;while(j>0)j--;}else{j=8;while(j>0)j--;ds=1;j++;j++;}}}void tempchange(){init_ds();delay(1);tempwritebyte(0xcc);tempwritebyte(0x44);}uchar tempreadbyte()//由时序图知,向DS18B20写入数据时,写0和写1的时序恰好相反{ //所以如下程序用if和else分别写入0和1;而当从DS18B20读数据时,uchar i,j,m,n; //读0和读1的时序根本一样,所以读0和读1的程序合二为一,不用分别写出bit dat;for(i=0;i<8;i++){ds=0;j++;j++;ds=1;j++;j++;dat=ds;j=8;while(j>0)j--;m=dat;//把一位数据赋给一个字节,且从最低位开始读数n=(m<<7)|(n>>1);}return n; //必须将n值返回去,以赋给a和b}void get_temp(){uchar a,b;init_ds();delay(1);tempwritebyte(0xcc);tempwritebyte(0xbe);a=tempreadbyte();b=tempreadbyte();temp=b;temp<<=8;temp=temp|a;f_temp=temp*0.0625*10+0.5;//保存一位小数,且最低位四舍五入temp=f_temp;//return temp;}void wr_data(date) {rs=1;rw=0;en=0;P0=date;delay(3);en=1;delay(3);en=0;}void wr_(uchar ) {rs=0;rw=0;en=0;P0=;delay(3);en=1;delay(3);en=0;}void lcd_init() {psb=1;wr_(0x30);delay(3);wr_(0x0c);delay(3);wr_(0x06);delay(3);// wr_(0x01);// delay(3);}void lcd_pos(uchar X,uchar Y){if(X==0)X=0x80;else if(X==1)X=0x90;else if(X==2)X=0x88;else if(X==3)X=0x98;wr_(X+Y);}void display()//显示第一行为 current temp: { //第二行为当前温度:uchar i; //第三行为 xxx摄氏度table[0]=temp/100+'0';table[1]=temp/10%10+'0';table[2]='.';table[3]=temp%10+'0';lcd_pos(2,2);for(i=0;i<4;i++)wr_data(table[i]);lcd_pos(2,4);i=0;while(table2[i]!='\0'){wr_data(table2[i]);i++;}lcd_pos(0,0);i=0;while(table0[i]!='\0'){wr_data(table0[i]);i++;}lcd_pos(1,0);i=0;while(table1[i]!='\0'){wr_data(table1[i]);i++;}}void main(){lcd_init();while(1){tempchange();get_temp();display();}}。

温度DS18B20数字测土壤湿度程序代码(AD和12864)

温度DS18B20数字测土壤湿度程序代码(AD和12864)
} */
void main()
{ char8 i;
int16 shidu;
init();
init12864();//液晶初始化
a=1;b=1;c=0; //选通道in3
st=0;
while(1)
{
st=1;
st=0;
while(!eoc);
PCA_PWM0=0;//pwm寄存器
//timer0工作在时钟输出
WAKE_CLKO=0X01;//允许P3^4为时钟输出口
AUXR=0X80;//设置定时器0工作在1t模式
TMOD=0X02;//设置定时器0工作方式TL0=(256-11)%256;
for(i=0;i<4;i++)
write(0xfa,table[i]);
write(0xf8,0x80);
delayD(50000);
}
}
/**********z=1时**延时大约100us********/
void delayD(int16 z)
{
int16 i,j;
for(i=z;i>0;i--)
for(j=86;j>0;j--);
}
/**********z=0时**延时大约1.54us********/
void delayX(int16 z)
{
while(z--);/***大约为9个周期1.54us**/
}
/**********12864串行通信******************/
//串行接口时序参数:(测试条件:T=25℃ VDD=4.5V)*/

12864_ds18b20

12864_ds18b20
{
Init_DS18B20();
WriteOneChar(0xcc);//发跳过ROM命令
WriteOneChar(0x44);//发读开始转换命令
Init_DS18B20();
WriteOneChar(0xcc);//发跳过ROM命令
WriteOneChar(0xbe);//读寄存器,共九字节,前两字节为转换值
0x04,0x04,0x7E,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00,
/*-- 文字: 度 --*/
/*-- 新宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00,
/*-- 文字: . --*/
(2)延时2微秒。
(3)将数据线拉低“0”。
(4)延时3微秒。
(5)将数据线拉高“1”。
(6)延时5微秒。
(7)读数据线的状态得到1个状态位,并进行数据处理。
(8)延时60微秒。
*/
uchar ReadOneChar(void)
{
uchar i;
uchar dat=0;
x3=temp%100/10; //个位
x4=temp%10; //小数
/* P2 = 0xf1; P0 = table[x1]; delay(x);//百
P2 = 0xf2; P0 = table[x2]; delay(x);//十
P2 = 0xf4; P0 = table1[x3]; delay(x);//个

18B20温度采集LCD12864显示

18B20温度采集LCD12864显示

基于单片机STC89C51的温度显示系统的设计引言随着电子技术,特别是随大规模集成电路的产生而出现的微型计算机技术的飞速发展,人类生活发生了根本性的改变。

如果说微型计算机的出现使现代科学研究得到了质的飞跃,那么可以毫不夸张地说,单片机技术的出现则是给现代工业测控领域带来了一次新的技术革命。

目前,单片机以其体积小、重量轻、抗干扰能力强、对环境要求不高、高可靠性、高性能价格比、开发较为容易,在工业控制系统、数据采集系统、智能化仪器仪表、办公自动化等诸多领域得到极为广泛的应用,并已走人家庭,从洗衣机、微波炉到音响、汽车,到处都可见到单片机的踪影。

因此,单片机技术开发和应用水平已逐步成为一个国家工业发展水平的标志之一。

本课题研究的内容就是以单片机为主要控制元件,通过温度传感器DS18B20实现对温度的测量,并通过LCD12864直接显示所测温度。

1、设计任务用温度传感器DS18b20检测空气中的温度,STC89C51采集DS18B20的数据并通过LCD12864显示实时温度。

2、系统硬件设计2.1 系统方框图见图2-1.图3-1 系统方框图2.2 各模块方案的选择2.2.1 温度检测方案选择方案一:采用热电偶或热敏电阻作感温元件,但热电偶需冷端补偿,电路设计复杂,热敏电阻虽然精度较高,但需要标准稳定电阻匹配才能使用,而且重复性、可靠性都比较差。

方案二:采用集成温度传感器 DS18B20 。

该传感器结构简单,不需外接电路,数据传输采用one-wire。

总线,可用一根 I/O数据线即供电又传输数据,在-10 ℃ --+85℃范围内精度为±0.5℃,分辨率较高,重复性和可靠性好。

故采用方案二。

2.2.2 显示模块方案的选择方案一:采用数码管显示。

数码管亮度高、体积小、重量轻,但其显示信息简单、有限,在本题目中应用受到很大的限制。

方案二:采用液晶显示。

液晶显示功耗低,轻便防震。

采用液晶显示界面友好清晰,操作方便,显示信息丰富。

(完整版)读取DS18B20温度传感器的64位ID号方法及程序

(完整版)读取DS18B20温度传感器的64位ID号方法及程序

#include <reg52.h>#define uchar unsigned char#define uint unsigned int/********************************************************************/ sbit DQ = P2^2; //温度传感器信号线sbit rs = P3^5; //LCD数据/命令选择端(H/L)位声明sbit lcden = P3^4; //LCD使能信号端位声明/********************************************************************/ //uint temp; //定义整型的温度数据//float f_temp; //定义浮点型的温度数据//uint warn_11 = 270; //定义温度设定值,是温度值乘以10后的结果//uint warn_12 = 250; //定义温度下限值//uint warn_h1 = 300; //定义温度上限值/********************************************************************/ void delay(uint z); //延时函数void DS18B20_Reset(void); //DQ18B20复位,初始化函数bit DS18B20_Readbit(void); //读1位数据函数uchar DS18B20_ReadByte(void); //读1个字节数据函数void DS18B20_WriteByte(uchar dat); //向DQ18B20写一个字节数据函数void LCD_WriteCom(uchar com); //1602液晶命令写入函数void LCD_WriteData(uchar dat); //1602液晶数据写入函数void LCD_Init(); //LCD初始化函数void Display18B20Rom(char Rom); //显示18B20序列号函数/**********************************************//* 主函数 *//**********************************************/void main(){ uchar a,b,c,d,e,f,g,h;LCD_Init();DS18B20_Reset();delay(1);DS18B20_WriteByte(0x33);delay(1);a = DS18B20_ReadByte();b = DS18B20_ReadByte();c = DS18B20_ReadByte();d = DS18B20_ReadByte();e = DS18B20_ReadByte();f = DS18B20_ReadByte();g = DS18B20_ReadByte();h = DS18B20_ReadByte();LCD_WriteCom(0x80+0x40);Display18B20Rom(h);Display18B20Rom(g);Display18B20Rom(f);Display18B20Rom(e);Display18B20Rom(d);Display18B20Rom(c);Display18B20Rom(b);Display18B20Rom(a);while(1);}/***************************************************//* 延时函数:void delay() *//* 功能:延时函数 *//***************************************************/void delay(uint z)//延时函数{uint x,y;for( x = z; x > 0; x-- )for( y = 110; y > 0; y-- );}/***************************************************//* DS18B20函数:void DS18B20_Reset() *//* 功能:复位18B20 */ /***************************************************/void DS18B20_Reset(void)//DQ18B20复位,初始化函数{uint i;DQ = 0;i = 103;while( i > 0 ) i--;DQ = 1;i = 4;while( i > 0 ) i--;}/***************************************************//* DS18B20函数:void DS18B20_Readbit() *//* 功能:读1个字节数据函数 *//***************************************************/bit DS18B20_Readbit(void) //读1位数据函数{uint i;bit dat;DQ = 0;i++; //i++起延时作用DQ = 1;i++;i++;dat = DQ;i = 8;while( i > 0 )i--;return( dat );}/***************************************************//* DS18B20函数:void DS18B20_ReadByte() *//* 功能:读1个字节数据函数 */ /***************************************************/uchar DS18B20_ReadByte(void) //读1个字节数据函数{uchar i,j,dat;dat = 0;for( i = 1; i <= 8; i++ ){j = DS18B20_Readbit();dat = ( j << 7 ) | ( dat >> 1 );}return(dat);}/***************************************************//* DS18B20函数:void DS18B20_WriteByte() *//* 功能:向DQ18B20写一个字节数据函数 *//***************************************************/void DS18B20_WriteByte(uchar dat) //向DQ18B20写一个字节数据函数{uint i;uchar j;bit testb;for( j=1; j<=8; j++){testb = dat&0x01;dat= dat>>1;if(testb) //写1{DQ = 0;i++;i++;DQ = 1;i = 8;while(i>0)i--;}else{DQ = 0; //写0i = 8;while(i>0)i--;DQ = 1;i++;i++;}}}/***********************************************//* LCD函数:void LCD_WriteCom() *//* 功能:向LCD写入命令 *//***********************************************/void LCD_WriteCom(uchar com){rs = 0;P0 = com;delay(5);lcden = 0;delay(5);lcden = 1;delay(5);lcden = 0;}/***********************************************//* LCD函数:void LCD_WriteData(uchar dat) *//* 功能:向LCD写入数据 *//***********************************************/void LCD_WriteData(uchar dat){rs = 1; //选择LCD为写入数据状态lcden = 0;P0 = dat; //将待写入数据放到总线上delay(5);lcden = 1; //给LCD使能端一个脉冲delay(5); //信号将之前放到总线上lcden = 0; //的数据写入LCDdelay(5);}/***********************************************//* LCD函数:void LCD_Init() */ /* 功能:初始化LCD,设定LCD的初始状态 *//***********************************************/void LCD_Init(){LCD_WriteCom(0x38); //LCD显示模式设定delay(15);LCD_WriteCom(0x08); //关闭LCD显示delay(3);LCD_WriteCom(0x01); //LCD显示清屏delay(3);LCD_WriteCom(0x06); //设定光标地址指针为自动加1delay(3);LCD_WriteCom(0x0c); //打开LCD显示,但不显示光标}/**********************************************//* */ /* 显示18B20序列号 *//* *//**********************************************/void Display18B20Rom(char Rom){uchar h,l;l = Rom & 0x0f; //取低4位h = Rom & 0xf0; //取高4位h >>= 4;if( ( h >= 0x00 )&&( h <= 0x09 ) )LCD_WriteData(h+0x30); //取ASCII码elseLCD_WriteData(h+0x37); //取ASCII码if( ( l >= 0x00 )&&( l <= 0x09 ) )LCD_WriteData(l+0x30); //取ASCII码elseLCD_WriteData(l+0x37); //取ASCII码}。

12864串行显示DS18B20测得的温度

12864串行显示DS18B20测得的温度
{
uchar start_data,Hdata,Ldata;
if(start==0)
start_data=0xf8; //写指令
else
start_data=0xfa; //写数据
Hdata=onedata&0xf0; //取高四位
{
uchar i=0;
uchar dat = 0;
for (i=8;i>0;i--)
{
DS1820_DQ = 0; //将总线拉低,要在1us之后释放总线
dat>>=1; //让从总线上读到的位数据,依次从高位移动到低位。
DS1820_DQ = 1; //释放总线,此后DS18B20会控制总线,把数据传输到总线上
temp1=DS1820_ReadByte(); //读取温度值低位
temp2=DS1820_ReadByte(); //读取温度值高位
DS1820_Reset(); //DS1302 复位,表示读取结束
tt=((temp2<<8)|temp1)*6.25; //得到真实十进制温度值 //0.0625=xx, 0.625=xx.x, 6.25=xx.xx
delay(10); //延时是必须的
}
void initinal() //LCD字库初始化程序
{
write(0,0x30); //8 位介面,基本指令集
write(0,0x0c); //显示打开,光标关,反白关
write(0,0x01); //清屏,将DDRAM的地址计数器归零
Ldata=(onedata<<4)&0xf0; //取低四位

基于12864及18B20的温度液晶显示

基于12864及18B20的温度液晶显示

基于12864及18B20的温度液晶显示#include <reg52.h>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <math.h>/*-------------------Define the register commandcode----------------------*/#define Disp_On 0x3f#define Disp_Off 0x3e#define Col_Add 0x40#define Page_Add 0xb8#define Start_Line 0xc0#define Lcd_Bus P0#define uchar unsigned char#define uint unsigned intuchar t1,t2,t3,ser;sbit DS=P2^2; /*----------与单片机的单总线接口--------*/uint temp; /*----------可变温度值------------------*/uchar flag1; /*----------温度正负标志----------------*/sbit Mcs=P3^7;sbit Scs=P2^4;sbit Enable=P3^4;sbit Di=P3^5;sbit RW=P3^6;sbit Lcd_Rst=P2^1;sbit DIO=P2^5;sbit DU=P2^6;sbit WE=P2^7;char code ezk[]={0xF8,0xFC,0x04,0xC4,0x24,0xFC,0xF8,0x00, // -0-0x07,0x0F,0x09,0x08,0x08,0x0F,0x07,0x00,0x00,0x10,0x18,0xFC,0xFC,0x00,0x00,0x00, // -1-0x00,0x08,0x08,0x0F,0x0F,0x08,0x08,0x00,0x08,0x0C,0x84,0xC4,0x64,0x3C,0x18,0x00, // -2-0x0E,0x0F,0x09,0x08,0x08,0x0C,0x0C,0x00,0x08,0x0C,0x44,0x44,0x44,0xFC,0xB8,0x00, // -3-0x04,0x0C,0x08,0x08,0x08,0x0F,0x07,0x00,0xC0,0xE0,0xB0,0x98,0xFC,0xFC,0x80,0x00, // -4-0x00,0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x7C,0x7C,0x44,0x44,0xC4,0xC4,0x84,0x00, // -5-0x04,0x0C,0x08,0x08,0x08,0x0F,0x07,0x00,0xF0,0xF8,0x4C,0x44,0x44,0xC0,0x80,0x00, // -6-0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,0x0C,0x0C,0x04,0x84,0xC4,0x7C,0x3C,0x00, // -7-0x00,0x00,0x0F,0x0F,0x00,0x00,0x00,0x00,0xB8,0xFC,0x44,0x44,0x44,0xFC,0xB8,0x00, // -8-0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,0x38,0x7C,0x44,0x44,0x44,0xFC,0xF8,0x00, // -9-0x00,0x08,0x08,0x08,0x0C,0x07,0x03,0x00,/*------- ----0x30,0x30-----':'已改为'.'-----*/0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x06,0x00,0x00,0x00,};uchar code FirstPage[] ={0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x30,0x10,0x80,0x30,0x10,0x80 ,0xC8,0xB0,0xB0,0x76,0xC8,0xE8,0x76,0x99,0x7C,0x7E,0x9F,0x79,0x7C,0x7E,0x79,0x3E ,0x1C,0x09,0x37,0x13,0x09,0x0E,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x20,0x20,0x00,0x23,0x32,0x98,0x44,0x63,0x33,0x9C,0x23,0x75,0xCC ,0x03,0x25,0x34,0x1B,0x04,0x26,0x33,0x04,0x82,0x03,0x30,0x03,0x80,0x80,0x40,0x08 ,0x98,0x80,0x40,0x40,0x00,0x80,0x40,0xC0,0x00,0x00,0x00,0x80,0x80,0xC0,0xC0,0x00 ,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x20,0x60,0xC0,0x02,0x12,0x98,0xC0,0xCC,0x85,0x20,0xEC,0xF0,0xF9,0xE4,0xFC ,0xFC,0xFD,0xFE,0xFC,0xFC,0xFF,0xFE,0xFF,0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFE,0xFF,0xFF,0xFE,0xFF,0xFF,0xFE,0xFC,0xFE,0xFE,0xF0,0xF8,0xF0,0xF0,0x20,0x00 ,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x0C,0x06,0x06,0x60,0x08,0x4D,0xED,0xF8,0xFE,0xFF,0xFF,0x1F,0x9F,0x9F,0x0F,0x67 ,0x63,0x01,0x00,0x48,0x6D,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xEF,0xE7,0xF3 ,0x81,0xC9,0xED,0xE7,0xF3,0xE2,0xE0,0x31,0x69,0x2D,0xDF,0x8F,0xD7,0x93,0x03,0x0F ,0x0F,0x6E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x03,0x08,0x08,0x60,0xFF,0xFF,0xFF,0x7F,0xFF,0xFF,0x79,0xB6,0xB8,0x78,0x96,0x78 ,0x78,0x33,0x78,0x38,0x18,0x68,0x99,0xBB,0x1F,0x3F,0x1F,0x0F,0x3F,0x1F,0x0F,0x0F ,0x07,0x07,0x03,0x01,0x06,0x05,0x01,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x03,0x01,0x00,0x1B,0x01,0x00,0x06,0x00,0x00,0x06,0x00,0x04,0x04,0x01,0x00 ,0x02,0x03,0x00,0x04,0x04,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00};uchar code SecondPage[] = // 数据表64*64{0xFF,0x03,0x01,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFF,0x01,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01 ,0xFF,0xFF,0xFF,0x0F,0x03,0x01,0x39,0x19,0x39,0x39,0x39,0x19,0x19,0x09,0x1B,0xFF ,0xC1,0x81,0x8F,0x9F,0x9F,0x9F,0x9F,0x01,0x01,0x1F,0x9F,0x9F,0x9F,0x9F,0x81,0xC1 ,0xF3,0xFF,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFF,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00 ,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xC0,0x60,0xF8,0xFF ,0x03,0x01,0x48,0xCC,0xCC,0xCC,0xCC,0x00,0x00,0x48,0xCC,0xCC,0xCC,0x48,0x01,0x01 ,0xCF,0xFF,0xFF,0xC0,0x80,0x80,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F ,0x9F,0x00,0x00,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xFC,0xFC ,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x78,0x78,0x78,0x78,0x78,0x78,0x00,0x00,0xC0,0xFF ,0x0F,0x06,0x44,0xE4,0xE4,0xE4,0xE4,0x00,0x00,0xE4,0xE4,0xE4,0xE4,0xE4,0x06,0x0E ,0xFF,0xFF,0xFF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF ,0xCF,0xC0,0xC0,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF ,0xFF,0xFF,0xFF,0xC0,0xC0,0xE0,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,0xFF,0xFF ,0xC6,0xC0,0xE0,0xFF,0xFF,0xFF,0xFF,0xC0,0xC0,0xFF,0xFF,0xFF,0xFF,0xF0,0xC0,0xC4 ,0xFF,0xFF,0xFF,0xFF,0xE0,0x00,0x01,0x1F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F ,0x3F,0x7D,0x78,0x78,0x38,0x38,0x28,0x00,0x00,0x20,0x38,0x38,0x38,0x19,0x01,0x83 ,0xE7,0xFF,,0xF9,0x09,0x09,0xF9,0xFF,0xDB,0x89,0x89,0x89,0x89,0x89,0x89,0x89,0x89,0x19,0x39 ,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xFE,0xFE,0xFE,0xFE,0xFF,0xFE,0xFE,0xFE ,0xFF,0xF1,0xE0,0xC0,0x8E,0x8E,0x8E,0x8E,0x8E,0x8E,0x8E,0x8E,0x8E,0x8F,0x1F,0x3F ,0x7F,0xFF,0xFF,0xDC,0x08,0x88,0x83,0x83,0x83,0x83,0x83,0x81,0x89,0x88,0x0C,0x8F ,0x8F,0x40,0x40,0xCF,0xFF,0x7B,0x33,0x13,0x93,0x93,0x81,0x81,0x91,0x91,0x18,0x3C ,0xFF,0xFF,0xFF,0xFF,0x3F,0x00,0x00,0xC0,0xC7,0xC7,0xC7,0xC7,0xC7,0xC7,0xC7,0xC7 ,0xC7,0xC7,0xE7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x03,0x00,0xC0 ,0xFC,0xFF,0xFF,0xC0,0x80,0x18,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x00,0x80,0xFF ,0xFF,0x00,0x00,0xFF,0xFF,0xFC,0x80,0x01,0x1F,0x8F,0x07,0x23,0xF3,0xF3,0xF8,0xF8 ,0xFF,0xFF,0xFF,0xFF,0xF0,0xE0,0xC0,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF ,0x8F,0xCF,0xCF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0xC0,0xC0 ,0xDF,0xFF,0xFF,0xDF,0xCF,0xC7,0xE7,0xE7,0xE7,0xE3,0xE3,0xE7,0xE7,0xC7,0xC7,0xCF ,0xC7,0xE0,0xF0,0xFF,0xFF,0xEF,0xC0,0xE0,0xFE,0xFF,0xFE,0xFC,0xF8,0xF1,0xE3,0xC7 ,0xFF,0xFF};/*------------------延时子程序-----------------------------*/void delay(uint t){uint i,j;for(i=0;i<t;i++)for(j=0;j<10;j++);}/*------------------写命令到LCD------------------------------*/void write_com(uchar cmdcode){Di=0;RW=0;Lcd_Bus=cmdcode;delay(0);Enable=1;delay(0);Enable=0;}/*-------------------写数据到LCD----------------------------*/void write_data(uchar Dispdata){Di=1;RW=0;Lcd_Bus=Dispdata;delay(0);Enable=1;delay(0);Enable=0;}/*------------------清除内存---------------*/void Clr_Scr(){uchar j,k;Mcs=1;Scs=1;write_com(Page_Add+0);write_com(Col_Add+0);for(k=0;k<8;k++){write_com(Page_Add+k);for(j=0;j<64;j++)write_data(0x00);}}/*---------------------指定位置显示8*16数字-----------------------*/ /* 使用全部ASCII字符x=(order-0x20)*16 *//*------------同时main中数字abcdef应分别加0x30--------------------*/void ez_disp(uchar pag,uchar col,uchar order){uchar i=0;uchar j=0;uint x;x=(order-0x30)*16;for(j=0;j<2;j++){write_com (Page_Add+pag+j);write_com (Col_Add+col);for(i=0;i<8;i++){write_data(ezk[x]);x++;}}}/*-----------------指定位置显示x*y的点阵图片-------------------*/ void Disp_Img(uchar pag,uchar col, uchar code *zk){uchar j=0,i=0;uchar k=0;for(j=0;j<8;j++){write_com (Page_Add+pag+j);write_com (Col_Add+col);for(i=0;i<64;i++)write_data(zk[64*j+i]);}}void dadian(uchar pag,uchar col,uchar x){uchar j=0,i=0;for(j=0;j<1;j++){write_com (Page_Add+pag+j);write_com (Col_Add+col);write_data(x);}}/*------------------初始化LCD屏--------------------------*/void init_lcd(){Lcd_Rst=0;delay(100);Lcd_Rst=1;delay(100);Mcs=1;Scs=1;delay(100);write_com(Disp_Off);write_com(Page_Add+0);write_com(Start_Line+0);write_com(Col_Add+0);write_com(Disp_On);}void Init_Com(void){TMOD = 0x20;PCON = 0x00;SCON = 0x50;TH1 = 0xFd;TL1 = 0xFd;TR1 = 1;}void dsreset(void) //send reset and initialization command {uint i;DS=0;i=103;while(i>0)i--;DS=1;i=4;while(i>0)i--;}bit tmpreadbit(void) //read a bit{uint i;bit dat;DS=0;i++; //i++ for delayDS=1;i++;i++;dat=DS;i=8;while(i>0)i--;return (dat);}uchar tmpread(void) //read a byte date{uchar i,j,dat;dat=0;for(i=1;i<=8;i++){j=tmpreadbit();dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里}return(dat);}void tmpwritebyte(uchar dat) //write a byte to ds18b20{uint i;uchar j;bit testb;for(j=1;j<=8;j++){testb=dat&0x01;dat=dat>>1;if(testb) //write 1{DS=0;i++;i++;DS=1;i=8;while(i>0)i--;}else{DS=0; //write 0i=8;while(i>0)i--;DS=1;i++;i++;}}}void readrom() //read the serial{uchar sn1,sn2;dsreset();delay(1);tmpwritebyte(0x33);sn1=tmpread();sn2=tmpread();}void tmpchange(void) //DS18B20 begin change{dsreset();delay(1);tmpwritebyte(0xcc); // address all drivers on bustmpwritebyte(0x44); // initiates a single temperature conversion}uint tmp() //get the temperature{float tt;uchar a,b;dsreset();delay(1);tmpwritebyte(0xcc);tmpwritebyte(0xbe);a=tmpread();b=tmpread();temp=b;temp<<=8; //two byte compose a int variable temp=temp|a;tt=temp*0.0625;temp=tt*10+0.5;return temp;}/******************************************************************** ***************//*-----------------------主程序--------------------------------*/main(){uchar a=0;uchar b=0;DU=0;WE=0;Clr_Scr();init_lcd();Init_Com();dsreset();readrom();while(1){tmp();ser=temp/10;SBUF=ser;tmpchange();delay(200);DIO=0;t1=temp/100;t2=temp%100/10;t3=temp%10;Mcs=1,Scs=0;#include <reg52.h>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <math.h>/*-------------------Define the register commandcode----------------------*/#define Disp_On 0x3f#define Disp_Off 0x3e#define Col_Add 0x40#define Page_Add 0xb8#define Start_Line 0xc0#define Lcd_Bus P0#define uchar unsigned char#define uint unsigned intuchar t1,t2,t3,ser;sbit DS=P2^2; /*----------与单片机的单总线接口--------*/uint temp; /*----------可变温度值------------------*/uchar flag1; /*----------温度正负标志----------------*/sbit Mcs=P3^7;sbit Scs=P2^4;sbit Enable=P3^4;sbit Di=P3^5;sbit RW=P3^6;sbit Lcd_Rst=P2^1;sbit DIO=P2^5;sbit DU=P2^6;sbit WE=P2^7;char code ezk[]={0xF8,0xFC,0x04,0xC4,0x24,0xFC,0xF8,0x00, // -0-0x07,0x0F,0x09,0x08,0x08,0x0F,0x07,0x00,0x00,0x10,0x18,0xFC,0xFC,0x00,0x00,0x00, // -1-0x00,0x08,0x08,0x0F,0x0F,0x08,0x08,0x00,0x08,0x0C,0x84,0xC4,0x64,0x3C,0x18,0x00, // -2-0x0E,0x0F,0x09,0x08,0x08,0x0C,0x0C,0x00,0x08,0x0C,0x44,0x44,0x44,0xFC,0xB8,0x00, // -3-0x04,0x0C,0x08,0x08,0x08,0x0F,0x07,0x00,0xC0,0xE0,0xB0,0x98,0xFC,0xFC,0x80,0x00, // -4-0x00,0x00,0x00,0x08,0x0F,0x0F,0x08,0x00,0x7C,0x7C,0x44,0x44,0xC4,0xC4,0x84,0x00, // -5-0x04,0x0C,0x08,0x08,0x08,0x0F,0x07,0x00,0xF0,0xF8,0x4C,0x44,0x44,0xC0,0x80,0x00, // -6-0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,0x0C,0x0C,0x04,0x84,0xC4,0x7C,0x3C,0x00, // -7-0x00,0x00,0x0F,0x0F,0x00,0x00,0x00,0x00,0xB8,0xFC,0x44,0x44,0x44,0xFC,0xB8,0x00, // -8-0x07,0x0F,0x08,0x08,0x08,0x0F,0x07,0x00,0x38,0x7C,0x44,0x44,0x44,0xFC,0xF8,0x00, // -9-0x00,0x08,0x08,0x08,0x0C,0x07,0x03,0x00,/*------- ----0x30,0x30-----':'已改为'.'-----*/0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x06,0x00,0x00,0x00,};uchar code FirstPage[] ={0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x30,0x10,0x80,0x30,0x10,0x80 ,0xC8,0xB0,0xB0,0x76,0xC8,0xE8,0x76,0x99,0x7C,0x7E,0x9F,0x79,0x7C,0x7E,0x79,0x3E ,0x1C,0x09,0x37,0x13,0x09,0x0E,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x20,0x20,0x00,0x23,0x32,0x98,0x44,0x63,0x33,0x9C,0x23,0x75,0xCC ,0x03,0x25,0x34,0x1B,0x04,0x26,0x33,0x04,0x82,0x03,0x30,0x03,0x80,0x80,0x40,0x08 ,0x98,0x80,0x40,0x40,0x00,0x80,0x40,0xC0,0x00,0x00,0x00,0x80,0x80,0xC0,0xC0,0x00 ,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00 ,0x00,0x20,0x60,0xC0,0x02,0x12,0x98,0xC0,0xCC,0x85,0x20,0xEC,0xF0,0xF9,0xE4,0xFC ,0xFC,0xFD,0xFE,0xFC,0xFC,0xFF,0xFE,0xFF,0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFE,0xFF,0xFF,0xFE,0xFF,0xFF,0xFE,0xFC,0xFE,0xFE,0xF0,0xF8,0xF0,0xF0,0x20,0x00 ,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x0C,0x06,0x06,0x60,0x08,0x4D,0xED,0xF8,0xFE,0xFF,0xFF,0x1F,0x9F,0x9F,0x0F,0x67 ,0x63,0x01,0x00,0x48,0x6D,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xEF,0xE7,0xF3 ,0x81,0xC9,0xED,0xE7,0xF3,0xE2,0xE0,0x31,0x69,0x2D,0xDF,0x8F,0xD7,0x93,0x03,0x0F ,0x0F,0x6E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x03,0x08,0x08,0x60,0xFF,0xFF,0xFF,0x7F,0xFF,0xFF,0x79,0xB6,0xB8,0x78,0x96,0x78 ,0x78,0x33,0x78,0x38,0x18,0x68,0x99,0xBB,0x1F,0x3F,0x1F,0x0F,0x3F,0x1F,0x0F,0x0F0x03,0x01,0x06,0x05,0x01,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x03,0x01,0x00,0x1B,0x01,0x00,0x06,0x00,0x00,0x06,0x00,0x04,0x04,0x01,0x00 ,0x02,0x03,0x00,0x04,0x04,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00};uchar code SecondPage[] = // 数据表64*64{0xFF,0x03,0x01,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFF,0x01,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01 ,0xFF,0xFF,0xFF,0x0F,0x03,0x01,0x39,0x19,0x39,0x39,0x39,0x19,0x19,0x09,0x1B,0xFF ,0xC1,0x81,0x8F,0x9F,0x9F,0x9F,0x9F,0x01,0x01,0x1F,0x9F,0x9F,0x9F,0x9F,0x81,0xC1 ,0xF3,0xFF,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFF,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00 ,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xC0,0x60,0xF8,0xFF0x48,0xCC,0xCC,0xCC,0xCC,0x00,0x00,0x48,0xCC,0xCC,0xCC,0x48,0x01,0x01 ,0xCF,0xFF,0xFF,0xC0,0x80,0x80,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F,0x9F ,0x9F,0x00,0x00,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xF8,0xFC,0xFC ,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x78,0x78,0x78,0x78,0x78,0x78,0x00,0x00,0xC0,0xFF ,0x0F,0x06,0x44,0xE4,0xE4,0xE4,0xE4,0x00,0x00,0xE4,0xE4,0xE4,0xE4,0xE4,0x06,0x0E ,0xFF,0xFF,0xFF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF ,0xCF,0xC0,0xC0,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF ,0xFF,0xFF,0xFF,0xC0,0xC0,0xE0,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,0xFF,0xFF ,0xC6,0xC0,0xE0,0xFF,0xFF,0xFF,0xFF,0xC0,0xC0,0xFF,0xFF,0xFF,0xFF,0xF0,0xC0,0xC4 ,0xFF,0xFF,0xFF,0xFF,0xE0,0x00,0x01,0x1F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F ,0x3F,0x7D,0x78,0x78,0x38,0x38,0x28,0x00,0x00,0x20,0x38,0x38,0x38,0x19,0x01,0x83 ,0xE7,0xFF,0xFF,0x0B,0x09,0x19,0x99,0x99,0x99,0x99,0x99,0x99,0x19,0x09,0x09,0xF9 ,0xF9,0x09,0x09,0xF9,0xFF,0xDB,0x89,0x89,0x89,0x89,0x89,0x89,0x89,0x89,0x19,0x39 ,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0xFE,0xFE,0xFE,0xFE,0xFF,0xFE,0xFE,0xFE ,0xFF,0xF1,0xE0,0xC0,0x8E,0x8E,0x8E,0x8E,0x8E,0x8E,0x8E,0x8E,0x8E,0x8F,0x1F,0x3F ,0x7F,0xFF,0xFF,0xDC,0x08,0x88,0x83,0x83,0x83,0x83,0x83,0x81,0x89,0x88,0x0C,0x8F ,0x8F,0x40,0x40,0xCF,0xFF,0x7B,0x33,0x13,0x93,0x93,0x81,0x81,0x91,0x91,0x18,0x3C ,0xFF,0xFF,0xFF,0xFF,0x3F,0x00,0x00,0xC0,0xC7,0xC7,0xC7,0xC7,0xC7,0xC7,0xC7,0xC7 ,0xC7,0xC7,0xE7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x03,0x00,0xC0 ,0xFC,0xFF,0xFF,0xC0,0x80,0x18,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x00,0x80,0xFF ,0xFF,0x00,0x00,0xFF,0xFF,0xFC,0x80,0x01,0x1F,0x8F,0x07,0x23,0xF3,0xF3,0xF8,0xF8 ,0xFF,0xFF,0xFF,0xFF,0xF0,0xE0,0xC0,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF,0xCF0xCF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0xC0,0xC0 ,0xDF,0xFF,0xFF,0xDF,0xCF,0xC7,0xE7,0xE7,0xE7,0xE3,0xE3,0xE7,0xE7,0xC7,0xC7,0xCF ,0xC7,0xE0,0xF0,0xFF,0xFF,0xEF,0xC0,0xE0,0xFE,0xFF,0xFE,0xFC,0xF8,0xF1,0xE3,0xC7 ,0xFF,0xFF};/*------------------延时子程序-----------------------------*/void delay(uint t){uint i,j;for(i=0;i<t;i++)for(j=0;j<10;j++);}/*------------------写命令到LCD------------------------------*/void write_com(uchar cmdcode){Di=0;RW=0;Lcd_Bus=cmdcode;delay(0);Enable=1;delay(0);Enable=0;}/*-------------------写数据到LCD----------------------------*/void write_data(uchar Dispdata){Di=1;RW=0;Lcd_Bus=Dispdata;delay(0);Enable=1;delay(0);Enable=0;}/*------------------清除内存---------------*/void Clr_Scr(){uchar j,k;Mcs=1;Scs=1;write_com(Page_Add+0);write_com(Col_Add+0);for(k=0;k<8;k++){write_com(Page_Add+k);for(j=0;j<64;j++)write_data(0x00);}}/*---------------------指定位置显示8*16数字-----------------------*/ /* 使用全部ASCII字符x=(order-0x20)*16 *//*------------同时main中数字abcdef应分别加0x30--------------------*/void ez_disp(uchar pag,uchar col,uchar order){uchar i=0;uchar j=0;uint x;x=(order-0x30)*16;for(j=0;j<2;j++){write_com (Page_Add+pag+j);write_com (Col_Add+col);for(i=0;i<8;i++){write_data(ezk[x]);x++;}}}/*-----------------指定位置显示x*y的点阵图片-------------------*/ void Disp_Img(uchar pag,uchar col, uchar code *zk){uchar j=0,i=0;uchar k=0;for(j=0;j<8;j++){write_com (Page_Add+pag+j);write_com (Col_Add+col);for(i=0;i<64;i++)write_data(zk[64*j+i]);}}void dadian(uchar pag,uchar col,uchar x){uchar j=0,i=0;for(j=0;j<1;j++){write_com (Page_Add+pag+j);write_com (Col_Add+col);write_data(x);}}/*------------------初始化LCD屏--------------------------*/ void init_lcd(){Lcd_Rst=0;delay(100);Lcd_Rst=1;delay(100);Mcs=1;Scs=1;delay(100);write_com(Disp_Off);write_com(Page_Add+0);write_com(Start_Line+0);write_com(Col_Add+0);write_com(Disp_On);}void Init_Com(void){TMOD = 0x20;PCON = 0x00;SCON = 0x50;TH1 = 0xFd;TL1 = 0xFd;TR1 = 1;}void dsreset(void) //send reset and initialization command {uint i;DS=0;i=103;while(i>0)i--;DS=1;i=4;while(i>0)i--;}bit tmpreadbit(void) //read a bit{uint i;bit dat;DS=0;i++; //i++ for delayDS=1;i++;i++;dat=DS;i=8;while(i>0)i--;return (dat);}uchar tmpread(void) //read a byte date{uchar i,j,dat;dat=0;for(i=1;i<=8;i++){j=tmpreadbit();dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里}return(dat);}void tmpwritebyte(uchar dat) //write a byte to ds18b20 {uint i;uchar j;bit testb;for(j=1;j<=8;j++){testb=dat&0x01;dat=dat>>1;if(testb) //write 1{DS=0;i++;i++;DS=1;i=8;while(i>0)i--;}else{DS=0; //write 0i=8;while(i>0)i--;DS=1;i++;i++;}}}void readrom() //read the serial{uchar sn1,sn2;dsreset();delay(1);tmpwritebyte(0x33);sn1=tmpread();sn2=tmpread();}void tmpchange(void) //DS18B20 begin change{dsreset();delay(1);tmpwritebyte(0xcc); // address all drivers on bustmpwritebyte(0x44); // initiates a single temperature conversion}uint tmp() //get the temperature{float tt;uchar a,b;dsreset();delay(1);tmpwritebyte(0xcc);tmpwritebyte(0xbe);a=tmpread();b=tmpread();temp=b;temp<<=8; //two byte compose a int variable temp=temp|a;tt=temp*0.0625;temp=tt*10+0.5;return temp;}/******************************************************************** ***************//*-----------------------主程序--------------------------------*/main(){uchar a=0;uchar b=0;DU=0;WE=0;Clr_Scr();init_lcd();Init_Com();dsreset();readrom();while(1){tmp();ser=temp/10;SBUF=ser;tmpchange();delay(200);DIO=0;t1=temp/100;t2=temp%100/10;t3=temp%10;Mcs=1,Scs=0;Disp_Img(0,a,SecondPage);a++;if(a==64)a=0;delay(50);Mcs=0;Scs=1;Disp_Img(0,b,FirstPage); // b++;// if(b==64)// b=0;// delay(50);ez_disp(6,12,t1);ez_disp(6,20,t2);ez_disp(6,28,':');ez_disp(6,36,t3);dadian(6,44,0x04);dadian(6,45,0x0a);dadian(6,46,0x04);}}。

多个18B20读取和12864显示 2

多个18B20读取和12864显示 2

自己用51单片机写的读取多个18B20温度并显示在12864上的实验程序,测试通过#include<reg51.h>#define u8 unsigned char#define u16 unsigned intsbit dq=P3^3;u16 sbuf;float t;bit n;void send_string(u8 *p);float t1,t2;//存放2个18b20 的温度//18b20u8 code id1[]={0x28,0x05,0x9d,0x06,0x03,0x00,0x00,0x43};//第一18b20的序列号u8 code id2[]={11111111};//第二18b20的序列号void delay_18b20(u8 x){for(;x>0;x--);}void delay(u16 x){while(x--);}void send_0(){dq=0;delay_18b20(12);dq=1;delay_18b20(0);}void send_1(){dq=0;delay_18b20(0);dq=1;delay_18b20(8);}void send_cmd(u8 cmd){u8 i;for(i=0;i<8;i++){if(cmd&0x01==0x01){send_1();}else{send_0();}cmd>>=1;}}u8 receive_8bit(){u8 d;u8 i;for(i=0;i<8;i++){dq=0;dq=0;dq=0;dq=1;d>>=1;if(dq==1){d|=0x80;}delay_18b20(10);}return d;}u8 start(){u16 k;k=0;dq=0;delay_18b20(120);dq=1;while(dq==1){k++;if(k>=6000){return 1;}}k=0;while(dq==0){k++;if(k>=6000){return 2;}}return 0;}void send_id(u8 *id) //向DS18B20发送序列号{u8 i;for(i=0;i<8;i++){send_cmd(*id++);}}//读温度float read_ds18b20(u8 *id){u8 k;u8 tH,tL;u16 d;k=start();if(k!=0){send_string("错误");return 0;}send_cmd(0x55);//("匹配序列号");send_id(id);send_cmd(0x44);delay(5000);start();send_cmd(0x55);send_id(id);send_cmd(0xbe);tL=receive_8bit();tH=receive_8bit();d=tH;d<<=8;d|=tL;t=d*0.0625;return t;}//18b20void serial_int(){TMOD|=0x20;TH1=250;TL1=250;TR1=1;SM0=0;SM1=1;SM2=0;REN=1;}void end(){EA=1;ES=1;}void send_end() interrupt 4{if(RI==1){RI=0;sbuf=SBUF;if((sbuf=='1')|(sbuf=='2')){n=1;}}}void send_byte(u8 x) //底层驱动{SBUF=x;//启动发送while(TI==0);//等待数据发送完成TI=0;}void send_string(u8 *p){while(*p)send_byte(*p++);}void send_temp(float t){u16 tt;tt=(u16)(t*10);send_byte(tt/1000+48);send_byte(tt%1000/100+48);send_byte(tt%100/10+48);send_byte(tt%10+48);}//18b20void main(){serial_int();send_byte(12);send_string("串口初始化完成...\r\n");send_string("单总线访问两只DS18B20...\r\n");send_string("请按键盘 1 或2 分别读取两只DS18B20的温度...\r\n"); end();while(1){t1=read_ds18b20(id1);delay(5000);t2=read_ds18b20(id2);delay(5000);if(n==1){n=0;if(sbuf=='1'){send_string("\r\nt1=");send_temp(t1);send_string("度");}if(sbuf=='2'){send_string("\r\nt2=");send_temp(t2);send_string("度");}}}}。

单片机12864LCD、DS1302、DS18B20

单片机12864LCD、DS1302、DS18B20

用汇编写了那么多程序,马上就要用C了,所以做一个我近期在单片机上用汇编写代码的心得和一些自认为有用的程序。

主要是近期自学过的模块,比如protues中的AMPIRE128X64—LCD,DS18B20—温度测量模块,DS1302—时钟芯片。

好啦,废话也不多说了,注意我是按照我写程序的规范来写的,不过很容易理解,我也希望能帮助更多的单片机爱好者。

AMPIRE128X64一、Protues上LCD与单片机相连如图:二、打开keil新建一个AT89C51单片机的工程。

建立如下工程12864.A51用于存放驱动程序,方便调用。

main.a51用于存放主程序。

如果想知道这款LCD的详细信息可去百度文库搜索。

三、以下是12864.A51内的驱动程序,有详细解说。

PUBLICDISPLAY_BMP,DISPLAY_LCD,CLEAR_SCREEN,CHEC_BUSY,WRITE_IR,WRITE_DR,INIT_LCD,SELECT_SCREEN,SET_LINE;以上是声明外部调用语句,后面的子程序可被本工程的其它文件调用CODE_12864 SEGMENT CODE ;声明代码段LCD_RS BIT P2.2LCD_RW BIT P2.1LCD_E BIT P2.0LCD_CS1 BIT P2.4 ;低电平有效,左半屏LCD_CS2 BIT P2.3 ;低电平有效,右半屏ZIMO_NUM EQU 37H ;取字模的大小数;;;;;;;;;;;;;;;;;;;;;;;;;RSEG CODE_12864 ;;;;;;;;;;;;;;;;;;;;;;;;;检查忙等待CHEC_BUSY: MOV P0,#00H;;;;;;;;;;;;;;;;CLR LCD_RSSETB LCD_RWSETB LCD_E;详细资料里有这种命令;;;;;;;;;;;;;;;后面的就不细说了JB P0.7,$CLR LCD_ERET;;;;;;;;;;;;;;;;;;;;;;;;;向指令寄存器传送指令,传送参数为AWRITE_IR: LCALL CHEC_BUSYCLR LCD_RSCLR LCD_RWMOV P0,A ;P0作为数据口SETB LCD_ENOPNOPCLR LCD_ERET;;;;;;;;;;;;;;;;;;;;;;;;;向数据寄存器传送指令,传送参数为AWRITE_DR: LCALL CHEC_BUSYSETB LCD_RSCLR LCD_RWMOV P0,ASETB LCD_ENOPNOPCLR LCD_ERET;;;;;;;;;;;;;;;;;;;;;;;;;设置"页"LCD12864共8页,一页是8行点阵点SET_PAGE: ORL A,#0B8H ;页的首地址为0xB8LCALL WRITE_IRRET;;;;;;;;;;;;;;;;;;;;;;;;;设置显示的起始行SET_LINE: ORL A,#0C0H ;起始行地址为0xC0LCALL WRITE_IR;设置从哪行开始:共0--63;一般从0 行开始显示RET;;;;;;;;;;;;;;;;;;;;;;;;;设置显示的列SET_COLUMN: ANL A,#3FH ;列的最大值为64ORL A,#40H ;列的首地址为0x40LCALL WRITE_IR;规定显示的列的位置RET;;;;;;;;;;;;;;;;;;;;;;;;;显示开关函数:0x3E是关显示,0x3F是开显示SET_ON_OFF: ORL A,#3EH ;0011 111x,onoff只能为0或者1LCALL WRITE_IRRET;;;;;;;;;;;;;;;;;;;;;;;;;选择屏幕SELECT_SCREEN: CJNE A,#0,SELECTN0;0全屏CLR LCD_CS1CLR LCD_CS2LJMP SELECT_ENDSELECTN0: CJNE A,#1,SELECTN1;1左半屏CLR LCD_CS1SETB LCD_CS2LJMP SELECT_ENDSELECTN1: CJNE A,#2,SELECT_END;2右半屏CLR LCD_CS2SETB LCD_CS1SELECT_END: RET;;;;;;;;;;;;;;;;;;;;;;;;;清屏函数CLEAR_SCREEN: LCALL SELECT_SCREEN;0--全屏;1---左半屏;2---右半屏MOV R7,#0 ;控制页数0-7,共8页CLR_K1: MOV A,R7LCALL SET_PAGEMOV A,#0LCALL SET_COLUMNMOV R6,#0 ;控制列数0-63,共64列CLR A ;0CLR_K0: LCALL WRITE_DR;写入0,地址指针自加1INC R6CJNE R6,#64,CLR_K0INC R7CJNE R7,#8,CLR_K1RET;;;;;;;;;;;;;;;;;;;;;;;;;LCD的初始化INIT_LCD: LCALL CHEC_BUSYCLR A;选择全屏LCALL SELECT_SCREENCLR A;关闭显示LCALL SET_ON_OFFCLR A;选择全屏LCALL SELECT_SCREENMOV A,#01H;开启显示LCALL SET_ON_OFFCLR A;选择全屏LCALL SELECT_SCREENCLR A;清屏LCALL CLEAR_SCREENCLR A;开始行0LCALL SET_LINERET ;;;;;;;;;;;;;;;;;;;;;;;;;显示汉字子程序DISPLAY_LCD:;R5传入那半屏显示,R6传入页,R7传入列,DPTR传入字模表MOV A,R5LCALL SELECT_SCREEN;选择屏幕MOV A,R6LCALL SET_PAGE;选上半页MOV A,R7LCALL SET_COLUMN;控制列MOV R5,#0 ;控制16列的数据输出DISP_K0: MOV A,R5MOVC A,@A+DPTRLCALL WRITE_DR;选择查出的字模INC R5CLR CMOV A,R5SUBB A,ZIMO_NUMJNZ DISP_K0MOV A,R6INC ALCALL SET_PAGE;选下半页MOV A,R7LCALL SET_COLUMN;控制列MOV R5,#0 ;控制16列的数据输出DISP_K1: MOV A,R5ADD A,ZIMO_NUMMOVC A,@A+DPTRLCALL WRITE_DR;选择查出的字模INC R5CLR CMOV A,R5SUBB A,ZIMO_NUMJNZ DISP_K1RET ;;;;;;;;;;;;;;;;;;;;;;;;;显示BMP图片DISPLAY_BMP:;DPTR为图片取模表首址MOV R7,#0DISP_BMP_K2: MOV A,#1LCALL SELECT_SCREEN;选择左半屏MOV A,R7LCALL SET_PAGE;选择页CLR ALCALL SET_COLUMN;选择第0列MOV R6,#0DISP_BMP_K0: CLR A;MOVC A,@A+DPTRLCALL WRITE_DR;每隔一行取一次数组中的数据INC R6INC DPTRCJNE R6,#64,DISP_BMP_K0MOV A,#2LCALL SELECT_SCREEN;选择右半屏MOV A,R7LCALL SET_PAGE;选择页CLR ALCALL SET_COLUMN;选择第0列MOV R6,#0DISP_BMP_K1: CLR A;MOVC A,@A+DPTRLCALL WRITE_DR;每隔一行取一次数组中的数据INC R6INC DPTRCJNE R6,#64,DISP_BMP_K1INC R7CJNE R7,#8,DISP_BMP_K2RET ;;;;;;;;;;;;;;;;;;;;;;;;;END四、下面介绍一款LCD取模软件。

基于51单片机的12864显示温度(ds18b20)

基于51单片机的12864显示温度(ds18b20)

#ifndef __LCD12864_H#define __LCD12864_H//---包含头文件---//#include<reg51.h>//---重定义关键词---//#ifndef uchar#define uchar unsigned char#endif#ifndef uint#define uint unsigned int#endif//---如果使用画图模式定义这个---//#define LCD12864_PICTURE//---定义使用的IO口---//#define LCD12864_DATAPORT P1 //数据IO口sbit LCD12864_RS = P2^6; //(数据命令)寄存器选择输入sbit LCD12864_RW = P2^5; //液晶读/写控制sbit LCD12864_EN = P2^7; //液晶使能控制sbit LCD12864_PSB = P3^2; //串/并方式控制sbit LCD12864_RST = P3^4; //复位端//---声明全局函数---//void LCD12864_Delay1ms(uint c);uchar LCD12864_Busy(void);void LCD12864_WriteCmd(uchar cmd);void LCD12864_WriteData(uchar dat);void LCD12864_Init();void LCD12864_ClearScreen(void);void LCD12864_SetWindow(uchar x, uchar y);void LCD12864_DrowPic(uchar *a);void LCD12864_DrowPoint(uchar x, uchar y);#endif#ifndef __TEMP_H_#define __TEMP_H_#include<reg51.h>//---重定义关键词---//#ifndef uchar#define uchar unsigned char#endif#ifndef uint#define uint unsigned int#endif//--定义使用的IO口--//sbit DSPORT=P3^3;//--声明全局函数--//void Delay1ms(uint );uchar Ds18b20Init();void Ds18b20WriteByte(uchar com);uchar Ds18b20ReadByte();void Ds18b20ChangTemp();void Ds18b20ReadTempCom();int Ds18b20ReadTemp();#endif#include"lcd12864.h"/****************************************************************************** ** 函数名: LCD12864_Delay1ms* 函数功能: 延时1MS* 输入: c* 输出: 无******************************************************************************* /void LCD12864_Delay1ms(uint c)uchar a,b;for(; c>0; c--){for(b=199; b>0; b--){for(a=1; a>0; a--);}}}/****************************************************************************** ** 函数名: LCD12864_Busy* 函数功能: 检测LCD是否忙* 输入: 无* 输出: 1或0(1表示不忙,0表示忙)******************************************************************************* /uchar LCD12864_Busy(void){uchar i = 0;LCD12864_RS = 0; //选择命令LCD12864_RW = 1; //选择读取LCD12864_EN = 1;LCD12864_Delay1ms(1);while((LCD12864_DA TAPORT & 0x80) == 0x80) //检测读取到的值{i++;if(i > 100){LCD12864_EN = 0;return 0; //超过等待时间返回0表示失败}}LCD12864_EN = 0;return 1;}/****************************************************************************** ** 函数名: LCD12864_WriteCmd* 函数功能: 写命令* 输入: cmd* 输出: 无******************************************************************************* /void LCD12864_WriteCmd(uchar cmd){uchar i;i = 0;while( LCD12864_Busy() == 0){LCD12864_Delay1ms(1);i++;if( i>100){return; //超过等待退出}}LCD12864_RS = 0; //选择命令LCD12864_RW = 0; //选择写入LCD12864_EN = 0; //初始化使能端LCD12864_DATAPORT = cmd; //放置数据LCD12864_EN = 1; //写时序LCD12864_Delay1ms(1);LCD12864_EN = 0;}/****************************************************************************** ** 函数名: LCD12864_WriteData* 函数功能: 写数据* 输入: dat* 输出: 无******************************************************************************* /void LCD12864_WriteData(uchar dat){uchar i;i = 0;while( LCD12864_Busy() == 0){LCD12864_Delay1ms(1);i++;if( i>100){return; //超过等待退出}}LCD12864_RS = 1; //选择数据LCD12864_RW = 0; //选择写入LCD12864_EN = 0; //初始化使能端LCD12864_DATAPORT = dat; //放置数据LCD12864_EN = 1; //写时序LCD12864_Delay1ms(1);LCD12864_EN = 0;}/****************************************************************************** ** 函数名: LCD12864_ReadData* 函数功能: 读取数据* 输入: 无* 输出: 读取到的8位数据******************************************************************************* /#ifdef LCD12864_PICTUREuchar LCD12864_ReadData(void){uchar i, readValue;i = 0;while( LCD12864_Busy() == 0){LCD12864_Delay1ms(1);i++;if( i>100){return 0; //超过等待退出}}LCD12864_RS = 1; //选择命令LCD12864_RW = 1;LCD12864_EN = 0;LCD12864_Delay1ms(1); //等待LCD12864_EN = 1;LCD12864_Delay1ms(1);readValue = LCD12864_DA TAPORT;LCD12864_EN = 0;return readValue;}#endif/****************************************************************************** ** 函数名: LCD12864_Init* 函数功能: 初始化LCD12864* 输入: 无* 输出: 无******************************************************************************* /void LCD12864_Init(){LCD12864_PSB = 1; //选择并行输入LCD12864_RST = 1; //复位LCD12864_WriteCmd(0x30); //选择基本指令操作LCD12864_WriteCmd(0x0c); //显示开,关光标LCD12864_WriteCmd(0x01); //清除LCD12864的显示内容}/****************************************************************************** ** 函数名: LCD12864_ClearScreen* 函数功能: 在画图模式下,LCD12864的01H命令不能清屏,所以要自己写一* * 屏函数* 输入: 无* 输出: 无******************************************************************************* /#ifdef LCD12864_PICTUREvoid LCD12864_ClearScreen(void){uchar i,j;LCD12864_WriteCmd(0x34); //开启拓展指令集for(i=0;i<32;i++) //因为LCD有纵坐标32格所以写三十二次{LCD12864_WriteCmd(0x80+i); //先写入纵坐标Y的值LCD12864_WriteCmd(0x80); //再写入横坐标X的值for(j=0;j<32;j++) //横坐标有16位,每位写入两个字节的的数据,也{ //就写入32次以为当写入两个字节之后横坐标会自LCD12864_WriteData(0x00); //动加1,所以就不用再次写入地址了。

DS18B20温测 12864显示

DS18B20温测  12864显示

#include<reg52.h>#include<intrins.h> //包含_nop_()函数定义的头文件#define uint unsigned int#define uchar unsigned charsbit ds=P3^3; //DS18B20接口uint temp;float f_temp;//12864端口定义sbit LCD12864_RS=P2^0;sbit LCD12864_WR=P2^1;sbit LCD12864_E=P2^2;sbit LCD12864_PSB=P2^3;sbit LCD12864_busy=P0^7;#define LCD12864_dat P0void delay(uint x){uint i,j;for(i=x;i>0;i--)for(j=110;j>0;j--) ;}//****************************************//以下为12864液晶显示部分子程序模块//****************************************//函数定义void wr_d_lcd(uchar content);void wr_i_lcd(uchar content);void clrram_lcd (void);void init_lcd(void);void busy_lcd(void);void rev_row_lcd(uchar row);void rev_co_lcd(uchar row,uchar col,uchar mode);void clr_lcd(void);void wr_co_lcd(uchar row,uchar col,uchar lcddata1,uchar lcddtta2);void wr_row_lcd(uchar row,char *p);//延时子程序模块//**********************************************void mdelay(uint delay){ uint i;for(;delay>0;delay--){for(i=0;i<80;i++) //1ms延时.{;}}}//***********************************//填充液晶DDRAM全为空格//**********************************void clrram_lcd (void){wr_i_lcd(0x30);wr_i_lcd(0x01);}//***********************************//对液晶写数据//content为要写入的数据//***********************************//*********************写数据函数void wr_d_lcd(uchar content){busy_lcd();LCD12864_RS=1;LCD12864_WR=0;LCD12864_dat=content;LCD12864_E=1;;LCD12864_E=0;}//********************************//对液晶写指令//content为要写入的指令代码//*****************************//**************************写命令函数void wr_i_lcd(uchar content){busy_lcd();LCD12864_RS=0;LCD12864_WR=0;LCD12864_dat=content;LCD12864_E=1;;LCD12864_E=0;}//********************************//液晶检测忙状态//在写入之前必须执行//********************************void busy_lcd(void){LCD12864_dat=0xff;LCD12864_RS=0;LCD12864_WR=1;LCD12864_E =1;while(LCD12864_busy==1);LCD12864_E=0;}//**********************************//液晶初始化//**********************************void init_lcd(void){wr_i_lcd(0x30);wr_i_lcd(0x06); /*光标的移动方向*/wr_i_lcd(0x0c); /*开显示,关游标*/wr_i_lcd(0x01); //清屏}//********************************//指定要显示字符的坐标//*******************************void LCD12864_pos(unsigned char y, unsigned char x){if(y==1)wr_i_lcd(0x80|x);if(y==2)wr_i_lcd(0x90|x);if(y==3)wr_i_lcd((0x80|x)+8);if(y==4)wr_i_lcd((0x90|x)+8);}//**********************************//液晶显示字符串程序//**********************************void LCD12864print(uchar *str){while(*str!='\0'){wr_d_lcd(*str);str++;}}//**********************************//液晶显示位置、字符串、显示间隔时间//**********************************//LCD12864_display1(1,3,500,"中国CHN")//意思为在第1行第4列显示"中国CHN",每个字显示间隔为500ms LCD12864_display1(unsigned char y, unsigned char x,uint t, uchar *str) {LCD12864_pos(y,x);while(*str!='\0'){wr_d_lcd(*str);str++;delay(t);}}LCD12864_display2(unsigned char y, unsigned char x,uchar n){LCD12864_pos(y,x);wr_d_lcd(0x30+n);}//此函数是显示变量的,比如要在第1行第2列显示一个不断变化的变量a代表的值,//就用LCD12864_display2(1,2,a);//****************************************//以上为12864液晶显示部分子程序模块//****************************************/*****************************************************以下为DS18B20模块语句***************************************************//*void delay(uint z)//延时函数{uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}*/void dsreset(void) //18B20复位,初始化函数{uint i;ds=0;i=103;while(i>0)i--;ds=1;i=4;while(i>0)i--;}bit tempreadbit(void) //读1位函数{uint i;bit dat;ds=0;i++; //i++ 起延时作用ds=1;i++;i++;dat=ds;i=8;while(i>0)i--;return (dat);}uchar tempread(void) //读1个字节{uchar i,j,dat;dat=0;for(i=1;i<=8;i++){j=tempreadbit();dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DA T里}return(dat);}void tempwritebyte(uchar dat) //向18B20写一个字节数据{uint i;uchar j;bit testb;for(j=1;j<=8;j++){testb=dat&0x01;dat=dat>>1;if(testb) //写1{ds=0;i++;i++;ds=1;i=8;while(i>0)i--;}else{ds=0; //写0i=8;while(i>0)i--;ds=1;i++;i++;}}}void tempchange(void) //DS18B20 开始获取温度并转换{dsreset();delay(1);tempwritebyte(0xcc); // 写跳过读ROM指令tempwritebyte(0x44); // 写温度转换指令}uint get_temp() //读取寄存器中存储的温度数据{uchar a,b;dsreset();delay(1);tempwritebyte(0xcc);tempwritebyte(0xbe);a=tempread(); //读低8位b=tempread(); //读高8位temp=b;temp<<=8; //两个字节组合为1个字temp=temp|a;f_temp=temp*0.0625; //温度在寄存器中为12位分辨率位0.0625°temp=f_temp*10+0.5; //乘以10表示小数点后面只取1位,加0.5是四舍五入f_temp=f_temp+0.05;return temp; //temp是整型}/*****************************************************以上为DS18B20模块语句***************************************************/void dis_temp(uint t){uchar shi,ge,xiao;shi=t/100;//十位ge=t%100/10;//个位xiao=t%100%10;//小数位LCD12864_display1(1,0,0,"当前温度为:");LCD12864_display1(3,0,0,"摄氏度");LCD12864_display2(3,3,shi);LCD12864_display2(3,4,ge);LCD12864_display1(3,5,0,".");LCD12864_display2(3,6,xiao);LCD12864_display1(3,7,0,"C");}main(){init_lcd();while(1){tempchange();dis_temp(get_temp());}}。

BS18B20温度值12864波形显示

BS18B20温度值12864波形显示

/******************************************************************************* ******************************************************************************** *******///程序说明:本程序为12864(st7920)驱动程序,只实现了最简单的显示功能//端口设置:RS、RW、EN分别为P0口的0、1、2,数据口为P2口用取模软件取的图形或汉字必须是逐行取的,因为本函数是在液晶上逐行打点的/******************************************************************************* ******************************************************************************** ******/#include<reg51.h>#include<intrins.h> //内含-NOP-函数#include<stdlib.h> //内含rand()函数#define uchar unsigned char#define uint unsigned int//**********宏定义所需指令#define BASIC_SET 0x30#define EXTEND_SET 0x34#define DRAW_ON 0x36#define DRAW_OFF 0x34//*************端口定义sbit LCD_RS = P2^5;sbit LCD_RW = P2^6;sbit LCD_EN = P2^7;sbit DS=P3^3; //定义温度DS18B20接口,详情见原理图//************变量定义uchar code t0[]="温度 . ℃";uchar code t1[]="0123456789"; //利用一个温度表解决温度显示乱码uchar i,k,a,b;//****************短延时void delay(uint k){uint i;uchar j;for(i = 0; i < k ;i ++)for(j = 0; j < 10 ;j ++);}//***********12864写指令函数void write_com(uchar cmd){LCD_RS = 0;LCD_RW = 0;P0 = cmd;delay(5);LCD_EN = 1;delay(5);LCD_EN = 0;}//********12864写数据函数void write_dat(uchar dat){LCD_RS = 1;LCD_RW = 0;P0 = dat;delay(5);LCD_EN = 1;delay(5);LCD_EN = 0;}//****************从LCD中读数据uchar read_dat(void){uchar temp;P0 = 0XFF; //释放数据线LCD_RS = 1; //数据LCD_RW = 1; // 读模式LCD_EN = 1; //E为高电平进行读数据或指令delay(1);temp = P0;LCD_EN = 0;return temp;}//******************************************************** //设置光标(地址)函数//参数说明:x---为行号,y为列号//********************************************************void set_cursor(unsigned char x, unsigned char y){unsigned char i;switch(x) //确定行号{case 0x00: i=0x80; break; //第一行case 0x01: i=0x90; break; //第二行case 0x02: i=0x88; break; //第三行case 0x03: i=0x98; break; //第四行default : break;}i = y+i; //确定列号write_com(i);}//********************************************************//显示字符函数//********************************************************void display_char(unsigned char Alphabet){write_dat(Alphabet); //写入需要显示字符的显示码}//********************************************************//指定位置显示字符串函数//参数说明:x为行号,y为列号//********************************************************void display_string(unsigned char x,unsigned char y,unsigned char *Alphabet) {unsigned char i=0;set_cursor(x,y); //设置显示的起始地址while(Alphabet[i]!='\0'){write_dat(Alphabet[i]); //写入需要显示字符的显示码i++;}}//***************************************************************************以下为GDRAM绘图部分************************************************************************////*********************绘图显示的清屏函数(因清屏指令在画图时不能用)------------------------------------------------------------------------------注意!!!!!!!void gui_clear(){uchar i , j , k;write_com(EXTEND_SET);//扩展指令集,8位数据传输write_com(DRAW_OFF);//绘图显示关闭for(i = 0; i < 2; i ++)//分上下两屏写{for(j = 0; j < 32; j ++){write_com(0x80 + j);//写y坐标delay(1);if(i == 0) //写x坐标{write_com(0x80);delay(1);}else //写下半屏{write_com(0x88);delay(1);}for(k = 0; k < 16; k ++)//写一整行数据{write_dat(0x00);//写高字节write_dat(0x00);//写低字节delay(1);}}}write_com(DRAW_ON);//打开绘图显示write_com(BASIC_SET);//打开基本指令集}//***********(给定坐标并打点的)任意位置打点函数void lcd_set_dot(uchar x,uchar y){uchar x_byte,x_bit;//确定在坐标的那一字节哪一位uchar y_ping , y_bit;//确定在坐标的哪一屏哪一行uchar tmph , tmpl;//定义两个临时变量,用于存放读出来的数据write_com(EXTEND_SET);//扩展指令集write_com(DRAW_OFF);//绘图显示关闭x_byte = x / 16;//算出在哪一字节,注意一个地址是16位的x_bit = x % 16;//& 0x0f;//算出在哪一位y_ping = y / 32;//确定在上半屏还是下半屏,0代表上半屏,1代表下半屏y_bit = y % 32;//& 0x1f;//确定在第几行write_com(0X80 + y_bit);//先写垂直地址(最高位必须)write_com(0x80 + x_byte + 8 * y_ping);//水平坐标,下半屏坐标起始地址为0x88,(+8*y_ping)就是用来确定上半屏还是下半屏read_dat();//预读取数据tmph = read_dat();//读取当前显示高8位数据tmpl = read_dat();//读取当前显示低8位数据delay(1);write_com(0x80 + y_bit);//读操作会改变AC,所以重新设置一下write_com(0x80 + x_byte + 8 * y_ping);delay(1);if(x_bit < 8){write_dat(tmph | (0x01 << (7 - x_bit)));//写高字节,因为坐标是从左向右的,GDRAM高位在昨,低位在右write_dat(tmpl);//原低位数据送回}else{write_dat(tmph);//原高位数据送回write_dat(tmpl | (0x01 << (15 - x_bit)));}write_com(DRAW_ON); //打开绘图显示write_com(BASIC_SET);//回到基本指令集}//************画水平线函数**********************************////x0、x1为起始点和终点的水平坐标,y为垂直坐标***************////**********************************************************// void gui_hline(uchar x0, uchar x1, uchar y){uchar bak;//用于对两个数互换的中间变量,使x1为大值if(x0 > x1){bak = x1;x1 = x0;x0 = bak;}do{lcd_set_dot(x0 , y);//从左到右逐点显示x0 ++;}while(x1 >= x0);}//***********画竖直线函数***********************************// //x为起始点和终点的水平坐标,y0、y1为垂直坐标***************// //**********************************************************// void gui_rline(uchar x, uchar y0, uchar y1){uchar bak;//用于对两个数互换的中间变量,使y1为大值if(y0 > y1){bak = y1;y1 = y0;y0 = bak;}do{lcd_set_dot(x , y0);//从上到下逐点显示y0 ++;}while(y1 >= y0);}//*********任意两点间画直线*********************************// //x0、y0为起始点坐标,x1、y1为终点坐标**********************// //**********************************************************//void gui_line(uchar x0 , uchar y0 , uchar x1 , uchar y1){char dx;//直线x轴差值char dy;//直线y轴差值char dx_sym;//x轴增长方向,为-1时减值方向,为1时增值方向char dy_sym;//y轴增长方向,为-1时减值方向,为1时增值方向char dx_x2;//dx*2值变量,用于加快运算速度char dy_x2;//dy*2值变量,用于加快运算速度char di; //决策变量if(x0 == x1)//判断是否为垂直线{gui_rline(x0 , y0 , y1);//画垂直线return;}if(y0 == y1)//判断是否为水平线{gui_hline(x0 , x1 , y0);//画水平线return;}dx = x1 - x0;//求取两点之间的差值dy = y1 - y0;//****判断增长方向,或是否为水平线、垂直线、点*//if(dx > 0)//判断x轴方向dx_sym = 1;else{if(dx < 0)dx_sym = -1;else{gui_rline(x0 , y0 , y1);return;}}if(dy > 0)//判断y轴方向dy_sym = 1;else{if(dy < 0)dy_sym = -1;else{gui_hline(x0 , x1 , y0);return;}}/*将dx、dy取绝对值***********/dx = dx_sym * dx;dy = dy_sym * dy;/****计算2倍的dx、dy值*******/dx_x2 = dx * 1;//我改为了一倍,这样才跟真实的两点对应dy_x2 = dy * 1;/***使用bresenham法进行画直线***/if(dx >= dy)//对于dx>=dy,使用x轴为基准{di = dy_x2 - dx;while(x0 != x1){lcd_set_dot(x0,y0);x0 +=dx_sym;if(di < 0)di += dy_x2;//计算出下一步的决策值else{di += dy_x2 - dx_x2;y0 += dy_sym;}}lcd_set_dot(x0, y0);//显示最后一点}else //对于dx<dy使用y轴为基准{di = dx_x2 - dy;while(y0 != y1){lcd_set_dot(x0, y0);y0 += dy_sym;if(di < 0)di += dx_x2;else{di += dx_x2 - dy_x2;x0 += dx_sym;}}lcd_set_dot(x0, y0);//显示最后一点}}//***************************************************************************以上为自定义字库部分**************************************************************////****************12864初始化函数void lcd_init(){write_com(0x30);//基本指令操作,8位并口delay(1);write_com(0x06);//设置为游标右移,DDRAM地址加一,画面不动delay(1);write_com(0x0c);//显示开,关光标delay(1);write_com(0x01);//清除lcd显示内容delay(1);}//5ms延时void Delay5Ms(void){unsigned int TempCyc = 5552;while(TempCyc--);}//400ms延时void Delay400Ms(void){unsigned char TempCycA = 5;unsigned int TempCycB;while(TempCycA--){TempCycB=7269;while(TempCycB--);};}/*************DS18B20温度读取模块*************/void tmpDelay(int num)//延时函数{while(num--) ;}void Init_DS18B20()//初始化ds1820{unsigned char x=0;DS = 1; //DS复位tmpDelay(8); //稍做延时DS = 0; //单片机将DS拉低tmpDelay(80); //精确延时大于 480usDS = 1; //拉高总线tmpDelay(14);x=DS; //稍做延时后如果x=0则初始化成功 x=1则初始化失败tmpDelay(20);}unsigned char ReadOneChar()//读一个字节{unsigned char i=0;unsigned char dat = 0;for (i=8;i>0;i--){DS = 0; // 给脉冲信号dat>>=1;DS = 1; // 给脉冲信号if(DS)dat|=0x80;tmpDelay(4);}return(dat);}void WriteOneChar(unsigned char dat)//写一个字节{unsigned char i=0;for (i=8; i>0; i--){DS = 0;DS = dat&0x01;tmpDelay(5);DS = 1;dat>>=1;}}unsigned int Readtemp()//读取温度{unsigned char a=0;unsigned char b=0;unsigned int t=0;float tt=0;Init_DS18B20();WriteOneChar(0xCC); // 跳过读序号列号的操作WriteOneChar(0x44); // 启动温度转换Init_DS18B20();WriteOneChar(0xCC); //跳过读序号列号的操作WriteOneChar(0xBE); //读取温度寄存器a=ReadOneChar(); //连续读两个字节数据 //读低8位b=ReadOneChar(); //读高8位t=b;t<<=8;t=t|a; //两字节合成一个整型变量。

使用DS18B20多点测温(64点)

使用DS18B20多点测温(64点)

DS18B20数据总线驱动的研究DS18B20温度传感器驱动形式有很多种,本文研究应用实现使用单片机的一个I/O口作为数据总线驱动64个DS18B20温度传感器。

硬件部分DS18B20温度传感器采用电线总线进行数据传输,总线有读时隙,写时隙和还原时隙,每片DS182B20温度传感器有独有的64为(8字节)片内固化ROM用来确定通讯目标。

理论上说在同一根总线上只要DS18B20温度传感器片内ROM的内容不重复,数据传输就不会出现错误。

但是实际上,驱动DS18B20的芯片(如单片机)的负载能力有限,所以目前常见的使用DS18B20多点测温的电路中一根数据总线上最多存在8个DS18B20芯片。

换句话说,要实现一根总线上驱动更多的DS18B20芯片,最主要的问题就是I/O口的负载能力。

下面对DS18B20工作的电路进行分析,以便找到驱动能力不足的原因,并加以增强。

首先就是单片机的负载能力到底是多少,图1是从AT89C52RC的数据手册中截取的内容。

图1由此可见,单片机(AT89C52RC)每个引脚的限制电流3.2mA。

一般情况下单线总线电路的结构及芯片输入部分结构如图2:图2总线上的电压、电流情况可分为以下四种情况讨论:单片机写1、单片机写0、单片机读1、单片机读0。

1.单片机写1:由图2每片芯片上电时有5uA的固定电流消耗,64片并联一共是320uA。

由图1可知,当单片机AT89C52RC P0口输出高电平时输出电压最低0.75Vcc时,最大输出电流300uA。

所以如果不加上拉电阻挂载64片DS18B20时,当单片机(AT89C52RC)输出高电平时总线上的电压将小于0.75Vcc。

所以至少多出来的20uA要由上拉电阻提供才能保证电路输出高电平时电压达到0.75Vcc。

此时求得上拉电阻阻值应小于250KΩ。

当单片机AT89C52RC P0口输出高电平时输出电压最低0.9Vcc时,最大输出电流80uA。

基于单片机的温度实时测控显示系统 DS18b20 液晶12864

基于单片机的温度实时测控显示系统 DS18b20 液晶12864

温度遥测遥控(D题)摘要:设计为一个无线温度遥测遥控系统, 包括传感器模块、微处理器模块、无线数据收发模块以及PC机管理模块。

利用“单总线”数字温度传感器DS18B20和压力式液位传感器对温度、液位信息进行采集;采用MCS-51系列单片机处理数据,控制加热、报警、数据传输以及实时数据显示;上位机与单片机之间采用HAC-uM系列微功率无线数传模。

系统能够完成题目要求的所有基本功能与发挥部分功能,除此之外,为使系统更加完善设计还增加了测量点实时显示液位、声音报警的创新功能,在0-800C条件下,实时显示的温度和液位分别可精确到±0. 50C 和0.5mm。

温度控制误差在±10C以内。

关键词:MCS-51系列单片机,DS18B20,微功率无线数传模块,TLC2543目录1 系统方案设计 (1)1.1引言 (1)1.2方案比较与选择 (1)1.2.1温度测控模块的选择 (1)1.2.2加热控制方法的选择 (1)1.2.3 无线遥控模块的选择 (2)1.3总体模块设计 (2)2 电路设计 (3)2.1电路元器件连接 (3)2.2系统结构和功能 (3)2.2.1 传感器部分 (3)2.2.2处理器部分 (3)2.2.3 无线数据收发部分和PC机管理部分 (4)3 软件实现 (5)3.1开发软件及编程语言简介 (5)3.2程序流程 (5)3.3上位机与单片机之间建立的协议 (6)4 系统测试 (7)4.1测试准备 (7)4.2测试结果 (7)5 结论 (8)5.1试验结果 (8)5.2改进意见 (8)参考文献: (9)附录: (10)附1:元器件明细表: (10)附2:仪器设备清单 (10)附3:电路图图纸 (11)1系统方案设计1.1引言在现实生活中,温度检测技术及其装置有着十分广泛的应用,而传统的温度检测方法,工作量大,可靠性差。

并且近年来管理逐步实现机械化、自动化与智能化,基于这一技术发展新动向,该设计采用微功率无线数传模块、MCS51系列单片机、单总线数字温度传感器DS18B20 构成温度遥测遥控系统。

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

#include<reg52.h>#include<intrins.h> //包含_nop_()函数定义的头文件#define uint unsigned int#define uchar unsigned charsbit ds=P3^3; //DS18B20接口uint temp;float f_temp;//12864端口定义sbit LCD12864_RS=P2^0;sbit LCD12864_WR=P2^1;sbit LCD12864_E=P2^2;sbit LCD12864_PSB=P2^3;sbit LCD12864_busy=P0^7;#define LCD12864_dat P0void delay(uint x){uint i,j;for(i=x;i>0;i--)for(j=110;j>0;j--) ;}//****************************************//以下为12864液晶显示部分子程序模块//****************************************//函数定义void wr_d_lcd(uchar content);void wr_i_lcd(uchar content);void clrram_lcd (void);void init_lcd(void);void busy_lcd(void);void rev_row_lcd(uchar row);void rev_co_lcd(uchar row,uchar col,uchar mode);void clr_lcd(void);void wr_co_lcd(uchar row,uchar col,uchar lcddata1,uchar lcddtta2);void wr_row_lcd(uchar row,char *p);//延时子程序模块//**********************************************void mdelay(uint delay){ uint i;for(;delay>0;delay--){for(i=0;i<80;i++) //1ms延时.{;}}}//***********************************//填充液晶DDRAM全为空格//**********************************void clrram_lcd (void){wr_i_lcd(0x30);wr_i_lcd(0x01);}//***********************************//对液晶写数据//content为要写入的数据//***********************************//*********************写数据函数void wr_d_lcd(uchar content){busy_lcd();LCD12864_RS=1;LCD12864_WR=0;LCD12864_dat=content;LCD12864_E=1;;LCD12864_E=0;}//********************************//对液晶写指令//content为要写入的指令代码//*****************************//**************************写命令函数void wr_i_lcd(uchar content){busy_lcd();LCD12864_RS=0;LCD12864_WR=0;LCD12864_dat=content;LCD12864_E=1;;LCD12864_E=0;}//********************************//液晶检测忙状态//在写入之前必须执行//********************************void busy_lcd(void){LCD12864_dat=0xff;LCD12864_RS=0;LCD12864_WR=1;LCD12864_E =1;while(LCD12864_busy==1);LCD12864_E=0;}//**********************************//液晶初始化//**********************************void init_lcd(void){wr_i_lcd(0x30);wr_i_lcd(0x06); /*光标的移动方向*/wr_i_lcd(0x0c); /*开显示,关游标*/wr_i_lcd(0x01); //清屏}//********************************//指定要显示字符的坐标//*******************************void LCD12864_pos(unsigned char y, unsigned char x){if(y==1)wr_i_lcd(0x80|x);if(y==2)wr_i_lcd(0x90|x);if(y==3)wr_i_lcd((0x80|x)+8);if(y==4)wr_i_lcd((0x90|x)+8);}//**********************************//液晶显示字符串程序//**********************************void LCD12864print(uchar *str){while(*str!='\0'){wr_d_lcd(*str);str++;}}//**********************************//液晶显示位置、字符串、显示间隔时间//**********************************//LCD12864_display1(1,3,500,"中国CHN")//意思为在第1行第4列显示"中国CHN",每个字显示间隔为500ms LCD12864_display1(unsigned char y, unsigned char x,uint t, uchar *str) {LCD12864_pos(y,x);while(*str!='\0'){wr_d_lcd(*str);str++;delay(t);}}LCD12864_display2(unsigned char y, unsigned char x,uchar n){LCD12864_pos(y,x);wr_d_lcd(0x30+n);}//此函数是显示变量的,比如要在第1行第2列显示一个不断变化的变量a代表的值,//就用LCD12864_display2(1,2,a);//****************************************//以上为12864液晶显示部分子程序模块//****************************************/*****************************************************以下为DS18B20模块语句***************************************************//*void delay(uint z)//延时函数{uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}*/void dsreset(void) //18B20复位,初始化函数{uint i;ds=0;i=103;while(i>0)i--;ds=1;i=4;while(i>0)i--;}bit tempreadbit(void) //读1位函数{uint i;bit dat;ds=0;i++; //i++ 起延时作用ds=1;i++;i++;dat=ds;i=8;while(i>0)i--;return (dat);}uchar tempread(void) //读1个字节{uchar i,j,dat;dat=0;for(i=1;i<=8;i++){j=tempreadbit();dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DA T里}return(dat);}void tempwritebyte(uchar dat) //向18B20写一个字节数据{uint i;uchar j;bit testb;for(j=1;j<=8;j++){testb=dat&0x01;dat=dat>>1;if(testb) //写1{ds=0;i++;i++;ds=1;i=8;while(i>0)i--;}else{ds=0; //写0i=8;while(i>0)i--;ds=1;i++;i++;}}}void tempchange(void) //DS18B20 开始获取温度并转换{dsreset();delay(1);tempwritebyte(0xcc); // 写跳过读ROM指令tempwritebyte(0x44); // 写温度转换指令}uint get_temp() //读取寄存器中存储的温度数据{uchar a,b;dsreset();delay(1);tempwritebyte(0xcc);tempwritebyte(0xbe);a=tempread(); //读低8位b=tempread(); //读高8位temp=b;temp<<=8; //两个字节组合为1个字temp=temp|a;f_temp=temp*0.0625; //温度在寄存器中为12位分辨率位0.0625°temp=f_temp*10+0.5; //乘以10表示小数点后面只取1位,加0.5是四舍五入f_temp=f_temp+0.05;return temp; //temp是整型}/*****************************************************以上为DS18B20模块语句***************************************************/void dis_temp(uint t){uchar shi,ge,xiao;shi=t/100;//十位ge=t%100/10;//个位xiao=t%100%10;//小数位LCD12864_display1(1,0,0,"当前温度为:");LCD12864_display1(3,0,0,"摄氏度");LCD12864_display2(3,3,shi);LCD12864_display2(3,4,ge);LCD12864_display1(3,5,0,".");LCD12864_display2(3,6,xiao);LCD12864_display1(3,7,0,"C");}main(){init_lcd();while(1){tempchange();dis_temp(get_temp());}}。

相关文档
最新文档