The simulation circuit diagram is as follows
The c program is as follows:
#include
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P3^7; //ds18b20 and microcontroller connection port
sbit RS=P3^0;
sbit RW=P3^1;
sbit EN=P3^2;
unsigned char co
unsigned char co
uchar data
uint tvalue; //temperature value
uchar tflag; //temperature positive and negative flag
/*************************lcd1602 program******************************/
void delay1ms(unsigned int ms)//Delay 1 millisecond (not precise enough)
{unsigned int i,j;
for(i=0;i
}
void wr_com(unsigned char com)// 写电影//
{ delay1ms(1);
RS=0;
RW=0;
EN=0;
P2=how;
delay1ms(1);
EN=1;
delay1ms(1);
EN=0;
}
void wr_dat(unsigned char dat)//写数据//
{ delay1ms(1);;
RS=1;
RW=0;
EN=0;
P2=dat;
delay1ms(1);
EN=1;
delay1ms(1);
EN=0;
}
void lcd_init()//initialization settings//
{delay1ms(15);
wr_com(0x38);delay1ms(5);
wr_com(0x08);delay1ms(5);
wr_com(0x01);delay1ms(5);
wr_com(0x06);delay1ms(5);
wr_com(0x0c);delay1ms(5);
}
void display(unsigned char *p)//显示//
{
while(*p!='\0')
{
wr_dat(*p);
p++;
delay1ms(1);
}
}
init_play()//Initialize display
{ lcd_init();
wr_com(0x80);
display(str1);
wr_com(0xc0);
display(str2);
}
/******************************ds1820 program*******************************************/
void delay_18B20(unsigned int i)//delay 1 microsecond
{
while(i--);
}
void ds1820rst()/*ds1820 reset*/
{ unsigned char x=0;
DQ = 1; //DQ reset
delay_18B20(4); //delay
DQ = 0; //DQ pull low
delay_18B20(100); //accurate delay greater than 480us
DQ = 1; //pull high
delay_18B20(40);
}
uchar ds1820rd()/*read data*/
{ 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_18B20(10);
}
return(dat);
}
void ds1820wr(uchar wdata)/*写数据*/
{unsigned char i=0;
for (i=8; i>0; i--)
{ DQ = 0;
DQ = wdata&0x01;
delay_18B20(10);
DQ = 1;
wdata>>=1;
}
}
read_temp()/*read temperature value and convert*/
{uchar a,b;
ds1820rst();
ds1820wr(0xcc);//*skip reading serial number*/
ds1820wr(0x44);//*start temperature conversion*/
ds1820rst();
ds1820wr(0xcc);//*skip reading serial number*/
ds1820wr(0xbe);//*read temperature*/
a=ds1820rd();
b=ds1820rd();
tvalue=b;
tvalue<<=8;
tvalue=tvalue|a;
if(tvalue<0x0fff)
tflag=0;
else
{tvalue=~tvalue+1;
tflag=1;
}
tvalue=tvalue*(0.625);//expand temperature value 10 times, accurate to 1 decimal place
return(tvalue);
}
/*******************************************************************/
void ds1820disp()//temperature value display
{ uchar flagdat;
disdata[0]=tvalue/1000+0x30;//hundreds digit
disdata[1]=tvalue%1000/100+0x30;//ten digit
disdata[2]=tvalue%100/10+0x30;//ones digit
disdata[3]=tvalue%10+0x30;//decimal digitif
(tflag==0)
flagdat=0x20;//positive temperature does not display a signelse
flagdat
=0x2d;//negative temperature displays a minus sign:-
if(disdata[0]==0x30)
{disdata[0]=0x20; //If the hundreds digit is 0, it will not be displayed
if(disdata[1]==0x30)
{disdata[1]=0x20; //If the hundreds digit is 0, the tens digit will not be displayed either
}
}
wr_com(0xc0);
wr_dat(flagdat);//display the sign bit
wr_com(0xc1);
wr_dat(disdata[0]);//display the hundreds digit
wr_com(0xc2);
wr_dat(disdata[1]);//display the tens digit
wr_com(0xc3);
wr_dat(disdata[2]);//display the ones digit
wr_com(0xc4);
wr_dat(0x2e);//display the decimal point
wr_com(0xc5);
wr_dat(disdata[3]);//display the decimal place
}
/********************Main program***********************************/
void main()
{ init_play();//initialize the display
while(1)
{read_temp();//read the temperature
ds1820disp();//display
}
}
Previous article:Based on the single chip microcomputer 89C51 to generate PWM signal to control the DC motor speed regulation program
Next article:Usage of sbit sfr in c51 programming
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- RT-Thread device framework learning RTC device
- Orcad learning notes (three) class and sub class in PCB
- [Hua Diao Experience] 17 Beetle ESP32C3 and WS2812 screen music visualization rhythm light
- EEWORLD University----42/5000 Power over Ethernet (PoE) Training Series
- How to effectively program a microcontroller active buzzer driver
- MSP430 SPI reads AFE4400 register value code
- WPG Live Broadcast Registration | Thundercomm, Lianda, Qualcomm IOT Platform Solutions and Success Stories
- The rain is a surprise in spring, the valley is clear, the summer is full of grains, and the summer heat is connected
- [Anxinke NB-IoT Development Board EC-01F-Kit] 4. Serial port MQTT networking and information sending and receiving test
- Microchip Live FAQ|ADAS Platform Root of Trust