Página 1 de 1

exemplo de comunicação i2c e lpc21xx...

MensagemEnviado: 17 Set 2009 17:17
por ze
estou há horas tentando fazer uma comunicação entre um lpc2102 e e um pcf8575 via i2c. por acaso você não tem vontade de compartilhar seu nobre conhecimento com um pobre véio desconhecido? melhor... voce não tem rotinas tipo i2cread() e i2cwrite() prontas?
Concordemos que o dataxit deste arm é 1/2 confuso se comparado aos pic´s e 51´s. com eles ná época consegui sucesso + rápido. talvez a idade.. sei lá.
grato!!!!

MensagemEnviado: 17 Set 2009 18:36
por austrax
To te mandando uma PM

MensagemEnviado: 17 Set 2009 19:35
por tcpipchip
no I2C do arm sempre gera evento para tudo...

http://www.inf.furb.br/~maw/arm/lpc-ARM-book_srn_p.pdf

Pagina 174

T+

TCPIPCHIP

MensagemEnviado: 18 Set 2009 11:54
por ze
oi tc & autrax. obrigado pelas dicas. falta pouco acho. mas está me faltando configurar a interrupção do i2c. veja o que já fiz:

Código: Selecionar todos
VICVectAddr0 = (unsigned long) tc0;         // set interrupt vector in 0
VICVectCntl0 = 0x20 | 4;                    // use it for Timer 0 Interrupt ... oque significa este 4?
VICVectAddr1 = (unsigned long) i2c1;        // set interrupt vector in 1: é isso mesmo?
VICVectCntl1 = 0x20 | [b]xxxx[/b];               // aqui é que o bicho pega acho
VICIntEnable = 0x00080010;                  // hab. i2c1 e Timer0 Interrupt

pro timer até que funcionou, mas pro i2c ...

ôrra, mó confusão estas ints deste armzinho do karái!

MensagemEnviado: 18 Set 2009 12:34
por tcpipchip
Ahhh, no começo assusta :)

Aqui um exemplo em BASIC para ARM (NXP) para comparares com teu código...
voce deu uma lida no DATASHEET ?

'''''''''''''''''''''''''''''''''''''''''''''''''''''''
' I2C
'''''''''''''''''''''''''''''''''''''''''''''''''''''''

'* Slave address
Const SlaveRcv As Byte = &hAA
Dim SlaveSnd As Byte

'* Initialize I2C0
Function init_I2C0(ByVal interval As Integer) as Boolean
Dim res As Boolean

#If _VERBOSE Then
Print #0, "Initializing I2C0";
#EndIf
' Install interrupt handler
init_I2C0 = __hbbr_install_irq(TIMER0_INT, AddressOf irq_I2C0)

#If _VERBOSE Then
If init_I2C0 Then
Print #0, "OK"
Else
Print #0, "Failed"
End If
#EndIf

End Function


'* Slave Irq handler
Irq I2C0_Isr()
Dim st As Byte
st = I2C0STAT
I2C0CONCLR = &h2C ' clear STA, AA and SI

Select Case (st)
Case &h60, &h68 ' &h60 own SLA+W received, Ack returned (slave receiver)
' &h68 Addressed as slave
I2C0CONSET = &h04 ' set AA, return ACK on first byte

Case &h80 ' Data received, ACK returned
SlaveRcv = I2C0DAT ' read and store data, NACK on next byte
IO1CLR = &h00FF0000 ' Turn off LEDs P1.16..23
IO1SET = SlaveRcv << 16 ' Turn on LED

Case &h88,&hA0,&hC0,&hC8 ' &h88 data received, NACK returned
' &hA0 STOP or REP.START received while addressed as slave
' &hC0 Data transmitted, NOT ACK received
' &hC8 Last data transmitted, ACK received
I2C0CONSET = &h04 ' set AA, switch to not addressed slave mode

Case &hA8, &hB8 ' &hA8 own SLA+R received, Ack returned (slave transmitter)
' &hB8 Data transmitted, ACK received
I2C0DAT = SlaveSnd ' Transmit last data AA = 0

End Select

VICVectAddr = 0 ' reset VIC
End Irq

Sub I2C0_Init()
PINSEL0 = PINSEL0 Or &h50 ' P0.3 = SDA, P0.2 = SCL
I2C0ADR = &h20 ' set I2C slave address
I2C0CONSET = &h44 ' enable I2C hardwar and set AA (ack)
'VICVectAddr0 = AddressOf I2C0_Isr
'VICVectCntl0 = &h29 ' Channel1 on Source#9 ... enabled
'VICIntEnable = VICIntEnable Or &h200 ' 9th bit is the I2C
End Sub


'* I2C0 Interrupt handler
Irq irq_I2C0()
PWMIR = 1 ' clear interrupt flag
VICVectAddr = 0 ' Acknowledge Interrupt
End Irq

Sub dump_I2C0()

Print #0, "I2C0 - registers"
Print #0, "-------------------------------------"

Print #0, "I2C0CONSET"
Print #0, "I2C0CONSET: "; I2C0CONSET

Print #0, "I2C0STAT"
Print #0, "I2C0STAT : "; I2C0STAT

Print #0, "I2C0DAT"
Print #0, "I2C0DAT : "; I2C0DAT

Print #0, "I2C0ADR"
Print #0, "I2C0ADR : "; I2C0ADR

Print #0, "I2C0SCLH"
Print #0, "I2C0SCLH : "; I2C0SCLH

Print #0, "I2C0SCLL"
Print #0, "I2C0SCLL : "; I2C0SCLL

Print #0, "I2C0CONCLR"
Print #0, "I2C0CONCLR: "; I2C0CONCLR

End Sub

Sub dump_I2C1()

Print #0, "I2C1 - register"
Print #0, "-------------------------------------"

Print #0, "I2C1CONSET"
Print #0, "I2C1CONSET: "; I2C1CONSET

Print #0, "I2C1STAT"
Print #0, "I2C1STAT : "; I2C1STAT

Print #0, "I2C1DAT"
Print #0, "I2C1DAT : "; I2C1DAT

Print #0, "I2C1ADR"
Print #0, "I2C1ADR : "; I2C1ADR

Print #0, "I2C1SCLH"
Print #0, "I2C1SCLH : "; I2C1SCLH

Print #0, "I2C1SCLL"
Print #0, "I2C1SCLL : "; I2C1SCLL

Print #0, "I2C1CONCLR"
Print #0, "I2C1CONCLR: "; I2C1CONCLR
End Sub

MensagemEnviado: 18 Set 2009 13:51
por austrax
Você tem os slots, onde determinará a prioridade da interrupção e os canais correspondentes aos periféricos geradores de interrupção.

Usando a int vetorada..


VICVectAddrA |= (unsigned long)i2c0; //local onde está o código para tratamento da int.
VICVectCntlB |= 0x02 | C ; //Seleciona slot
VICIntEnable |= (1<<D); //habilita a int.

A= Slot escolhido (número menor = maior prioridade)
B= Slot escolhido (número menor = maior prioridade)
C= Canal escolhido (Interrupt sources user manual pág. 53... utilize a Hex Mask)
D= Canal escolhido( Interrupt sources user manual pág. 53... utilize a Vic Channel)

http://www.keil.com/dd/docs/datashts/philips/user_manual_lpc2101_2102_2103.pdf

Abraços

MensagemEnviado: 18 Set 2009 14:00
por proex
Fala sério, eu fiz I2C por soft. O código fica muita mais enxuto e bem mais curto que isso tudo ai acima.

.

MensagemEnviado: 18 Set 2009 15:13
por austrax
Proex isso depende muito da sua aplicação. Se eu utilizasse I2C por software em uma aplicação que fiz com RTOS, teria corrompimento de dados da eeprom. A configuração da interrupção e a maquina de estados que vai no tratamento da int não gera código extenso.

MensagemEnviado: 18 Set 2009 15:23
por ze
curiosamente infelizmente e coincidentemente pensei seriamente em fazer por sw. mas quando fui ver meus fontes do século passado que já tinha feito pro 51, estão em asm e a mente não deixou portar.

Ler o datashit: não me ofendo pois faço esta pergunta com frequencia pra alguns discipulos também

é austrax. o código não fica extenso, mas o aprendizado. . .enviei mp

valeu caras e bfds (pra mim com chuva. saco)

MensagemEnviado: 18 Set 2009 16:28
por austrax
Veja lá :twisted: