C language program design and development based on 18B20 temperature sensor + 1602 LCD display

Publisher:EternalSmileLatest update time:2016-10-27 Source: eefocusKeywords:18B20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include < reg51.h >
#include < intrins.h >
#define uchar unsigned char
#define uint unsigned int
sbit DQ = P3^3 ; //Define DS18B20 port DQ  
sbit BEEP=P3^6 ; //Buzzer drive line
bit presence ; //Check whether 18b20 is properly plugged in
sbit LCD_RS = P2^0 ;             
sbit LCD_RW = P2^1 ;
sbit LCD_EN = P2^2 ;
uchar code cdis1[ ] = {"  Niuniu Intelligent Technology  "} ;
uchar code cdis2[ ] = {" WENDU: . C "} ;
uchar code cdis3[ ] = {" DS18B20 ERR0R "} ;
uchar code cdis4[ ] = {" PLEASE CHECK "} ;

unsigned char data temp_data[2] = {0x00,0x00};
unsigned char data display[5] = {0x00,0x00,0x00,0x00,0x00};
unsigned char code ditab[16] = {0x00,0x01,0x01,0x02 ,0x03,0x03,0x04,0x04,
                                                        0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09} ;
void beep() ; 
unsigned char code mytab[8] = {0x0C,0x12,0x12,0x0C,0x00, 0x00,0x00,0x00};

#define delayNOP() ; {_nop_() ;_nop_() ;_nop_() ;_nop_() ;} ;

/****************************************************** ******************/
void delay1(int ms)
{
 unsigned char y;
  while(ms--)
 {
  for(y = 0; y<250; y++)
  {
   _nop_() ;
   _nop_() ;
   _nop_() ;
   _nop_() ;
  }
 }
}

/******************************************************************/
/*Check LCD busy status */
/*When lcd_busy is 1, it is busy and waiting. When lcd-busy is 0, it is idle and can write instructions and data. */
/**********************************************************************/ 
bit lcd_busy()
 {                          
    bit result ;
    LCD_RS = 0 ;
    LCD_RW = 1 ;
    LCD_EN = 1 ;
    delayNOP() ;
    result = (bit)(P0&0x80) ;
    LCD_EN = 0 ;
    return(result) ; 
 }

/*Write instruction data to LCD */
/*RS=L, RW=L, E=high pulse, D0-D7=instruction code. */
/********************************************************************/
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 ;  
}

/*******************************************************************/
/*Write display data to LCD */
/*RS=H, RW=L, E=high pulse, D0-D7=data. */
/***********************************************************************/
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 ; 
}


/* LCD initialization settings */
/********************************************************************/
void lcd_init()

    delay1(15);   
    lcd_wcmd(0x01); //Clear LCD display contents            
    lcd_wcmd(0x38); //16*2 display, 5*7 dot matrix, 8-bit data
    delay1(5);
    lcd_wcmd(0x38);         
    delay1(5);
    lcd_wcmd(0x38);         
    delay1(5);

    lcd_wcmd(0x0c); //Display on and off cursor
    delay1(5);
    lcd_wcmd(0x06); //Move cursor
    delay1(5);
    lcd_wcmd(0x01); //Clear LCD display
    delay1(5);
}

/* Set display position */
/***********************************************************************/
void lcd_pos(uchar pos)
{                          
  lcd_wcmd(pos | 0x80); //data pointer = 80 + address variable
}

/*Write custom characters to CGRAM */
/*******************************************************************/
void writetab()  
{  
    unsigned char i ;
    lcd_wcmd(0x40) ; //Write CGRAM
    for (i = 0 ; i< 8 ; i++)       
    lcd_wdat(mytab[ i ]) ;        
}

/*us level delay function */
/***********************************************************************/

void Delay(unsigned int num)
{
  while( --num );
}

/*Initialize ds1820 */
/*******************************************************************/
Init_DS18B20(void)
{  
     DQ = 1 ; //DQ reset
     Delay(8); //delay a little

     DQ = 0; //MCU pulls DQ down
     Delay(90); //Precise delay greater than 480us

     DQ = 1; //Pull the bus high
     Delay(8);

     presence = DQ ; //If = 0, initialization is successful, = 1, initialization fails
     Delay(100) ;
     DQ = 1 ; 
     
     return(presence) ; //Return signal, 0 = presence, 1 = no presence
}


/* Read one byte */
/*******************************************************************/
 ReadOneChar(void)
{
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(4);
  }

    return (dat) ;
}

/* Write one byte */
/********************************************************************/
 WriteOneChar(unsigned char dat)
{
  unsigned char i = 0 ;
  for (i = 8 ; i > 0 ; i--)
  {
    DQ = 0 ;
    DQ = dat&0x01 ;
    Delay(5) ;

    DQ = 1 ;
    dat>>=1 ;
  }
}

/* Read temperature */
/*******************************************************************/
 Read_Temperature(void)
{
     Init_DS18B20() ;
  
     WriteOneChar(0xCC) ; // Skip reading serial number and column number
     WriteOneChar(0x44) ; // Start temperature conversion

     Init_DS18B20() ;
     WriteOneChar(0xCC) ; //Skip the operation of reading the serial number and column number
     WriteOneChar(0xBE) ; //Read the temperature register

     temp_data[0] = ReadOneChar() ; //low 8 bits of temperature
     temp_data[1] = ReadOneChar() ; //high 8 bits of temperature 
}

/* Data conversion and temperature display */
/***************************************************************************/
 Disp_Temperature()
{
  display[4]=temp_data[0]&0x0f ;
  display[0]=ditab[display[4]]+0x30 ;//Look up the table to get the decimal value
  
  display[4]=((temp_data[0]&0xf0)>>4)|((temp_data[1]&0x0f)<<4) ;
  display[3]=display[4]/100+0x30 ;
  display[1]=display[4]%100 ;
  display[2]=display[1]/10+0x30 ;
  display[1]=display[1]%10+0x30 ;

    if(display[3]==0x30) //The high bit is 0, not displayed
   { 
     display[3]=0x20 ;              
     if(display[2]==0x30) //The second high bit is 0, not displayed
     display[2]=0x20 ;
   }

     lcd_pos(0x48);             
     lcd_wdat(display[3]); //Hundreds display 
     lcd_pos(0x49);             
     lcd_wdat(display[2]); //Tens display 
  lcd_pos(0x4a);             
     lcd_wdat(display[1]); //Ones display 
  lcd_pos(0x4c);             
     lcd_wdat(display[0]); //Decimal places display 

/***********************************************************************/
/* The buzzer sounds once */
/*******************************************************************/
void beep()
  {
    unsigned char y ;
    for (y=0 ;y<100 ;y++)
    {
      Delay(60) ;
      BEEP=!BEEP ; //Invert BEEP
    } 
    BEEP=1 ; //Turn off the buzzer
 Delay(40000) ;
  }

/* DS18B20 OK displays the menu */
/*******************************************************************/
void Ok_Menu ()

    uchar m ;
    lcd_init() ; //Initialize LCD 
            
    lcd_pos(0) ; //Set the display position to the first character of the first line
     m = 0 ;
    while(cdis1[m] != '\0')
     { //Display character
       lcd_wdat(cdis1[m]) ;
       m++ ;
     }

    lcd_pos(0x40) ; //Set the display position to the first character of the second line
     m = 0 ;
    while(cdis2[m] != '\0')
     {
       lcd_wdat(cdis2[m]) ; //Display character
       m++ ;
     }

     writetab() ; //Write custom characters to CGRAM
     delay1(5) ;
     lcd_pos(0x4d) ;
     lcd_wdat(0x00) ; //Display custom characters
}


/* DS18B20 ERROR display menu */
/****************************************************************/
void Error_Menu ()
{
     uchar m ;
     lcd_init() ; //Initialize LCD 
 
    lcd_pos(0) ; //Set the display position to the first character of the first line
     m = 0 ;
     while(cdis3[m] != '\0')
     { //Display character
       lcd_wdat(cdis3[m]) ;
       m++ ;
     }

     lcd_pos(0x40) ; //Set the display position to the first character of the second line
     m = 0 ;
     while(cdis4[m] != '\0')
     {
       lcd_wdat(cdis4[m]) ; //Display character
       m++ ;
     }
}


/* Main function */
/****************************************/
void main()
 {
     Ok_Menu () ;

 do
  {
    Read_Temperature() ; //Read temperature
   Disp_Temperature() ; //Display temperature
     }
    while(!presence) ;

     Error_Menu () ;

 do
  {  
    Init_DS18B20() ;
    beep() ;
     }
 while(presence) ;
}

Keywords:18B20 Reference address:C language program design and development based on 18B20 temperature sensor + 1602 LCD display

Previous article:C language programming of single chip microcomputer controlling multiple stepper motors
Next article:1602 LCD display infrared remote control decoding C language programming

Recommended ReadingLatest update time:2024-11-17 00:44

ATMEGA16A MCU 1602 character display program
* 【Compilation environment】: ICCAVR   * 【Function】: 1602 character display         * 【Crystal】: 8M                 * 【Chip】: ATMEGA16A          #include iom16v.h #define uchar unsigned char  #define uint unsigned int  uchar wz ={"mcustudio1com-cn"}; uchar gd ={"Tel:15980xx5601"}; #define uchar unsigned char  #def
[Microcontroller]
51 MCU study notes: merge 1602 and 12864 LCD strip interface
  Today I successfully merged the 1602 and 12864 LCD strip interfaces! Share the code   The two pictures above are the 1602 and 12864 LCD socket interfaces, which are usually found on MCU development boards. A careful observation revealed that their sockets are mostly the same.   For the contrast adjustment of the t
[Microcontroller]
51 MCU study notes: merge 1602 and 12864 LCD strip interface
C language programming of 18B20 temperature sensor and LCD display based on AVR microcontroller
******************************************************/ #include #include #define uchar unsigned char #define uint unsigned int #include "1602LCD_drive.h" //Include LCD driver software package #include "DS18B20_drive.h" //DS18B20 driver software package #define beep_0 (PORTD=PORTD&0xbf) //The buzzer on PD6 sou
[Microcontroller]
1602 CVAVR Program
// 1602 display characters #include mega16.h    #include delay.h #define lcd_bus PORTA // Data bus #define rs PORTC.0 // Data & command selection, H: write data, L: write command     #define rw PORTC.1 // Read & write selection, H: read, L: write #define e PORTC.2 // Read and write enable #define bf 7 // Busy/i
[Microcontroller]
MCU I2C communication and LCD1602 display C program
Download the schematic used in this program:  click here  , the microcontroller chip used is stc89c52; just find the schematic diagram of the digital tube. This is the circuit diagram of the entire microcontroller development board, ignore the others. Download the keil project file of this program: http://www.51hei.com
[Microcontroller]
Design of adjustable electronic calendar and clock based on 51 microcontroller using DS1302 and LCD1602
Specific function implementation: After powering on, the current year, month, day, week, hour, minute, second and other information can be displayed on the LCD1602. Press the K1 key to display the real-time clock; press the K2 key to select the year, month, day, hour, Minutes and seconds settings; press the K3 key for
[Microcontroller]
Ultrasonic distance measurement program using 18B20 for temperature compensation
Ultrasonic distance measurement program using 18B20 for temperature compensation #include REGX51.H #include intrins.h #define uint unsigned int #define uchar unsigned char sbit rs=P1^0; sbit rw=P1^1; sbit en=P1^2; sbit wave=P1^4; uint  tvalue;//Temperature value using 18B20 for temperature compensation ult
[Microcontroller]
51 MCU 8255 18b20 digital tube displays temperature C program
#include #include absacc.h #include intrins.h #define uint unsigned int #define uchar unsigned char #define PA 0x7cff    //PA口 #define PB 0x7dff    //PB口 #define con 0x7fff  //控制字 sbit A8=P2^0;            //Address line A0  sbit A9=P2^1;            //Address line A1 sbit cs=P2^7;            //Chip select
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号