C language program using DS18B20 temperature sensor in single chip microcomputer (reference 1)

Publisher:Xingfu8888Latest update time:2016-09-21 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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.2
    (2) Seven-segment digital tube connected to P0 port
    (3) Use external power supply to power ds18b20, no parasitic power supply 
    Software: Kei uVision 3
    
**********************************************************************************/
#include "reg52.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit ds=P2^2;
sbit dula=P2^6;
sbit wela=P2^7;
uchar flag ;
uint temp; //Parameter temp must be declared as int type 
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //Digital encoding without decimal point

uchar code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
0x87,0xff,0xef}; //Digital encoding with decimal point

/*Delay function*/
void TempDelay (uchar us)
{
   while(us--);
}

void delay(uint count) //delay sub-function
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}

/*Serial port initialization, baud rate 9600, mode 1 */
void init_com()
{
   TMOD=0x20; //Set timer 1 to mode 2
   TH1=0xfd; //Install initial value and set baud rate
   TL1=0xfd;
   TR1=1; //Start timer
   SM0=0; //Serial port communication mode setting
   SM1=1;
// REN=1; //Serial port allows receiving data
   PCON=0; //Baud rate is not multiplied
// SMOD=0; //Baud rate is not multiplied
// EA=1; //Open general interrupt
//ES=1; //Open serial interrupt
}

/*Display of digital tube*/
void display(uint temp)
{
    uchar bai,shi,ge;
    bai=temp/100;
    shi=temp%100/10;
    ge=temp%100%10;

dula=0;
    P0=table[bai]; //Display hundreds digit
dula=1; //From 0 to 1, there is a rising edge, release the latch and display the corresponding segment 
dula=0; //From 1 to 0, latch again
          
    wela=0;
    P0=0xfe;
wela=1;
wela=0;
delay(1); //Delay about 2ms

P0=table1[shi]; //display tens digit
dula=1;
    dula=0;
    
P0=0xfd;
wela=1;
    wela=0;
    delay(1);

    P0=table[ge]; //Display the unit digit
dula=1;
    dula=0;
    
P0=0xfb;
wela=1;
    wela=0;
    delay(1);
}
/********************************************* 
Timing: initialization timing, read timing, write timing.
All timings use the host (MCU) as the master device and the single
bus device as the slave device. Each command and data transmission
starts with the host actively starting the write timing. If the single
bus device is required to send back data, after the write command, the host needs to start
the read timing to complete the data reception. The transmission of data and commands is low
-order first. 
Initialization timing: reset pulse existence pulse
        read; 1 or 0 timing
        write; 1 or 0 timing
Only the existence pulse signal is sent from 18b20 (slave), and other
signals are sent by the host.
Existence pulse: let the host (bus) know that the slave (18b20) is
ready.
******************************************/
/*------------------------------------------------------------------------------------------------------------------------------------
Initialization: Detect the reset pulse sent by the bus controller
Any communication with ds18b20 must start with initialization

The initialization sequence consists of a reset pulse from the bus master
followed by a presence pulse from the slave.

Initialization: reset pulse + presence pulse

Specific operation:
The bus controller sends (TX) a reset pulse (a low-level signal that lasts at least 480μs), then releases the bus and
enters the receiving state (RX). The single-wire bus is pulled to a high level by a 5K pull-up resistor. After detecting the rising edge on the I/O pin,
the DS1820 waits for 15~60μs and then sends a presence pulse (a low-level signal of 60~240μs).

Specifically see the timing diagram of 18b20 single-line reset pulse timing and 1-wire presence detect "
--------------------------------------------------------------------------------------------------------------------------------*/
void ds_reset(void)
{
   ds=1;
   _nop_(); //1us
   ds=0;
   TempDelay(80); //When the bus stays at a low level for more than 480us, all devices on the bus will be reset . Here //the delay is about 530us. The bus stays at a low level for more than 480μs, and all devices on the bus
will //be reset.
   _nop_(); 
   ds=1; //After generating the reset pulse, the microprocessor releases the bus and puts the bus in an idle state. The reason is checked in //18b20 Chinese documentation

   TempDelay(5); //After releasing the bus, the slave 18b20 can indicate whether it is online by pulling the bus low.
                  //Presence detection high level time: 15~60us, so delay 44us, perform 1-wire presence //detect (single-wire presence detection)
   _nop_();
   _nop_();
   _nop_();
   if(ds==0)
        ​​flag=1; //detect 18b20 success
   else
        flag=0; //detect 18b20 fail
TempDelay(20); //Presence detection low level time: 60~240us, so delay about 140us
   _nop_();
   _nop_();
   ds=1; //Pull the bus high again to make the bus idle
/**/
}

/*----------------------------------------
Read/write time gap:
DS1820 data reading and writing is processed through time gaps
bits and command words to confirm information exchange.
------------------------------------------*/
bit ds_read_bit(void) //read one bit
{
   bit dat;
   ds=0; //MCU (microprocessor) pulls the bus low
_nop_(); //read time slot starts when microprocessor pulls the bus low for at least 1us
   ds=1; //pull the bus low and then release the bus, so that slave 18b20 can take over the bus and output valid data
   _nop_();
   _nop_(); //delay a little and read the data on 18b20, because the data output from ds18b20
is valid within 15us of the falling edge of the read "time slot"
   dat=ds; //host reads the data output from slave 18b20, and these data appear within 15us of the falling edge of the read time slot 
   TempDelay(10); //all read "time slots" must be 60~120us, here 77us
   return(dat); //return valid data
}
uchar ds_read_byte(void ) //read one byte
{

uchar value,i,j;
value=0; //Don't forget to give the initial value
for(i=0;i<8;i++)
{
    j=ds_read_bit();
     value=(j<<7)|(value>>1); //The instructions for this step are in a word document
}
return(value); //Return a byte of data
}
void ds_write_byte(uchar dat) //Write a byte
{
uchar i;
bit onebit; //Don't forget, onebit is one bit
for(i=1;i<=8;i++) 
{
    onebit=dat&0x01;
    dat=dat>>1;
if(onebit) //Write 1
{
ds=0;
_nop_();    
      _nop_(); //Look at the timing diagram, at least 1us is delayed to generate a write "time gap" 
ds=1; //The data line is allowed to be pulled high within 15μs after the start of the write time gap
     TempDelay(5); //All write time gaps must last at least 60us
}
else //Write 0
{
ds=0;
     TempDelay(8); //The host needs to generate a write-0 time gap. It must pull the data line to a low level and maintain it for at least 60μs, 64us here.
ds=1;
_nop_();
     _nop_();
}
}
}

/******************************************** 
The host (single-chip microcomputer) controls 18B20 to complete temperature conversion through three steps:
18B20 must be reset before each read and write, and after the reset is successful,
a ROM instruction is sent, and finally a RAM instruction is sent, so that the DS18b20 can perform
the predetermined operation.
Reset requires the main CPU to pull down the data line for 500us and then release it. When ds18B20
receives the signal, it waits for 16~60us, and then sends a 60~240us low pulse.
The main CPU receives this signal to indicate that the reset is successful
**********************************************/

/*----------------------------------------
Perform temperature conversion:
Initialize first
Then skip ROM: Skip 64-bit ROM address and send temperature conversion command directly to ds18B20, suitable for single chip operation
Send temperature conversion command
------------------------------------------*/

void tem_change()
{
ds_reset(); 
delay(1);              //约2ms
ds_write_byte(0xcc);
ds_write_byte(0x44);
}

/*----------------------------------------
Get temperature:
------------------------------------------*/
uint get_temperature()
{
float wendu;
uchar a,b;
ds_reset();
delay(1); //about 2ms
ds_write_byte(0xcc);
ds_write_byte(0xbe);
a=ds_read_byte();
b=ds_read_byte();
temp=b;
temp<<=8;
temp=temp|a;
wendu=temp*0.0625; //Get the true decimal temperature value, because DS18B20
                                    //can be accurate to 0.0625 degrees, so the lowest bit of the read back data represents //0.0625 degrees

temp=wendu*10+0.5; //Magnify ten times. The purpose of doing this is to
                                       convert the first decimal point into a displayable number and perform a rounding operation.
return temp;
}
/*----------------------------------------
Read ROM   
------------------------------------------*/
/*
void ds_read_rom() //Not used here
{
   uchar a,b;
   ds_reset();
   delay(30);
   ds_write_byte(0x33);
   a=ds_read_byte();
   b=ds_read_byte();
}
*/
void main()
{
   uint a;
   init_com();
   while(1)
   {
tem_change(); //12-bit conversion time is up to 750ms
for(a=10;a>0;a--)
{
display( get_temperature());
}
   }
}

Keywords:MCU Reference address:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 1)

Previous article:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 2)
Next article:Interrupt Priority of MCS-51 Series Microcontrollers

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

The simplest and most basic program for microcontroller to read and write SD card
  The simplest and most basic program for microcontroller to read and write SD card /> /> 2010-11-01 21:14   Reprinted from Daojin Kaisen   Final editor zlulu2008   Processor: s3c44b0 (arm7)   SD card and processor pin connection: MISO --> SIORxD MOSI --> SIOTxD CLK --> SCLK CS --> PE5   includes four
[Microcontroller]
Design of program-controlled three-phase AC power source based on C8051F016 microcontroller and power amplifier
Preface Program-controlled three-phase AC power sources are widely used in many industries such as metallurgy, communications, chemical industry, electric power and military industry. It is used in industrial automation control fields such as AC voltage regulation, power regulation, dimming and motor soft start. It ca
[Microcontroller]
Design of program-controlled three-phase AC power source based on C8051F016 microcontroller and power amplifier
MCU drives H-bridge circuit
    When I was chatting today, an idea suddenly popped up: the driving circuit of the remote control car is an H-bridge circuit, which should be driven by a single-chip microcomputer. If it can be driven, it can save a lot of trouble. In this way, the control loop only needs the minimum system of the single-chip microc
[Microcontroller]
MCU drives H-bridge circuit
Renesas MCU Learning Notes (1) Basic Configuration
      Choose different code editing tools and simulation tools according to your chip family       For 78K0 series microcontrollers, the recommended one is cubeSuite+ integrated development environment. For specific usage, please refer to the help manual.      I have been exposed to PIC and STC microcontrollers. R
[Microcontroller]
ADC Analysis of MCU_S3C2410
basic concept: 1. Sampling theorem, also known as Shannon sampling theorem, Nyquist sampling theorem: the sampling frequency must be greater than twice the highest frequency of the analog signal 2. The number of bits of A/D, that is, the resolution of A/D, determines the size of the quantization error (LSB) 1LSB = 1
[Microcontroller]
ADC Analysis of MCU_S3C2410
Assembling 51 microcontroller button detection
Without further ado, let’s start with the code: ORG 00H LOOP: JB P3.4,LOOP LCALL DELAY JB P3.4,LOOP LOOP1:JB P3.4,LOOP JB P1.0,TC1 SETB P1.0 JMPLOOP TC1: CLR P1.0 JMPLOOP DELAY: MOV R7,#50 D1: MOV R6,#50 D2: DJNZ R6,D2 DJNZ R7,D1 RET END I won't talk about the static display of the digital tube, the dynamic displa
[Microcontroller]
51 single chip microcomputer controls LCD display
In order to realize human-computer interaction, a display device is indispensable. This article mainly talks about how to control the LCD display, and on this basis, add the function of the timer, and move the timer that was originally displayed by the digital tube to the LCD screen. The LCD used here is LCD1602, wh
[Microcontroller]
51 single chip microcomputer controls LCD display
Implementing Complex Discrete Logic Using Microcontrollers
Developers can use the Configurable Logic Block (CLB) peripheral in the PIC16F13145 family of microcontrollers to implement complex discrete logic functions in hardware, thereby reducing the bill of materials (BOM) and developing custom application-specific logic. In many embedded system application
[Microcontroller]
Implementing Complex Discrete Logic Using Microcontrollers
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号