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