电子说
DS18B20一般都是由64位ROM、温度传感器、非挥发的温度报警触发器TH和TL、配置寄存器组成。ROM的作用是使每一个DS18B20都各不相同,这样就可以实现一根总线上挂接多个DS18B20的目的。
DS18B20的工作过程:
1.复位操作
2.执行ROM操作的5条指令之一:1)读ROM,2)匹配ROM,3)搜索ROM,4)跳过ROM,5)报警搜索。
3.存储器操作命令:温度转换、读取温度、设定上下限温度值等指令
4.读取温度数据:主机读取温度数据后进行数据处理。
应用实例
数字电子温度计
功能:显示当前温度值,可显示正和负温度值
程序:
#include《reg52.h》
/*------SEG PORT-----*/
#define LEDADDR P2
#define LED_port P0
/*------DS18B20 PORT-----*/
sbit DB_B20=P0^0;
/*-----COMMON FUNCTION----*/
void delay_us(unsigned int time);
/*-----SEG FUNCTION-------*/
unsigned char seg_val[6];
unsigned char ledcode[12] = {0XC0,0XF9,0XA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xbf,0xff};//共阳极数码管0,1,2,3,4,5,6,7,8,9,-,null
unsigned char currled=0;
unsigned char led_en=0x04;//
void display(void);
void display_tem(void);
/*--- DS18B20 FUNCTION-----*/
unsigned char tem_pnt;
unsigned char tem_num;
bit tem_sign;
bit reset_B20(void);
unsigned char read_b20_byte(void);
void write_b20_byte(unsigned char);
void convert_cmd_b20(void);
void read_cmd_b20(void);
void data_convert_b20(void);
/*--- TIME FUNCTION-----*/
unsigned char times;
void times_init(void);
/*-----------------------*/
main()
{
times_init();
while(1)
{
display_tem();
display();
}
}
/*-----COMMON FUNCTION----*/
void delay_us(unsigned int us)
{
while(us--);//for(;us》0;us--);//while(time--);
}
/*-----SEG FUNCTION-------*/
void display(void)
{
LEDADDR=0xff;
LED_port=(LED_port&0x03)|led_en;
led_en《《=1;
if(currled==1)
LEDADDR=ledcode[seg_val[currled]]& 0x7f;
else
LEDADDR=ledcode[seg_val[currled]];
currled++;
if(currled==6)
{
currled=0;
led_en=0x04;
}
delay_us(300);
}
void display_tem(void)
{
unsigned char n;
unsigned char p;
if(tem_sign==0)
{
n=~(tem_num-1);
p=~(tem_pnt-1);
}
else
{
n=tem_num;
p=tem_pnt;
}
seg_val[5]=11;
seg_val[4]=11;
seg_val[3]=n/100;
n=n%100;
seg_val[2]=n/10;
if(seg_val[3]==0)
{
seg_val[3]=11;
if(seg_val[2]==0)
seg_val[2]=11;
}
seg_val[1]=n%10;
seg_val[0]=p;
if(seg_val[2]==11 && seg_val[3]==11 && tem_sign==0)
seg_val[2]=10;
else if(seg_val[3]==11 && tem_sign==0)
seg_val[3]=10;
else if(tem_sign==0)
seg_val[4]=10;
}
/*--- DS18B20 FUNCTION-----*/
bit reset_B20(void)
{
bit state;
DB_B20=0;
delay_us(29);//》480us 29
DB_B20=1;
delay_us(3);//15---60us 3
state=DB_B20;
delay_us(25);//480us 25
return state;
}
void write_b20_byte(unsigned char cmd)
{
unsigned char i;
for(i=0;i《8;i++)
{
DB_B20=0;
DB_B20=cmd&0x01;
delay_us(5);
DB_B20=1;
cmd》》=1; //6
}
delay_us(5);
}
unsigned char read_b20_byte(void)
{
unsigned char rbyte=0;
unsigned char i;
for(i=0;i《8;i++)
{
rbyte》》=1;
DB_B20=0;
DB_B20=1;
//delay_us(1);
if(DB_B20)
rbyte |= 0x80;
delay_us(6); //6
}
return rbyte;
}
void convert_cmd_b20(void)
{
reset_B20();
write_b20_byte(0xcc);
write_b20_byte(0x44);
}
void read_cmd_b20(void)
{
reset_B20();
write_b20_byte(0xcc);
write_b20_byte(0xbe);
}
void data_convert_b20(void)
{
unsigned char a;
unsigned char b;
int point;
a=read_b20_byte();
b=read_b20_byte();
point=(a & 0x0f)*625+500;
tem_pnt=point/1000;
tem_num=a》》4 | b《《4;
if(tem_num & 0x80)
tem_sign=0;
else
tem_sign=1;
}
/*--- TIME FUNCTION-----*/
void times_init(void)
{
times=29;
TMOD=0X01;
TH0=0X3C;
TL0=0XB0;
EA=1;
ET0=1;
TR0=1;
}
void time0(void) interrupt 1
{
if(times==19)
convert_cmd_b20();
else if(times==9)
read_cmd_b20();
else if(times==0)
{
data_convert_b20();
times=29;
}
times--;
TH0=0X3C;
TL0=0XB0;
}
/*---------------------*/
整合自:李天生博客园、CSDN_朱坚强
编辑:jq
全部0条评论
快来发表一下你的评论吧 !