PIC a I2C
Jan Babjak
jaybee
Středa Březen 17 12:05:42 CET 2004
> Potrebuji poslat program pro komunikaci mezi PICem 16C84 a seriovou pameti
24c08 (protokol i2c)
Zdravim, nevim jestli je to presne ono ale mozna jo, mam to odskousene (Sice
ne z obvodem 24c08) a beha to:
--
S pozdravem
Jan Babjak
jaybee@volny.cz
Bohumin.
; --------------------------------------------------------------------------
---
; Podprogramy pro rizeni I2C sbernice - MASTER
; --------------------------------------------------------------------------
---
i2c_init clrf PORTB ; Inicializace I2C
bsf STATUS, RP0
bsf TRISB, SDA
bsf TRISB, SCL
bcf STATUS, RP0
return
i2c_start bsf STATUS, RP0 ; START sekvence
bcf TRISB, SDA
nop
nop
nop
bcf TRISB, SCL
bcf STATUS, RP0
return
i2c_stop bsf STATUS, RP0 ; STOP sekvence
bsf TRISB, SCL
bcf STATUS, RP0
i2c_stp1 btfss PORTB, SCL ;cekej na uvolneni SCL SLAVEm
goto i2c_stp1
bsf STATUS, RP0
bsf TRISB, SDA
bcf STATUS, RP0
return
; Podprogram zapisu bytu do SLAVE
; - Byte dat pro vysilani je ulozen v I2C_Byte
; - Po ukonceni podprogramu je v PSW (ACKF) priznak prichodu ACK ze SLAVE
i2c_write movlw 08h ;pocet prenasenych bitu
movwf BITS
bsf STATUS, RP0
i2c_wr1 rlf I2C_Byte,f ;vyslani bitu dat
bcf TRISB, SDA ;nastav SDA podle bitu dat
btfsc STATUS, C
bsf TRISB, SDA
bsf TRISB, SCL ;SCL do 1
bcf STATUS, RP0
i2c_wr2 btfss PORTB, SCL ;cekej na uvolneni SCL SLAVEm
goto i2c_wr2
bsf STATUS, RP0
bcf TRISB, SCL ;SCL do 0
decfsz BITS,f
goto i2c_wr1
nop ;cekej 4 ms
bsf TRISB, SCL
bcf STATUS, RP0
i2c_wr3 btfss PORTB, SCL ;cekej na uvolneni SCL SLAVEm
goto i2c_wr3
bsf FLAGS, ACKF ;test ACK
btfsc PORTB, SDA
bcf FLAGS, ACKF
bsf STATUS, RP0 ;SCL do nuly
bcf TRISB, SCL
bcf STATUS, RP0
return
; Podprogram pro cteni dat ze SLAVE
; - Podprogram prijme data a ulozi je do I2C_Byte
; - Je-li nastaveno ACKFdo jednicky, vysle po prijeti bytu ACK,
; - v opacnem pripade ACK nevysle.
i2c_read bsf STATUS, RP0 ;SDA jako vstup
bsf TRISB, SDA
movlw 08h ;BITS nastav na 08
movwf BITS
i2c_rd1 bsf TRISB, SCL ;SCL do 1
bcf STATUS, RP0
i2c_rd2 btfss PORTB, SCL ;cekej na uvolneni SCL SLAVEm
goto i2c_rd2
rrf PORTB,w ;SDA do I2C_Byte
rlf I2C_Byte,f
bsf STATUS, RP0
bcf TRISB, SCL ;SCL do 0
decfsz BITS,f
goto i2c_rd1
btfsc FLAGS, ACKF ;je-li ACKF = 1 vysli ACK
bcf TRISB, SDA
bsf TRISB, SCL ;SCL do 1
bcf STATUS, RP0
i2c_rd3 btfss PORTB, SCL ;cekej na uvolneni SCL SLAVEm
goto i2c_rd3
bsf STATUS, RP0
bcf TRISB, SCL ;SCL do 0
bcf STATUS, RP0
return
; --------------------------------------------------------------------------
---
end
Další informace o konferenci Hw-list