por Guri » 12 Dez 2010 14:17
Bom se ajudar usa essa rotina para i2c, eu a utilizo para manipular um banco de memórias (8 no total)...A seleção das memórias deve ser feita via hardware em primeiro lugar, em A0,A1,A2 e depois no sofware de controle na variavel word...
Se tiver dúvidas avise, já faz tempo que mexi com essa rotina e hoje a utilizo como um include e não me lembro direito...mas funciona muito bem a mais de 10 anos...
Aquí voce configura qual memo quer ler/gravar esse é o comando gloabal de acesso a i2c:
#define rd_wt word, 0
#define a_m word, 1
#define b_m word, 2
#define c_m word, 3
#define x0 word, 4
#define x1 word, 5
#define x2 word, 6
#define x3 word, 7
NOTA: X0,X1,X2 e X3 são configs basicos do fabricante, vc só altera a,b,c para selecionar a memoria e o rd_wt é escreita leitura.
;*Bits de memo*
bcf x0
bsf x1
bcf x2
bsf x3
bcf a_m
bcf b_m
bcf c_m
;---------------
; *** INICIA BLOCO LEITOR i2C ***
;* Transmite dados em 16 bits *
txdados_i2c:
bcf ackf
rbyte:
bcf rd_wt
call bstart
movf word,w
movwf txbuf
call tx
movf addr_h,w
movwf txbuf
call tx
movf addr_l,w
movwf txbuf
call tx
movf datao,w
movwf txbuf
call tx
call bstop
call delaymem
return
;---------------
;*Recebe dados em 16 bit*
rxdados:
bcf ackf
rbyte_:
bcf rd_wt
call bstart
movf word, w
movwf txbuf
call tx
movf addr_h, w
movwf txbuf
call tx
movf addr_l, w
movwf txbuf
call tx
bsf rd_wt
call bstart
movf word, w
movwf txbuf
call tx
call rx
call bstop
return
bstart:
bsf sdata
bsf status, rp0
bcf trisc, 3
bcf trisc, 4
bcf status, rp0
bcf sclk
nop
bsf sclk
nop
nop
nop
nop
nop
bcf sdata
nop
nop
nop
nop
nop
bcf sclk
nop
nop
return
bstop:
bcf sdata
bsf status, rp0
bcf trisc, 3
bcf trisc, 4
bcf status, rp0
bcf sdata
nop
nop
nop
bsf sclk
nop
nop
bsf sdata
nop
nop
bcf sclk
nop
nop
nop
return
bitout:
bsf status,rp0
bcf trisc,3
bcf trisc,4
bcf status,rp0
btfss do
goto bitlow
bsf sdata
goto clkout
bitlow:
bcf sdata
clkout:
bsf sclk
nop
nop
nop
nop
bcf sclk
return
bitin:
bsf di
bsf status,rp0
bcf trisc,3
bsf trisc,4
bcf status,rp0
bsf sclk
nop
nop
nop
nop
nop
btfss sdata
bcf di
bcf sclk
return
tx:
movlw .8
movwf count
txlp:
bcf do
btfsc txbuf, 7
bsf do
call bitout
rlf txbuf, f
decfsz count, f
goto txlp
call bitin
btfsc di
bsf ackf
return
rx:
clrf datai
movlw .8
movwf count
bcf status, 0
rxlp:
rlf datai, f
call bitin
btfsc di
bsf datai, 0
decfsz count, f
goto rxlp
bsf do
call bitout
return
;---------------
delaymem: ;+/- 10 MS
bank2
clrf loops
movlw .60
movwf loops2
delaymemo:
decfsz loops,f
goto delaymemo
decfsz loops2,f
goto delaymemo
bank0
return
;---------------