Advertisement
MarcoAOC

STM8L i2c help

May 6th, 2018
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.84 KB | None | 0 0
  1.  void I2C_NFC_Init(void){
  2.    
  3.     CLK_PeripheralClockConfig(CLK_Peripheral_I2C1,ENABLE);
  4.  
  5.     I2C_Init(I2C1,100000, 0xA0 , I2C_Mode_I2C, I2C_DutyCycle_2,
  6.             I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit);
  7.    I2C_AcknowledgeConfig(I2C1,ENABLE);
  8.    I2C_Cmd(I2C1,ENABLE);
  9.     //sEE_LowLevel_Init();
  10.    //DMA_Init();
  11.    I2C_DMACmd(I2C1, ENABLE);
  12.  }
  13.  
  14.  void i2c_ByteWrite(u8 I2C_Slave_Adress,u8 iData, u16 WriteAddr){
  15.    
  16.  
  17.       I2C_GenerateSTART(I2C1,ENABLE);
  18.       //I2C_Event_TypeDef teste = I2C_GetLastEvent(I2C1);
  19.      
  20.       while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
  21.  
  22.       I2C_Send7bitAddress(I2C1,(uint8_t)I2C_Slave_Adress,I2C_Direction_Receiver);
  23.      
  24.     while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));//This while never stops
  25.      
  26.        /* Send Address (on 2 bytes) of first byte to be written & wait event detection */
  27.       I2C_SendData(I2C1,(u8)(WriteAddr >> 8)); /* MSB */
  28.       /* Test on EV8 and clear it */
  29.       while (!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  30.       I2C_SendData(I2C1,(u8)(WriteAddr & 0xFF)); /* LSB */
  31.       /* Test on EV8 and clear it */
  32.      while (!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  33.      
  34.       /* Send the byte to be written */
  35.       I2C_SendData(I2C1,iData);
  36.        
  37.       /* Test on EV8 and clear it */
  38.       while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  39.      
  40.       /* Send STOP condition */
  41.       I2C_SendData(I2C1,iData);
  42.      I2C_GenerateSTOP(I2C1,ENABLE);
  43.      
  44.      
  45.      while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  46.      
  47.      I2C_GenerateSTOP(I2C1,ENABLE);
  48.     //}
  49.  }    
  50. #define M24LR04_Addr                 0xa6 //i'v tried 0x53 and 0xa7
  51. void main(void){
  52.     I2C_DeInit(I2C1);
  53.     I2C_NFC_Init();
  54.     i2c_ByteWrite(M24LR04_Addr,temp,0x00);
  55. while(1){}
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement