1. Basic knowledge of DS18B20
The DS18B20 digital thermometer is a 1-Wire device produced by DALLAS, which has the characteristics of simple circuit and small size. Therefore, it is used to form a temperature measurement system, which has simple circuit. Many such digital thermometers can be hung on one communication line, which is very convenient.
1. Features of DS18B20 products
(1) Only one port is required to achieve communication.
(2) Each device in DS18B20 has a unique serial number.
(3) In actual application, no external components are required to achieve temperature measurement.
(4) The measurement temperature range is between -55. C and +125. C.
(5) The resolution of the digital thermometer can be selected from 9 bits to 12 bits.
(6) There are internal temperature upper and lower limit alarm settings. 2.
Pin introduction of DS18B20 The
pin arrangement of DS18B20 in TO-92 package is shown in Figure 1, and its pin function description is shown in Table 1.
(Bottom view) Figure 1
Table 1 DS18B20 detailed pin function description
3. How to use DS18B20
Since DS18B20 uses the 1-Wire bus protocol, that is, bidirectional data transmission is achieved on one data line, and for the AT89S51 microcontroller , the hardware does not support the single bus protocol, so we must use software methods to simulate the protocol timing of the single bus to complete the access to the DS18B20 chip.
Since DS18B20 reads and writes data on one I/O line, there are strict timing requirements for the data bits to be read and written. DS18B20 has a strict communication protocol to ensure the correctness and integrity of each bit of data transmission. The protocol defines the timing of several signals: initialization timing, read timing, and write timing. All timings use the host as the master device and the single bus device as the slave device. Each transmission of commands and data starts with the host actively starting the write timing. If the single bus device is required to send back data, the host needs to start the read timing to complete the data reception after the write command. The transmission of data and commands is low-order first.
DS18B20 reset timing
DS18B20 read timing The
read timing of DS18B20 is divided into two processes: read 0 timing and read 1 timing.
For the read time slot of DS18B20, after the host pulls the single bus low, the single bus must be released within 15 seconds to allow DS18B20 to transfer data to the single bus. It takes at least 60us for DS18B20 to complete a read timing process.
DS18B20 write timing The
write timing of DS18B20 is still divided into two processes: write 0 timing and write 1 timing.
The requirements for DS18B20 write 0 timing and write 1 timing are different. When writing 0 timing, the single bus must be pulled down for at least 60us to ensure that DS18B20 can correctly sample the "0" level on the IO bus between 15us and 45us . When writing 1 timing, the single bus must be released within 15us after being pulled low.
4. Experimental task:
Use a DS18B20 chip to form a temperature measurement system. The temperature measurement accuracy reaches 0.1 degrees. The temperature measurement range is between -20 degrees and +100 degrees, and it is displayed with an 8-bit digital tube
. 5. Circuit schematic diagram
6. Hardware connection on the system board
(1). Connect P0.0-P0.7 in the "MCU system" area to the ABCDEFGH terminals in the "Dynamic digital display" area with an 8-core cable.
(2). Connect P2.0-P2.7 in the "MCU system" area to the S1S2S3S4S5S6S7S8 terminals in the "Dynamic digital display" area with an 8-core cable.
(3). Insert the DS18B20 chip into any socket in the "Four-way single bus" area. Be careful not to connect the power supply and ground signals in reverse.
(4). Connect the corresponding DQ terminal in the "Four-way single bus" area to the P3.7/RD terminal in the "MCU system" area.
7. C language source program
#include
#include
unsigned char code displaybit[]={0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f};
unsigned char code displaycode[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x 6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00,0x40};
unsigned char code dotcode[32]={0,3,6,9,12,16,19,22,
25,28,31,34,38,41,44,48,
50,53,56,59,63,66,69,72,
75,78,81,84,88,91,94,97};
unsigned char displaycount;
unsigned char displaybuf[8]={16,16,16,16,16,16,16,16};
unsigned char timecount;
unsigned char readdata[8];
sbit DQ=P 3^7;
bit sflag;
bit resetpulse(void)
{
unsigned char i;
DQ=0;
for(i=255;i>0;i--);
DQ=1;
for(i =60;i>0;i--); return ( DQ
) ; signed char j; for(i=0;i<8;i++) { if((command & 0x01)==0) { DQ=0; for(j=35;j>0;j--); DQ=1; } else {
DQ=0;
for(j=2;j>0;j--);
DQ=1;
for(j=33;j>0;j--);
}
command=_cror_(command,1);
}
}
unsigned char readdatafromds18b20(void)
{
unsigned char i;
unsigned char j;
unsigned char temp;
temp=0;
for(i=0;i<8;i++)
{
temp=_cror_(temp,1);
DQ=0;
_nop_ ();
_nop_();
DQ=1;
for(j=10;j>0;j--);
if(DQ==1)
{
temp=temp | 0x80;
}
else
{
temp=temp | 0x00;
}
for(j=200;j>0;j--);
}
return(temp);
}
void main(void)
{
TMOD=0x01;
TH0=(65536-4000)/256;
TL0=(65536-4000)%6;
ET0=1;
EA=1;
while(resetpulse());
writecommandtods18b20(0x CC );
writecommandtods18b20(0x44);
TR0=1;
while(1)
{
;
}
}
void t0(void) interrupt 1 using 0
{
unsigned char x;
unsigned int result;
TH0=(65536-4000)/256;
TL0=(65536-4000)%6;
if(displaycount= =2)
{
P0=displaycode[displaybuf[displaycount]] | 0x80;
}
else
{
P0=displaycode[displaybuf[displaycount]];
}
P2=displaybit[displaycount];
displaycount++;
if(displaycount==8)
{
displaycount=0;
}
timecount++;
if(timecount==150)
{
timecount=0;
while(resetpulse());
writecommandtods18b20(0xcc);
writecommandtods18b20( 0xbe);
readdata[0]=readdatafromds18b20();
readdata[1]=readdatafromds18b20();
for(x=0;x<8;x++)
{
displaybuf[x]=16;
}
sflag=0;
if((readdata [1] & 0xf8)!=0x00)
{
sflag=1;
readdata[1]=~readdata[1];
readdata[0]=~readdata[0];
result=readdata[0]+1;
readdata[0] =result;
if(result>255)
{
readdata[1]++;
}
}
readdata[1]=readdata[1]<<4;
readdata[1]=readdata[1] & 0x70;
x=readdata[0];
x=x>>4;
x=x & 0x0f;
readdata[1]= readdata[1] | x;
x=2; result= readdata
[1]; while (
result/10)
{
displaybuf[x]=result;
result= result /10
; sflag==1) { displaybuf[x+1]=17; } x=readdata[0] & 0x0f; x=x<<1; displaybuf[0]=(dotcode[x]) ; displaybuf[1]=( dotcode[x])/10; while(resetpulse()); writecommandtods18b20(0xcc); writecommandtods18b20(0x44); } }
Previous article:Digital thermometer with memory function - DS1624 technology application and principle analysis
Next article:Principle design and analysis of motor control circuit composed of single chip microcomputer and L293
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Current protection working principle
- STM32L151 reads W25Q16 ID error, please help me find out what is the reason
- Lesson 4: Programmable Logic Solutions for Video and Image Processing
- 2020 02 02 Should be distributed at 20:20 Leave a footprint
- Can you provide a text box where I can paste the names of the 8 teachers I often invite directly, so that I don’t have to search them one by one?
- EEWORLD University ---- Miz702 zynq video advanced tutorial (Milinke)
- [National Technology N32G430] 6. STK33B62 optical sensor detection
- Capacitor usage in power supply design
- How to implement an iterative equation using Verilog?
- Qorvo PAC Series Chips in High-Speed DC Motor Control