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

Publisher:和谐相伴Latest 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"reg51.h"

sbit DQ =P3^7; //define communication port

//Delay function
void delay(unsigned int i)
{
while(i--);
}

//Init function
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ reset
delay(8); //Slight delay
DQ = 0; //MCU pulls DQ down
delay(80); //Precise delay is 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 a
byteReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; //Give pulse signaldat
>>=1;
DQ = 1; //Give pulse signalif
(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}

//Write one byte
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;
}
}

//Read temperatureReadTemperature
(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); //Skip the operation of reading the serial number and column
numberWriteOneChar(0x44); //Start temperature conversionInit_DS18B20
();
WriteOneChar(0xCC); //Skip the operation of reading the serial number and column
numberWriteOneChar(0xBE); //Read temperature registers, etc. (9 registers can be read in total) The first two are temperaturesa
=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625; //Merge the high and low bits of the temperaturet
= tt*10+0.5; //Round the result
return(t);
}

//Digital codes from 0 to 9 respectively. Note that this code is only suitable for the QQ microcontroller experiment board. For codes on other circuit boards, please refer to the above algorithm to calculate them yourself.
unsigned char code Num[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

//QQ MCU private function, may not work well on other circuit boards
//Display any characters on 8 digital tubes, the character content is determined by 8 parameters LED1--LED8 If the displayed character is empty, this digital tube will not be scanned to shorten the scanning time
ShowAny(unsigned char LED1,unsigned char LED2,unsigned char LED3,unsigned char LED4,unsigned char LED5,unsigned char LED6,unsigned char LED7,unsigned char LED8)
{
if(LED1) { P2=0xEF; P0=LED1; delay(100); }
if(LED2) { P2=0xDF; P0=LED2; delay(100); }
if(LED3) { P2=0xBF; P0=LED3; delay(100); }
if(LED4) { P2=0x7F; P0=LED4; delay(100); }
if(LED5) { P2=0xFE; P0=LED5; delay(100); }
if(LED6) { P2=0xFD; P0=LED6; delay(500); }
if(LED7) { P2=0xFB; P0=LED7; delay(500); } if
(LED8) { P2=0xF7;

main()
{
unsigned int i=0;
while(1)
{
i=ReadTemperature(); //Read the current temperature
ShowAny(0,0,0,0,0,Num[i/100],Num[i/10%10] | 0x80,Num[i%10]); //Show the current temperature
}
}

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

Previous article:A design scheme of high-precision ultrasonic intelligent distance measurement system
Next article:Single chip ultrasonic sensor to measure distance

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号