Temperature Measurement System Based on DS18B20 and TMS320LF2407A

Publisher:心怀梦想Latest update time:2012-07-07 Source: 21ic Keywords:DS18B20  TMS320LF2407A Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Preface

DS18B2 is generally used in conjunction with a single-chip microcomputer, and there are few reports on the interface between DSP and DS18B20. Therefore, this article introduces in detail the connection method between TMS320LF2407 and DS18B20, and also introduces in detail how to use C language to complete precise software delay, thereby completing the data communication between DS18B20 and TMS320LF2407 based on the single-wire bus protocol.

1 DS18B20 Timing

1. 1 Reset Timing

When resetting DS18B20, it must be reset first before executing other commands. When resetting, the host pulls the data line to a low level and maintains it for 480us to 960us, then releases the data line, and then the pull-up resistor pulls the data line high for 15 to 60us, waiting for DS18B20 to send a presence pulse. The presence pulse is valid for 60 to 240us, and the reset operation is completed.

1.2 Write Timing

When the host writes data to DS18B20, the data line is first set to a high level and then to a low level. The low level should be greater than 1us. Within 15us after the data line becomes a low level, the data line is made high or continues to be low according to writing "1" or writing "0". DS18B20 will sample the data line within 15us to 60us after the data line becomes a low level. The duration of data written to DS18B20 should be greater than 60us and less than 120us, and the time interval between two writes should be greater than 1us.

1.3 Reading Timing

Read time slot When the host reads data from DS18B20, the host first sets the data line to high level, then changes it to low level. The low level should be greater than 1us, and then releases the data line to make it high level. DS18B20 sends data to the data line within 15us when the data line changes from high level to low level. The host can read the data line after 15us.

2 Connection between TMS320LF2407 and DS18B20

DS18B20 has three pins. The wiring diagram between it and TMS320LF2407A is as follows. The VDD pin is connected to 5V voltage to power the sensor . The DQ pin is the data line. When connected to I OP A6 of TMS320LF2407A, it is also connected to a 4.7K pull-up resistor and connected to a 5V power supply so that the data line can be automatically pulled up to a high level in the idle state. The GND pin is grounded.

Connection between TMS320LF2407 and DS18B20

3 Software Design

3.1 Implementation of Accurate Software Timing

As we all know, the core of TMS320LF2407 belongs to the C2000 series processor , and the processing speed reaches 30MIPs, which means that the time spent on each instruction is only 33ns in theory. However, in reality, due to factors such as the data processing speed and data call of the DSP external circuit, the processing speed often cannot reach this order of magnitude. So how to measure the processing time of each instruction and calculate the precise time of the software delay program? The calculation method is as follows:

1. The C language code of the delay program is:

for(loopindex=0;loopindex
where loopindex is unsigned int, unsigned integer value, N is a constant, calculated according to the required delay time. The specific algorithm is given below.

2. The assembly language generated by the compiler in CCS after compiling this for statement is:

LACL #0h
SACL *, 0
LACL *
SUB N (calculated number of loops)
BCND Address to branch to, GEQ
LACC *, 0
ADD #1h
SACL *, 0
LACL *
SUB N (calculated number of loops)
BCND Address to branch to, LT

The time taken by these assembly statements can be calculated as follows: DELAY = (9N + 4) * single instruction cycle.

3. Test an instruction cycle

The crystal used in the system is 20M, the clock frequency after multiplication is 40M, and the division frequency of timer 1 is 1, which means that the timing cycle of the timer is 25ns. Turn on the timer and run the above assembly statement in single step to see the value increased after each time the timer runs. The test shows that after each single step of running the assembly statement that takes up one instruction cycle, the value of the timer increases by 10. In other words, the time occupied by the instruction of each single instruction cycle is: 25ns*10=250ns, which is 0.25us.

4. The final calculation formula of the delay program is:

DELAY = (9n + 4) * 0.25us.

It can be seen from the formula that when n=0, DELAY=1us, and when n=65535, DELAY=150ms.

3.2 Main procedures

Due to the length of the article, only the reset program, byte reading program and the entire temperature reading main program are given here. The program for writing command words can be written by yourself according to the timing described above.

#define nop() {asm( nop );}
#define DATA_PORT PADATDIR
#define DATA_MODE 0x0040
#define DATA_OUT 0x4000
#define DATA_BIT 0x0040
#define PIN_HIGH() {PADATDIR=PADATDIR|DATA_OUT|DATA_BIT;}
#define PIN_LOW() {DATA_PORT =(PADATDIR|DATA_OUT)&(~DATA_BIT);}


[page]

#define PIN_LEAVE() {DATA_PORT=DATA_PORT&(~DATA_OUT)|DATA_BIT;}
/* Sensor Reset procedure*/
unchar reset(void)
{ unchar retval;
unint loopindex="0";
PIN_HIGH();
nop();nop();nop();nop(); /* Delay 1us*/
PIN_LOW();
for(loopindex=0;loopindex<213;loopindex++){;} /* Set the bus to low level and keep it for at least 480us */
PIN_HIGH();
for(loopindex=0;loopindex<26;loopindex++){;} /* Wait for the resistor to pull the bus high and keep it for 15-60us */
PIN_LEAVE(); /* Receive the response signal*/
nop();
if((DATA_PORT & DATA_BIT) == 0x0000)
{ retval = 0; }
else
{ retval = 1; }
PIN_HIGH();
for(loopindex=0;loopindex<106;loopindex++){;} /*Delay 60-240us */
return(retval); /*Return response signal*/
}
/*Read a byte from the sensor*/
void read_byte(unchar *ReadByte)
{
int i;
unchar temp="0";
unint loopindex="0";
PIN_HIGH();
for(i=0;i<8;i++)
{
temp="temp">>1;
PIN_LOW();
nop();nop();nop();nop(); /* Delay 2us */
nop();nop();nop();nop();nop();
PIN_HIGH();
for(loopindex=0;loopindex<6;loopindex++){;} /* Delay 14us */
PIN_LEAVE();
nop();
if((DATA_PORT & DATA_BIT) == 0x0000)
{ temp="temp" & 0x7F; }
else
{ temp="temp" | 0x80; }
PIN_HIGH();
for(loopindex=0;loopindex<26;loopindex++){;} /* Delay 60us */
}
*ReadByte=temp;
return;
}
/* Convert the read temperature into decimal*/
float transform(unchar *T)
{
unchar temp="0";
float temprature="0";
temp=temp|(*(T+1)<<8);
temp=temp|*T;
if((temp&0x0F800)==0x0F800)
{ temprature="0-"((~temp)+1)*0.0625; }
else
{ temprature="temp"*0. 0625; }
return(temprature);
}
/* Main function to read temperature, return decimal temperature value*/
float read_temp()
{
Unchar buff[2];
float temprature="0";
unint loopindex="0";
while (reset()==1){}; /* Reset and wait for slave response*/
write_byte(0xCC); /* Ignore ROM match*/
write_byte(0x44); /* Send temperature conversion command*/
for(loopindex=0;loopindex<65535;loopindex++){;} /* Delay 300ms, wait for digital-to-analog conversion */
for(loopindex=0;loopindex<65535;loopindex++){;}
while(reset()==1){}; /* Reset again, wait for slave response*/
write_byte(0xCC); /* Ignore ROM match*/
write_byte(0xBE); /* Send temperature read command*/
read_byte(buff); /* Read the lower 8 bits of temperature*/
read_byte(buff+1); /* Read the higher 8 bits of temperature*/
temprature="transform"(buff);
PIN_HIGH(); /* Release the bus */
return(temprature);
}

4 Conclusion

DS18B20 is an excellent single bus digital sensor. The hardware design is simple and the operation is reliable. By analyzing the assembly language compiled by C language, the software delay time can be clearly calculated to meet the timing requirements of single-wire bus communication, so that the software design of DSP and DS18B20 communication can be completed excellently.

5 This paper has two innovative points:

1. Compile the corresponding C language delay program through the C compiler to get its corresponding assembly language, and then use the timer to know the time of a single instruction cycle, so as to accurately calculate the delay time of the software delay program.

2. Usually DS18B20 is used in conjunction with a single-chip microcomputer, but this article introduces in detail the combined use of DS18B20 and DSP. The main difference is in the operation of the port and the control of the delay.

Keywords:DS18B20  TMS320LF2407A Reference address:Temperature Measurement System Based on DS18B20 and TMS320LF2407A

Previous article:Electromagnetic compatibility analysis of single-chip computer system for intelligent frame-type circuit breaker
Next article:Noteworthy design principles of single chip microcomputer control board

Recommended ReadingLatest update time:2024-11-16 15:57

51 MCU - Single Bus OneWrite, DS18B20 Temperature Sensor C Language Introduction Programming
Single bus OneWrite: (1) Initialization sequence: All communications on the single bus start with an initialization sequence. The host outputs a low level and maintains the low level for at least 480us (the time range can be from 480 to 960 microseconds) to generate a reset pulse. Then the host releases the bus, and t
[Microcontroller]
51 MCU - Single Bus OneWrite, DS18B20 Temperature Sensor C Language Introduction Programming
Greenhouse temperature monitoring system module circuit based on MSP430 and DS18B20
  1 Temperature acquisition sensor DS18B20   DS18B20 is a single bus digital temperature measurement chip, which is easy to use, wear-resistant, collision-resistant, and has strong anti-interference ability. It can directly read the temperature of the object being measured. Its measurement range is -55~+125℃, and it
[Microcontroller]
Greenhouse temperature monitoring system module circuit based on MSP430 and DS18B20
PIC microcontroller: temperature sensor ds18B20 C language code
/*Temperature sensor Using Tianxiang temperature conversion module algorithm Download address of the complete version of the program: http://www.51hei.com/f/ds1820pic.rar */ #include __CONFIG(0X3B31); #define uint unsigned int #define uchar unsigned char #define DQ RC1 #define DQ_DIR TRISC1 #de
[Microcontroller]
51 single chip temperature alarm DS18B20 system program
51 single chip temperature alarm DS18B20 system program: #include "AT89X52.h" #include "stdio.h"    #define uint unsigned int #define uchar unsigned char //macro definition #define SET P3_1 //Define adjustment key #define DEC P3_2 //define the decrease key #define ADD P3_3 //define add key #define BEEP P3_7 //define b
[Microcontroller]
PIC microcontroller DS18B20 sampling LCD1602 display
#include "18b20.h"  #include "main.h"  //--------------Generate reset pulse--------------------//  void tx_reset()  {      ADCON1=0X06;      TR ISA 5=0;     DQ=1;      delay_us2(10,5); //65us      while(!DQ); //Judge whether the bus is busy      DQ=0;      delay_us2(10,23); //730us  }  //----------------Waiting for t
[Microcontroller]
Design of networked intelligent temperature sensor based on LPC2210 and DS18B20
Design of Networked Intelligent Temperature Sensor Based on LPC2210 and DS18B20 Time: 2010-07-30 08:38:04 Source: Author: Sensors, as an important tool for obtaining information, play a significant role in industrial production, science and technology, etc. However, with the rapid development of microprocessor techn
[Security Electronics]
Design of networked intelligent temperature sensor based on LPC2210 and DS18B20
DS18b20 Temperature Detection Description
******************************************************************** ** This is the DS18b20 program I wrote when I was learning about the msp430g2553 microcontroller ** ** DS18b20 single bus, strict timing, completely self-taught to understand the waste of n time! ! ** ** Highest accuracy 0.0625, that is, every temper
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号