Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void readmem (unsigned char addr, char *buf, int n)
- {
- while(I2C_GetFlagStatus (I2C_ITG3200, I2C_FLAG_BUSY));
- /* Send START condition */
- I2C_GenerateSTART (I2C_ITG3200, ENABLE);
- /* Test on EV5 and clear it */
- while (!I2C_CheckEvent(I2C_ITG3200, I2C_EVENT_MASTER_MODE_SELECT));
- /* In the case of a single data transfer disable ACK before reading the data */
- if (n == 1)
- I2C_AcknowledgeConfig (I2C_ITG3200, DISABLE);
- /* Send EEPROM address for write */
- I2C_Send7bitAddress (I2C_ITG3200, ITG3200_SLAVE_ADDR, I2C_Direction_Transmitter);
- /* Test on EV6 and clear it */
- while (!I2C_CheckEvent (I2C_ITG3200, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
- /* Clear EV6 by setting again the PE bit */
- I2C_Cmd (I2C_ITG3200, ENABLE);
- /* Send the EEPROM's internal address to write to */
- I2C_SendData (I2C_ITG3200, addr);
- /* Test on EV8 and clear it */
- while (!I2C_CheckEvent (I2C_ITG3200, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
- /* Send STRAT condition a second time */
- I2C_GenerateSTART (I2C_ITG3200, ENABLE);
- /* Test on EV5 and clear it */
- while (!I2C_CheckEvent (I2C_ITG3200, I2C_EVENT_MASTER_MODE_SELECT));
- /* Send EEPROM address for read */
- I2C_Send7bitAddress (I2C_ITG3200, ITG3200_SLAVE_ADDR, I2C_Direction_Receiver);
- /* Test on EV6 and clear it */
- while (!I2C_CheckEvent (I2C_ITG3200, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
- /* While there is data to be read */
- while (n) {
- /* Test on EV7 and clear it */
- if (I2C_CheckEvent (I2C_ITG3200, I2C_EVENT_MASTER_BYTE_RECEIVED)) {
- if(n == 2)
- I2C_AcknowledgeConfig (I2C_ITG3200, DISABLE); /* Disable Acknowledgement */
- if(n == 1)
- I2C_GenerateSTOP (I2C_ITG3200, ENABLE); /* Send STOP Condition */
- /* Read a byte from the EEPROM */
- *buf = I2C_ReceiveData (I2C_ITG3200);
- /* Point to the next location where the byte read will be saved */
- buf ++;
- /* Decrement the read bytes counter */
- n --;
- }
- }
- /* Enable Acknowledgement to be ready for another reception */
- I2C_AcknowledgeConfig (I2C_ITG3200, ENABLE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement