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

Publisher:Xiaohan521Latest update time:2016-09-21 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#define uchar unsigned char
#define uint unsigned int

sbit DQ=P2^7; //define interface

uint temp; // variable of temperature defines a variable

uchar flag1; // sign of the result positive or negative Define
a flag to indicate whether the temperature is still positive

sbit P2_0=P2^0; //digital tube bit selection
sbit P2_1=P2^1;
sbit P2_2=P2^2;

unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82, 0xf8,0x80,0x90}; //digital encoding

unsigned char code table1[]={0x40,0x79,0x24,0x30,
0x19,0x12,0x02, 0x78,0x00,0x10}; //Code with decimal point

void delay(uint i) //delay delay subroutine
{
     while(i--);
}


/*******************************************************************/
/* Initialize ds18b2 sub-function* */
/*******************************************************************/
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ reset
delay(8); //Slight delay
DQ = 0; //MCU pulls DQ low
delay(80); //Precise delay greater than 480us
DQ = 1; //Pull the bus high
delay(14);
x=DQ; //After a slight delay, if x=0, initialization is successful, if x=1, initialization fails
delay(20);
}


/***********************************************************************/
/* Read byte sub-function */
/*******************************************************************/
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
   DQ = 0; // Give pulse signal
   dat>>=1;
   DQ = 1; // Give pulse signal
   if(DQ)
    dat|=0x80;
   delay(4);
}
return(dat);
}
/************************************************************************/
/* Write byte sub-function */
/************************************************************************/
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
   DQ = 0;
   DQ = dat&0x01;
   delay(5);
   DQ = 1;
   dat>>=1;
}
}

/*void tmpwritebyte(uchar dat) //write a byte to ds18b20 Write a byte to the temperature sensor
{
   uint i;
   uchar j;
   bit testb;
   for(j=1;j<=8;j++)
   {
     testb=dat&0x01;
     dat=dat>>1;
     if(testb) //write 1
     {
       DQ=0;
       i++;i++;
       DQ=1;
       i=8;while(i>0)i--;
     }
     else
     {
       DQ=0; //write 0
       i=8;while(i>0)i--;
       DQ=1;
       i++;i++;
     }
   }
}*/

void tmpchange(void) //DS18B20 begin change send temperature conversion command
{
   Init_DS18B20(); //Initialize DS18B20
   delay(200); //Delay
   WriteOneChar(0xcc); //Skip serial number command
   WriteOneChar(0x44); //Send temperature conversion command
}

uint tmp() //get the temperature Get the temperature value
{
   float tt;
   uchar a,b;
   Init_DS18B20();
   delay(1);
   WriteOneChar(0xcc);
   WriteOneChar(0xbe); //Send the read data command
   a=ReadOneChar(); //Continuously read two bytes of data
   b=ReadOneChar();
   temp=b;
   temp<<=8;                                               
   temp=temp|a; //Two bytes are combined into an integer variable.
   tt=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 data represents
                                                            //0.0625 degrees.
   temp=tt*10+0.5; //Magnify ten times, the purpose of doing this is to convert the first digit after the decimal point
                                                            //to a displayable number, and perform a rounding operation at the same time.
   return temp; //Return the temperature value
}

/*void readrom() //read the serial Read the serial number of the temperature sensor
{ //This function is not used in this program
   uchar sn1, sn2;
   Init_DS18B20();
   delay(1);
   WriteOneChar(0x33);
   sn1=ReadOneChar();
   sn2=ReadOneChar();
}*/

void delay10ms() //delay 10MS sub-function
   {
     uchar a,b;
     for(a=50;a>0;a--)
       for(b=60;b>0;b--);
    }
void display(uint tem) //display display sub-function
   {
      uchar A1,A2,A2t,A3;
   if(tem>=100)
   {
         A1=table[tem/100];
         A2t=tem%100;
         A2=table1[A2t/10];
         A3=table[A2t%10];
   }
   else
   {
      A2t=tem%100;
         A1=table1[A2t/10];
         A2=table[A2t%10];
   A3=0xFF;
   }
        P0=A1;
        P2_0=0;
        P2_1=1;
        P2_2=1;
        delay10ms();
        P0=A2;
        P2_1=0;
        P2_0=1;
        P2_2=1;
        delay10ms();
     if(A3!=0xFF)
     {
          P0=A3;
          P2_2=0;
          P2_0=1;
          P2_1=1;
     }
    }

void main() //Main function
{
   do
   {
     tmpchange(); //Temperature conversion,
     display(tmp()); //Display temperature value
   }
      while(1);
}

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

Previous article:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 6)
Next article:C language program using DS18B20 temperature sensor in single chip microcomputer (reference 4)

Recommended ReadingLatest update time:2024-11-17 00:19

[Bluebridge Cup MCU Group] PCF8591 Complaints about Bluebridge Cup's I2C
According to the timing of the datasheet: Programming steps: Initialize AD, transmit sampling channel and input mode to AD (write process, above picture) (1) Open the bus (2) Send ADDRESS (BIT0 = 0) (3) Waiting for PCF response (4) Send control bit (refer to the figure below for control) (5) Wait for PCF r
[Microcontroller]
[Bluebridge Cup MCU Group] PCF8591 Complaints about Bluebridge Cup's I2C
Renesas MCU factory in Japan struck by lightning, causing up to 2 weeks of production loss
On July 6, Renesas Electronics said in a statement that at 5:43 a.m. on July 5, Japan time, due to heavy rains caused by Typhoon Aere No. 4, a transmission line at the company's Kawajiri factory in Kumamoto City, Kumamoto Prefecture was struck by lightning, causing an instantaneous voltage drop, causing the factory's
[Automotive Electronics]
Renesas MCU factory in Japan struck by lightning, causing up to 2 weeks of production loss
Use the internal MSI oscillator to provide 80MHz clock to the STM32L476RG microcontroller
#include stdio.h #include stm32l4xx.h   // 1 =nus =13107 void delay_us(uint16_t nus) { if ((RCC- APB1ENR2 & RCC_APB1ENR2_LPTIM2EN) == 0) { RCC- APB1ENR2 |= RCC_APB1ENR2_LPTIM2EN; LPTIM2- CFGR = 4 LPTIM_CFGR_PRESC_Pos; // 80MHz/16=5MHz LPTIM2- CR = LPTIM_CR_ENABLE; } LPTIM2- ARR = nus * 5 - 1; LPTIM2- CR |=
[Microcontroller]
Automatic dialer implemented with W78E51 single chip microcomputer
The automatic dialer is an intelligent alarm monitoring device. It uses a single-chip microcomputer as its core device and can send alarms to designated BP machines at any time using wireless paging and program-controlled telephones. When equipped with different sensors, it can not only alarm for toxic gas leaks, ho
[Microcontroller]
Automatic dialer implemented with W78E51 single chip microcomputer
Be careful when learning MCU
Now, some people have some fixed ideas about learning. When it comes to learning MCU, they will ask what to learn first. Some people say that they should learn C language first, then buy a MCU tutorial book, and then read it from beginning to end, which is equivalent to learning MCU. However, I want to say that the ch
[Microcontroller]
Application of C504 microcontroller in stepper motor control
    Abstract: C504 is a single-chip microcontroller produced by the German SIEMENS company. This article introduces the application of C504 single-chip microcomputer in stepper motor control, and gives a method of using the resource advantages of C504 to control stepper motors through certain expansions. Control syst
[Industrial Control]
The microcontroller performs different functions by pressing a single button (long or short press)
If you want to save the IO port of the single-chip microcomputer, you can use a button to execute different subroutines by judging the pressing time. I wrote one at my leisure. It may not be concise enough, but it has all the functions. Proteus simulation can be produced. There are three kinds of button time. Short pr
[Microcontroller]
The microcontroller performs different functions by pressing a single button (long or short press)
ADC Button Design Techniques Based on AVR Microcontroller
1 Introduction In the field of single-chip microcomputer applications, the ways and methods of human-computer interaction are flexible and diverse, but keyboard input is still the most common and important method. The usual ways to implement keyboard interface are: direct input, matrix structure, A/D mode, keyb
[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号