2011-02-01 07:09 AM
Hi!
I want that stm8s-discovery read color using a color sensor (
ADJD-S371-QR999).
I use i2c protocol in this way:
I2C_GenerateSTART(ENABLE);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_START_SENT));</span></div>
// i want to read data from color sensor (0x74)
I2C_Send7bitAddress(0x74, I2C_DIRECTION_RX);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_ADDRESS_ACKED));I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);while (Rx_Idx < RX_BUFFERSIZE){while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED));I2C_Buffer_Rx[Rx_Idx++] = I2C_ReceiveData();}//nack.... in this point, master have to send a NACK to sensor in order to finish the i2c protocol.I2C_ClearITPendingBit(I2C_ITPENDINGBIT_ACKNOWLEDGEFAILURE); </span></div>I2C_GenerateSTOP(ENABLE);
I don't Know if this is the correct method to send NACK and moreover i not sure that the implementation of protocol is right.
I need help, please!
Thank you!
2011-02-01 11:55 PM
Hi Danielina,
try in this way, it'd work: /* Phase of I²C transmission */ /* Generate START condition and test on EV5 and clear it */ I2C_GenerateSTART(ENABLE); while(!I2C_CheckEvent(I2C_EVENT_MASTER_START_SENT)); /* Send slave 7 bits adress and test on EV6 and clear it */ I2C_Send7bitAddress(0x74, I2C_DIRECTION_RX); while(!I2C_CheckEvent(I2C_EVENT_MASTER_ADDRESS_ACKED)); I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED); /* Wait RXNE=1 EV7 bit is setI2C_Buffer_RX is ready to read */ while (Rx_Idx < RX_BUFFERSIZE) { while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED)); I2C_Buffer_Rx[Rx_Idx++] = I2C_ReceiveData(); } /* Generate STOP condition and leave the serial bus free */ I2C_GenerateSTOP(ENABLE); protocol seems ok, I think you can close comunication generating a STOP. brazov