M16 Temperature Sensor DS18B20 Control Example Program

Publisher:风清扬yxLatest update time:2017-12-13 Source: eefocusKeywords:M16 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//DS18b20 C language driver, three-digit digital tube display, read the current ambient temperature, accuracy of 0.1 degrees, temperature range 0-99 degrees 
//DS18B20 detailed pin function description 1. GND ground signal; 2. DQ data input and output pin. Open drain single bus interface pin. When used under parasitic power supply 
//It can also provide power to the device; 3. VDD optional VDD pin. When working under parasitic power supply, this pin must be grounded. 
//How to use DS18B20. DS18B20 uses 1-Wire bus protocol 

: //That is, to realize bidirectional transmission of data on one data line 
//Compilation environment AVR Studio 4.13/AVR GCC 
//System clock 7.3728MHz, set the fuse bit to external high-frequency quartz crystal oscillator, startup time 4.1ms   //  ***********************************************************************  //  
Include 
files 
//*********************************************************************** 

#include  
#include #include <  string.h > #include #include   #include   #include   #include //Interrupt signal header file  //*******************************************************************************  // Define variable area  // CH451 various function command values, write as needed  //*******************************************************************  #define CH451_RESET 0x0201 //Reset  #define CH451_LEFT MOV 0x0300 //Set the movement mode - left shift  #define CH451_LEFTCYC 0x0301 //Set the movement mode - left cycle  #define CH451_RIGHT MOV 0x0302 //Set the movement mode - right shift  #define CH451_RIGHTCYC 0x0303 //Set the movement mode - right cycle   #define CH451_SY SOF F 0x0400 //Turn off display, keyboard, watchdog  #define CH451_SYSON1 0x0401 //Turn on display  #define CH451_SYSON2 0x0403 //Turn on display and keyboard  #define CH451_SYSON3 0x0407 //Open the display, keyboard and watchdog functions  #define CH451_ DSP        0x0500 //Set the default display mode  #define CH451_BCD 0x0580 //Set the BCD decoding mode  #define CH451_TWINKLE 0x0600 //Set the blinking control  #define CH451_DIG0 0x0800 //Display the digital tube position 0  #define CH451_DIG1 0x0900 //Display the digital tube position 1   #define CH451_DIG2 0x0a00 //Display the digital tube position 2  #define CH451_DIG3 0x0b00 //Display the digital tube position 3  #define CH451_DIG4 0x0c00 //Display the digital tube position 4  #define CH451_DIG5 0x0d00 //Display the digital tube position 5   #define CH451_DIG6 0x0e00 //Display of digital tube position 6   #define CH451_DIG7 0x0f00 //Display of digital tube position 7  #define  LED 0 0x0000 //Data displayed by digital tube position 0  #define led1 0x0000 //Data displayed by digital tube position 0; 0x0010 means off  #define led2 0x0000 //Data displayed by digital tube position 0  #define led3 0x0010 //Data displayed by digital tube  position 0#define led4 0x0010 //Data displayed by digital tube position 0  #define led5 0x0010 //Data displayed  by digital tube position 0#define led6 0x0010 //Data displayed by digital tube position 0  #define led7 0x0010 //Data displayed by digital tube position 0  #define dclk0 PORTD &= ~(1 << PD6) //Serial data clock, rising delay activation  #define dclk1 PORTD |= (1 << PD6)  #define din0 PORTD &= ~(1 << PD5) //Serial data output, connected to CH451 data input  #define din1 PORTD |= (1 << PD5) 












































#define load0 PORTD &= ~(1 << PD4) //Serial command loading, rising delay activation 
#define load1 PORTD |= (1 << PD4) 

#define DQ_IN DDRC &= ~(1 << PC6) //Set input 
#define DQ_OUT DDRC |= (1 << PC6) //Set output 
#define DQ_CLR PORTC &= ~(1 << PC6) //Set low level 
#define DQ_SET PORTC |= (1 << PC6) //Set high level 
#define DQ_R          PIN C & (1 << PC6) //Read level 


#define delay_us(x) _delay_us(x) //AVR GCC delay function x(us) 
#define delay_ms(x) _delay_ms(x) //AVR GCC delay function x(ms) 

#define uchar unsigned char 
#define uint unsigned int 
uint value; 
uint temp,A1,A2,A3; //Defined variables, display data processing 
uchar flag1;             
uchar table[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06, 
               0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d, 
     0x0e,0x0f}; //BCD code of digital tube 


//**************************************************************************** 
// Initialization subroutine 
//**************************************************************************** 

void system_init() 


SFIOR=(1<<2)|SFIOR; //PUD=1; turn off the pull-up resistor 
PORTD=0x70; 
DDRD=0x70; 



void ch451_init() 

  din0; //first low then high, select 4-wire input 
  din1; 


//**************************************************************************** 
// Output command subroutine 
// Define an unsigned integer variable to store 12 bytes of command word 
 //******************************************************************** 
  void ch451_write(unsigned int command) 

  unsigned char i; 
  load0; // Command start        
  for(i=0;i<12;i++) 
   { // Input 12 bits of data, low bit firstif 
      
     (command&1) 
  { 
    din1; 

  } 
  else 
    din0;  
     dclk0; 
     dclk1; // Rising edge valid 
     command>>=1;                                 
   } 
     load1; // Load data 



//************************************************************************* 
// Display function 
//********************************************************************* 
void display(uint b_d,uint s_d,uint g_d) 

 ch451_init(); 
 ch451_write(CH451_SYSOFF); //Turn off display, keyboard, and watchdog 
 ch451_write(CH451_SYSON1); //Turn on display 
 ch451_write(CH451_BCD); //Set BCD decoding mode 
 ch451_write(CH451_TWINKLE); //Set twinkle control  

 ch451_write(CH451_DIG0|table[b_d]); //Display hundreds digit 
 datach451_write(CH451_DIG1|table[s_d]|0x80); //Display tens digit 
 datach451_write(CH451_DIG2|table[g_d]); //Display units digit  
 datach451_write(CH451_DIG3|led3); //Other digital tubes are not displayed and are offch451_write 
 (CH451_DIG4|led4); 
 ch451_write(CH451_DIG5|led5); 
 ch451_write(CH451_DIG6|led6); 
 ch451_write(CH451_DIG7|led7); 



  
//*************************************************************************  
// DS18B20 initialization 
//************************************************************************ 

unsigned char ds1820_reset(void) //initialization and reset 


unsigned char i; 
        DQ_OUT; 
 DQ_CLR; 
 delay_us(500); //delay 500uS(480-960) 
 DQ_SET; 
 DQ_IN; 
 delay_us(80); //delay 80uS 
 i = DQ_R; 
 delay_us(500); //delay 500uS(keep>480uS) 
  
 if (i)  
        { 
  return 0x00; 
   
        } 
 else  
        { 
  return 0x01; 
   
        } 




//*****************************************************************************  
// DS18B20 read a byte function 
//*************************************************************************  
    
unsigned char ds1820_read_byte(void)  


 unsigned char i; 
 unsigned char value = 0; 
 for (i = 8; i != 0; i--)  
        { 
  value >>= 1; 
  DQ_OUT; 
  DQ_CLR; 
                delay_us(4); //*Delay 4uS 
   
  DQ_SET; 
  DQ_IN; 
                delay_us(10); //*Delay 10uS 
   
  if (DQ_R)  
                { 
   value|=0x80; 
    
                } 
  delay_us(60); //*Delay 60uS 
   
        } 
  
 return(value); 


//*****************************************************************************  
// Write a byte function to 18B20 
//*************************************************************************   

/*DS18B20 byte write function*/ 
void ds1820_write_byte(unsigned char value)  


 unsigned char i; 
 for (i = 8; i != 0; i--)  
        { 
  DQ_OUT; 
  DQ_CLR; 
                delay_us(4); //delay 4uS 
  if (value & 0x01)  
                { 
   DQ_SET; 
    
                } 
  delay_us(80); //delay 80uS 
  DQ_SET; //bit end 
  value >>= 1; 
   
        } 
  

//************************************************************************ 
// Send temperature conversion command 
//****************************************************************************  

/*Start ds1820 conversion*/ 
void ds1820_start(void) { 

 ds1820_reset(); 
 ds1820_write_byte(0xCC); //Do not omit address 
 ds1820_write_byte(0x44); //Start conversion 


//***************************************************************************** 
// DS8B20 reads temperature information 
//*****************************************************************************  

unsigned int ds1820_read_temp(void)  


 unsigned int i; 
 unsigned char buf[9]; 

 ds1820_reset(); 
 ds1820_write_byte(0xCC); //Do not omit address 
 ds1820_write_byte(0xBE); //Read temperaturefor 
 (i = 0; i < 9; i++)  
        { 
  buf[i] = ds1820_read_byte(); 
   
        } 
 i = buf[1]; 
 i <<= 8; 
 i |= buf[0]; 
 value=i; 
        value=value*0.625; //The reason for not multiplying by 0.0625 is to convert the data after the decimal point into data that can be displayed 
                          . //For example, the temperature itself is 27.5 degrees. In order to get the BCD code in the subsequent data processing program, we first enlarge it to 275. 
                                        //Then determine the position of the decimal point when displaying, and 27.5 degrees can be displayed. 
        //value=value*10; 
 return i; 


  

//******************************************************************************** 
// Temperature data processing function 
//**************************************************************************** 
 void data_do(uint d) 
 { 
     uint A2t; 
     A1=d/100; //Separate hundreds, tens, and onesA2t 
     =d%100; 
     A2=A2t/10; 
     A3=A2t%10; 


//**************************************************************************** 
// Main program 
//**************************************************************************** 

void main(void) 


 DDRC = 0x00; 
 PORTC = 0xFF; 
 system_init(); //System initialization 
 delay_ms(250);  
 ds1820_reset(); //Reset D18B20 
 while (1) 
        { 
  ds1820_start(); //Start a conversion 
  delay_ms(300); //Wait for the conversion to end 
  ds1820_read_temp(); //Read the temperature value 
        data_do(value); //Process the data and get the value to be displayed 
     display(A1,A2,A3); //Display the temperature value 
     delay_ms(1000); //Delay 1S 
   
        } 


Keywords:M16 Reference address:M16 Temperature Sensor DS18B20 Control Example Program

Previous article:ATmega16 read and write EEPROM AT25256 (ICC compilation) program
Next article:M16 GCC LED Flash Example Program

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号