DS18B20 driver on S3C2440

Source: Internet
Author: User

 

DS18B20 is a digital temperature sensor produced by Dallas. It is small in size, applicable to voltage width, and economical and flexible. It uses the patented Onboard technology internally. All sensing elements and conversion circuits are integrated into an integrated circuit, such as a transistor. The DS18B20 has three pin wires: power cord, ground wire, and data cable. The working voltage range is 3 ~ 5.5 V, Supporting single-bus interfaces. I. Development Environment

  • MASTER: virtualbox -- fedora 9
  • Development Board: mini2440 -- 128 MB nand, kernel: 2.6.32.2
  • Compiler: arm-linux-gcc-4.3.2
Ii. Architecture and working principle of DS18B20The external structure 1 of DS18B20 is shown. VDD indicates the power input, DQ indicates the digital signal input/output, And Gnd indicates the power source.

The internal structure of DS18B20 consists of four parts: 64-bit optical print Rom, temperature sensor, non-Easy temperature alarm trigger th and TL, and configuration register, as shown in figure 2.

In the 64-bit Rom, the 64-bit serial number is burned by the manufacturer through photoprint before the product leaves the factory. This serial number can be seen as the Address Sequence Code of DS18B20, which is used to distinguish each DS18B20, so as to better measure the field temperature.

The latches in Figure 2 are the most important registers in DS18B20. A temporary storage consists of nine bytes. Each byte is defined in Table 1.

The configuration register is used to set the conversion precision of the temperature sensor. Its definitions are as follows:

The TM bit is the test mode bit. It is used to set whether the DS18B20 is the work mode (0) or the test mode (1). Its factory value is 0. R1 and R0 are used to set the conversion precision of the temperature sensor: 00, the resolution is 9 bits, the conversion time is 93.75 ms; 01, the resolution is 10 bits, the conversion time is 187.5 MS; 10, the resolution is 11 bits, the conversion time is 375 MS, 11, the resolution is 12 bits, the conversion time is 750 ms. The factory value of R1 and R0 is 11. The remaining 5 bits are always 1.

0th and 1st bytes are the binary temperature values after 16-bit conversion. The first four are the symbol bits, and the remaining 12 are the converted data bits (the resolution is 12 bits ). If the temperature is greater than 0, the first four digits are 0. If the measured value is multiplied by 0.062 5, the actual temperature is obtained. If the temperature is less than 0, the first four digits are 1, the measured value needs to be reversed to 1, and then multiplied by 0.062 5. The binary values of 0th and 1st bytes are as follows:

Application Circuit Structure of 2.2 DS18B20

According to the power supply method of DS18B20, its application circuit structure can be divided into three types: parasitic Power Supply Mode, parasitic Power Supply Mode, strong pull-up Power Supply Mode, external power supply mode. In practical application, external power supply is the main method. Its application schematic 3 is shown in.

2.3ds18b20 Working Principle

Based on the communication protocol of DS18B20, the MCU has the following three steps: reset the low level of 500 μs sent to the DS18B20 before reading and writing; reset successful, send the ROM command; send Ram commands. The specific operation process of MCU on DS18B20 is shown in step 4.

Iii. Implementation of the Linux DS18B20 driver

Choose the mini2440 Development Board as the hardware platform (the main chip is Samsung's S3C2440) and the latest Linux kernel linux2.6.29 as the software platform. The expanded interface of mini2440 leads to the gpio port (gpbl) as the data line DQ.

DS18B20 is a single-bus device, so the operation time is strict. The key to ensuring that the DS18B20 driver can run properly and obtain the real-time temperature value is whether the reset program, bit Write Program, and bit read program can be correctly written.

3.1 reset program

Before Reading and Writing DS18B20, you must reset and initialize it to check the existence of DS18B20. MCU is required to drop data lines down 480 ~ 960 μs, and then release the data line, waiting for about 60 μs. If the MCU receives a low level from DS18B20, the reset is successful.

The following is the reset program code:

3.2 write a 1-byte subroutine

Send ROM and ram commands and write data to DS18B20. Write a 1-byte subroutine as follows:

3.3 read N-byte subroutine

When the temperature conversion is complete, you need to read 0th bytes and 1st bytes of binary data from the DS18B20 Ram.

The subprogram for reading 1 byte is as follows:

The subroutine for reading n Bytes is as follows:

4. Final Drive.

# Include <Linux/errno. h>

# Include <Linux/kernel. h> # include <Linux/module. h> # include <Linux/slab. h> # include <Linux/input. h> # include <Linux/init. h> # include <Linux/Serio. h> # include <Linux/delay. h> # include <Linux/CLK. h> # include <Linux/miscdevice. h> # include <ASM/Io. h> # include <ASM/IRQ. h> # include <ASM/uaccess. h> # include <Mach/regs-clock.h> # include <plat/regs-timer.h> # include <Mach/regs-gpio.h> # include <Mach/gpio. h> # Include <Linux/cdev. h> // define dqpin # define DQ s3c2410_gpb (1) # define dq_input partition (DQ, s3c2410_gpio_input) // Input Function # define dq_output s3c2410_gpio_cfgpin (DQ, s3c2410_gpio_output) // output function # define device_name "DS18B20" # define device_major 0 static int device_major = device_major; typedef unsigned char uchar; static struct class * DS18B20 class; static uchar data [2]; // Save the data static void reset (void) {do {dq_output; // set the DQ output mode s3c2410_gpio_setpin (DQ, 1); udelay (1); s3c2410_gpio_setpin (DQ, 0); udelay (600); s3c2410_gpio_setpin (DQ, 1); udelay (60); dq_input;} while (s3c2410_gpio_getpin (DQ )! = 0); While (s3c2410_gpio_getpin (DQ) = 0); Return 0;} static void writebyte (uchar cmd) {uchar I; dq_output; for (I = 0; I <8; I ++) {s3c2410_gpio_setpin (DQ, 0); udelay (1); If (CMD & 0x01) s3c2410_gpio_setpin (DQ, 1 ); udelay (65); s3c2410_gpio_setpin (DQ, 1); cmd = cmd> 1 ;}} static uchar readbyte (void) {uchar I, temp = 0; for (I = 0; I <8; I ++) {temp> = 1; dq_output; s3c2410_gpio_setpin (DQ, 0); udelay (1); s3c2410_gpio_setpin (DQ, 1); dq_input; udelay (10); If (s3c2410_gpio_getpin (DQ) temp = temp | 0x80; udelay (65); dq_output; s3c2410_gpio_setpin (DQ, 1);} return temp;} static void read (void) {reset (); udelay (120); writebyte (0xcc); writebyte (0x44 ); udelay (5); reset (); writebyte (0xcc); writebyte (0xbe); Data [0] = readbyte (); Data [1] = readbyte ();} static int DS18B20 (struct file * file, char _ User * Buf, size_t count, loff_t * OFFP) {read (); Buf [0] = data [0]; Buf [1] = data [1]; return 1;} static struct file_operations f_ops = {. owner = this_module ,. read = DS18B20 _ read,}; static int _ init DS18B20 _ Init (void) {// register a character device. device_major = 0 is defined here for the system to allocate, after successful registration, the system returns the dynamically assigned master device number device_major = register_chrdev (device_major, device_name, & f_ops); If (device_major <0) {printk (device_name "register faild! \ N "); Return device_major;} // register a device class so that mdev can create a device node in the/dev/directory. The value of this class is" maid ); if (is_err (DS18B20 b20_class) {printk (device_name "create class faild! \ N "); Return-1;} device_create (DS18B20 b20_class, null, mkdev (device_major, 0), null, device_name); Return 0 ;} static void _ exit DS18B20 b20_exit (void) {// unregister_chrdev (device_major, device_name); // Delete the device node. Note that the function name of earlier versions of kernel 2.6 is class_device_destroy, device_destroy (DS18B20 b20_class, mkdev (device_major, 0); // deregister class class_destroy (DS18B20 b20_class);} module_init (logout); module_exit (logout ); module_license ("GPL "); V. Test Procedure# Include <stdio. h>

# Include "sys/types. H "# include" sys/IOCTL. H "# include" stdlib. H "# include" termios. H "# include" sys/STAT. H "# include" fcntl. H "# include" sys/time. H "int main (INT argc, char * argv) {int FD; unsigned char Buf [2]; float result; If (FD = open ("/dev/DS18B20 ", o_rdwr | o_ndelay | o_noctty) <0) {printf ("open device DS18B20 failed. \ r \ n "); exit (1) ;}else {printf (" open device DS18B20 successed. \ r \ n "); While (1) {read (FD, Buf, 1); Result = (float) BUF [0]; Result/= 16; result + = (float) BUF [1] * 16); printf ("%. 1f 'C \ r \ n ", result); sleep (1) ;}close (FD);} return 0;

}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.