Temperature sensor driver based on ARM-LINUX-DS18B20

Publisher:疯狂小马Latest update time:2020-03-03 Source: eefocusKeywords:ARM-LINUX Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The DS18B20 digital temperature sensor is easy to wire and can be used in a variety of occasions after packaging, such as pipeline type, threaded type, magnet adsorption type, stainless steel package type, and various models, including LTM8877, LTM8874, etc. Its appearance is mainly changed according to different application occasions. The packaged DS18B20 can be used for cable trench temperature measurement, blast furnace water circulation temperature measurement, boiler temperature measurement, machine room temperature measurement, agricultural greenhouse temperature measurement, clean room temperature measurement, warehouse temperature measurement and other non-extreme temperature occasions. It is wear-resistant and collision-resistant, small in size, easy to use, and has a variety of packaging forms. It is suitable for various digital temperature measurement and control fields of equipment in small spaces.


Technical performance description

1. Unique single-wire interface mode. When DS18B20 is connected to a microprocessor, only one line is needed to realize two-way communication between the microprocessor and DS18B20.
2. Temperature measurement range: -55℃~+125℃, inherent temperature measurement resolution: 0.5℃.
3. Support multi-point networking function. Multiple DS18B20 can be connected in parallel on a single three-wire.
4. Working power supply: 3~5V/DC
5. No peripheral components are required during use
6. The measurement results are transmitted serially in the form of 9~12-bit digital quantities


DS18b20 package

DS18B20 pin functions: GND voltage ground • DQ single data bus • VDD power supply voltage • NC empty pin


Connection between DS18b20 and processor


DS18B20 working principle and application

The temperature detection and digital data output of DS18B20 are fully integrated on one chip, so it has stronger anti-interference ability. Its one working cycle can be divided into two parts, namely temperature detection and data processing. Before explaining its working process, it is necessary to understand the internal memory resources of 18B20. 18B20 has three forms of memory resources. They are:

ROM Read Only Memory:

Used to store the DS18B20 ID code. The first 8 bits are the single-line serial code (DS18B20 code is 19H), the next 48 bits are the chip's unique serial number, and the last 8 bits are the CRC code (redundancy check) of the above 56 bits. The data is set at production and cannot be changed by the user. DS18B20 has a total of 64 bits of ROM.

RAM Data Register:

Used for internal calculation and data access. Data is lost after power failure. DS18B20 has 9 bytes of RAM, each byte is 8 bits. The 1st and 2nd bytes are the data value information after temperature conversion. The 3rd and 4th bytes are the mirror images of the user's EEPROM (commonly used for temperature alarm value storage). Its value will be refreshed when power is reset. The 5th byte is the mirror image of the user's 3rd EEPROM. The 6th, 7th and 8th bytes are count registers, which are designed to allow users to obtain higher temperature resolution. They are also temporary storage units for internal temperature conversion and calculation. The 9th byte is the CRC code of the first 8 bytes. EEPROM is a non-volatile memory used to store data that needs to be saved for a long time, upper and lower temperature alarm values ​​and verification data. DS18B20 has 3 bits of EEPROM, and there are mirror images in RAM to facilitate user operation.


Controller operation process for 18B20:

1. Reset: First we must reset the DS18B20 chip. Reset means the controller (MCU) sends a low level signal of at least 480uS to the DS18B20 single bus. When 18B20 receives this reset signal, it will send back a chip existence pulse after 15~60uS.

2. Presence pulse: After the reset level ends, the controller should pull up the data single bus to receive the presence pulse after 15~60uS. The presence pulse is a 60~240uS low-level signal. So far, the two parties have reached a basic agreement, and the next step will be data communication between the controller and 18B20. If the reset low level time is insufficient or the single bus circuit is broken, the presence pulse will not be received. Pay attention to the handling of unexpected situations during design.

3. The controller sends ROM instructions: After the two parties have said hello, they will communicate. There are 5 ROM instructions in total, and only one can be sent in each working cycle. The ROM instructions are read ROM data, specify matching chip, jump ROM, chip search, and alarm chip search. The ROM instruction is 8 bits in length, and its function is to operate the 64-bit photolithography ROM in the chip. Its main purpose is to distinguish and process multiple devices connected to a bus. It is true that multiple devices can be connected to a single bus at the same time, and they are distinguished by the unique ID number on each device. Generally, the ROM instruction can be skipped when only a single 18B20 chip is connected (Note: skipping the ROM instruction here does not mean not sending the ROM instruction, but using a unique "skip instruction")

4. The controller sends memory operation instructions: After the ROM instruction is sent to 18B20, the memory operation instruction is sent immediately (uninterruptedly). The operation instruction is also 8 bits, a total of 6, and the memory operation instructions are writing RAM data, reading RAM data, copying RAM data to EEPROM, temperature conversion, copying the alarm value in EEPROM to RAM, and working mode switching. The function of the memory operation instruction is to command 18B20 to do what kind of work, which is the key to chip control.

5. Execution or data reading and writing: After a memory operation instruction is completed, the instruction execution or data reading and writing will be carried out. This operation depends on the memory operation instruction. If the temperature conversion instruction is executed, the controller (MCU) must wait for 18B20 to execute its instruction. The general conversion time is 500uS. If the data reading and writing instruction is executed, it is necessary to strictly follow the reading and writing timing of 18B20 to operate.


To read the current temperature data, we need to execute two working cycles. The first cycle is reset, skip ROM instructions, execute temperature conversion memory operation instructions, and wait for 500uS temperature conversion time. Then execute the second cycle to reset, skip ROM instructions, execute RAM memory operation instructions, and read data (up to 9 bytes, which can be stopped in the middle. If you only read simple temperature values, just read the first 2 bytes). The other operation processes are similar, so I won’t introduce them here.


There are many online resources about the data sheet of ds18b20, so I won't introduce it in detail here. The following is a driver based on ARM-LINUX, which has been tested after being compiled by arm-gcc. (Original by Huaqing Yuanjian, please indicate the source when reprinting)

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");

#define GPHCON (*(volatile unsigned int *)S3C2410_GPHCON)
#define GPHDAT (*(volatile unsigned int *)S3C2410_GPHDAT)
#define GPHUP (*(volatile unsigned int *)S3C2410_GPHUP)

static int ds18b20_major = 230;/*Statically apply for device number*/

struct cdev cdev;
struct class *my_class;

spinlock_t lock;

dev_t dev = 0;
int number_of_devices = 1;

/*Configure to input mode*/
void set_conIN(void)
{
GPHCON &= ~(1<<19);
GPHCON &= ~(1<<18);
}

/*Configure to output mode*/
void set_conOUT(void)
{
GPHCON |= (1<<18);
GPHCON &= ~(1<<19);
}

/*引脚置位*/
void set_data(int i)
{
if( i == 0 ){
GPHDAT &= ~(1<<9);
}else if( i == 1 ){
GPHDAT |= (1<<9);
}
}

/*复位ds18b20*/
unsigned int reset_ds18b20(void)
{
unsigned int retValue;
set_conOUT();

set_data(1);
__udelay(1);
set_data(0);
__udelay(600);
set_data(1);
__udelay(20);
set_conIN();
__udelay(100);
/*After a short delay, if x=0, initialization is successful,
if x=1, initialization fails*/
retValue = (GPHDAT >> 9) & 0x01;
printk("init is %dn",retValue);
return retValue;
}

/*读取一位温度*/
unsigned int read_bit(void)
{
spin_lock(&lock);
set_conOUT();
//set_data(1);
//__udelay(2);
set_data(0);
__udelay(2);
set_conIN();
__udelay(1);
spin_unlock(&lock);
return ((GPHDAT >> 9) & 0x01);
}

/*写一位命令*/
void write_bit(char bitValue)
{
spin_lock(&lock);
set_conOUT();
set_data(0);
__udelay(15);
if( bitValue == 1 ){
set_data(1);
}else{
set_data(0);
}
spin_unlock(&lock);
__udelay(45);
set_conIN();
__udelay(2);
}

/*写命令*/
void write_cmd(char cmd)
{
unsigned char i;
unsigned char temp;


for(i=0; i<8;i++){
temp = cmd>>i;
temp &= 0x01;
write_bit(temp);
}
//__udelay(10);
}
/*打开设备*/
static int ds18b20_open(struct inode *inode,struct file *filp)
{
printk (KERN_INFO "HEY! device openedn");
//GPHUP &= ~(1<<9);
GPHUP |= (1<<9);
spin_lock_init(&lock);
return 0;
}
/*读取数据*/
static int ds18b20_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
char lowValue=0,highValue=0;
unsigned int i;
//float value;

if(reset_ds18b20()){
printk("init errorn");
}
__udelay(400);
set_conOUT();
set_data(1);
write_cmd(0xCC);
write_cmd(0x44);
__udelay(100000);

if(reset_ds18b20()){
printk("init errorn");
}
__udelay(400);
set_conOUT();
set_data(1);
write_cmd(0xcc);
write_cmd(0xBE);
/*读取温度转化数值*/

for(i=0; i<8; i++){
if( read_bit() ){
lowValue |= (0x01<}
__udelay(62);
}
printk("lowValue is %dn",lowValue);
for(i=0; i<8; i++){
if( read_bit() ){
highValue |= (0x01<}
__udelay(62);
}
printk("highValue is %dn",highValue);
#if 0
i = highValue;
i <<= 8;
i = i|lowValue;
value = i*0.0625;
printk("kernel is %dn",value);
#endif

[1] [2]
Keywords:ARM-LINUX Reference address:Temperature sensor driver based on ARM-LINUX-DS18B20

Previous article:s3c2440A development board UART summary and UART code details
Next article:ARM Litian Electronics LPC2148 Detailed explanation of the temperature acquisition experimental procedure based on DS18b20

Recommended ReadingLatest update time:2024-11-17 04:52

C language program using DS18B20 temperature sensor in single chip microcomputer (reference 1)
C language program using DS18B20 temperature sensor in single chip microcomputer (reference 1) /********************************************************************************                                DS18B20 temperature measurement program     Hardware: AT89S52      (1) Single-wire ds18b20 connected to P2.
[Microcontroller]
Detailed explanation of DS18B20 temperature sensor driver based on 51 single chip microcomputer
//This part is the driver of 18B20   //This program has been verified, the crystal oscillator is 12MHz   #include reg52.H #include intrins.h   sbit D18B20=P3^7; //DQ connects to P3^7 #define NOP() _nop_()   #define _Nop() _nop_()    void TempDelay (unsigned char idata us); //delay function defini
[Microcontroller]
Characteristics and applications of digital thermometer DS18B20
    Abstract: DS18B20 is a new single-line digital thermometer produced by DALLAS Company in the United States that can completely replace DS1820. The article introduces the performance structure of DS18B20 and its different characteristics compared with DS1820, and explains the usage requirements of DS18B20. The app
[Test Measurement]
ds18b20 refrigerator control system
# include "reg52.h" # include "math.h" #include intrins.h # define uchar unsigned char # define uint unsigned int //Write eeprom storage sbit scl=P1^1; sbit sda=P1^2; sbit DQ = P3^0; //Temperature input port sbit DQ2 = P3^1; //Temperature input port sbit kmbj=P1^4; //Entry for door opening warning lig
[Microcontroller]
ds18b20 refrigerator control system
Using ds18b20 to detect the current temperature isd1420 voice chip temperature alarm program
Use ds18b20 to detect the current temperature. The temperature alarm program function of the isd1420 voice chip : Use ds18b20 to detect the current temperature, and send an alarm to ISD1420 by comparing it with the set parameters.  The alarm pulse drives the speaker to alarm through the power amplifier LM386.  Note:
[Microcontroller]
STM32 DS18B20 driver transplantation
After a night of hard work, I finally ported the DS18B20 driver to STM32. I have used single and multiple DS18B20 on 51 before, and I have a ready-made program. I thought I could get it done quickly, but I was still stuck. Here are a few key points:    The first is the delay problem. If you use software delay on STM32
[Microcontroller]
Temperature measurement system based on DS18B20 and TMS320LF2407A
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 comple
[Test Measurement]
Temperature measurement system based on DS18B20 and TMS320LF2407A
Perfectly implement STM32 single bus to connect multiple DS18B20
  Generally, the common STM32 routines about DS18B20 are to detect a sensor, and the code usually skips the ROM detection and directly obtains the temperature value. This writing method is not suitable for the case where multiple DS18B20s are mounted on a single bus. Sandeepin's code is perfect for this situation. It
[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号