#define uchar unsigned char
#define uint unsigned int
sbit DQ=P2^7; //define interface
uint temp; // variable of temperature defines a variable
uchar flag1; // sign of the result positive or negative Define
a flag to indicate whether the temperature is still positive
sbit P2_0=P2^0; //digital tube bit selection
sbit P2_1=P2^1;
sbit P2_2=P2^2;
unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82, 0xf8,0x80,0x90}; //digital encoding
unsigned char code table1[]={0x40,0x79,0x24,0x30,
0x19,0x12,0x02, 0x78,0x00,0x10}; //Code with decimal point
void delay(uint i) //delay delay subroutine
{
while(i--);
}
/*******************************************************************/
/* Initialize ds18b2 sub-function* */
/*******************************************************************/
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ reset
delay(8); //Slight delay
DQ = 0; //MCU pulls DQ low
delay(80); //Precise delay 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 byte sub-function */
/*******************************************************************/
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // Give pulse signal
dat>>=1;
DQ = 1; // Give pulse signal
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
/************************************************************************/
/* Write byte sub-function */
/************************************************************************/
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;
}
}
/*void tmpwritebyte(uchar dat) //write a byte to ds18b20 Write a byte to the temperature sensor
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //write 1
{
DQ=0;
i++;i++;
DQ=1;
i=8;while(i>0)i--;
}
else
{
DQ=0; //write 0
i=8;while(i>0)i--;
DQ=1;
i++;i++;
}
}
}*/
void tmpchange(void) //DS18B20 begin change send temperature conversion command
{
Init_DS18B20(); //Initialize DS18B20
delay(200); //Delay
WriteOneChar(0xcc); //Skip serial number command
WriteOneChar(0x44); //Send temperature conversion command
}
uint tmp() //get the temperature Get the temperature value
{
float tt;
uchar a,b;
Init_DS18B20();
delay(1);
WriteOneChar(0xcc);
WriteOneChar(0xbe); //Send the read data command
a=ReadOneChar(); //Continuously read two bytes of data
b=ReadOneChar();
temp=b;
temp<<=8;
temp=temp|a; //Two bytes are combined into an integer variable.
tt=temp*0.0625; //Get the true decimal temperature value, because DS18B20
//can be accurate to 0.0625 degrees, so the lowest bit of the read data represents
//0.0625 degrees.
temp=tt*10+0.5; //Magnify ten times, the purpose of doing this is to convert the first digit after the decimal point
//to a displayable number, and perform a rounding operation at the same time.
return temp; //Return the temperature value
}
/*void readrom() //read the serial Read the serial number of the temperature sensor
{ //This function is not used in this program
uchar sn1, sn2;
Init_DS18B20();
delay(1);
WriteOneChar(0x33);
sn1=ReadOneChar();
sn2=ReadOneChar();
}*/
void delay10ms() //delay 10MS sub-function
{
uchar a,b;
for(a=50;a>0;a--)
for(b=60;b>0;b--);
}
void display(uint tem) //display display sub-function
{
uchar A1,A2,A2t,A3;
if(tem>=100)
{
A1=table[tem/100];
A2t=tem%100;
A2=table1[A2t/10];
A3=table[A2t%10];
}
else
{
A2t=tem%100;
A1=table1[A2t/10];
A2=table[A2t%10];
A3=0xFF;
}
P0=A1;
P2_0=0;
P2_1=1;
P2_2=1;
delay10ms();
P0=A2;
P2_1=0;
P2_0=1;
P2_2=1;
delay10ms();
if(A3!=0xFF)
{
P0=A3;
P2_2=0;
P2_0=1;
P2_1=1;
}
}
void main() //Main function
{
do
{
tmpchange(); //Temperature conversion,
display(tmp()); //Display temperature value
}
while(1);
}
Previous article:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 6)
Next article:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 4)
Recommended ReadingLatest update time:2024-11-17 00:19
- 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
- Answers to some questions raised in the review of Espressif's ESP32-S2-Kaluga-1 development board
- ST MEMS Device Resource Library-Device Application Guide
- Qinheng CH4XX series technical manual and sample program download
- Kaige found a Guoxin product at the Expo that uses RISC-V to replace TI DSP. Are you interested in reviewing it?
- What the f*ck Python!
- MSP430FR5964 program download problem
- 【TouchGFX Design】+Simple solution to previous problems+
- Anxinke PB-02 module evaluation (2) --- PHY62XX architecture introduction && design task introduction
- Migrate from M2M to IIoT
- What is your answer to the Huawei interview question that is trending on the Internet?