您当前的位置:五五电子网电子知识单片机-工控设备源码-程序使用PIC读写1-wire EEPROM DS2430的实例 正文
使用PIC读写1-wire EEPROM DS2430的实例

使用PIC读写1-wire EEPROM DS2430的实例

点击数:7608 次   录入时间:03-04 12:03:21   整理:http://www.55dianzi.com   源码-程序
;-------------------------------------------------------------
; reset pulse
;
;--------------------------------------------------------------
RstPulse
  call SetHi
  call SetLo
  call delay500us
  call    SetHi   ;Pull up by resistor
  return
;---------------------------------------------------------------
; reset pulse followed presence pulse
;
;---------------------------------------------------------------
DS2430AInitState
        call SetHi
        call SetLo
        call delay500us   ; reset pulse
  call SetHi
  call delay500us   ; presence pulse, pull up by resistor
  return


;-----------------------------------------------------------------------
  
init
 movlw H'3f'
 movwf PBDir   ; 0x00111111
 banksel TRISB
 movwf TRISB   ; force all portC as input
; movlw b'00000000'  ; GP2 pull-up,GP4 output
; movwf WPU    ; connect the D of DS2430A to Port A
;--------------------------------------------------------------------
;  toggle from bank0 to bank1
;   option register
;   7:  1: port A pull-up OFF, 0:port A pull-up ON
;   6:  1: rising INT , 0:fall INT
;   5:  1: T0CKI to TMR0, 0: internal cycle to TMR0
; 4:  1: Hi to Lo count 0: Lo to Hi count
; 3:  1: Precale to WDT 0: Prescale to TMR0
;   2:0 prescale value
;--------------------------------------------------------------------- 
 movlw b'11000010'  ;; pull-up ON,rising INT, internal cycle to TMR0,Lo to Hi count,Pre to TMR0,1:8
 movwf OPTION_REG  ;; ,prescal over 8
 banksel INTCON
 clrf INTCON
 clrf PORTB
 movlw 0x07   ;; comparator off
 movwf CMCON   ;; comparator off
 clrf TMR0

;;--------------------------------------------------------
;; 7: X
;; 6:    1: Timer1 on if T1G
;; 5:4   : 11:/8,10:/4,01:/2,00:/1
;;  3 :   1 LP for timer 1 on, 0: LP for timer1 of
;;  2 :   1 do not sync external , 0 sync external cLOCk
;; 1 :   1: clock source external , 0: clock source internal  
;; 0 :   1: enable timer 1 ,0: dISAble timer 1
;;---------------------------------------------------------
 
; movlw b'00000000'  ;x,no gate control,over 8,LP osc to timer1 off,x,internal clock,stop timer
; movwf T1CON
 movlw b'00111000'
 movwf T1CON
 clrf PIR1
 return
 
;;-------------------------
;; init interrup enable,
;;-------------------------
init_int_en 

www.55dianzi.com

 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

;------------------------------------------------
; 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    ;1us<tlow1<15us
 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



上一页  [1] [2] [3] 


本文关键字:暂无联系方式源码-程序单片机-工控设备 - 源码-程序