sbit DQ=P3^7; //DS18B20 data transmission line connects to the corresponding pin of the microcontroller
void delaymm(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//Read one byte of data from DS18B20, starting from the low bit
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat=0;
for (i=8;i>0;i--)
{
DQ=1;
delay(1);
DQ=0;
dat>>=1; //Shift the read value to the left by one bit to form a byte
DQ=1;
if(DQ)
dat|=0x80; //If the highest DQ is 1, the highest position is 1
delay(4);
}
return(that);
}
//Write one byte of data to DS18B20, starting from the lowest bit
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01; //If the lowest bit is 1, write 1
delay(5);
DQ=1;
dat>>=1; //Shift left by one bit, for example, i = 8, after comparison, it is consistent, i = 7 is the first shift left by one bit, so that the second bit is compared in the next cycle & 0X01
}
delay(4);
}
void Init_DS18B20(void)/*Function: DS18B20 initialization subroutine*/
{
unsigned char x=0;
DQ=1; //DQ is set high first
delay(8); //delay
DQ=0; //Send reset pulse
delay(85); //delay (>480ms)
DQ=1; //Pull up the data line
delay(14); //wait (15~60ms)
}
//Function: Read temperature value from DS18B20
ReadTemperature(void)
{
Init_DS18B20(); //Initialization
WriteOneChar(0xcc); //Skip the operation of reading the serial number
WriteOneChar(0x44); //Start temperature conversion
delaymm(125); //The conversion takes a little time, delay
Init_DS18B20(); //Initialization
WriteOneChar(0xcc); //Skip the operation of reading the serial number
WriteOneChar(0xbe); //Read the temperature register (the first two values are the low and high bits of the temperature respectively)
delaymm(125);
tempL=ReadOneChar(); //Read the low LSB of the temperature
tempH=ReadOneChar(); //Read the high MSB of temperature
//Temperature conversion, convert the high and low bits into actual temperature
temperature=((tempH*256)+tempL)*0.0625;
delay(200);
return(temperature);
}
Previous article:A little knowledge about MCU
Next article:Serial communication uses parity check to send data
Recommended ReadingLatest update time:2024-11-16 13:23
- Popular Resources
- Popular amplifiers
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- Modern Product Design Guide
- 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
- Does anyone want to buy the new DY-FFTB6638 Deyan Electronics MSP430 development kit?
- Boost circuit
- What is the difference between the underlying media photons of 50 Hz and 10,000 Hz electromagnetic waves transmitted in a wire?
- Domestic 32-bit MCU compatibility test non-authoritative report - AT32F403 compatibility test
- A young man uses stones to extract silicon to make chips, claiming to "solve" the chip crisis in 99 seconds
- Learn about vector network analysis Smith chart and filter bandwidth measurement!
- Answer a question to win a Kindle, "New Concept Analog Circuit": Browse ADI IoT reference designs together
- TI CCS & controlSUITE Troubleshooting Log
- RF Microwave Circuit 01
- 【Mil MYS-8MMX】Part 5: Download music online