×
单片机 > 单片机程序设计 > 详情

#单片机#ds18b20 单位转换 30度报警

发布时间:2020-06-22 发布时间:
|
#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;  //温度上限值,温度为乘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; //将总线拉低480us~960us
i=70;
while(i--);//延时642us
DSPORT=1;//然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
i=0;
while(DSPORT)//等待DS18B20拉低总线
{
i ;
if(i>5000)//等待>5MS
return 0;//初始化失败
}
return 1;//初始化成功
}
 
 
void Ds18b20WriteByte(unsigned char dat)
{
unsigned int i,j;
for(j=0;j<8;j )
{
DSPORT=0;//每写入一位数据之前先把总线拉低1us
i ;
DSPORT=dat&0x01; //然后写入一个数据,从最低位开始
i=6;
while(i--); //延时68us,持续时间最少60us
DSPORT=1;//然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
dat>>=1;
}
}
 
 
unsigned char Ds18b20ReadByte()
{
unsigned char byte,bi;
unsigned int i,j;
for(j=8;j>0;j--)
{
DSPORT=0;//先将总线拉低1us
i ;
DSPORT=1;//然后释放总线
i ;
i ;//延时6us等待数据稳定
bi=DSPORT; //读取数据,从最低位开始读取
byte=(byte>>1)|(bi<<7);  
i=4;//读取完之后等待48us再接着读取下一个数
while(i--);
}
return byte;
}
 
void  Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);//跳过ROM操作命令 
Ds18b20WriteByte(0x44);  //温度转换命令
}
 
void  Ds18b20ReadTempCom()
{
 
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳过ROM操作命令
Ds18b20WriteByte(0xbe); //发送读取温度命令
}
 
int Ds18b20ReadTemp()
{
int temp=0;
unsigned char tmh,tml;
Ds18b20ChangTemp(); //先写入转换命令
Ds18b20ReadTempCom();//然后等待转换完后发送读取温度命令
tml=Ds18b20ReadByte();//读取温度值共16位,先读低字节
tmh=Ds18b20ReadByte();//再读高字节
temp=tmh;
temp<<=8;
temp|=tml;
return temp;
}
void Display(int temp) //显示
{
unsigned char datas[] = {0, 0, 0, 0, 0}; //定义数组
float tp;
if(temp< 0)//当温度值为负数
{
//因为读取的温度是实际温度的补码,所以减1,再取反求出原码
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*10 0.5;
//留两个小数点就*100, 0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
//后面的数自动去掉,不管是否大于0.5,而 0.5之后大于0.5的就是进1了,小于0.5的就
//算加上0.5,还是在小数点后面。
}
else
{
tp=temp;//因为数据处理有小数点所以将温度赋给一个浮点型变量
//如果温度是正的那么,那么正数的原码就是补码它本身
temp=tp*0.0625*10 0.5;
//留两个小数点就*100, 0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
//后面的数自动去掉,不管是否大于0.5,而 0.5之后大于0.5的就是进1了,小于0.5的就
//算加上0.5,还是在小数点后面。
}
datas[0] = temp / 100;
datas = temp % 100 / 10;
datas = temp % 10;
P0=0xff;
P0 = table[datas[0]];
P2 = 0xfd;
Delay1ms(1);
 
P0=0xff;
P0 = table[datas] & 0x7f;
P2 = 0xfb;
Delay1ms(1);
P0=0xff;
P0 = table[datas];
P2 = 0xf7;
Delay1ms(1);
}
 
void Exter_Init()
{
EX0=1; //打开外部中断
EA=1; //打开总中断
IT0=0; //低电平触发
EX0 = 1;
IT0 = 1;
}
 
void main()
{
Exter_Init();
beep = 1;
while(1)
{
if(flag%2 == 1)//奇数次按键,显示华氏度
{
t = Ds18b20ReadTemp();   //华氏度=(摄氏度*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  //偶数次按键,显示摄氏度
{
t =  Ds18b20ReadTemp();
tmp = t*0.0625*10 0.5;
Display(Ds18b20ReadTemp());
if(tmp >= warn_d)//若温度超限,蜂鸣器报警
{
beep = 0;
Delay1ms(1);
beep = 1;
Delay1ms(1);
}
else beep = 1;
}
}
}
void exter0() interrupt 0
{
Delay1ms(5);//消抖
while(!key3);
flag ;
}
关键字:单片机  ds18b20  单位转换  30度报警 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
C51 特殊功能寄存器SFR的名称和地址