sbit DQ =P3^7; //define communication port
//Delay function
void delay(unsigned int i)
{
while(i--);
}
//Init function
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ reset
delay(8); //Slight delay
DQ = 0; //MCU pulls DQ down
delay(80); //Precise delay is greater than 480us
DQ = 1; //Pull the bus high
delay(14);
x=DQ; //After a slight delay, if x=0, initialization is successful, if x=1, initialization fails
delay(20);
}
//Read a
byteReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; //Give pulse signaldat
>>=1;
DQ = 1; //Give pulse signalif
(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 temperatureReadTemperature
(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); //Skip the operation of reading the serial number and column
numberWriteOneChar(0x44); //Start temperature conversionInit_DS18B20
();
WriteOneChar(0xCC); //Skip the operation of reading the serial number and column
numberWriteOneChar(0xBE); //Read temperature registers, etc. (9 registers can be read in total) The first two are temperaturesa
=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625; //Merge the high and low bits of the temperaturet
= tt*10+0.5; //Round the result
return(t);
}
//Digital codes from 0 to 9 respectively. Note that this code is only suitable for the QQ microcontroller experiment board. For codes on other circuit boards, please refer to the above algorithm to calculate them yourself.
unsigned char code Num[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
//QQ MCU private function, may not work well on other circuit boards
//Display any characters on 8 digital tubes, the character content is determined by 8 parameters LED1--LED8 If the displayed character is empty, this digital tube will not be scanned to shorten the scanning time
ShowAny(unsigned char LED1,unsigned char LED2,unsigned char LED3,unsigned char LED4,unsigned char LED5,unsigned char LED6,unsigned char LED7,unsigned char LED8)
{
if(LED1) { P2=0xEF; P0=LED1; delay(100); }
if(LED2) { P2=0xDF; P0=LED2; delay(100); }
if(LED3) { P2=0xBF; P0=LED3; delay(100); }
if(LED4) { P2=0x7F; P0=LED4; delay(100); }
if(LED5) { P2=0xFE; P0=LED5; delay(100); }
if(LED6) { P2=0xFD; P0=LED6; delay(500); }
if(LED7) { P2=0xFB; P0=LED7; delay(500); }
if
(LED8) { P2=0xF7;
main()
{
unsigned int i=0;
while(1)
{
i=ReadTemperature(); //Read the current temperature
ShowAny(0,0,0,0,0,Num[i/100],Num[i/10%10] | 0x80,Num[i%10]); //Show the current temperature
}
}
Previous article:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 4)
Next article:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 2)
Recommended ReadingLatest update time:2024-11-16 16:17
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications