DS18B20 Temperature Sensor Complete C Program

Publisher:RadiantEnergyLatest update time:2018-06-20 Source: eefocusKeywords:DS18B20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include
#include //_nop_(); delay function
#define Disdata P0 //segment code output port
#define discan P2 //scan port
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P3^7; //temperature input port
sbit DIN=P0^0; //LED decimal point control
uint h; 
uint temp;
//
//
//**************Temperature decimal part uses table lookup method***********//
uchar code ditab[16]=
{0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
//
uchar code dis_7[12]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0xff,0x02};
//Common anode LED segment code table "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "off" "-"              
uchar code scan_con[4]={0xf7,0xfB,0xfD,0xff}; //Column scan control word
uchar data temp_data[2]={0x00,0x00}; //Read temperature temporarily
uchar data display[5]={0x00,0x00,0x00,0x00,0x00}; //Display unit data, a total of 4 data and one operation temporarily

//
//
//
/*********************11us delay function****************************/
//
void delay(uint t)
{
  for (;t>0;t--);
}
//

/****************Display scan function****************************/
scan()
{
  char k;
     for(k=0;k<4;k++) //4-bit LED scan control
{
  Disdata=dis_7[display[k]]; //Data displayif
  (k==1){DIN=1;} //Decimal point displaydiscan
  =scan_con[k]; //Bit selectiondelay
  (300);
}
}
//
//
/****************DS18B20 reset function****************************/
ow_reset(void)
{
char presence=1;
while(presence)
{
  while(presence)
   {
    DQ=1;_nop_();_nop_();//Pull from high to low
DQ=0;                                       
delay(50); //550 us
DQ=1;                                        
delay(6); //66 us
presence=DQ; //presence=0 Reset successful, continue to the next step
   } 
   delay(45); //Delay 500 us
   presence=~DQ;         
}
DQ=1; //Pull high
}
//
//
/****************DS18B20 write command function****************************/
//Write 1 byte to the 1-WIRE bus
void write_byte(uchar val)

  uchar i;
  for(i=8;i>0;i--)
  {
   DQ=1;_nop_();_nop_(); //Pull from high to low
   DQ=0;_nop_();_nop_();_nop_();_nop_(); //5 us
   DQ=val&0x01; //The lowest bit is moved out
   delay(6); //66 us
   val=val/2; //Shift right 1 bit
   }
   DQ=1;
   delay(1);
}
//
/****************DS18B20 read 1 byte function****************************/
//Get 1 byte from the bus
uchar read_byte(void)
{
uchar i;
uchar value=0;
for(i=8;i>0;i--)
{
  ow_reset(); delay(200); write_byte(0xcc); //send command write_byte(0x44); //send conversion command ow_reset(); delay(1); write_byte
  (
  0xcc
  )   ;
  // send   command write_byte(   0xbe)   ;   temp_data   [ 0]= read_byte ( ) ; // read the byte of temperature value   temp_data   [1   ] =   read_byte (   )  ; //Read the high byte of temperature value   temp=temp_data[1];





















  temp<<=8;                                               
  temp=temp|temp_data[0]; // Two bytes are combined into an integer variable.
  return temp; //Return temperature value
}
//
/****************Temperature data processing function****************************/

//The lower half byte of the binary high byte and the higher half byte of the low byte form a byte.
After the binary of this //byte is converted to decimal, it is the hundreds, tens, and units of the temperature value, and
the lower half byte of the remaining //low byte is converted to decimal to be the decimal part of the temperature value.

/************************************************************/
work_temp(uint tem)
{
uchar n=0;
if(tem>6348) // Temperature value positive or negative judgment
     {tem=65536-tem;n=1;} // Negative temperature two's complement, flag position 1
      display[4]=tem&0x0f; // Get the value of the decimal part
  display[0]=ditab[display[4]]; // Store the decimal part display
      [4]=tem>>4; // Get the middle eight digits, that is, the value of the integer part
  display[3]=display[4]/100; // Get the hundreds digit data and store it
  display[1]=display[4]%100; // Get the last two digits and store it
  display[2]=display[1]/10; // Get the tens digit data and store
  it display[1]=display[1]%10;  
/******************Sign bit display judgment*****************************/
  if(!display[3]) 
  {
    display[3]=0xF5; //No display when the highest bit is 0if
    (!display[2])
{
   display[2]=0xF5; //No display when the second highest bit is 0
}
  }
  if(n){display[3]=0xF4;} //The highest bit displays "-" when the temperature is negative
}
//
//
/********************Main function****************************/
main()
{
  Disdata=0x00; //Initialize portdiscan
  =0x00;
  for(h=0;h<4;h++) //Display "8888" when powered on
      {display[h]=8;} 
  ow_reset(); //Convert once before powering
  onwrite_byte(0xcc); //Skip ROM
  write_byte(0x44); //Send conversion commandfor
  (h=0;h<100;h++) //Display "8888" when powered on
      {scan();}
  while(1)
  {
work_temp(read_temp()); //Process temperature data
scan(); //Display temperature value
  }
}


Keywords:DS18B20 Reference address:DS18B20 Temperature Sensor Complete C Program

Previous article:Example of reading temperature sensor DS18B20
Next article:Example of STC15F2K60S2 reading DHT11 temperature and humidity serial port display

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号