英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流
标题:
用PIC编写的读DS18B20的程序
[打印本页]
作者:
winnie
时间:
2009-5-21 09:48
标题:
用PIC编写的读DS18B20的程序
;---------------------------------
; 单线通信的温度IC
; DS1820
; 实用程序
;---------------------------------
ORG PIC54
GOTO MAIN
ORG 0
;----------------------
;----------------------------
DELAY22
MOVLW D'200' ; DELAY 2*250=500mS
MOVWF COUNT1
DE32 MOVLW D'250' ; 8*250=2mS
MOVWF COUNT2
DE42 NOP ; 1+2+1=5uS
DECFSZ COUNT2,1
GOTO DE42
DECFSZ COUNT1,1
GOTO DE32
RETLW 0
;--------------------------------
; 实用程序范例
;********************************
MAIN
MOVLW 0FFH
TRIS RA
LOOP
CALL CONVERT
CALL DELAY22
CALL DELAY22
CALL GET_TEMP
GOTO LOOP
;------------------------
GET_TEMP
CALL INI
MOVLW 0CCH
MOVWF COMD
CALL WR1820
MOVLW 0BEH
MOVWF COMD
CALL WR1820
CALL RE1820
MOVF TEM_DATE,W
MOVWF TEM_DATEL
CALL RE1820
MOVF TEM_DATE,W
MOVWF TEM_DATEH
RETLW 0
;------------------------
CONVERT
CALL INI
MOVLW 0CCH
MOVWF COMD
CALL WR1820
MOVLW 44H
MOVWF COMD
CALL WR1820
RETLW 0
;---------DS1820---------
INI
;MOVLW B'00100000'
MOVLW 0
TRIS RA
BSF RA,3
NOP
BCF RA,3
MOVLW D'200'
MOVWF COUNT1
L0
DECFSZ COUNT1,1
GOTO L0
;MOVLW B'01100000'
MOVLW 0FH
TRIS RA
MOVLW D'30'
MOVWF COUNT1
L1
DECFSZ COUNT1,1
GOTO L1
BTFSC RA,3
GOTO INI
L2
MOVLW D'80'
MOVWF COUNT1
L3
DECFSZ COUNT1,1
GOTO L3
BTFSS RA,3
GOTO L0
RETLW 0
;-------------------------
WR1820
MOVLW 8
MOVWF COUNT2
WR0
;MOVLW B'00100000'
MOVLW 0
TRIS RA
BCF RA,3
NOP
NOP
MOVLW 2
MOVWF COUNT1
WR1
DECFSZ COUNT1,1
GOTO WR1
;MOVLW B'01111111'
MOVLW 0FH
BTFSC COMD,0
BSF RA,3
BTFSC COMD,0
TRIS RA
BCF STATUS,C
RRF COMD,1
MOVLW D'20'
MOVWF COUNT1
WR2
DECFSZ COUNT1,1
GOTO WR2
BSF RA,3
;MOVLW B'01100000'
MOVLW 0FH
TRIS RA
DECFSZ COUNT2,1
GOTO WR0
RETLW 0
;--------------------------
RE1820
MOVLW 8
MOVWF COUNT2
RE0
;MOVLW B'00100000'
MOVLW 0
TRIS RA
BCF RA,3
MOVLW 2
MOVWF COUNT1
RE1
DECFSZ COUNT1,1
GOTO RE1
;MOVLW B'01100000'
MOVLW 0FH
TRIS RA
NOP
BTFSS RA,3
BCF STATUS,C
BTFSC RA,3
BSF STATUS,C
RRF TEM_DATE,1
MOVLW D'20'
MOVWF COUNT1
RE2
DECFSZ COUNT1,1
GOTO RE2
DECFSZ COUNT2,1
GOTO RE0
BSF RA,3
RETLW 0
;-------------------------
;****************************
;**************************
END
作者:
winnie
时间:
2009-5-21 09:49
**************************************************
;**************************************************
;2002.4.5更改ds18b20电源由RA4供电
;晶振16MHz
;**************************************************
;**************************************************
INCLUDE "D:\PIC\P16XX.EQU"
INCLUDE "D:\PIC\LGL.MAC"
;**************************************************
;**************************************************
;MACRO
;**************************************************
WriteBit macro bitsel
BANK1_
bcf TRISA,bitsel ;RA,Data1 设置为输出
BANK0_
bcf RA,bitsel
movlw .5 ;dly 25us
call Dl
btfsc Ds1820Fg,Do
bsf RA,bitsel
movlw .55 ;dly 45us
call Dl
bsf RA,bitsel
endm
;**************************************************
;RA
Data1 equ 0
Data2 equ 1
;**************************************************
;**************************************************
;**************************************************
;**************************************************
W_TEMP EQU 0X20 ;(0XA0)
STATUS_TEMP EQU 0X21
;======================
ACCAH EQU 22H
;TEMP1 EQU ACCAH
ACCAL EQU 23H
;TEMP2 EQU ACCAL
ACCBH EQU 24H
TEMP3 equ ACCBH
ACCBL EQU 25H
TEMP4 equ ACCBL
ACCCH equ 26h
ACCCL equ 27h
TEMP equ 28h
fg EQU 29H
Sflag equ 2ah
TxBuf equ 2bh
RxBuf equ 2ch
KeyVal equ 2dh
KeyBak equ 2eh
KeyCnt equ 2fh
;=======================
BeepLen equ 30h
RunStep equ 31h
ScourMode equ 32h
FgOut equ 33h
RunSec equ 34h
PxTemper equ 35h
XdTemper equ 36h
JinShuiCnt equ 37h
AutoXdDly12s equ 38h
T500ms equ 39h
T1s equ 3ah
DspBuf1 equ 3bh
DspBuf2 equ 3ch
CNT equ 3dh
TEMP1 equ 3eh
TEMP2 equ 3fh
Ds1820Fg equ 40h
DspDly equ 41h
BeepDly equ 42h
FgAlm equ 43h
;======================
Tdata0 equ 50h
Tdata1 equ 51h
Tdata2 equ 52h
Tdata3 equ 53h
Tdata4 equ 54h
Tdata5 equ 55h
Tdata6 equ 56h
Tdata7 equ 57h
Tdata8 equ 58h
;======================
;**************************************************
;Sflag
fg500ms equ 0
fg1s equ 1
fg_1s equ 2
fgFlash equ 3
FgAutoXd EQU 4
;EnbHot equ 5
KeyRun equ 5
FgRun equ 6
Fg
;**************************************************
;**************************************************
;**************************************************
;Ds1820Fg
Di equ 0
Do equ 1
TimeOut equ 2
Ok equ 3
FgPxErr equ 4
FgXdErr equ 5
;**************************************************
;**************************************************
;**************************************************
;**************************************************
;**************************************************
ORG 0000H;
GOTO MAIN ; Skip over interrupt vecter
;*********************************
ORG 04H ; Interrupt Vector
GOTO INTZ
;*********************************
ORG 10H ; Begining of Program space
;**************************************************
;**************************************************
;**************************************************
;**************************************************
;**************************************************
;**************************************************
MAIN:
call TemperTrans1
call TemperRead1
B MAIN
;**************************************************
;**************************************************
;**************************************************
;**************************************************
;**************************************************
;**************************************************
DspOff:
bsf RB,DspCom1
bsf RB,DspCom2
return
;**************************************************
;**************************************************
;**************************************************
Dsp:
; dsp1 dsp2 dsp3 dsp4
return
;**************************************************
;
;**************************************************
;**************************************************
;**************************************************
;**************************************************
;**************************************************
;**************************************************
;**************************************************
Dly1us: ;dly (2+2)*0.25=1
return
;**************************************************
;**************************************************
;**************************************************
;**************************************************
;**************************************************
;;入口:比特值在acc中
;**************************************************
Dl:
movwf TEMP1 ;dly
nop
decfsz TEMP1,f ;
b $-2
return
;**************************************************
;**************************************************
;**************************************************
;**************************************************
WriteBit1:
WriteBit Data1
return
;**************************************************
;ds1820 比特读
;出口:比特值在Ds1820Fg,Di中
;**************************************************
ReadBit1:
BANK1_
bcf TRISA,Data1 ;RA,Data1 设置为输出
BANK0_
bcf RA,Data1 ;
bcf Ds1820Fg,Di
call Dly1us ;保持低电平至少1us
call Dly1us
call Dly1us
call Dly1us
call Dly1us
BANK1_
bsf TRISA,Data1 ;RA,Data1 设置为输入
BANK0_
movlw .6
call Dl
btfsc RA,Data1
bsf Ds1820Fg,Di
movlw .50
call Dl
return
;**************************************************
;**************************************************
;ds1820 比特读
;出口:比特值在Ds1820Fg,Di中
;**************************************************
ReadBit2:
BANK1_
bcf TRISA,Data2 ;RA,Data2 设置为输出
BANK0_
bcf RA,Data2 ;
bcf Ds1820Fg,Di
call Dly1us ;保持低电平至少1us
call Dly1us
call Dly1us
call Dly1us
call Dly1us
BANK1_
bsf TRISA,Data2 ;RA,Data2 设置为输入
BANK0_
movlw .6
call Dl
btfsc RA,Data2
bsf Ds1820Fg,Di
movlw .50
call Dl
return
;**************************************************
;**************************************************
;ds1820 字节读
;出口:比特值在RxBuf中
;**************************************************
ReadByte1:
bcf INTCON,GIE ;关所有中断
clrf RxBuf
movlw .8
movwf TEMP
RxLoop1:
call ReadBit1
CLRC
btfsc Ds1820Fg,Di
SETC
rrf RxBuf,f
decfsz TEMP,f
b RxLoop1
bsf INTCON,GIE ;开所有中断
return
;**************************************************
;**************************************************
;**************************************************
;**************************************************
;ds1820 字节写
;入口:字节在acc中
;**************************************************
WriteByte1:
movwf TxBuf
bcf INTCON,GIE ;关所有中断
movlw .8
movwf TEMP
WriteLoop1:
bcf Ds1820Fg,Do
btfsc TxBuf,0
bsf Ds1820Fg,Do
call WriteBit1
rrf TxBuf,f
decfsz TEMP,f
b WriteLoop1
bsf INTCON,GIE ;开所有中断
return
;**************************************************
;**************************************************
;;**************************************************
;**************************************************
;初始化ds1820
;如初始化成功则Ds1820Fg,Ok=1
;否则Ds1820Fg,Ok=0
;**************************************************
Ds1820Init1:
movlw .100
movwf TEMP
BANK1_
bcf TRISA,Data1 ;RA,Data1 设置为输出
BANK0_
bsf RA,Data1
movlw .10
call Dl
bcf RA,Data1
movlw .200
call Dl
movlw .200
call Dl
movlw .200
call Dl
BANK1_
bsf TRISA,Data1 ;RA,Data1 设置为输入
BANK0_
movlw .80
call Dl
WaitLow:
btfss RA,Data1 ;等待Ds1820 拉低
b WaitHi
call Dly1us
decfsz TEMP,f
b WaitLow
;BANK1_ ;超时释放总线
;bcf TRISA,Data1 ;RA,Data1 设置为输出
;BANK0_
bsf RA,Data1
bcf Ds1820Fg,Ok ;
return
WaitHi:
movlw .100
movwf TEMP
WaitHi1:
btfsc RA,Data1
b Ds1820InitOk
call Dly1us
decfsz TEMP,f
b WaitHi1
bsf RA,Data1 ;超时释放总线
bcf Ds1820Fg,Ok
return
Ds1820InitOk:
bsf Ds1820Fg,Ok
return
;**************************************************
;**************************************************
;**************************************************
;发送温度转换指令
;如成功则Ds1820Fg,Ok=1
;否则Ds1820Fg,Ok=0
;**************************************************
TemperTrans1:
call Dsp
call Ds1820Init1
btfsc Ds1820Fg,Ok
b TransOk
call Dsp
call Dly1ms
call Dsp
call Dly1ms
call Dsp
call Dly1ms
bcf Ds1820Fg,Ok
return
TransOk:
movlw 0xcc
call WriteByte1
call Dsp
movlw 0x44
call WriteByte1
movlw .200
movwf TEMP1
TransLoop:
decfsz TEMP1,f
b TransLoop1
bcf Ds1820Fg,Ok
return
TransLoop1:
CLRWDT
call Dsp
call ReadByte1
movlw 0xff
xorwf RxBuf,w
btfss status,z
b TransLoop
bsf Ds1820Fg,Ok
return
;**************************************************
;**************************************************
;**************************************************
Dly1ms:
movlw .200
call Dl
movlw .200
call Dl
movlw .200
call Dl
movlw .200
call Dl
movlw .200
call Dl
return
;**************************************************
;**************************************************
TemperRead1:
call Dsp
call Ds1820Init1
btfsc Ds1820Fg,Ok
b ReadInit1Ok
CLRWDT
call Dsp
call Dly1ms
call Dsp
call Dly1ms
bcf Ds1820Fg,Ok
return
ReadInit1Ok:
CLRWDT
movlw 0xcc
call WriteByte1
call Dsp
movlw 0xbe
call WriteByte1
call ReadByte1
movf RxBuf,w
movwf Tdata0
;=======================
call Dsp
call ReadByte1
movf RxBuf,w
movwf Tdata1
bsf Ds1820Fg,Ok
return
;**************************************************
;**************************************************
欢迎光临 英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 (http://enroobbs.com/)
Powered by Discuz! X3.2