Advertisement
Guest User

Untitled

a guest
Mar 9th, 2012
1,698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. static void I2C1_GPIO_Config(void)
  2. {
  3.   GPIO_InitTypeDef  GPIO_InitStructure;
  4.   //GPIO_DeInit(GPIOB);  
  5.  
  6.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  7.  
  8.   // Configure I2C1 pins: SCL and SDA
  9.   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
  10.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;  
  12.   GPIO_Init(GPIOB, &GPIO_InitStructure);  
  13. }
  14.  
  15. void I2C_delay(void)
  16. {
  17.    unsigned int i=150;
  18.    while(i)
  19.    {
  20.      i--;
  21.    }
  22. }
  23.  
  24. bool I2C1_Start(void)
  25. {
  26.  SDAH;
  27.  SCLH;
  28.  I2C_delay();
  29.  if(!SDAread)return FALSE;
  30.  SDAL;
  31.  I2C_delay();
  32.  if(SDAread) return FALSE;
  33.  SDAL;
  34.  I2C_delay();
  35.  return TRUE;
  36. }
  37.  
  38. void I2C1_Stop(void)
  39. {
  40.  SCLL;
  41.  I2C_delay();
  42.  SDAL;
  43.  I2C_delay();
  44.  SCLH;
  45.  I2C_delay();
  46.  SDAH;
  47.  I2C_delay();
  48. }
  49.  
  50. void I2C1_Ack(void)
  51. {
  52.  SCLL;
  53.  I2C_delay();
  54.  SDAL;
  55.  I2C_delay();
  56.  SCLH;
  57.  I2C_delay();
  58.  SCLL;
  59.  I2C_delay();
  60. }
  61.  
  62. void I2C1_NoAck(void)
  63. {
  64.  SCLL;
  65.  I2C_delay();
  66.  SDAH;
  67.  I2C_delay();
  68.  SCLH;
  69.  I2C_delay();
  70.  SCLL;
  71.  I2C_delay();
  72. }
  73.  
  74. bool I2C1_WaitAck(void)  
  75. {
  76.  SCLL;
  77.  I2C_delay();
  78.  SDAH;  
  79.  I2C_delay();
  80.  SCLH;
  81.  I2C_delay();
  82.  if(SDAread)
  83.  {
  84.           SCLL;
  85.           return FALSE;
  86.  }
  87.  SCLL;
  88.  return TRUE;
  89. }
  90.  
  91. void I2C1_SendByte(unsigned char SendByte)
  92. {
  93.     unsigned char i=8;
  94.     while(i--)
  95.     {
  96.         SCLL;
  97.         I2C_delay();
  98.         if(SendByte&0x80)
  99.           SDAH;  
  100.         else
  101.         SDAL;  
  102.         SendByte<<=1;
  103.         I2C_delay();
  104.  SCLH;
  105.         I2C_delay();
  106.     }
  107.     SCLL;
  108. }
  109.  
  110. unsigned char I2C1_ReceiveByte(void)  
  111. {
  112.     unsigned char i=8;
  113.     unsigned char ReceiveByte=0;
  114.  
  115.     SDAH;    
  116.     while(i--)
  117.     {
  118.       ReceiveByte<<=1;      
  119.       SCLL;
  120.       I2C_delay();
  121.       SCLH;
  122.       I2C_delay();
  123.       if(SDAread)
  124.       {
  125.         ReceiveByte|=0x01;
  126.       }
  127.     }
  128.     SCLL;
  129.     return ReceiveByte;
  130. }
  131.  
  132.  
  133. unsigned char I2C1_ReadByte( unsigned char DeviceAddress, int ReadAddress)
  134. {  
  135.     unsigned char temp;
  136.     if(!I2C1_Start())return FALSE;    
  137.  
  138.     I2C1_SendByte((DeviceAddress & 0xFE));
  139.     if(!I2C1_WaitAck()){I2C1_Stop(); return FALSE;}
  140.    
  141.  
  142.     I2C1_SendByte((unsigned char)((ReadAddress>>8) & 0xFF));    
  143.     I2C1_WaitAck();
  144.     I2C1_SendByte((unsigned char)((ReadAddress) & 0xFF));        
  145.     I2C1_WaitAck();
  146.     I2C1_Start();
  147.     I2C1_SendByte((DeviceAddress & 0xFE)|0x01);    
  148.     I2C1_WaitAck();
  149.    
  150.     temp = I2C1_ReceiveByte();
  151.    
  152.     I2C1_NoAck();
  153.      
  154.     I2C1_Stop();
  155.     return temp;
  156. }
  157.  
  158. /************************************MAIN****************************/
  159. int main( void )
  160. {
  161.   prvSetupHardware();
  162.   I2C1_GPIO_Config();
  163.   initDisplay();
  164.   itoc(I2C1_ReadByte(I2C_ADDRESS, 0x0F));
  165.   GLCD_displayStringLn(Line3, string);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement