DS1820的PIC单片机接口程序
说明:DS1820是美国DALAS公司推出的单线串行数字温度计,测量范围为-50至+125摄氏度,精度为0.5摄氏度。DS1820的三端口分别是地,数据,电源。DS1820遵循严格的单线串行通信协议,每一个DS1820在出厂时都用激光进行了调较,并具有唯一的64位序列号。
PORTA EQU 0X05 ;端口A
PORTB EQU 0X06 ;端口B
PCL EQU 2 ;程序计数器PCL
DCOMMAND EQU 0X0C ;发送命令DCOMMAND
DDATA EQU 0X0D ;发送数据DDATA
COUNT0 EQU 0X0E ;通用寄存器
COUNT1 EQU 0X0F
COUNT2 EQU 0X10
COUNT3 EQU 0X11
COUNT4 EQU 0X12
COUNT5 EQU 0X13
FLAG EQU 0X14 ;标志位
TEMP EQU 0X15 ;由DS1820 测出的温度
;PORTA的RA0 是与DS1820通信的数据线
Reset
movlw b'00000000'
movwf PORTB
tris PORTB
START
call Reset1820
btfss FLAG,5
goto START
call Delay1ms
call Delay1ms
call Delay1ms
call Delay1ms
movlw 0xcc
movwf DCOMMAND
call WR1820
call Delay1ms
movlw 0x44
movwf DCOMMAND
call WR1820
bsf PORTA,0
call Delay1s
call Delay1s
START1
call Reset1820
btfss FLAG,5
goto START1
call Delay1ms
call Delay1ms
call Delay1ms
call Delay1ms
movlw 0xcc
movwf DCOMMAND
call WR1820
movlw 0xbe
movwf DCOMMAND
call WR1820
call Delay1ms
call RD1820
movf TEMP,w
movwf PORTB
call Delay1s
call Delay1s
call Delay1s
clrw
movwf PORTB
goto START
;读取所测量温度的子程序
;测出的温度存放在变量TEMP
RD1820
Clrf TEMP
RD18201
movlw 0x08
movwf COUNT4
RD18202
movlw b'11111110'
tris PORTA
nop
bcf PORTA,0
call Delay15us
movlw b'11111111'
tris PORTA
nop
bcf TEMP,7
btfsc PORTA,0
bsf TEMP,7
rrf TEMP,1
decfsz COUNT4,1
goto RD18202
return
WR1820 ;IN :DCOMMAND
movlw 0x08
movwf COUNT4
movlw b'11111110'
tris PORTA
WR18201
bcf PORTA,0
call Delay15us
nop
btfsc DCOMMAND,0
bsf PORTA,0
rrf DCOMMAND,1
call Delay15us
bsf PORTA,0
decfsz COUNT4,1
goto WR18201
bsf PORTA,0
return
Reset1820
movlw b'11111110'
tris PORTA
bcf PORTA,0
call Delay500us
bsf PORTA,0
movlw b'11111111'
tris PORTA
call Delay15us
call Delay15us
call Delay15us
call Delay15us
bcf FLAG,5
btfss PORTA,0
bsf FLAG,5
return
;以下是延时子程序组
Delay15us
nop ;-nop- Delay 11us
nop ;-call- and -return- Delay 2*2=4us
nop ;total Delay 4+11=15us
nop
nop
nop
nop
nop
nop
nop
nop
return
Delay500us
movlw 0x19 ;0x14 = 20D
movwf COUNT0
Delay500usLoop1
movlw 0x05
movwf COUNT1
Delay500usLoop2 ;100 * 5 = 500 us, but it is 506us in fact.
decfsz COUNT1,1
goto Delay500usLoop2
clrwdt
decfsz COUNT0,1
goto Delay500usLoop1
return
Delay1ms
movlw 0x28 ;0x25 = 40D = 28H
movwf COUNT0
Delay1ms_loop1
movlw 0x05
movwf COUNT1
Delay1ms_loop2 ;40 * 25 = 1000 us = 1ms
clrwdt ;清看门狗
decfsz COUNT1,1
goto Delay1ms_loop2
decfsz COUNT0,1
goto Delay1ms_loop1
return
Delay1s
movlw 0xce ;every time use 4.85ms, 1000/4.85=206.1D=CEH
movwf COUNT2
Delay1s_loop1
movlw 0x05
movwf COUNT3
Delay1s_loop2 ;4.85 ms * 206 = 1 s
call Delay1ms
decfsz COUNT3,1
goto Delay1s_loop2
decfsz COUNT2,1
goto Delay1s_loop1
return
;Delay program end
end
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。