Advertisement
Guest User

Untitled

a guest
Dec 29th, 2010
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. static void readmem (unsigned char addr, unsigned char *buf, int n)
  2. {
  3.     while(I2C_GetFlagStatus (I2C_ITG3200, I2C_FLAG_BUSY));
  4.     /* Send START condition */
  5.     I2C_GenerateSTART (I2C_ITG3200, ENABLE);
  6.  
  7.     /* Test on EV5 and clear it */
  8.     while (!I2C_CheckEvent(I2C_ITG3200, I2C_EVENT_MASTER_MODE_SELECT));
  9.     /* In the case of a single data transfer disable ACK before reading the data */
  10.  
  11.     /* Send EEPROM address for write */
  12.     I2C_Send7bitAddress (I2C_ITG3200, ITG3200_SLAVE_ADDR, I2C_Direction_Transmitter);
  13.  
  14.     /* Test on EV6 and clear it */
  15.     while (!I2C_CheckEvent (I2C_ITG3200, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
  16.  
  17.     /* Send the EEPROM's internal address to write to */
  18.     I2C_SendData (I2C_ITG3200, addr);
  19.     /* Test on EV8 and clear it */
  20.     while (!I2C_CheckEvent (I2C_ITG3200, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  21.     /* Send STRAT condition a second time */  
  22.     I2C_GenerateSTART (I2C_ITG3200, ENABLE);
  23.     /* Test on EV5 and clear it */
  24.     while (!I2C_CheckEvent (I2C_ITG3200, I2C_EVENT_MASTER_MODE_SELECT));
  25.     /* Send EEPROM address for read */
  26.     I2C_Send7bitAddress (I2C_ITG3200, ITG3200_SLAVE_ADDR, I2C_Direction_Receiver);
  27.     /* Test on EV6 and clear it */
  28.     while (!I2C_CheckEvent (I2C_ITG3200, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
  29.    
  30.     /* While there is data to be read */
  31.     while (n) {
  32.         if(n == 1) {
  33.             I2C_AcknowledgeConfig (I2C_ITG3200, DISABLE);       /* Disable Acknowledgement */
  34.             I2C_GenerateSTOP (I2C_ITG3200, ENABLE);         /* Send STOP Condition */
  35.         }
  36.         /* Test on EV7 and clear it */
  37.         if (I2C_CheckEvent (I2C_ITG3200, I2C_EVENT_MASTER_BYTE_RECEIVED)) {
  38.             if(n == 2)
  39.                 I2C_AcknowledgeConfig (I2C_ITG3200, DISABLE);       /* Disable Acknowledgement */
  40.            
  41.             /* Read a byte from the EEPROM */
  42.             *buf = I2C_ReceiveData (I2C_ITG3200);
  43.             /* Point to the next location where the byte read will be saved */
  44.             buf ++;
  45.             /* Decrement the read bytes counter */
  46.             n --;    
  47.         }  
  48.     }
  49.  
  50.     /* Enable Acknowledgement to be ready for another reception */
  51.     I2C_AcknowledgeConfig (I2C_ITG3200, ENABLE);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement