|
2#
楼主 |
发表于 2009-5-21 09:49:22
|
只看该作者
**************************************************
;**************************************************
;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
;**************************************************
;************************************************** |
|