使用PIC讀寫1-wire EEPROM DS2430的實(shí)例程序
;--------------------------------------------------------------------
;
; 1-wire device access
;DS2430
;memory function
;write scratchpad
;read scratchpad
;
;read memory :
; resource occupy by ICD2
; PROM 0x1f00 - 0x1fff
; RAM 0x70,0xf0,0x170,0x1e5~0x1f0
;--------------------------------------------------------------------
; master action
; 0xf0 + start addr(8) => scratchpad(8) --> master
; none => scratchpad(9) --> master
; none => scartchpad(a) --> master
;
; none => scratchpad(0x1f)--> master[wrap around]
; reset pulse => stop sending data
;
; 0xf0+reset pulse => EEPROM(0x00-0x1F) --> scratchpad(0x00-0x1F)
; copy scratchpad
; master
; 0x55 + 0xa5 => scratchpad(000-0x1f) --> EEPROM(0x00-0x1F)
; held hi for 10ms
; 0xf0 + reset pulse => copy whole from EEPROM to SCRATCHPAD
; use timer as a delay timer
;
; author : Rex Chen
; date : May,8th
; ver : 1.81
;
list p=16F877A
#i nclude p16f877a.inc
__CONFIG _CPD_OFF _CP_ALL _DEBUG_ON _BODEN_OFF _LVP_OFF _PWRTE_OFF _WDT_OFF _XT_OSC
#define OneWireDHi bsf PORTC,Bit2
#define OneWireDLo bcf PORTC,Bit2
#define CARRY STATUS,C
#define ZERO STATUS,Z
Bit0 equ H'0000'
Bit1 equ H'0001'
Bit2 equ H'0002'
Bit3 equ H'0003'
Bit4 equ H'0004'
Bit5 equ H'0005'
Bit6 equ H'0006'
Bit7 equ H'0007'
;variable defination
cblock 0x20 ; RAM area 0x20- 0x7f
SYSFLAG
TxB ; Command buffer been send
TxBuffer ; data buffer been send
RxB ; Command buffer been received
TempB ; Temp Byte to roll the data
LoopCounter
Addr ; address to access BQ2430A
PBDir ; port C direction log file
PADir ; port A direction log file
Temp
ErrCode ;
W_SAVE
STATUS_SAVE
TxBuf:8 ; Tx Temp buffer
Offset
endc
;;
cblock 0xa0
SARS1
SARS2
SARS3
endc
cblock 0x100
HateSARS1
HateSARS2
HateSARS3
HateSARS4
endc
cblock 0x190
SARSIsAPig0
SARSIsAPig1
SARSIsAPig2
SARSIsAPig3
SARSIsAPig4
SARSIsAPig5
endc
;---------------- SYSFLAG ----------------------------------
fTxHi equ H'0000'
fTxLo equ H'0001'
fRxMode equ H'0002'
;;
;;
;; there are 4 memory block
;; 1. EEPROM 2. memory scratchpad
;; 3. application register
;; 4.register scratchpad
;; aliase
;;
;; memory function
;; write memory scratch
;; read memory scratch
;; copy memory scratch to EEPROM
;; read EEPROM to memory scratch
;;
WScratch equ H'0F' ; 0x0f + Addr + data + data + .+ reset pulse
; // [Master --> ScratchPad ]
RScratch equ H'AA' ; 0xaa + Addr + R + R +.. + reset pulse
; // [ScratchPad --> Master ]
CPScratch equ H'55' ; 0x55 + 0xa5 + hi 10ms // [ScratchPad --> EEPROM ]
REEROM equ H'F0' ; 0xf0 + Addr +R +R + reset pulse
; // [EEPROM(addr) --> ScratchPad]
; 0xf0 + reset pulse // [EEPROM --> ScratchPad]
;; application register , application register is OTP, there are 7 bytes
;; pay attention when you use application register , it's a 64 bit OTP
WApR equ H'99' ; 0x99 + Addr + data + data .. + reset pulse
; [Master --> Application Reg]
RStatus equ H'66' ; 0x66 + 0x00 + status + reset pulse
RApR equ H'C3' ; 0xc3 + addr + data + .. + reset pulse
; application register --> master ( locked)
; register scratchpad --> master ( not lock)
LockAp equ H'5A' ; 0x5A + 0xa5
; [ register scratchpad --> application register]
; 0x5A + reset pulse cancel copy command
; a OTP type do once only
AppLocked equ B'11111100' ; application locked, the value is data in the status
;; ROM function command// implement skip rom only
SkipROM equ H'CC' ;access memory directly
SerachROM equ H'f0' ;read a bit , read the complement of the bit,write bit
MatchROM equ H'55' ;0x55 + 64 bit rom bit
ReadROM equ H'33' ;[family code serial number CRC ==> master ]
org H'00'
nop
goto main
;;ISR **
org H'04'
goto isr
isr
movwf W_SAVE ;; W_SAVE=W
swapf STATUS,W ;;
movwf STATUS_SAVE ;; STATUS_SAVE=STATUS[3:0] STATUS[7:4]
btfss PIR1,TMR1IF ;; TMR1IF
goto exit_int_source_check
;; timer 1 service
nop
nop
nop
nop
exit_int_source_check ;; restore STATUS W before exit from
swapf STATUS_SAVE,W ;; W=STATUS_SAVE=STATUS[3:0] STATUS[7:4]
movwf STATUS
swapf W_SAVE,F ;; restore W
swapf W_SAVE,W
retfie
;; ISR
main
call init
call init_int_en
movlw H'21'
movwf FSR
movlw H'3c'
movwf INDF ;
;-----------------------------------------------
;--------- read test --------------------------
;-----------------------------------------------
; movlw H'00'
; banksel Addr
; movwf Addr
; call RdDS2430
; movlw H'01'
; banksel Addr
; movwf Addr
; call RdDS2430
;
; movlw H'02'
; banksel Addr
; movwf Addr
; call RdDS2430
;------------------------------------------------
; banksel Offset
; clrf Offset
; pagesel VendorTable
; call VendorTable
; movwf Temp
;
;
movlw H'00'
banksel Addr
movwf Addr
movlw H'34'
banksel TxBuffer
movwf TxBuffer
call WrDS2430
movlw H'01'
banksel Addr
movwf Addr
movlw H'35'
banksel TxBuffer
movwf TxBuffer
call WrDS2430
movlw H'02'
banksel Addr
movwf Addr
movlw H'36'
banksel TxBuffer
movwf TxBuffer
call WrDS2430
;-------- read address 0 --------------------------
movlw H'00'
movwf Addr
call RdDS2430
goto $
;----------------------------------------------
; template to init before write
; to DS2430
;-----------------------------------------------
; movlw H'00'
; movwf Addr
; movlw H'3C'
; movwf TxBuffer
;---------------------------------------------
; write DS2430 one byte
; input
; Addr = starting address to write
; TxB = data to save
; output =
; ErrCode = 0 -> write ok
; = 1 -> write fail
;-----------------------------------------------
WrDS2430
call WrScratchPAD
call CP2EE
return
評(píng)論