stc89c52+18b20共同完成温度采集
//////////////////以下是DS18B20驱动程序////////////////
//延时函数
void delay1(uint i)
{
while(i--);
}
//延时函数
void delay1(uint i)
{
while(i--);
}
//初始化函数
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay1(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay1(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay1(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay1(20);
}
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay1(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay1(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay1(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay1(20);
}
//读一个字节
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--){
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ) dat|=0x80;
delay1(4);
}
return(dat);
}
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--){
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ) dat|=0x80;
delay1(4);
}
return(dat);
}
//写一个字节
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--){
DQ = 0;
DQ = dat&0x01;
delay1(5);
DQ = 1;
dat>>=1;
}
}
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--){
DQ = 0;
DQ = dat&0x01;
delay1(5);
DQ = 1;
dat>>=1;
}
}
//读取温度
ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625; //将温度的高位与低位合并
t= tt*10+0.5; //对结果进行4舍5入
return(t);
}
//////////////////以上是DS18B20驱动程序////////////////
/*定义数字ascii编码*/
unsigned char mun_char_table[]={"0123456789abcdef"};
main()/*主程序实现顺序循环检测*/
{
uint i;
ReadTemperature(); //读取当前温度
lcd_system_reset(); /*LCD1602 初始化*/
lcd_bad_check(); /*LCD1602 坏点检查*/
while(1)
{
i=ReadTemperature(); //读取当前温度
lcd_char_write(6,0,mun_char_table[i/100]); /*把温度显示出来*/
lcd_char_write(7,0,mun_char_table[i%100/10]);
lcd_char_write(8,0,.);
lcd_char_write(9,0,mun_char_table[i%10]);
if(i>=290)/*温度报警值设定,目前设定为29度*/
{
dula=1;
beep=0;
delay1(100);
beep=1;
}
}
ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625; //将温度的高位与低位合并
t= tt*10+0.5; //对结果进行4舍5入
return(t);
}
//////////////////以上是DS18B20驱动程序////////////////
/*定义数字ascii编码*/
unsigned char mun_char_table[]={"0123456789abcdef"};
main()/*主程序实现顺序循环检测*/
{
uint i;
ReadTemperature(); //读取当前温度
lcd_system_reset(); /*LCD1602 初始化*/
lcd_bad_check(); /*LCD1602 坏点检查*/
while(1)
{
i=ReadTemperature(); //读取当前温度
lcd_char_write(6,0,mun_char_table[i/100]); /*把温度显示出来*/
lcd_char_write(7,0,mun_char_table[i%100/10]);
lcd_char_write(8,0,.);
lcd_char_write(9,0,mun_char_table[i%10]);
if(i>=290)/*温度报警值设定,目前设定为29度*/
{
dula=1;
beep=0;
delay1(100);
beep=1;
}
}
关键词:
stc89c5218b20共温度采
评论