Advertisement
Guest User

Untitled

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