#include
sbit DQ = P3^3; //define DS18B20 port DQ
sbit BEEP=P3^7 ;
unsigned char presence ;
unsigned char code LEDData[ ] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0xff};
unsigned char data temp_data[2] = {0x00,0x00};
unsigned char data display[5] = {0x00,0x00,0x00,0x00,0x00};
unsigned char code ditab[16] = {0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,
0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
void beep();
sbit DIN = P0^7; //decimal point
bit flash=0; //display switch mark
/**********************************************************/
void Delay(unsigned int num)//delay function
{
while( --num );
}
/**********************************************************/
Init_DS18B20(void) // Initialize DS1820
{
DQ = 1; //DQ reset
Delay(8); //Slight delay
DQ = 0; //MCU pulls DQ low
Delay(90); //Precise delay greater than 480us
DQ = 1; //Pull the bus high
Delay(8);
presence = DQ; //If = 0, initialization is successful, = 1, initialization fails
Delay(100);
DQ = 1;
return(presence); //Return signal, 0=presence, 1= no presence
}
/**********************************************************/
ReadOneChar(void) //Read one byte
{
unsigned char i = 0;
unsigned char dat = 0;
for (i = 8; i > 0; i--)
{
DQ = 0; // Give pulse signal
that >>= 1;
DQ = 1; // Give pulse signal
if(DQ)
that |= 0x80;
Delay(4);
}
return (that);
}
/**********************************************************/
WriteOneChar(unsigned char dat) //write one byte
{
unsigned char i = 0;
for (i = 8; i > 0; i--)
{
DQ = 0;
DQ = dat&0x01;
Delay(5);
DQ = 1;
that>>=1;
}
}
/*********************************************************/
Read_Temperature(void) //Read temperature
{
Init_DS18B20();
if(presence==1)
{ beep(); flash=1;} //DS18B20 is abnormal, buzzer alarm
else
{
flash=0;
WriteOneChar(0xCC); // Skip reading the serial number and column number
WriteOneChar(0x44); // Start temperature conversion
Init_DS18B20();
WriteOneChar(0xCC); //Skip the operation of reading the serial number and column number
WriteOneChar(0xBE); //Read temperature register
temp_data[0] = ReadOneChar(); //lower 8 bits of temperature
temp_data[1] = ReadOneChar(); //high 8 bits of temperature
}
}
/*********************************************************/
Disp_Temperature() //Display temperature
{
unsigned char n=0;
display[4]=temp_data[0]&0x0f;
display[0]=ditab[display[4]]; //Look up the table to get the decimal value
display[4]=((temp_data[0]&0xf0)>>4)|((temp_data[1]&0x0f)<<4);
display[3]=display[4]/100;
display[1]=display[4]%100;
display[2]=display[1]/10;
display[1]=display[1]%10;
if(!display[3]) //high bit is 0, not displayed
{
display[3]=0x0a;
if(!display[2]) //The second highest bit is 0, not displayed
display[2]=0x0a;
}
P0 = 0xc6; //Display C
P2 = 0x7f;
Delay(300);
P0 = 0x9c; //display °
P2 = 0xbf;
Delay(300);
P0 =LEDData[display[0]]; //Display decimal places
P2 = 0xdf;
Delay(300);
P0 =LEDData[display[1]]; //Display the unit digit
DIN = 0;
P2 = 0xef;
Delay(300);
P0 =LEDData[display[2]]; //Display the tens digit
P2 = 0xf7;
Delay(300);
P0 =LEDData[display[3]]; //Display the tens digit
P2 = 0xfb;
Delay(300);
P2 = 0xff; // Turn off display
}
/*********************************************************/
void beep()
{
unsigned char i;
for (i=0;i<100;i++)
{
Delay(60);
BEEP=!BEEP; //BEEP取反
}
BEEP=1; //Turn off the buzzer
}
/*********************************************************/
void main(void)
{
while(1)
{
Read_Temperature();
if(flash==0)
{Disp_Temperature();}
else P2 = 0xff ; //DS18B20 is abnormal, turn off the display
}
}
Previous article:Generation and use of static library in Keil C51
Next article:Make a calculator with a single chip microcomputer
Recommended ReadingLatest update time:2024-11-17 07:41
- 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!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- 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
- Silicon Labs EFM32PG22 Development Kit enables you to create whatever you want! Application in progress!
- 7. Common Collector Amplifier Circuit (Emitter Follower)
- Verilog state machine programming skills
- Review summary: Qinheng RISC-V core BLE 5.3 wireless MCU CH582
- MSP-EXP430F5529LP Development Board 002-GPIO Library Function
- Lederui specializes in Type-C interface and HOST solution for laptops/tablets
- Please help me take a look at this strange MICRO BIT board
- Free simulation software SimulIDE
- Many branches of MicroPython
- Choosing Machine Learning Processors at the Edge