DS18B20 temperature microcontroller control display C program

Publisher:Yinyue1314Latest update time:2016-11-08 Source: eefocusKeywords:DS18B20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The temperature value is accurate to 0.1 degrees, LCD1602 display

The simulation circuit diagram is as follows

 

DS18B20 temperature microcontroller control display C program - mybjs2008 - mybjs2008's blog

The c program is as follows:

#include

#define uchar unsigned char
#define uint unsigned int

sbit DQ=P3^7; //ds18b20 and microcontroller connection port
sbit RS=P3^0;
sbit RW=P3^1;
sbit EN=P3^2;

unsigned char code str1[]={"temperature: "};
unsigned char code str2[]={"              "};

uchar data disdata[5];
uint tvalue; //temperature value
uchar tflag; //temperature positive and negative flag

/*************************lcd1602 program******************************/
void delay1ms(unsigned int ms)//Delay 1 millisecond (not precise enough)
{unsigned int i,j;
   for(i=0;i     for(j=0;j<100;j++);
}

void wr_com(unsigned char com)// 写电影//
{ delay1ms(1);
   RS=0;
   RW=0;
   EN=0;
   P2=how;
   delay1ms(1);
   EN=1;
   delay1ms(1);
   EN=0;
}

void wr_dat(unsigned char dat)//写数据//
{ delay1ms(1);;
   RS=1;
   RW=0;
   EN=0;
   P2=dat;
   delay1ms(1);
   EN=1;
   delay1ms(1);
   EN=0;
}

void lcd_init()//initialization settings//
{delay1ms(15);
wr_com(0x38);delay1ms(5);
   wr_com(0x08);delay1ms(5);
    wr_com(0x01);delay1ms(5);
     wr_com(0x06);delay1ms(5);
      wr_com(0x0c);delay1ms(5);
}


void display(unsigned char *p)//显示//
{
while(*p!='\0')
{
wr_dat(*p);
p++;
delay1ms(1);
}
}

init_play()//Initialize display
{ lcd_init();
    wr_com(0x80);
display(str1);
wr_com(0xc0);
display(str2);
   }

/******************************ds1820 program*******************************************/
void delay_18B20(unsigned int i)//delay 1 microsecond
{
   while(i--);
}

void ds1820rst()/*ds1820 reset*/
{ unsigned char x=0;
DQ = 1; //DQ reset
delay_18B20(4); //delay
DQ = 0; //DQ pull low
delay_18B20(100); //accurate delay greater than 480us
DQ = 1; //pull high
delay_18B20(40); 
   } 

   uchar ds1820rd()/*read data*/
{ unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{ DQ = 0; //give pulse signal
    dat>>=1;
    DQ = 1; //give pulse signal
    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()/*read temperature value and convert*/
{uchar a,b;
ds1820rst();    
ds1820wr(0xcc);//*skip reading serial number*/
ds1820wr(0x44);//*start temperature conversion*/
ds1820rst();    
ds1820wr(0xcc);//*skip reading serial number*/ 
ds1820wr(0xbe);//*read temperature*/ 
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);//expand temperature value 10 times, accurate to 1 decimal place
return(tvalue);
}

/*******************************************************************/
   void ds1820disp()//temperature value display
{ uchar flagdat;
   disdata[0]=tvalue/1000+0x30;//hundreds digit
     disdata[1]=tvalue%1000/100+0x30;//ten digit
     disdata[2]=tvalue%100/10+0x30;//ones digit
     disdata[3]=tvalue%10+0x30;//decimal digitif
    
     (tflag==0)
     flagdat=0x20;//positive temperature does not display a signelse
     flagdat
       =0x2d;//negative temperature displays a minus sign:-

     if(disdata[0]==0x30)
    {disdata[0]=0x20; //If the hundreds digit is 0, it will not be displayed
   if(disdata[1]==0x30)
    {disdata[1]=0x20; //If the hundreds digit is 0, the tens digit will not be displayed either
    }
   }

    wr_com(0xc0);
   wr_dat(flagdat);//display the sign bit
    wr_com(0xc1);
    wr_dat(disdata[0]);//display the hundreds digit
    wr_com(0xc2);
    wr_dat(disdata[1]);//display the tens digit 
    wr_com(0xc3);
    wr_dat(disdata[2]);//display the ones digit 
    wr_com(0xc4);
    wr_dat(0x2e);//display the decimal point 
    wr_com(0xc5);
    wr_dat(disdata[3]);//display the decimal place
   }
/********************Main program***********************************/
void main()
{ init_play();//initialize the display
   while(1)
{read_temp();//read the temperature
    ds1820disp();//display
}
}


Keywords:DS18B20 Reference address:DS18B20 temperature microcontroller control display C program

Previous article:Based on the single chip microcomputer 89C51 to generate PWM signal to control the DC motor speed regulation program
Next article:Usage of sbit sfr in c51 programming

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号