stc DS18B0 超温报警
细节
开关蜂鸣器 led
/************************/
P2 = (P2 & 0x1f) | 0x80; //数码管 Y4C
P0 = 1; //全开
P0 = 0; //全关 (可以只开几个 如0x66)
P2 = 0x1f; //关闭数码管
#define LOCKY4 P2=(P2&0x1f)|0x80
#define LOCKY5 P2=(P2&0x1f)|0xA0 //蜂鸣器 Y5C
#define LOCKY6 P2=(P2&0x1f)|0xC0 //位选 Y6C
#define LOCKY7 P2=(P2&0x1f)|0xe0 //段选 Y7C
#define LOCKOFF P2=0x1f
main.c
#include <stc15f2k60s2.h>
#include "onewire.h"
unsigned char duanma[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //共阳
unsigned char duanma_x[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; //带小数点的共阳
unsigned int temp=0;
void Delay_SMG(unsigned int t)
{
while(t--);
}
void DisplaySMG_Bit(unsigned char pos,unsigned char dat)
{
P2=0xE0;P0=0xff; //先全部关掉数码管。避免显示不正常 Y4C-led
P2=0xC0;P0=0x01<<pos; //显示在哪个管子上 Y6C-位选 Y7C-段选
P2=0xE0;P0=dat; //显示什么内容
}
void DisplaySMG_temp()
{
DisplaySMG_Bit(7,duanma[temp%10]); //显示小数点后第3位
Delay_SMG(100);
DisplaySMG_Bit(6,duanma[(temp/10)%10]); //显示小数点后第2位
Delay_SMG(100);
// DisplaySMG_Bit(6,0x80); //显示小数点后第2位
// Delay_SMG(100);
DisplaySMG_Bit(5,duanma[(temp/100)%10]); //显示小数点后第1位
Delay_SMG(100);
DisplaySMG_Bit(4,duanma_x[(temp/1000)%10]); //显示带小数点的个位
Delay_SMG(100);
DisplaySMG_Bit(3,duanma[temp/10000]); //显示十位
Delay_SMG(100);
DisplaySMG_Bit(2,0xff); //不显示
Delay_SMG(100);
DisplaySMG_Bit(1,0xff); //不显示
Delay_SMG(100);
DisplaySMG_Bit(0,0xff); //不显示
Delay_SMG(100);
// DisplaySMG_Bit(0,0xff); //不显示
// Delay_SMG(100);
P2=0xC0;P0=0xff; //关LED灯
P2=0xE0;P0=0xff; //关数码管
}
void Delay(unsigned int t)
{
while(t--)
{
DisplaySMG_temp();
}
}
void Read_DS18B20_temp()
{
unsigned char LSB,MSB;
init_ds18b20(); //DS18B20复位
Write_DS18B20(0xcc); //跳过ROM操作指令
Write_DS18B20(0x44); //开始温度转换
Delay(1000); //延时700ms左右,等待温度转换完成
init_ds18b20(); //DS18B20复位
Write_DS18B20(0xcc); //跳过ROM操作指令
Write_DS18B20(0xbe); //开始读取高速暂存器
LSB=Read_DS18B20(); //读取温度数据的低8位
MSB=Read_DS18B20(); //读取温度数据的高8位
temp=MSB;
temp=(temp<<8)| LSB; //将LSB和MSB整合成为一个16位的整数
//首先通过温度数据的高5位判断采用结果是正温度还是负温度
if((temp&0xf800)==0x0000) //正温度的处理方法
{
temp>>=4; //取出温度结果的整数部分
temp=temp*1000; //放大1000倍,然后加上小数部分
temp=temp+(LSB& 0x0f)*6.25;
}
if(temp > 29000)
{
P2 = (P2&0x1f)|0xA0 ;//开beer
P0 = 0xff;
Delay_SMG(300000);
P0 = 0;
P2 = 0x1f; //关beer
//
// P0 = (P2&0x1f)|0xA0 ;
// Delay_SMG(3);
// P0 = 0x1f;
P2=(P2&0x1f)|0x80; //亮ked
Delay_SMG(3000);
}
else{
P2 = 0x1f;
}
}
void main()
{
P2=0x80;P0=0xff; //关LED灯
while(1)
{
Read_DS18B20_temp();
DisplaySMG_temp();
}
}
onewire.h
#ifndef_ONEWIRE_H
#define_ONEWIRE_H
unsigned char rd_temperature(void);
bit init_ds18b20(void);
unsigned char Read_DS18B20(void);
void Write_DS18B20(unsigned char dat);
#endif
onewire.c
#include "stc15f2k60s2.h"
sbit DQ=P1^4;
void Delay_OneWire(unsigned int t)
{
while(t--);
}
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ=0;
DQ=dat & 0x01;
Delay_OneWire(50);
DQ=1;
dat >>= 1;
}
Delay_OneWire(50);
}
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ=0;
dat >>= 1;
DQ=1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(50);
}
return dat;
}
bit init_ds18b20(void)
{
bit initflag=0;
DQ=1;
Delay_OneWire(120);
DQ=0;
Delay_OneWire(800);
DQ=1;
Delay_OneWire(100);
initflag=DQ;
Delay_OneWire(50);
return initflag;
}