Pagina 1 di 1

Protocollo i2c per pic 16f876

MessaggioInviato: mer mar 31, 2004 12:32 pm
da conan
Ragazzi sto disperatamente cercando una routine che mi permetta di interfacciare il pic 16f876 con la eeprom 24c16 usando il protocollo i2c.
Grazie

Protocollo i2c per pic 16f876

MessaggioInviato: mer mar 31, 2004 12:32 pm
da conan
Ragazzi sto disperatamente cercando una routine che mi permetta di interfacciare il pic 16f876 con la eeprom 24c16 usando il protocollo i2c.
Grazie

MessaggioInviato: gio apr 15, 2004 10:34 pm
da ciber_eln
ciao, il codice da te cercato lo trovi sul sito ufficiale microchip;www.microchip.com sotto application notes; vanno bene tutti i codici per la famiglia 16xxx, se voui sempre sul sito trovi un programma chiamato Microchip application maestro che ti consente di accedere a molte altre librerie comprese quelle che cerchi.ciao e buon lavoro

MessaggioInviato: sab apr 17, 2004 11:15 pm
da fox007
ciao conan queste sono le rutine di comunicazione i2c per 16F876

Codice: Seleziona tutto
;--------------------------------------------------------------------------------
; I2C_busy - Invia il Byte di controllo fino a che non ottiene l'ACK di risposta
;--------------------------------------------------------------------------------


I2C_busy    CALL    i2c_start
            BCF     STATUS        ,C       ; Clear carry flag, R/W bit = 0 (write)
            CALL    i2cw_conbyte           ; Write control byte, returns ACK into carry
            BTFSC   STATUS        ,C       ; Skip if not busy
            GOTO    I2C_busy               ; Loop while busy

            RETURN





;--------------------------------------------------------------------------------
; i2c_rd - Legge un byte dalla EEprom esterna all'indirizzo specificato da
;        Buffer_HIGH e da W, che però deve essere stato precedentemente
;          selezionato da una sequenza composta da un Byte di controllo
;          (settato in scrittura) seguito poi dal "word Address".
;          Il valore letto viene riportato in W.
;--------------------------------------------------------------------------------

i2c_rd      CALL    i2c_start         ; start
            BSF     STATUS        ,C          ; Set carry flag, next i2c access is for reading
            CALL    i2cw_conbyte         ; Send again control byte to i2c
            CALL    i2cr_byte           ; Read byte from i2c into EEDATA
            CALL    i2c_stop
            BSF     STATUS        ,RP1
       BCF     STATUS        ,RP0      ; Passa al Banco 2

            MOVF    EEDATA        ,W          ; Return read byte in W
       BCF     STATUS        ,RP1      ; Torna al Banco 0
            RETURN

;--------------------------------------------------------------------------------
; i2c_wr - Scrive l'indirizzo completo nella EEprom esterna. Il blocco pagina
;        è contenuto in Buffer_HIGH 0X7C, mentre il "word address" è in W
;--------------------------------------------------------------------------------

i2c_wr      BSF     STATUS        ,RP1
       BCF     STATUS        ,RP0      ; Passa al Banco 2
       MOVWF   EEADR                ; Save word address
       BCF     STATUS        ,RP1      ; Torna al Banco 0
            CALL    I2C_busy             ; Loop writing cotrol byte until ACK
       MOVF    0x7C     ,W           ; Recupera il blocco pagina
       CALL    i2cw_byte           ; /e lo scrive
            BSF     STATUS        ,RP1
       BCF     STATUS        ,RP0      ; Passa al Banco 2
            MOVF    EEADR         ,W             ; Recover word address
       BCF     STATUS        ,RP1      ; Torna al Banco 0
            GOTO    i2cw_byte           ; Write it

; Write a data byte to external EEPROM
; The external EEPROM internal address pointer should have been set previously
; by a control byte, word address write sequence

wr_last     CALL    i2cw_byte
            GOTO    i2c_stop


;****************************************************************************
; i2cw_conbyte  - Costruisce un Byte di controllo in W dal blocco pagina
;         contenuto in PCLATH e dal bit R/W contenuto nel Carry flag
;****************************************************************************

i2cw_conbyte 
            CLRF    0x68
       RLF     0x68          ,W       ; Load R/W bit from carry into W
            IORLW   0xA0                   ; Load control code bits 1010 into W, control byte in W is complete

;--------------------------------------------------------------------------------
; i2cw_byte   - Invia un Byte caricato in W alla EEprom esterna
;--------------------------------------------------------------------------------

i2cw_byte   BSF     STATUS        ,RP1
       BCF     STATUS        ,RP0      ; Passa al Banco 2
       MOVWF   EEDATA               ; Store W into EEDATA
       BCF     STATUS        ,RP1      ; Torna al Banco 0
            MOVLW   0xCF                 ; Define PORT B I/O RB4,RB5=output, all others=input
            TRIS    PORTB                ;/
            CALL    i2cw_nibble          ; Write byte contained in EEDATA
            CALL    i2cw_nibble          ;/
            GOTO    i2cr_bit             ; Read ACK

;*************************************************************
; i2cw_nibble - Invia un nibble da EEDATA alla EEprom esterna
;*************************************************************

i2cw_nibble 
            CALL    i2cw_nibble_2                ; 1st bit
            CALL    i2cw_nibble_2                ; 2nd bit
            CALL    i2cw_nibble_2                ; 3rd bit, 4th bit below

;************************************************************
; i2cw_nibble_2 - Invia un bit di EEDATA alla EEprom esterna
;************************************************************

i2cw_nibble_2

            BSF     STATUS        ,RP1
       BCF     STATUS        ,RP0      ; Passa al Banco 2
            RLF     EEDATA        ,f          ; Put bit to be sent into carry
       BCF     STATUS        ,RP1      ; Torna al Banco 0
            GOTO    i2cw_bit_x           ; Send

;--------------------------------------------------------------------------------
; i2cr_byte - Legge un Byte dalla EEprom esterna in EEDATA
;--------------------------------------------------------------------------------

i2cr_byte   MOVLW   0xDF                        ; Define PORT B I/O RB5=output, all others=input
            TRIS    PORTB                       ;/
            CALL    i2cr_nibble                 ; Read byte
            CALL    i2cr_nibble                 ;/
            BSF     STATUS,C                    ; Set carry flag
            GOTO    i2cw_bit                    ; NACK, stop

;**************************************************************
; i2cr_nibble - Legge un nibble dalla EEprom esterna in EEDATA
;**************************************************************

i2cr_nibble CALL    i2cr_nibble_2               ; 1st bit
            CALL    i2cr_nibble_2               ; 2nd bit
            CALL    i2cr_nibble_2               ; 3rd bit, 4th bit below

;*************************************************************
; i2cr_nibble_2 - Legge un bit dalla EEprom esterna in EEDATA
;*************************************************************

i2cr_nibble_2
            CALL    i2cr_bit_x           ; Read bot into carry

            BSF     STATUS        ,RP1
       BCF     STATUS        ,RP0      ; Passa al Banco 2
            RLF     EEDATA        ,f          ; Store into EEDATA
       BCF     STATUS        ,RP1      ; Torna al Banco 0
            RETURN

;****************************************************************
; i2c_start - Inizia la procedura di accesso alla EEprom Esterna
;****************************************************************

i2c_start   BSF     PORTB         ,0x04
            MOVLW   0xCF                        ; Define PORT B I/O RB4,RB5=output, all others=input
            TRIS    PORTB                       ;/
            BSF     PORTB         ,0x05          ; Clock high
            NOP
            BCF     PORTB         ,0x04         ; Data start
            GOTO    clock_low

;--------------------------------------------------------------------------------
; i2c_stop - Chiude la procedura di accesso alla EEprom Esterna
;--------------------------------------------------------------------------------

i2c_stop    BCF     PORTB         ,04       ; Data start
            MOVLW   0xCF                    ; Define PORT B I/O RB4,RB5=output, all others=input
            TRIS    PORTB                   ;/
            NOP
            BSF     PORTB         ,05       ; Clock high
            NOP
            BSF     PORTB         ,04       ; Data stop
nclock_low  NOP

clock_low   BCF     PORTB         ,05       ; Clock low
            RETURN


i2cw_bit    MOVLW   0xCF                    ; Define PORT B I/O RB4,RB5=output, all others=input
            TRIS    PORTB                   ;/

;**********************************************************************
; i2cw_bit_x - Scrive Bit dal Carry flag a RB4 verso la EEprom esterna
;**********************************************************************

i2cw_bit_x 
            BTFSS   STATUS        ,C        ; Skip if carry set
            BCF     PORTB         ,04
            BTFSC   STATUS        ,C        ; skip if no carry
            BSF     PORTB         ,04
            BSF     PORTB         ,05       ; Clock high
            GOTO    nclock_low

;-----------------------------------------------------------------------

i2cr_bit    MOVLW   0xDF                    ; Define PORT B I/O RB5=output, all others=input
            TRIS    PORTB                   ;/

;*********************************************************
; i2cw_bit_x - Legge un da RB4 e lo scrive nel Carry flag
;*********************************************************

i2cr_bit_x  BSF     PORTB         ,05       ; Clock high
            NOP
            BSF     STATUS        ,C        ; Set carry flag
            BTFSS   PORTB         ,04
            BCF     STATUS        ,C        ; Clear carry flag
            GOTO    clock_low