#include "reg51.h"
char disp[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
char disp_dot[11]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xc0};
sbit DQ = P1^3; //define communication port
//晶振12MHz
void delay_18B20(unsigned int i)
{
while(i--);
}
//Init function
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ reset
delay_18B20(8); //Slight delay
DQ = 0; // MCU pulls DQ low
delay_18B20(80); //Precise delay greater than 480us
DQ = 1; //Pull the bus high
delay_18B20(14);
x=DQ; //After a slight delay, if x=0, initialization is successful, if x=1, initialization fails
delay_18B20(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_18B20(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_18B20(5);
DQ = 1;
dat>>=1;
}
}
//Read temperatureReadTemperature
(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0x CC ); // Skip reading serial number and column
numberWriteOneChar(0x44); // Start temperature conversion
delay_18B20(100);
Init_DS18B20();
WriteOneChar(0xCC); // Skip reading serial number and column numberWriteOneChar
(0xBE); //Read temperature registers, etc. (9 registers can be read in total) The first two are temperaturea
=ReadOneChar();
b=ReadOneChar();
// Sensor return value divided by 16 to get the actual temperature value
//In order to get 2 decimal places, multiply by 100 first, then divide by 16. Considering the length of integer data,
//After processing, multiply by 25 first, then divide by 4. Divide by 4 with right shift
t = (b*256+a)*25;
return( t >> 2 );
}
main()
{
unsigned int tmp;
unsigned char counter;
while(1)
{
//The temperature measurement frequency does not need to be too high. Too high a frequency will affect the digital display
. //So use a counter to control it
if(counter-- == 0)
{
tmp = ReadTemperature();
counter = 20;
}
P2 = 0xff;
P0 = disp[tmp%10];
P2 = 0xfb;
delay_18B20(1000);
P2 = 0xff;
P0 = disp[tmp/10%10];
P2 = 0xf7;
delay_18B20(1000);
P2 = 0xff;
P0 = disp_dot[tmp/100%10];
P2 = 0xef;
delay_18B20(1000);
P2 = 0xff;
P0 = disp[tmp/1000%10];
P2 = 0xdf;
delay_18B20(1000);
}
}
Previous article:Using the MCU's serial port to drive 74LS165
Next article:89C51 MCU Programming Procedure
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
- Operational amplifier stability analysis problem
- 【NXP Rapid IoT Review】+ My Review Summary
- VHDL Introduction. Questions and Answers. Classic Examples. Experience Summary
- ROM and RAM How does the microcontroller execute step by step after power is turned on?
- How does the program execute from the reset sequence to the main function?
- [AutoChips AC7801x motor demo board review] + Motor drive - BLDC sensorless method
- EEWORLD University ---- Practical Projects for IoT Embedded Engineers
- AC 220v to DC 24v (LM7824CT)
- Today's live broadcast! How to design high-performance servo drives using Infineon IGBT7
- STM32MP157A-DK1 Evaluation + HelloWorld and GPIO (2)