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 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
-
DS18B20
+關注
關注
10文章
778瀏覽量
80694
發布評論請先 登錄
相關推薦
評論