#singlechip#ds18b20 unit conversion 30 degree alarm

Publisher:XinmeiLatest update time:2015-09-25 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include "reg52.h"
#define uchar unsigned char
#define uint   unsigned int
sbit DSPORT=P3^7;
sbit key3 = P3^2;
sbit beep = P1^0;
uchar flag;
int tmp;
float t;
uint warn_d = 300;   // upper temperature limit, the temperature is the value after multiplying by 10
uchar code table[] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
    0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
 
 
void Delay1ms(unsigned int y)
{
unsigned int x;
for(y;y>0;y--)
for(x=110;x>0;x--);
}
 
unsigned char Ds18b20Init()
{
unsigned int i;
DSPORT=0; //Pull the bus down for 480us~960us
i=70;
while(i--); //delay 642us
DSPORT=1; //Then pull the bus high. If DS18B20 responds, it will pull the bus low after 15us~60us
i=0;
while(DSPORT) //Wait for DS18B20 to pull the bus low
{
i ;
if(i>5000) //wait>5MS
return 0; // Initialization failed
}
return 1; // Initialization successful
}
 
 
void Ds18b20WriteByte(unsigned char dat)
{
unsigned int i,j;
for(j=0;j<8;j )
{
DSPORT=0; //Pull the bus down for 1us before writing each bit of data
i ;
DSPORT=dat&0x01; //Then write a data, starting from the lowest bit
i=6;
while(i--); //Delay 68us, duration at least 60us
DSPORT=1; //Then release the bus, at least 1us is needed for the bus to recover before writing the second value
dat>>=1;
}
}
 
 
unsigned char Ds18b20ReadByte()
{
unsigned char byte,bi;
unsigned int i,j;
for(j=8;j>0;j--)
{
DSPORT=0; //Pull the bus down for 1us first
i ;
DSPORT=1; //Then release the bus
i ;
i ; // Delay 6us to wait for data to stabilize
bi=DSPORT; //Read data, starting from the lowest bit
byte=(byte>>1)|(bi<<7);  
i=4; //After reading, wait 48us before reading the next number
while(i--);
}
return byte;
}
 
void   Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //Skip ROM operation command 
Ds18b20WriteByte(0x44);   //temperature conversion command
}
 
void   Ds18b20ReadTempCom()
{
 
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //Skip ROM operation command
Ds18b20WriteByte(0xbe); //Send the command to read temperature
}
 
int Ds18b20ReadTemp()
{
int temp=0;
unsigned char tmh,tml;
Ds18b20ChangTemp(); //Write the conversion command first
Ds18b20ReadTempCom(); //Then wait for the conversion to complete and then send the temperature reading command
tml=Ds18b20ReadByte(); //Read the temperature value, a total of 16 bits, read the low byte first
tmh=Ds18b20ReadByte(); // read high byte again
temp=tmh;
temp<<=8;
temp|=tml;
return temp;
}
void Display(int temp) //Display
{
unsigned char datas[] = {0, 0, 0, 0, 0}; //define array
float tp;
if(temp< 0) //When the temperature value is negative
{
//Because the temperature read is the complement of the actual temperature, subtract 1 and then invert it to get the original code
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*10 0.5;
//Leave two decimal points and *100. 0.5 is rounded because the decimal point is rounded when the C language floating point number is converted to an integer.
//The numbers behind are automatically removed, regardless of whether they are greater than 0.5. After 0.5, the numbers greater than 0.5 are incremented, and the numbers less than 0.5 are incremented.
// Calculate and add 0.5, or after the decimal point.
}
else
{
tp=temp; //Because the data processing has a decimal point, the temperature is assigned to a floating point variable
//If the temperature is positive, then the original code of the positive number is the complement code itself
temp=tp*0.0625*10 0.5;
//Leave two decimal points and *100. 0.5 is rounded because the decimal point is rounded when the C language floating point number is converted to an integer.
//The numbers behind are automatically removed, regardless of whether they are greater than 0.5. After 0.5, the numbers greater than 0.5 are incremented, and the numbers less than 0.5 are incremented.
// Calculate and add 0.5, or after the decimal point.
}
datas[0] = temp / 100;
datas [1] = temp % 100 / 10;
datas [2] = temp % 10;
P0=0xff;
P0 = table[datas[0]];
P2 = 0xfd;
Delay1ms(1);
 
P0=0xff;
P0 = table[datas [1]] & 0x7f;
P2 = 0xfb;
Delay1ms(1);
P0=0xff;
P0 = table[datas [2]];
P2 = 0xf7;
Delay1ms(1);
}
 
void Exter_Init()
{
EX0=1; //Open external interrupt
EA=1; //Open the general interrupt
IT0=0; //Low level trigger
EX0 = 1;
IT0 = 1;
}
 
void main()
{
Exter_Init();
beep = 1;
while(1)
{
if(flag%2 == 1) // odd number of key presses, display Fahrenheit
{
t = Ds18b20ReadTemp();    //Fahrenheit = (Celsius*9)/5 32
t = t*0.0625*10 0.5;
tmp = t*1.8 320;
 
P0=0xff;
P0 = table[15];
P2 = 0xfe;
Delay1ms(1);
 
P0=0xff;
P0 = table[tmp/100];
P2 = 0xfd;
Delay1ms(1);
P0=0xff;
P0 = table[tmp0/10] & 0x7f;
P2 = 0xfb;
Delay1ms(1);
P0=0xff;
P0 = table[tmp];
P2 = 0xf7;
Delay1ms(1);
}
else   //Even number of key presses, display degrees Celsius
{
t =   Ds18b20ReadTemp();
tmp = t*0.0625*10 0.5;
Display(Ds18b20ReadTemp());
if(tmp >= warn_d) //If the temperature exceeds the limit, the buzzer will sound an alarm
{
beep = 0;
Delay1ms(1);
beep = 1;
Delay1ms(1);
}
else beep = 1;
}
}
}
void exter0() interrupt 0
{
Delay1ms(5); //Debounce
while(!key3);
flag ;
}
Keywords:MCU Reference address:#singlechip#ds18b20 unit conversion 30 degree alarm

Previous article:51 MCU expands external RAM
Next article:#Single-chipcomputer#Matrix keyboard-electronic piano

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号