Код: init_ds1307 call iicstart movlw 0xD0 call iicwrite call ack_check movlw 0x00 call iicwrite call ack_check movlw 0x00 call iicwrite call ack_check call iicstop call leds_delay call iicstart movlw 0xD0 call iicwrite call ack_check movlw 0x07 call iicwrite call ack_check movlw b'10010000' call iicwrite call ack_check call iicstop return
; *************************** ; **** **** ; **** IIC (IІC) Routine **** ; **** **** ; *************************** ; Nessaccary Variable: ; -------------------- ; ; iicbuffer = 0x27 ; Buffer for IIC ; iicbuffercount = 0x28 ; Counter for IIC rotary ; iicdelaycount = 0x29 ; Counter for IIC delay iicdelayus = 15 ; How much delay for IIC ; IICPORT = PORTB ; Port for IIC ; IICTRIS = TRISB ; Tris for IIC ; scl = 1 ; Interner IIC Bus - Clock ; sda = 0 ; Interner IIC Bus - Data
; **** I_IICRead ********************************************************* ; Read a Byte from IIC ; Input: - ; Output: iicbuffer = received Byte ; w = received Byte
iicread: call iicin movlw 0x08 ; 8 Bits movwf iicbuffercount ; write it to the buffer counter nextbitin: bsf IICPORT,scl ; Clock goes HIGH call iicdelay btfss IICPORT,sda ; if SDA is high then skip next cmd goto iicbitin0 iicbitin1: bsf STATUS,C ; Carry set to 1 goto iicbitincnt iicbitin0: bcf STATUS,C ; Carry set to 0 iicbitincnt: rlf iicbuffer,f ; Increase IIC bit count to next bit
bcf IICPORT,scl ; Clock goes LOW ;* call iicdelay decfsz iicbuffercount,f ; buffer down 1 step goto nextbitin
movf iicbuffer,w ; copy buffer to W return
; **** IICWrite ******************************************************** ; Write a Byte to IIC ; Input: W = Byte to send ; Output: -
iicwrite: movwf iicbuffer ; copy W to IIC Buffer call iicout movlw 0x08 ; 8 bit long movwf iicbuffercount ; copy into BufferCounter nextbitout: rlf iicbuffer,f ; roll left; MSB ins Carry btfss STATUS,C ; if C is high then skip next cmd goto iicbitout0 ; 0 ausgeben iicbitout1: bsf IICPORT,sda ; HIGH to SDA goto iicbitoutcnt iicbitout0: bcf IICPORT,sda ; LOW to SDA iicbitoutcnt: bsf IICPORT,scl ; Clock HIGH call iicdelay bcf IICPORT,scl ; Clock LOW ;* call iicdelay
decfsz iicbuffercount,f ; Buffer counter down 1 step goto nextbitout
goto iicwaitack ; And wait for ACK
; **** IICwaitAck ****************************************************** ; wait for ACK ; Input: - ; Output: C = 1 ACK received ; C = 0 no ACK reveived
iicwaitack: call iicin bsf IICPORT,scl ; Clock goes HIGH clrf iicbuffercount ; Buffer counter = 0 waitack: decfsz iicbuffercount,f ; Decrease Buffer counter goto chksda
bcf STATUS,C ; Carry = 0 - No ACK recieved bcf IICPORT,scl ; Clock auf LOW return
chksda: btfsc IICPORT,sda ; Wait till SDA goes LOW goto waitack bcf IICPORT,scl ; Clock goes LOW bsf STATUS,C return
; **** IICsendAck ****************************************************** ; send ACK to IIC ; Input: - ; Output: -
iicsendack: call iicout bcf IICPORT,sda ; Data goes LOW bsf IICPORT,scl ; Clock goes HIGH call iicdelay bcf IICPORT,scl ; Clock goes LOW return
; **** IICsendNAck ***************************************************** ; send NACK to IIC ; Input: - ; Output: -
iicsendnack: call iicout bsf IICPORT,sda ; Data goes HIGH bsf IICPORT,scl ; Clock goes HIGH call iicdelay bcf IICPORT,scl ; Clock goes LOW return
; **** IICOut ********************************************************** ; set SDA-Port Pin to Output ; Input: - ; Output: -
iicout: bsf STATUS,RP0 ; Bank 1 bcf IICTRIS & 0x7f,sda ; Data is now Output bcf STATUS,RP0 return
; **** IICIn *********************************************************** ; set SDA-Port Pin to Input ; Input: - ; Output: -
iicin: bsf STATUS,RP0 ; Bank 1 bsf IICTRIS & 0x7f,sda ; Data is now Input bcf STATUS,RP0 ; Bank 0 return
; **** IICStart ******************************************************** ; send a Start-Condition to IIC ; Input: - ; Output: -
iicstart: call iicout bsf IICPORT,scl ; Clock goes HIGH bsf IICPORT,sda ; Data goes HIGH call iicdelay bcf IICPORT,sda ; Data goes LOW call iicdelay bcf IICPORT,scl ; Clock goes LOW goto iicdelay
; **** IICStop ********************************************************* ; send a Stop-Condition to IIC ; Input: - ; Output: -
iicstop: call iicout bcf IICPORT,scl ; Clock goes LOW bcf IICPORT,sda ; Data goes LOW call iicdelay bsf IICPORT,scl ; Clock goes HIGH call iicdelay bsf IICPORT,sda ; Data goes HIGH goto iicdelay
; **** IICReset ******************************************************** ; Reset IIC Bus ; Input: - ; Output: -
iicreset: call iicout movlw .18 movwf iicbuffercount inextresetbit: bsf IICPORT,sda ; Data goes High bsf IICPORT,scl ; Clock goes High call iicdelay bcf IICPORT,scl ; Clock goes Low call iicdelay decfsz iicbuffercount,f ; Decrease Counter goto inextresetbit return
; **** IICDelay ********************************************************** ; Delay routine for IIC ; Input: - ; Output: -
iicdelay: ; movlw iicdelayus ; movwf iicdelaycount ;iicdelaydown: ; decfsz iicdelaycount,f ; goto iicdelaydown movlw .26 movwf iicdelaycount iicdelaydown: decfsz iicdelaycount,f goto iicdelaydown
return ack_check btfss STATUS,C ; C=1 = ACK received goto write_err ; There was a problem - display "ERROR" return
write_err bsf L_SERVICE goto INIT err_read bsf L_DOWNL goto INIT
ack_read btfss STATUS,C goto INIT return
|