The boss gave me a temporary task, gave me an LPC213x board, and asked me to add a DS18B20 temperature sensor. I used to write upper-level applications.
I know nothing about ARM. I had no choice but to do it anyway. After several days of tuning, I finally finished it. Here is a hot screenshot:
The hexadecimal data "18 4B" you see is the value read from the temperature sensor register.
I will post some Chinese and English information about DS18B20 at the end of the article. Okay, let's get started.
1. A brief introduction to DS18B20
DS18B20 temperature reading function reference steps:
(1) DS18B20 starts conversion:
1.DS18B20 reset.
2. Write the byte command to skip ROM, 0xCC.
3. Write the function command to start conversion, 0x44.
4. Delay is about 750~900 milliseconds
(2) DS18B20 reads temporary data:
1.DS18B20 reset.
2. Write the byte command to skip ROM, 0xCC.
3. Write the function command to read the temporary storage, 0xee.
4. Read the 0th byte (LS Byte), the lower eight bits of the conversion result.
5. Read the first byte MS Byte and the high eight bits of the conversion result.
6.DS18B20 is reset, indicating that the temporary storage reading is completed.
(3) Calculate the hexadecimal value of the data:
1. Integrate LS Byte and MS Byte data
2. Determine the sign and get the integer part
3. Get the decimal part
In addition, there are the most important timings, such as reset, read, and write timings. I will try to explain them clearly with the code, one by one.
2. DS18B20 reset
The reset timing of DS18B20 is as follows:
1. The MCU pulls the bus down for 480us~950us, and then releases the bus (pull the level high).
2. At this time, DS18B20 will pull the signal low for about 60~240us to indicate a response.
3. DS18B20 pulls the level down between 60 and 240us, and the microcontroller reads the bus level. If it is a low level, it means the reset is successful.
4.DS18B20 will release the bus after pulling the level down for 60~240us.
My LPC213x board (I have to say a few more words here. For novice programmers like me, you must pay attention to different boards. The instructions are different, so the code cannot be copied at will. This is not acceptable) The corresponding reset code is as follows:
//DS18B20 reset function
void DS18B20_Reset()
{
//IO0CLR // out 0
//IO0SET // out 1
//IO0DIR //Direction
//IO0PIN //read IO status 0/1
IO0DIR|=DQ; //DQ is output status
IO0CLR|=DQ; //output low level
Delay_1us(600); //delay 600 microseconds
IO0SET|=DQ; //Release the bus and pull the level high
Delay_1us(30); //Delay 30 microseconds
IO0DIR|=DQ; //DQ bit output status
Delay_1us(240); //Delay 240 microseconds
if((IO0PIN&DQ) != 0){ //Wait for slave DS18B20 to respond (low level is valid)
IO0SET|=DQ; //Release bus
}
}
The code comments are very detailed, so I won’t go into details here.
3. Write timing
The steps to write logic 0 to DS18B20 are as follows:
(1) The microcontroller pulls the level down for about 10~15us
(2) The MCU continues to pull the level down for about 20 to 45 us
(3) Release the bus
The steps to write logic 1 to DS18B20 are as follows:
(1) The microcontroller pulls the level down for about 10~15us
(2) The MCU continues to pull the level high for about 20~45us
(3) Release the bus
The corresponding code:
//DS18B20 write byte function
void DS18B20_Write(unsigned char Data)
{
unsigned char i;
IO0DIR|=DQ; //DQ is output
for(i=0;i<8;i++)
{
IO0CLR|=DQ; //Pull the bus low
Delay_1us(10); //Delay 10 microseconds (maximum 15 microseconds)
if(Data&0x01)
IO0SET|=DQ;
else
IO0CLR|=DQ;
Delay_1us(40); //Delay 40 microseconds
IO0SET|=DQ; //Release the bus
Delay_1us(1); //slight delay
Data>>=1;
}
}
4. Reading Timing
The steps to read logic 0 of DS18B20 are as follows:
1. When reading, the microcontroller pulls the level down for about 1us
2. The microcontroller releases the bus and then reads the bus level.
3. At this time, DS18B20 will pull the level down.
4. After reading the level, the delay is about 40~45 microseconds
The steps for DS18B20 to read logic 1 are as follows:
1. When reading, the microcontroller pulls the level down for about 1us
2. The microcontroller releases the bus and then reads the bus level.
3. At this time, DS18B20 will pull the level high.
4. After reading the level, the delay is about 40~45 microseconds
The corresponding code is as follows:
//DS18B20 read byte function
unsigned char DS18B20_Read()
{
unsigned char i,Data;
for(i=0;i<8;i++)
{
Data>>=1; //Data right shift
IO0DIR|=DQ; //DQ is output status
IO0CLR|=DQ; //Pull the bus low to start input
Delay_1us(1);
IO0SET|=DQ; //Release the bus
IO0DIR&=(~DQ); //DQ is input status
if(IO0PIN&DQ)
Data|=0x80;
Delay_1us(45); //Delay 45 microseconds
}
return Data;
}
5. The real total function of reading temperature
/*************** Read temperature function***********************
Data example:
Tem[0]=0x1a, Tem[1]=0x32, then it is positive 26.50 degrees Celsius
Tem[0]=0xb7, Tem[1]=0x4b, which is -55.75 degrees Celsius
*****************************************************/
void Read_Temperature(unsigned char *Tem)
{
unsigned int Temp1,Temp2;
DS18B20_Reset(); //DS18B20 reset
DS18B20_Write(0xCC); //skip ROM
DS18B20_Write(0x44); //Temperature conversion
DS18B20_Reset(); //DS18B20 reset
DS18B20_Write(0xCC); //skip ROM
DS18B20_Write(0xBE); //Read RAM
Temp1=DS18B20_Read(); //Read lower eight bits, LS Byte, RAM0
Temp2=DS18B20_Read(); //Read high eight bits, MS Byte, RAM1
DS18B20_Reset(); //DS18B20 reset, indicating the end of reading
Convert_Data(Tem,Temp1,Temp2); //For example:Tem[0]=0x19,Tem[1]=0x00, the temperature value is 25.00 degrees
}
We are almost done here. Temp1 and Temp2 are used to read the values in the registers. You may have noticed the Convert_Data function. This function has little to do with our project. Its main function is to convert the two bytes read into readable hexadecimal. You will know the importance of this after reading the data sheet. Let's go straight to the code. This is just a reference for the conversion.
/******************************************************************************
Convert the 16-bit data from the temperature sensor register into char[] and save it;
temp_str is the storage destination array, temp1 is the low-order register data, and temp2 is the high-order data;
Store the integer part (including the sign bit, the highest bit is 1 if it is negative, and 0 if it is positive) in temp_str[0];
Store the read (2-digit) decimal part (excluding the decimal point) in temp_str[1];
The conversion error is about 0.25 degrees Celsius;
*******************************************************************************/
void Convert_Data(unsigned char *temp_str,unsigned int temp1,unsigned int temp2)
{
unsigned int Temp;
unsigned int Integer_tem; //Save the integer part
unsigned int Decimal_tem; //Save the decimal part
/*Determine the sign bit*/
if(temp2 & 0xF8){ //is a negative number
Temp = ((temp2<<8)|temp1); //Merge high 8 bits and low 8 bits
Temp = ((~Temp)+1); // invert and add 1
Temp = (Temp & 0x0000ffff); //Restore the first 16 bits of 32 bits
/* Get the integer part */
Integer_tem = Temp;
Integer_tem = (Integer_tem & 0x000007F0); //Get a 7-bit integer
Integer_tem = (Integer_tem | 0x00000800); //Set the highest position to 1 (negative)
Integer_tem = (Integer_tem>>4); //Get 8 digits
/* Get the decimal part */
Decimal_tem = Temp;
Decimal_tem = (Decimal_tem & 0x0000000c); // Get two decimal places
if(Decimal_tem == 0x00000000){//The decimal is 0.00
Decimal_tem = 0x00;
}else if (Decimal_tem == 0x00000004 ){//The decimal is 0.25
Decimal_tem = 0x19;
}else if (Decimal_tem == 0x00000008 ){//The decimal is 0.50
Decimal_tem = 0x32;
}if (Decimal_tem == 0x0000000c ){//decimal 0.75
Decimal_tem = 0x4B;
}
}else{ // is a positive number
Temp = ((temp2<<8)|temp1); //Merge high 8 bits and low 8 bits
/* Get the integer part */
Integer_tem = Temp;
Integer_tem = (Integer_tem & 0x000007F0); //Get a 7-bit integer
Integer_tem = (Integer_tem & 0xFFFFF7FF); //Set the highest position to 0 (positive)
Integer_tem = (Integer_tem>>4); //Get 8 digits
/* Get the decimal part */
Decimal_tem = Temp;
Decimal_tem = (Decimal_tem & 0x0000000c); // Get two decimal places
if(Decimal_tem == 0x00000000){//The decimal is 0.00
Decimal_tem = 0x00;
}else if (Decimal_tem == 0x00000004 ){//The decimal is 0.25
Decimal_tem = 0x19;
}else if (Decimal_tem == 0x00000008 ){//The decimal is 0.50
Decimal_tem = 0x32;
}if (Decimal_tem == 0x0000000c ){//decimal 0.75
Decimal_tem = 0x4B;
}
}
temp_str[0] = Integer_tem; //Store the integer part in the first element
temp_str[1] = Decimal_tem; //Store the decimal part in the second element
}
In addition, add some header files, which are self-defined and have no effect on you. You need to define the pin macros well, because you can only connect the pin operations of DS18B20.
//The link corresponding to the DS18B20 DQ pin is P0.23
#define DQ ANT4_LED //(1<<23)
/*1 microsecond delay time function*/
void Delay_1us(unsigned int iTime)
{
unsigned int i,j ;
for(i=0;i for(j=0;j<10;j++); } } Two more important things to say: You have to use an oscilloscope to adjust the Delay_1us function to within the error range, because ARM embedded machines have very high requirements for timing.
Previous article:IIS of s3c2440 (2) I2S audio bus learning - digital audio technology
Next article:IIS of s3c2440 (3) I2S bus protocol
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- A record of the development experience of a new embedded CPU chip CC2540
- if(Data==1)start=1; Why is there no space after if and before start in this sentence? This will not cause errors during compilation.
- Xiaomi Robot Vacuum Cleaner Features and Chip Parameters
- [Raspberry Pi 4B Review] Raspberry Pi 4 pin functions and operation methods
- Live: Embedded to Cloud Journey with PIC and AVR MCUs from the Microchip Ecosystem Part 2
- Purgatory Legend-RAM War
- [CY8CKIT-149 PSoC 4100S Review] + Familiar with the development environment
- How to write program control for AT28C64
- Motor control design, accuracy? resolution? arc minutes?
- External interrupt and time interrupt count 0-999 displayed on the digital tube example