ds18b20 c程序
//DS1820 C51 子程序
//這里以11.0592M晶體為例,不同的晶體速度可能需要調(diào)整延時的時間
//sbit DQ =P2^1;//根據(jù)實際情況定義端口
typedef unsigned char byte;
typedef unsigned int word;
//延時
void delay(word useconds)
{
for(;useconds>0;useconds--);
}
//復(fù)位
byte ow_reset(void)
{
byte presence;
DQ = 0;?????????????? //pull DQ line low
delay(29);?????????? // leave it low for 480us
DQ = 1;?????????????? // allow line to return high
delay(3);???????????? // wait for presence
presence = DQ; // get presence signal
delay(25);?????????? // wait for end of timeslot
return(presence);??????????? // presence signal returned
}???????????????????????????????????? // 0=presence, 1 = no part
//從 1-wire 總線上讀取一個字節(jié)
byte read_byte(void)
{
byte i;
byte value = 0;
for (i=8;i>0;i--)
{
value>>=1;
DQ = 0;???????????????? // pull DQ low to start timeslot
DQ = 1;???????????????? // then return high
delay(1);?????????????? //for (i=0; i<3; i++);
if(DQ)value|=0x80;
delay(6);????????????? // wait for rest of timeslot
}
return(value);
}
//向 1-WIRE 總線上寫一個字節(jié)
void write_byte(char val)
{
byte i;
for (i=8; i>0; i--)????????? // writes byte, one bit at a time
{
DQ = 0;????????????????????? // pull DQ low to start timeslot
DQ = val&0x01;
delay(5);??????????????????? // hold value for remainder of timeslot
DQ = 1;
val=val/2;
}
delay(5);
}
//讀取溫度
char Read_Temperature(void)
{
union{
byte c[2];
int x;
}temp;
ow_reset();
write_byte(0xCC);????????? // Skip ROM
write_byte(0xBE);????????? // Read Scratch Pad
temp.c[1]=read_byte();
temp.c[0]=read_byte();
ow_reset();
write_byte(0xCC);?????????? //Skip ROM
write_byte(0x44);??????????? // Start Conversion
return temp.x/2;
}
ds18b20匯編程序:
本匯編程序僅適合單個DS18B20和51單片機的連接,晶振為12MHZ左右
DQ:DS18B20的數(shù)據(jù)總線接腳
FLAG1:標(biāo)志位,為"1"時表示檢測到DS18B20
TEMPER_NUM:保存讀出的溫度數(shù)據(jù)
TEMPER_LEQU36H
TEMPER_HEQU35H
DQBITP1.7
; DS18B20初始化匯編程序
;//*****************************************//
INIT_1820:
SETBDQ
NOP
CLRDQ
MOVR0,#06BH
TSR1:
DJNZR0,TSR1; 延時
SETBDQ
MOVR0,#25H
TSR2:
JNBDQ,TSR3
DJNZR0,TSR2
LJMPTSR4; 延時
TSR3:
SETBFLAG1; 置標(biāo)志位,表示DS1820存在
LJMPTSR5
TSR4:
CLRFLAG1; 清標(biāo)志位,表示DS1820不存在
LJMPTSR7
TSR5:
MOVR0,#06BH
TSR6:
DJNZR0,TSR6; 延時
TSR7:
SETBDQ
RET
;//*****************************************//
; 重新寫DS18B20暫存存儲器設(shè)定值
;//*****************************************//
RE_CONFIG:
JBFLAG1,RE_CONFIG1; 若DS18B20存在,轉(zhuǎn)RE_CONFIG1
RET
RE_CONFIG1:
MOVA,#0CCH; 發(fā)SKIP ROM命令
LCALLWRITE_1820
MOVA,#4EH; 發(fā)寫暫存存儲器命令
LCALLWRITE_1820
MOVA,#00H; TH(報警上限)中寫入00H
LCALLWRITE_1820
MOVA,#00H; TL(報警下限)中寫入00H
LCALLWRITE_1820
MOVA,#1FH; 選擇9位溫度分辨率
LCALLWRITE_1820
RET
;//*****************************************//
; 讀出轉(zhuǎn)換后的溫度值
;//*****************************************//
GET_TEMPER:
SETBDQ; 定時入口
LCALLINIT_1820
JBFLAG1,TSS2
RET; 若DS18B20不存在則返回
TSS2:
MOVA,#0CCH; 跳過ROM匹配
LCALLWRITE_1820
MOVA,#44H; 發(fā)出溫度轉(zhuǎn)換命令
LCALLWRITE_1820
LCALLINIT_1820
MOVA,#0CCH; 跳過ROM匹配
LCALLWRITE_1820
MOVA,#0BEH; 發(fā)出讀溫度命令
LCALLWRITE_1820
LCALLREAD_1820
MOVTEMPER_NUM,A; 將讀出的溫度數(shù)據(jù)保存
RET
;//*****************************************//
; 讀DS18B20的程序,從DS18B20中讀出一個字節(jié)的數(shù)據(jù)
;//*****************************************//
READ_1820:
MOVR2,#8
RE1:
CLRC
SETBDQ
NOP
NOP
CLRDQ
NOP
NOP
NOP
SETBDQ
MOVR3,#7
DJNZR3,$
MOVC,DQ
MOVR3,#23
DJNZR3,$
RRCA
DJNZR2,RE1
RET
;//*****************************************//
; 寫DS18B20的程序
;//*****************************************//
WRITE_1820:
MOVR2,#8
CLRC
WR1:
CLRDQ
MOVR3,#6
DJNZR3,$
RRCA
MOVDQ,C
MOVR3,#23
DJNZR3,$
SETBDQ
NOP
DJNZR2,WR1
SETBDQ
RET
;//*****************************************//
; 讀DS18B20的程序,從DS18B20中讀出兩個字節(jié)的溫度數(shù)據(jù)
;//*****************************************//
READ_18200:
MOVR4,#2; 將溫度高位和低位從DS18B20中讀出
MOVR1,#36H; 低位存入36H(TEMPER_L),高位存入35H(TEMPER_H)
RE00:
MOVR2,#8
RE01:
CLRC
SETBDQ
NOP
NOP
CLRDQ
NOP
NOP
NOP
SETBDQ
MOVR3,#7
DJNZR3,$
MOVC,DQ
MOVR3,#23
DJNZR3,$
RRCA
DJNZR2,RE01
MOV@R1,A
DECR1
DJNZR4,RE00
RET
;//*****************************************//
; 將從DS18B20中讀出的溫度數(shù)據(jù)進行轉(zhuǎn)換
;//*****************************************//
TEMPER_COV:
MOVA,#0F0H
ANLA,TEMPER_L; 舍去溫度低位中小數(shù)點后的四位溫度數(shù)值
SWAPA
MOVTEMPER_NUM,A
MOVA,TEMPER_L
JNBACC.3,TEMPER_COV1; 四舍五入去溫度值
INCTEMPER_NUM
TEMPER_COV1:
MOVA,TEMPER_H
ANLA,#07H
SWAPA
ORLA,TEMPER_NUM
MOVTEMPER_NUM,A; 保存變換后的溫度數(shù)據(jù)
LCALLBIN_BCD
RET
;//*****************************************//
; 將16進制的溫度數(shù)據(jù)轉(zhuǎn)換成壓縮BCD碼
;//*****************************************//
BIN_BCD:
MOVDPTR,#TEMP_TAB
MOVA,TEMPER_NUM
MOVCA,@A+DPTR
MOVTEMPER_NUM,A
RET
TEMP_TAB:
DB00H,01H,02H,03H,04H,05H,06H,07H
DB08H,09H,10H,11H,12H,13H,14H,15H
DB16H,17H,18H,19H,20H,21H,22H,23H
DB24H,25H,26H,27H,28H,29H,30H,31H
DB32H,33H,34H,35H,36H,37H,38H,39H
DB40H,41H,42H,43H,44H,45H,46H,47H
DB48H,49H,50H,51H,52H,53H,54H,55H
DB56H,57H,58H,59H,60H,61H,62H,63H
DB64H,65H,66H,67H,68H,69H,70H
;//*****************************************//
下面還介紹一個ds18b20匯編程序
;**********************************
?FLAG1?? BIT??? F0??????? ;DS18B20存在標(biāo)志位
?DQ????? BIT??? P1.7
?TEMPER_L??? EQU?? 29H
?TEMPER_H??? EQU?? 28H
?A_BIT????? EQU??? 35H
?B_BIT????? EQU???? 36H
?;************ds18b20匯編程序起始********************
???? ORG??? 0000H
???? AJMP?? MAIN
???? ORG??? 0100H
;**************主程序開始************
MAIN:
????? LCALL??? INIT_18B20
????? ;LCALL??? RE_CONFIG
????? LCALL??? GET_TEMPER
???? AJMP???? CHANGE
;**********DS18B20復(fù)位程序*****************
INIT_18B20:??? SETB?? DQ
?????????????? NOP
?????????????? CLR?? DQ
?????????????? MOV??? R0,#0FBH
???????? TSR1: DJNZ?? R0,TSR1?????? ;延時
?????????????? SETB?? DQ
?????????????? MOV??? R0,#25H
???????? TSR2: JNB?? DQ ,TSR3
?????????????? DJNZ?? R0,TSR2
???????? TSR3: SETB?? FLAG1?????? ;置標(biāo)志位,表明DS18B20存在
?????????????? CLR??? P2.0??????? ;二極管指示
?????????????? AJMP?? TSR5
???????? TSR4: CLR?? FLAG1
?????????????? LJMP?? TSR7
????????? TSR5: MOV?? R0,#06BH
????????? TSR6: DJNZ?? R0,TSR6
????????? TSR7:SETB?? DQ????????? ;表明不存在
??????????????? RET
;********************設(shè)定DS18B20暫存器設(shè)定值**************
;RE_CONFIG:
?????????? ;JB?? FLAG1,RE_CONFIG1
??????????? ;RET
;RE_CONFIG1: MOV? A,#0CCH?????? ;放跳過ROM命令
??????????? ;LCALL? WRITE_18B20
??????????? ;MOV?? A,#4EH
??????????? ;LCALL? WRITE_18B20? ;寫暫存器命令
??????????? ;MOV??? A,#00H??????? ;報警上限中寫入00H
??????????? ;LCALL? WRITE_18B20
??????????? ;MOV??? A,#00H???????? ;報警下限中寫入00H
?????????? ; LCALL? WRITE_18B20
?????????? ;MOV?? A,#1FH?????????? ;選擇九位溫度分辨率
?????????? ; LCALL? WRITE_18B20
?????????? ; RET
;*****************讀轉(zhuǎn)換后的溫度值****************
GET_TEMPER:
??????????? SETB?? DQ
??????????? LCALL? INIT_18B20
??????????? JB???? FLAG1,TSS2
??????????? RET????????????????? ;若不存在則返回
????? TSS2: MOV? A,#0CCH??????? ;跳過ROM
??????????? LCALL? WRITE_18B20
??????????? MOV??? A,#44H???????? ;發(fā)出溫度轉(zhuǎn)換命令
??????????? LCALL?? WRITE_18B20
??????????? LCALL? DISPLAY?????????????????????? ;延時
??????????? LCALL??? INIT_18B20
??????????? MOV? A,#0CCH??????? ;跳過ROM
??????????? LCALL? WRITE_18B20
??????????? MOV??? A,#0BEH???????? ;發(fā)出讀溫度換命令
??????????? LCALL?? WRITE_18B20
??????????? LCALL?? READ2_18B20??? ;讀兩個字節(jié)的溫度
??????????? RET
?;***************寫ds18b20匯編程序************
?WRITE_18B20:
??????????? MOV??? R2,#8
??????????? CLR??? C
??????? WR1:
????????????? CLR?? DQ
????????????? MOV? R3,#6
????????????? DJNZ? R3,$
????????????? RRC?? A
????????????? MOV??? DQ,C
????????????? MOV?? R3,#23
????????????? DJNZ?? R3,$
????????????? SETB??? DQ
????????????? NOP
????????????? DJNZ??? R2,WR1
????????????? SETB? DQ
????????????? RET
?;***********讀18B20程序,讀出兩個字節(jié)的溫度*********
?READ2_18B20:
??????????? MOV?? R4,#2????????????? ;低位存在29 H,高位存在28H
??????????? MOV?? R1,#29H
????? RE00: MOV? R2,#8
????? RE01: CLR?? C
??????????? SETB?? C
??????????? NOP
??????????? NOP
??????????? CLR? DQ
??????????? NOP
??????????? NOP
??????????? NOP
??????????? SETB? DQ
??????????? MOV?? R3,#7
??????????? DJNZ?? R3,$
??????????? MOV??? C,DQ
??????????? MOV??? R3,#23
???????????? DJNZ?? R3,$
??????????? RRC?? A
??????????? DJNZ?? R2,RE01
??????????? MOV??? @R1,A
??????????? DEC??? R1
??????????? DJNZ?? R4,RE00
??????????? RET
?;************讀出的溫度進行數(shù)據(jù)轉(zhuǎn)換**************
CHANGE:???? MOV A,29H
??????????? MOV C,28H.0?????????? ;將28H中的最低位移入C
??????????? RRC A
??????????? MOV C,28H.1
??????????? RRC A
??????????? MOV C,28H.2
??????????? RRC A
??????????? MOV C,28H.3
??????????? RRC A
??????????? MOV 29H,A
??????????? ;setb? p2.0
??????????? LCALL? DISPLAY????? ;調(diào)用數(shù)碼管顯示子程序
?????????? ; setb P2.0
??????????? LJMP MAIN
?;*******************DISPLAY******
?DISPLAY:??? mov a,29H;將29H中的十六進制數(shù)轉(zhuǎn)換成10進制
???????????? mov b,#10 ;10進制/10=10進制
???????????? div ab
???????????? mov b_bit,a ;十位在a
???????????? mov a_bit,b ;個位在b
???????????? mov dptr,#TAB ;指定查表啟始地址
???????????? mov r0,#4
???????????? dpl1: mov r1,#250 ;顯示1000次
???????????? dplop: mov a,a_bit ;取個位數(shù)
???????????? MOVC A,@A+DPTR ;查個位數(shù)的7段代碼
???????????? mov p0,a ;送出個位的7段代碼
???????????? clr p2.5;開個位顯示
???????????? acall d1ms ;顯示1ms
???????????? setb p2.5
???????????? mov a,b_bit ;取十位數(shù)
???????????? MOVC A,@A+DPTR ;查十位數(shù)的7段代碼
???????????? mov p0,a ;送出十位的7段代碼
???????????? clr p2.4;開十位顯示
???????????? acall d1ms ;顯示1ms
???????????? setb p2.4
???????????? djnz r1,dplop ;100次沒完循環(huán)
???????????? djnz r0,dpl1 ;4個100次沒完循環(huán)
???????????? ret
?;***********************************
???????????? D1MS: MOV R7,#80?? ;1MS延時(按12MHZ算)
???????????? DJNZ R7,$
???????????? RET
?;*************************
?TAB:???????? DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
評論
查看更多