DS18B20 and the microcontroller use a single bus connection. The file used in the competition is the provided "onewire.c"
Programming steps:
(1) Initialization
(2) Send ROM operation commands, including: Read ROM [33H], Match ROM [55H], Skip ROM [CCH], Search ROM [F0H], Alarm search [ECH]
(3) Send memory operation commands, including: Write Scratchpad (write temporary storage memory) [4EH], Read Scratchpad (read temporary storage memory) [BEH], Copy Scratchpad (copy temporary storage memory) [48H], Convert Temperature (temperature conversion) [44H], Recall EPROM (recall) [B8H], Read Power supply (read power supply) [B4H]
(4) Memory or appropriate delay
PS: When using IAP15, make the following changes to void Delay_OneWire(unsigned int t) in onewire.c:
// Single bus delay function
void Delay_OneWire(unsigned int t)
{
unsigned char i;
while(t--){
for(i=0;i<12;i++);
}
}
Multiply the previous delay time by 12, because the machine cycle of the IAP15 used is the oscillation cycle, and it cannot be read if it is not modified.
Attachment: Code for measuring temperature
uchar low,high;
int gettemper()
{
int t;
Init_DS18B20(); //1. Initialization timing
Write_DS18B20(OW_SKIP_ROM); //2. ROM operation command
Write_DS18B20(DS18B20_CONVERT); //3. Memory operation command
Delay_OneWire(200);
//end
Init_DS18B20(); //1. Initialization timing
Write_DS18B20(OW_SKIP_ROM); //2. ROM operation command
Write_DS18B20(DS18B20_READ); //3. Memory operation command
EA = 0;
low = Read_DS18B20();
high = Read_DS18B20();
EA = 1;
t = high<<8;
t = t|low;
return t;
// return 0xf13c; //test
}
Does this function return the exact data in the DS18B20 temperature register? If you want to choose to return an integer value or a floating point value, data processing is required.
The delay 2 value (200) cannot be changed.
Appendix: Data processing functions
float get_f_temp()
{
int t;
float temp;
t = gettemper();
if((t&0xf000)>>12 == 0x000f) //negative number
temp = ((~t)+1)*0.0625*(-1.0);
else
temp = t*0.0625;
return temp;
}
Note: When the temperature read is negative, the highest bit returned by gettemper() is 1. In this case, you need to invert the returned value, add 1, and finally multiply it by the 12-bit precision of 0.0625.
Previous article:【Blue Bridge Cup MCU Group】(3)Keyboard
Next article:【Blue Bridge Cup MCU Group】Real-time clock RTC (DS1302)
Recommended ReadingLatest update time:2024-11-16 11:45
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Single-chip microcomputer technology and application - electronic circuit design, simulation and production (edited by Zhou Runjing)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
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
- Leading domestic analog chip manufacturers are recruiting - a great opportunity to reach the pinnacle of life Location: Shanghai
- Op amp output calculation
- Upload two photosensitive detection circuits. The target detects different voltages according to different light exposure. Which one is correct?
- SIMterix-Simplis~8~
- ADI Award-winning Live Broadcast: Things about Inertial MEMS Applications
- MM32F103 BUG reminder to avoid pitfalls
- Topology Selection of Bidirectional DCDC Converter
- [Second Batch of Shortlist] 2022 Digi-Key Innovation Design Competition
- Will MakeCode support Python?
- What are the specific differences between triodes and MOS tubes?