DS18b20 temperature detection and display library program

Publisher:云淡风轻2014Latest update time:2016-12-13 Source: eefocusKeywords:DS18b20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

**************************************************************************************************

DS18b20.H

*******************
   DQ connects to p2.2
*******************

#ifndef __DS18b20_H__
#define __DS18b20_H__

#include"msp430f2232.h"

*********************************
**         引脚定义           **
*********************************
#define DQ_1 P2OUT |= BIT2
#define DQ_0 P2OUT &= ~BIT2
#define DQ_in  P2DIR &= ~BIT2
#define DQ_out P2DIR |= BIT2
#define DQ_val (P2IN & BIT2)

**********************************
**** Command character definition *****
**********************************
#define Read_ROM 0x33 //Read ROM
#define Match_ROM 0x55 //Match ROM
#define Skip_ROM 0xcc //Skip ROM
#define Search_ROM 0xf0 //Search ROM
#define Alarm_Search 0xec //Alarm search

#define Convert_Temperature 0x44 //Temperature conversion
#define Read_Scratchpad 0xbe //Read 9 bytes of scratchpad content
#define Write_Scratchpad 0x4e //Write scratchpad, write TH and TL, then send two bits of data

*********************************
** Define variables **
*********************************

//extern unsigned int Check_val;//Initialize the detection variable
//extern unsigned int Temp;//Store the temperature
//extern unsigned int Temp_l;//Store the lower four bits of the temperature
//extern unsigned int Temp_h;//Store the higher four bits of the temperature
************************************
**** Function definition ****
************************************
extern void DS18b20_Port_Init(void);
extern unsigned int DS18b20_Init(void);
extern void DS18b20_write_byte(unsigned int dat);
extern unsigned int DS18b20_read_byte(void);
extern unsigned int get_one_temperature(void);

# endif

**************************************************************************************************

DS18B20.C

***********************
**** DQ 接 p2.4   ***
***********************
#include"DS18b20.h"

void DS18b20_Port_Init(void)
{
 P2DIR = BIT2;
}
******************************************************************************
                         DS18b20 operation sequence:
                         1.DS18b20 initialization
                         2.Operate 64-bit ROM
                              Read ROM
                              Search ROM
                              Skip ROM
                              alarm search
                         3.Operate registers
                            including reading temperature...
                         4.The default precision is 0.0625 and cannot be reset. No corresponding instruction was found.
                           The corresponding conversion time is 750ms
*******************************************************************************

******************************************************************************
DS18b20 initialization method:
 1 The host sends a low level of 480 - 960 us to release the bus
 2 Wait for 15 - 60 us
 3 Check whether there is a low level on DQ
     Yes: reset successful, usually the time is 60-240 us
     No: reset failed, continue to wait
 4 After a low level appears on DQ, the low level lasts for 15us, and then DS18b20 starts sampling the data sent by the MCU
**********************************************************************************

unsigned int DS18b20_Init(void)
{
   unsigned Check_val;
   DQ_out;
   DQ_0;
   __delay_cycles(600);
   DQ_1;
   __delay_cycles(60);
   DQ_in;
   _NOP();
   
   if(DQ_val==1)     
   {
       Check_val = 0;         //初始化失败
   }
   if(DQ_val==0)
   {
       Check_val = 1;         //初始化成功
   }
   __delay_cycles(10);
   DQ_out;
   DQ_1;
   __delay_cycles(100);
   return Check_val;
}

******************************************************************************
DS18b20 write data method:
 1 DS18b20 writes '0' and '1' one bit at a time
 2 Each write of '1' or '0' is a cycle, and each cycle is about 45 - 60 us
 3 DQ is pulled low for 1 us, indicating the start of the write cycle, releasing the bus, and allowing DQ to change with the written value
 4 If writing 1: DQ is pulled high for at least 60us to ensure that the values ​​sampled within the sampling cycle are all high
 5 If writing 0: DQ is pulled low for at least 60us to ensure that the values ​​sampled within the sampling cycle are all low
 6 Release the bus
**********************************************************************************
* When the microcontroller sends data, it starts from the highest bit of the written data*

void DS18b20_write_byte(unsigned int dat)
{
   unsigned int i;
   for(i = 0; i < 8;i++)
   {
       DQ_0;
       __delay_cycles(2);           
       if(dat & 0X01)   
         DQ_1;
       else 
         DQ_0;
       __delay_cycles(60);
       dat >>= 1;;         
       DQ_1;
       __delay_cycles(10);
   }
}

**********************************************************************************************
DS18b20 read data method:
 1 DS18b20 reads '0' and '1' one bit at a time
 2 Each read of '1' or '0' is a cycle, and each cycle is about 45 - 60 us
 3 DQ is pulled low for 1 us, indicating that the read cycle starts, releasing the bus, and letting DQ change with the value transmitted by DS18b20
 4 If 1 is transmitted: a high level is detected, and the duration is about 60us, so after one detection, it is necessary to delay 60us and then detect the next bit of data to be transmitted
 5 If 0 is transmitted: a low level is detected, and the duration is about 60us
*********************************************************************************************************
*DS18b20 also transmits data from the highest bit*
*So when the MCU receives data, the storage variable moves a total of 8 times, receives all data and returns to the highest bit*

unsigned int DS18b20_read_byte(void)
{
   unsigned i;
   unsigned int byte = 0;
   for(i = 0;i < 8;i++)
   {
       byte >>= 1; 
       DQ_0;
       __delay_cycles(2); 
       DQ_1;
       __delay_cycles(2); 
       DQ_in;
       _NOP();
       if(DQ_val)  
         byte |= 0x80;
       __delay_cycles(60);
       DQ_out;
       DQ_1;
       __delay_cycles(10);
   }
   return byte;
}

**********************************************************************************
When using a DS18b20 for temperature measurement Steps
1. Initialization
2. Skip ROM
3. Control registers: Temperature conversion, read ROM, read temperature low 8 bits, temperature high 8 bits
Note that it must be initialized twice
**********************************************************************************

unsigned int get_one_temperature(void) //Only the integer is read, not the decimal part
{
 unsigned int Temp_l,Temp_h,Temp;
 DS18b20_Init();
 DS18b20_write_byte(Skip_ROM);
 DS18b20_write_byte(Convert_Temperature);
 __delay_cycles(500000);
 
 DS18b20_Init();
 DS18b20_write_byte(Skip_ROM);
 DS18b20_write_byte(Read_Scratchpad);
 Temp_l=DS18b20_read_byte();
 Temp_h=DS18b20_read_byte();
 Temp_l>>=4;
 Temp_h<<=4;
 Temp=Temp_h+Temp_l;
 return Temp;
}

**************************************************************************************************

LCD1602.H

************************
P1 port is data port
rs connects to p2.0
rw external ground
en connects to p2.1
*************************

#ifndef __LCD1602_H__
#define __LCD1602_H__

//#include"msp430g2553.h"
#include"msp430f2232.h"

******************************
         Pin Definition
******************************

#define rs_0 P2OUT &= ~BIT1
#define rs_1 P2OUT |= BIT1
#define en_0 P2OUT &= ~BIT0
#define en_1 P2OUT |= BIT0

**************************************************************
                       Function definition
*******************************************************

extern void LCD1602_Port_Init(void);
extern void LCD1602_write_command(unsigned int com);
extern void LCD1602_write_data(unsigned int dat);
extern void LCD1602_Init(void);

extern void LCD1602_set_position(unsigned int x,unsigned int y);
extern void LCD1602_write_string(unsigned int x,unsigned int y,unsigned char *str);
extern void LCD1602_display_temperature(unsigned int x,unsigned int y,unsigned int temp,unsigned int lit);
extern void LCD1602_write_varia(unsigned int x,unsigned int y,unsigned int varia,unsigned int n);

#endif
**************************************************************************************************

LCD1602.C

 

#include"LCD1602.h"
**********************************
     Digital variable display definition
************************************

 char Digital[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};

void LCD1602_Port_Init(void)
{
 P1DIR = 0XFF;
 P2DIR |= BIT0 + BIT1 ;
}
void LCD1602_write_command(unsigned int com)
{
 rs_0;
 P1OUT=com;
 __delay_cycles(500);
 en_1;
 __delay_cycles(500);
 en_0;
}

void LCD1602_write_data(unsigned int dat)
{
 rs_1;
 P1OUT=dat;
 __delay_cycles(500);
 en_1;
 __delay_cycles(500);
 en_0;
}

void LCD1602_Init(void)
{
  LCD1602_write_command(0x38); //5*7 dot matrix, double line display
  LCD1602_write_command(0x0c); //0x0f has cursor,
  LCD1602_write_command(0x01); //Clear screen
  LCD1602_write_command(0x06); //After writing data, the cursor moves right and the display screen does not move
}

 void LCD1602_set_position(unsigned int x,unsigned int y)
{
 if(x==1)
   LCD1602_write_command(0x80+y);
 if(x==2)
   LCD1602_write_command(0x80+0x40+y);
}

 void LCD1602_write_string(unsigned int x,unsigned int y,unsigned char *str)
{
 __delay_cycles(1000);
 LCD1602_set_position(x,y);
 while(*str!='\0')
 {
   LCD1602_write_data(*str);
   str++;
 }
}

void LCD1602_display_temperature(unsigned int x,unsigned int y,unsigned int temp,unsigned int lit)
{
   LCD1602_set_position(x,y);
   LCD1602_write_data(Digital[temp/10]);
   LCD1602_write_data(Digital[temp]);
   LCD1602_write_data('.');
   LCD1602_write_data(Digital[lit/10]);
   LCD1602_write_data(Digital[lit]);
   LCD1602_write_data('C');
}


void LCD1602_write_varia(unsigned int x,unsigned int y,unsigned int varia,unsigned int n)
{
 __delay_cycles(1000);
 LCD1602_set_position(x,y);
 switch(n)
 {
   case 1 : LCD1602_write_data(Digital[varia]); break;
   case 2 : LCD1602_write_data(Digital[varia/10]);
            LCD1602_write_data(Digital[varia]);break;
   case 3 : LCD1602_write_data(Digital[varia/100]);
            LCD1602_write_data(Digital[varia0/10]);
            LCD1602_write_data(Digital[varia]); break;
   case 4 : LCD1602_write_data(Digital[varia/1000]);
            LCD1602_write_data(Digital[varia00/100]);
            LCD1602_write_data(Digital[varia00/10]);
            LCD1602_write_data(Digital[varia]);break;
 }
}

**************************************************************************************************

MAIN.C

#include "LCD1602.h"
#include "DS18B20.h"

int main( void )
{
 // Stop watchdog timer to prevent time out reset
 WDTCTL = WDTPW + WDTHOLD;
 P1DIR =0XFF;
 P2DIR = BIT0+BIT1+BIT2;
 unsigned int t;
 LCD1602_Init();
 DS18b20_Init();
 while(1)
 {
  t=get_one_temperature();
 LCD1602_write_string(1,1,"The temprature ");
  LCD1602_write_string(2,0,"is");
 LCD1602_display_temperature(2,3,t,t);
 }
}

**************************************************************************************************

Simulation results:

(0x02) DS18b20 temperature detection and display library program
**************************************************************************************************


Keywords:DS18b20 Reference address:DS18b20 temperature detection and display library program

Previous article:12864_8 line library program
Next article:Analysis and Evaluation Method of Communication Performance of Single Chip Microcomputer

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号