51 single chip microcomputer-temperature sensor and serial port

Publisher:MysticalGardenLatest update time:2021-08-30 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Question requirements


Sometimes we need to know the historical data of the temperature measured by the temperature sensor over a period of time. The previous temperature sensor routine only displayed the data in real time on the LCD screen, and its historical data could not be viewed, so we used all the knowledge we learned before to complete this task.


First, we start with a simple concept and use the serial port to print out the current temperature value every second. In this way, all historical data can be seen in the computer window. However, the USB cable of our development board cannot leave the computer, so the temperature can only be measured by connecting it to the computer and observing the changes in the data in the computer window.


This time we use digital tube to display the real-time temperature. The data format printed by the serial port is as follows:


1st second: 10.2 degrees


2nd second: 15.7 degrees


3rd second: 7.6 degrees


4th second: 0.5 degrees


2.main.c test code


#include

#include //See Chapter 6, Lecture 8 for details

#include //See Chapter 8, Lecture 11 for details

#include //See Chapter 10, Lecture 4 for details

#include //See Chapter 13, Lecture 5 for details

 

u8 flag1s = 0;

void main()

{

    u32 x=1;

    u8 str[13];    

    u16 temp;

    EA = 1;  

    LED_Init();

    Start18B20(); //Start DS18B20

    TIM0_Init(1000,33); //Timing 1ms, 33 is fine-tuning to make the timing more accurate, each fine-tuning is determined by the content in the interrupt function

    ConfigUART(9600);

    while (1)

    {    

        if (flag1s) //Update the temperature once a second

        {

            flag1s = 0;

            if( TEMP_CONV(&temp,str)==1 ) //The return value is 1, indicating that the temperature reading is successful

            {

                printf_str("第");

                printf_num(x);

                x++;

                printf_str("Seconds:");

                printf_str(str+temp_i);

                printf_str("degrees");

                printf_rn(); //Carriage return and line feed

                temp_i=0;

 

                //The following is the content displayed by the digital tube

                LedBuff[0]=LedChar[str[4]-'0'];

                LedBuff[1]=0x7F; //Display decimal point

                LedBuff[2]=LedChar[str[2]-'0'];

 

                if(str[0]=='-') //temperature is less than 0

                {

                    if(str[1]=='0')

                    { 

                        LedBuff[3]=0xBF; //The digital tube displays "-"

                        LedBuff[4]=0xFF;

                    }

                    else

                    { 

                        LedBuff[3]=LedChar[str[1]-'0'];

                        LedBuff[4]=0xBF; //The digital tube displays "-"

                    }

                }

 

                else //The temperature is greater than or equal to 0

                { 

                    if(str[1]=='0')LedBuff[3]=0xFF; //The high bit does not display 0

                    else LedBuff[3]=LedChar[str[1]-'0'];

 

                    if(str[0]=='0')LedBuff[4]=0xFF; //The high bit does not display 0

                    else LedBuff[4]=LedChar[str[0]-'0'];

                }

            }

            Start18B20(); //Restart the next conversion

        }

    }

}

  

void TIM0_IRQHandler() interrupt 1

    static u16 tmr1s = 0;

    TH0 = T0RH; //Reload the reload value

    TL0 = T0RL;

   

    tmr1s++;

    SEG_Scan();

    if (tmr1s >= 1000) //Timer 1s

    {

        tmr1s = 0;

        flag1s= 1;

    }

}

  

void InterruptUART() interrupt 4

{

    if (RI) // Byte received

    {

        RI = 0; //Manually clear the receive interrupt flag                     

    }

}


3. Serial port printing screenshot


After I powered on the device, I deliberately blew hot air onto the temperature sensor on the development board, and the effect was great.

15.11.png

Reference address:51 single chip microcomputer-temperature sensor and serial port

Previous article:51 single chip microcomputer - remote control servo
Next article:51 MCU - Simulate reversing radar alarm

Latest Microcontroller Articles
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号