MCU DS18b20 Temperature detection LCD display

Publisher:科技火箭Latest update time:2018-05-12 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*-----------------------------------------------  

  Name: DS18b20 temperature detection LCD display  

  Forum: www.doflye.net  

  Written by: shifang  

  Date: 2009.5  

  Modification: None  

  content:  

------------------------------------------------*/  

#include //Include the header file. Generally, no modification is required. The header file contains the definition of special function registers.  

#include  

#include "18b20.h"  

#include "1602.h"  

#include "delay.h"  

  

  

#define KeyPort P3 //Define the key port  

  

  

bit ReadTempFlag; //define read time flag  

  

  

void Init_Timer0(void); //Timer initialization  

unsigned char KeyScan(void); //keyboard scan  

/*------------------------------------------------  

              Serial communication initialization  

------------------------------------------------*/  

void UART_Init(void)  

{  

    SCON = 0x50; // SCON: Mode 1, 8-bit UART, receive enabled    

    TMOD |= 0x20;               // TMOD: timer 1, mode 2, 8-bit 重装  

    TH1 = 0xFD; // TH1: reload value 9600 baud rate crystal 11.0592MHz    

    TR1 = 1; // TR1: timer 1 is turned on                           

    //EA = 1; //Open the general interrupt  

    //ES = 1; //Open serial port interrupt  

IF=1;  

}  

/*------------------------------------------------  

                    Main function  

------------------------------------------------*/  

void main (void)  

{                    

int temp,tempH=50,tempL=1;  

float temperature;  

unsigned char TempFlag=0;  

char displaytemp[16],num; //define the temporary storage array of the display area  

  

  

LCD_Init(); //Initialize LCD  

DelayMs(20); //Delay helps to stabilize  

LCD_Clear(); //Clearing  

Init_Timer0();  

UART_Init();  

Lcd_User_Chr(); //Write custom characters  

  

  

  

  

while (1) //main loop  

  {  

 num=KeyScan();  

 switch(num)  

       {  

   case 1:if(tempH<127)tempH++;break;  

case 2:if(tempH>-55)tempH--;break;  

case 3:if(tempL<127)tempL++;break;  

case 4:if(tempL>-55)tempL--;break;  

default:break;  

  }  

   

switch(TempFlag)  

       {  

   case 0: // Output temperature upper and lower limits  

     sprintf(displaytemp,"H.%3d  L.%3d ",tempH,tempL);  

              LCD_Write_String(0,1,displaytemp);//Display the second line  

 break;  

case 1:LCD_Write_String(0,1,"over  tempH    ");break;  

case 2:LCD_Write_String(0,1,"under  tempL   ");break;  

default:break;  

  }  

if(ReadTempFlag==1)  

 {  

  ReadTempFlag=0;  

  temp=ReadTemperature();  

  

  

  

  

  temperature=temp*0.0625;  

  

  

  temp>>=4;  

 if(temp>tempH)  

    TempFlag=1; //Higher than high temperature flag  

 else if(temp

    TempFlag=2; //Below low temperature flag  

 else  

   TempFlag=0; //Normal display flag  

  

  

      

  

  

  sprintf(displaytemp,"Temp %6.2f ",temperature); //Print temperature value  

  LCD_Write_String(0,0,displaytemp);//Display the first line  

  LCD_Write_Char(13,0,0x01); //Write the upper right corner of the temperature  

  LCD_Write_Char(14,0,'C'); //Write character C  

  }   

 }  

}  

  

  

/*------------------------------------------------  

                    Timer initialization subroutine  

------------------------------------------------*/  

void Init_Timer0(void)  

{  

 TMOD |= 0x01; //Use mode 1, 16-bit timer, use the "|" symbol to avoid being affected when using multiple timers  

      

 //TH0=0x00; //Give initial value  

 //TL0=0x00;  

 EA=1; //General interrupt is turned on  

 ET0=1; //Timer interrupt is turned on  

 TR0=1; //Timer switch is turned on  

}  

/*------------------------------------------------  

                 Timer interrupt subroutine  

------------------------------------------------*/  

void Timer0_isr(void) interrupt 1   

{  

 static unsigned int num;  

 TH0=(65536-2000)/256; //Reassign 2ms  

 TL0=(65536-2000)%256;  

   

 num++;  

 if(num==400)        //  

   {  

    num=0;  

    ReadTempFlag=1; //Read flag position 1  

}  

}  

  

  

/*------------------------------------------------  

Key scanning function, returns the scanned key value  

------------------------------------------------*/  

unsigned char KeyScan(void)  

{  

 unsigned char keyvalue;  

 if(KeyPort!=0xff)  

   {  

    DelayMs(10);  

    if(KeyPort!=0xff)  

  {  

   keyvalue=KeyPort;  

   while(KeyPort!=0xff);  

switch(keyvalue)  

{  

case 0xfe:return 1;break;  

case 0xfd:return 2;break;  

case 0xfb:return 3;break;  

case 0xf7:return 4;break;  

case 0xef:return 5;break;  

case 0xdf:return 6;break;  

case 0xbf:return 7;break;  

case 0x7f:return 8;break;  

default:return 0;break;  

}  

 }  

   }  

   return 0;  


Keywords:MCU Reference address:MCU DS18b20 Temperature detection LCD display

Previous article:Single chip practical password lock
Next article:Single chip dual color dot matrix display specific graphics

Recommended ReadingLatest update time:2024-11-15 14:56

51 MCU-Clock Chip DS1302
In many electronic devices, some time-related controls are usually performed. If the system timer is used to design the clock, accidental power failure or crystal oscillator error will cause time confusion. What's worse, if the clock is completely designed by program, it will occupy a lot of system resources, thus seri
[Microcontroller]
51 MCU-Clock Chip DS1302
51 single chip washing machine proteus simulation and source code design
A fully functional washing machine simulation and source code, controlled by a 51 single-chip microcomputer, with strong wash button, weak wash button, standard wash button, and spin dry button. It has all the functions such as liquid level sensor, drain valve button, etc. The simulation schematic diagram is as foll
[Microcontroller]
51 single chip washing machine proteus simulation and source code design
How to use C51 to realize the communication between single chip microcomputer and PLC
1. HMI (human-machine interface) is increasingly used in industrial automation systems and equipment due to its small size, high performance, and strong real-time characteristics. It has different displays such as letters, Chinese characters, graphics and pictures, and the interface is simple and friendly. It is equip
[Microcontroller]
How to use C51 to realize the communication between single chip microcomputer and PLC
Introduction to PIC series microcontrollers
1) The biggest feature of PIC is that it does not simply pile up functions, but starts from reality, attaches importance to the performance and price ratio of products, and develops multiple models to meet different levels of application requirements.  Initial 8-bit microcontroller: PIC12C5XXX/16C5X series  PIC16C5X
[Microcontroller]
Design of 89C52 microcontroller electronic clock
1. Experimental tasks (1). When turned on, the time starts at 12:00:00; (2). P0.0/AD0 controls the adjustment of "seconds", each press adds 1 second; (3). P0.1/AD1 controls the adjustment of "points", each press adds 1 point; (4). P0.2/AD2 controls the adjustment of "hour", each press adds 1 hour; 2. Circuit schemat
[Microcontroller]
Design of 89C52 microcontroller electronic clock
MCU production of light cube data (based on 74hc595 and 573)
    Friends who need information about the light cube, I will put it here for your convenience. You can take it if you need it.     It requires a certain electronic foundation, and considerable hands-on practical skills and perseverance. It is not difficult to do, but more patience is needed. If you don’t even know ho
[Microcontroller]
MCU production of light cube data (based on 74hc595 and 573)
Is the anti-interference ability of PIC microcontroller stronger or that of AVR microcontroller?
The anti-interference performance of single-chip microcomputers has always been valued by everyone. There are about ten single-chip microcomputers on the market that I have come into contact with, including Samsung and Hyundai from South Korea; Mitsubishi, Hitachi, Toshiba, Fujitsu, NEC from Japan; EMC, Songhan, McInt
[Microcontroller]
Introduction to 51 MCU Special Function Registers (SFR)
1. Introduction to 21 registers         There are four main functional modules inside the 51 series microcontroller, namely I/O port module, interrupt module, timer module and serial communication module (serial I/O port). Its structure and function are shown in the figure below:   Figure 1 51 single-chip micr
[Microcontroller]
Introduction to 51 MCU Special Function Registers (SFR)
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号