使用PIC讀寫1-wire EEPROM DS2430的實(shí)例程序
banksel INTCON
bsf INTCON,GIE ;; GIE=1
bcf INTCON,PEIE ;;
bcf INTCON,T0IE ;; T0IE=1
bcf INTCON,INTE ;; GP2 interrupt enable
; bcf INTCON,RAIE ;; disable GPIO port change interrupt本文引用地址:http://2s4d.com/article/171047.htm
;------------------------------------------------
; clear int flag
;------------------------------------------------
bcf INTCON,T0IF
bcf INTCON,INTF
; bcf INTCON,RAIF
bcf PIR1,TMR1IF
; movlw H'00'
; movwf INTCON
banksel PIE1
bcf PIE1,EEIE
bcf PIE1,ADIE
bcf PIE1,CMIE
bsf PIE1,TMR1IE ; enable Timer 1, timer 1 use as delay
return
;--------------------------------------------
; Tx BYTE to 2430A
; LSB first, MSB last
;--------------------------------------------
TxBYTE
movlw .8
movwf LoopCounter
bcf CARRY
RollTxB
banksel TxB
rrf TxB,F
btfss CARRY
goto LoBit
call TxHi
goto bloop
LoBit
call TxLo
bloop
banksel LoopCounter
decfsz LoopCounter,F
goto RollTxB
return
;;---------------------------------------------------------
TxL
call SetLo
call delay60us ; min 60 us
call SetHi
nop ; recovery time min 1us
nop
return
;-----------------------------------------------------------
; tx hi
;-----------------------------------------------------------
TxHi:
call SetLo ;1ustlow115us
nop
call SetHi
call delay60us
nop
nop ;recovery time min 1us
return
;;---------------------------------------------------------------------------
;; toggle the RC0 as input and the bus was pulled up by externl resistor
;; application default PCDir.Bit0 to output
;;---------------------------------------------------------------------------
SetHi:
bsf PBDir,Bit2
movf PBDir,w
; bsf bank1
banksel TRISB
movwf TRISB
return
;;----------------------------------------------------------------------------
;; clear register file RC0
;;----------------------------------------------------------------------------
SetL
banksel PORTB
bcf PORTB,Bit2 ; data lo
bcf PBDir,Bit2 ; direction out
movf PBDir,W
banksel TRISB
movwf TRISB
return
;---------------------------
; fix delay 60 us
; 0xffff - 8 = 0xfff7
;---------------------------
delay60us
banksel PIR1
bcf PIR1,TMR1IF
movlw H'EE'
movwf TMR1L
movlw H'FF'
movwf TMR1H
bsf T1CON,TMR1ON
btfss PIR1,TMR1IF
goto $-1
bcf T1CON,TMR1ON
bcf PIR1,TMR1IF
return
;---------------------------
; fix delay 500 us
; 0xffff - 0x3e = 0xffc1
;---------------------------
delay500us
banksel PIR1
bcf PIR1,TMR1IF
movlw H'C0'
movwf TMR1L
movlw H'FF'
movwf TMR1H
bsf T1CON,TMR1ON
btfss PIR1,TMR1IF
goto $-1
bcf T1CON,TMR1ON
bcf PIR1,TMR1IF
return
;---------------------------
; fix delay 100 ms
; 0xffff - 30d4 = 0xcf26
;---------------------------
delay100ms
banksel PIR1
bcf PIR1,TMR1IF
movlw H'26'
movwf TMR1L
movlw H'CF'
movwf TMR1H
bsf T1CON,TMR1ON
btfss PIR1,TMR1IF
goto $-1
bcf T1CON,TMR1ON
bcf PIR1,TMR1IF
return
org H'800'
VendorTable
movf Offset,W ; Save offset in Temp
andlw H'ff'
addwf PCL,f ; PCL = PCL+Offset
dt MiCallChip,0
end
評論