Microcontroller tutorial and learning LCD1602 display DS18B20 temperature experiment

Publisher:GoldenSunriseLatest update time:2015-09-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#include
typedef  unsigned int uint;
typedef unsigned char  uchar;

sbit LCD_RS=P2^0;
sbit LCD_RW=P2^1;
sbit LCD_EN=P2^2;
sbit DQ=P3^4;
uchar Temp_Value[]={0x00,0x00};
uchar Temp=0;
uchar Display_Digit[]={0,0,0,0};
bit DS18B20_IS_OK=1;
uchar code df_tab[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};//decimal fraction
uchar code Display_LINE0[]={" Current Temp:"};
uchar Display_LINE1[]={" Temp:          "};


void _delay_ms(unsigned int x)
{
 unsigned char i;
 while(x--)
 {
  for(i=0;i<125;i++);
 }
}

void _delay_us(uint x)
{
 while(--x);
}

bit LCD_Busy(void)//busy detection
{
 bit LCD_Status;//return value variable
 LCD_RS=0;//read status
 LCD_RW=1;
 LCD_EN=1;
 _nop_();_nop_();_nop_();_nop_();
 LCD_Status=(bit)(P0&0x80);
 LCD_EN=0;
 return LCD_Status;
}

void LCD_Write_Command(uchar cmd)//写指令
{
 while(LCD_Busy());
 LCD_RS=0;//
 LCD_RW=0;
 LCD_EN=0;
 _nop_();_nop_();
 P0=cmd;
 _nop_();_nop_();_nop_();_nop_();
 LCD_EN=1;
 _nop_();_nop_();_nop_();_nop_();
 LCD_EN=0;
 
}

void LCD_Write_Data(uchar dat)//write data
{
 while(LCD_Busy());//Busy signal needs to be detected before each data writing operation
 LCD_RS=1;
 LCD_RW=0;
 LCD_EN=0;
 P0=dat;
 _nop_();_nop_();_nop_();_nop_();
 LCD_EN=1;
 _nop_();_nop_();_nop_();_nop_();
 LCD_EN=0;
 
}

void Init_LCD(void)//LCD initialization
{
 _delay_ms(15);//15MS delay
 LCD_Write_Command(0x38);
 _delay_ms(5); 
 LCD_Write_Command(0x38);
 _delay_ms(5); 
 LCD_Write_Command(0x38);//Busy signal needs to be detected before each write command operation
 while(LCD_Busy()); 
 _delay_ms(5);
 LCD_Write_Command(0x01);//Clear screen
 while(LCD_Busy());
 _delay_ms(5);
 LCD_Write_Command(0x38);//Set 16*2 display, 5*7 dot matrix, 8-bit data interface
 _delay_ms(5);
 while(LCD_Busy());
 LCD_Write_Command(0x0c);//Turn on display, no cursor
 _delay_ms(5);
 while(LCD_Busy());
 LCD_Write_Command(0x06); //After reading or writing a character, the address pointer increases by one, and the cursor increases by one
 }

void LCD_POS(uchar pos)//character display position
{
 LCD_Write_Command(0x80|pos);
}

void Show_String(uchar *str)//显示字符串
{
 while(*str!='')
 LCD_Write_Data(*str++);
}
uchar DS18B20_Init(void)
{
 uchar status;
 DQ=1;
 _delay_us(10);
 DQ=0;
 _delay_us(90);
 DQ=1;
 _delay_us(8);
 status=DQ;
 _delay_us(100);
 DQ=1;
 return status;
}
uchar Read_One_Byte(void)
{
 uchar i,dat=0;
 DQ=1;
 _nop_();
 for(i=8;i>0;i--)
 {
  DQ=0;
  dat>>=1;
  DQ=1;
  _nop_();_nop_();
  if(DQ)
   dat|=0x80;
  _delay_us(30);
  DQ=1;
 }
 return dat;
}
void Write_One_Byte(uchar dat)
{
 uchar i;
 for(i=8;i>0;i--)
 {
  DQ=0;
  DQ=dat&0x01;
  _delay_us(5);
  DQ=1;
  dat>>=1;
 }
}

void Read_Temp(void)
{
 if(DS18B20_Init()==1)
  DS18B20_IS_OK=0;
 else
 {
  Write_One_Byte(0xcc);
  Write_One_Byte(0x44);
  DS18B20_Init();
  Write_One_Byte(0xcc);
  Write_One_Byte(0xbe);
  Temp_Value[0]=Read_One_Byte();
  Temp_Value[1]=Read_One_Byte();
  DS18B20_IS_OK=1;
 }
}
void Display_Temperature(void)
{
 uchar ng=0;
 if((Temp_Value[1]&0xf8)==0xf8)
 {
  Temp_Value[1]=~Temp_Value[1];
  Temp_Value[0]=~Temp_Value[0]+1;
  if(Temp_Value[0]==0x00)
  Temp_Value[1]++;
  ng=1;
 }
 Display_Digit[0]=df_tab[Temp_Value[0]&0x0f];
 Temp=((Temp_Value[0]&0xf0)>>4)|((Temp_Value[1]&0x07)<<4);
 Display_Digit[3]=Temp/100;
 Display_Digit[2]=Temp0/10;
 Display_Digit[1]=Temp;
 Display_LINE1[13]=0x43;
 Display_LINE1[12]=0xdf;
 Display_LINE1[11]=Display_Digit[0]+'0';
 Display_LINE1[10]='.';
 Display_LINE1[9]=Display_Digit[1]+'0';
 Display_LINE1[8]=Display_Digit[2]+'0';
 Display_LINE1[7]=Display_Digit[3]+'0';
 if(Display_Digit[3]==0)
  Display_LINE1[7]=' ';
 if(Display_Digit[2]==0&&Display_Digit[3]==0)
  Display_LINE1[8]=' ';
 if(ng)
 {
  if(Display_LINE1[8]==' ')
   Display_LINE1[8]='-';
  else if(Display_LINE1[7]==' ')
   Display_LINE1[7]='-';
  else
   Display_LINE1[6]='-';
 }
 LCD_POS(0);
 Show_String(Display_LINE0);
 LCD_POS(0x40);
 Show_String(Display_LINE1);
}

void main(void)
{
 Init_LCD();
 Read_Temp();
 _delay_ms(1000);
 while(1)
 {
  Read_Temp();
  if(DS18B20_IS_OK)
   Display_Temperature();
  _delay_ms(200);
 }
}

Reference address:Microcontroller tutorial and learning LCD1602 display DS18B20 temperature experiment

Previous article:Digital tube display based on 89C52 and DT9122D infrared remote control
Next article:NRF24L01 receiving program (MCU is STC89C52)

Recommended ReadingLatest update time:2024-11-16 14:33

Single chip microcomputer learning - LCD1602 display experiment 2
Title: Display on LCD1602 display screen meets the following conditions:                   1. Display "000-999 CLOCK" on the first line                   2. Display "BY XIAO WU" at the end of the second line                   3. Use three digits at the beginning of the second line to display the stopwatch 000-999
[Microcontroller]
Design of temperature acquisition system based on STC89C52 microcontroller and DS18B20 temperature sensor
With the rapid development of modern information technology, data collection technology is also constantly developing and plays an important role in information technology. Whether data is collected timely and accurately and how the data is collected have become the focus of attention. With the continuous development
[Microcontroller]
Design of temperature acquisition system based on STC89C52 microcontroller and DS18B20 temperature sensor
PIC16F877A Example --- DS18B20
#include #include pic1687x.h __CONFIG(0x3F32); //Chip configuration word     #define LCDRS      RB2 #define LCDRW      RB1 #define LCDE        RB0 #define LCDDATA  PORTD     #define DS18B20   RE0                    #define TRIS_B20 TRISE0     void LCD1602_INIT(void);    void WRITE_LCD_CM
[Microcontroller]
Digital thermometer based on microcontroller and DS18B20
Temperature measurement starts from the thermal expansion and contraction of metal (material). Common detection methods include resistance type, thermocouple type, PN junction type, radiation type, fiber type and quartz resonance type. These detection methods are based on the principle that temperature changes cause
[Microcontroller]
Digital thermometer based on microcontroller and DS18B20
The principle and implementation of temperature measurement experiment based on DS18B20
The temperature measurement system composed of DS18B20 has a temperature measurement accuracy of 0.1 degrees. The temperature range is between -20 degrees and +50 degrees, and it is displayed with a 4-digit digital tube. DPY-1 experimental board connection Use the cable to connect JP-CODE to JP8, note: a con
[Industrial Control]
The principle and implementation of temperature measurement experiment based on DS18B20
STM32 MCU Learning (11) DS18B20 Temperature Sensor Experiment
This program mainly realizes the acquisition of DS18B20 temperature sensor data and transmits the temperature data to the computer using serial communication. Note: When using the Puzhong Technology Development Board for testing, you need to unplug the Boot1 socket because the PA15 pin is used. From the development bo
[Microcontroller]
Single chip temperature measurement system (AT89C51, DS18B20 temperature sensor, LCD1602)
1. Introduction This system is mainly composed of AT89C51, DS18B20 temperature module and LCD1602. The general principle is that the temperature data collected by DS18B20 is transmitted to P3.3/INT1 (external interrupt 1) of AT89C51, and finally the current real-time temperature is displayed through LCD1602. 2. Rend
[Microcontroller]
Single chip temperature measurement system (AT89C51, DS18B20 temperature sensor, LCD1602)
35. Use of DS18B20 digital thermometer
1. Basic knowledge of DS18B20   The DS18B20 digital thermometer is a 1-Wire device produced by DALLAS, which has the characteristics of simple circuit and small size. Therefore, it is used to form a temperature measurement system, which has simple circuit. Many such digital thermometers can be hung on one communicati
[Microcontroller]
35. Use of DS18B20 digital thermometer
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号