cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Problem STM32f072b

eeyjws
Associate II
Posted on March 21, 2015 at 16:51

Hi, I' am trying to let STM32f072b discovery board to communicate with my I2C device(TSL2561T).

But I'm having problem in that which I don't know why the code is not working.

I know where it stops (marked with orange color).

And please point out any mistake in my code.

 Here's my code.

&sharpdefine IIC_SDA_PIN GPIO_Pin_11

 

&sharpdefine IIC_SCL_PIN GPIO_Pin_10

 

&sharpdefine IIC_GPIO GPIOB

 

 

&sharpdefine IIC_SDA_PS GPIO_PinSource11

 

&sharpdefine IIC_SCL_PS GPIO_PinSource10

 

&sharpdefine IIC_PIN_AF GPIO_AF_1

 

 

&sharpdefine TSL2561_Add 0x39 //Slave addr � also 0x29 or 0x49

 

&sharpdefine TIMING  0x81  

 

&sharpdefine Control   0x80

 

&sharpdefine powerup      0x03

 

 

&sharpdefine DATA0LOW  0x8C

 

&sharpdefine DATA0HIGH 0x8D

 

&sharpdefine DATA1LOW  0x8E

 

&sharpdefine DATA1HIGH 0x8F

 

 

&sharpdefine LED1_Pin GPIO_Pin_9

 

&sharpdefine LED2_Pin GPIO_Pin_8

 

&sharpdefine LED3_Pin GPIO_Pin_7

 

&sharpdefine LED4_Pin GPIO_Pin_6

 

&sharpdefine LED_GPIO GPIOC

 

 

&sharpdefine uchar unsigned char

 

 

uchar  DataLow0,DataHigh0,DataLow1,DataHigh1,Channel0,Channel1;

 

uint32_t E;

 

 

void IIC_init(void)

 

{

 

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE );

 

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

 

 

 

  GPIO_PinAFConfig(IIC_GPIO, IIC_SCL_PS, GPIO_AF_1); // SCL

 

  GPIO_PinAFConfig(IIC_GPIO, IIC_SDA_PS, GPIO_AF_1); //SDA

 

 

 

  //Configure pins: SCL and SDA ------------------

 

  GPIO_InitTypeDef GPIO_InitStruct;

 

  GPIO_InitStruct.GPIO_Pin = IIC_SDA_PIN | IIC_SCL_PIN;

 

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_1;

 

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

 

  //GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

 

  GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;

 

  GPIO_Init(GPIOB, &GPIO_InitStruct);

 

 

 

  I2C_InitTypeDef  I2C_InitStruct;

 

  I2C_InitStruct.I2C_Timing = 0x00001010;

 

  I2C_InitStruct.I2C_AnalogFilter = I2C_AnalogFilter_Enable;

 

  I2C_InitStruct.I2C_OwnAddress1 = 0x08;  

 

  I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;

 

  I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;

 

  I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

 

  I2C_Init(I2C2, &I2C_InitStruct);

 

  I2C_Cmd(I2C2, ENABLE);

 

}

 

 

 

void IIC_write(uint8_t I2C_Register, uint8_t Val)

 

{

 

  while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) == SET);

 

  I2C_TransferHandling( I2C2, TSL2561_Add, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);

 

     

 

  //Ensure the transmit interrupted flag is set

 

  while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TXIS) == RESET);

 

  //Send the register address we wish to write to

 

   GPIO_SetBits(LED_GPIO,LED3_Pin);

 

  I2C_SendData(I2C2, I2C_Register);

 

 

 

  while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TCR) == RESET);

 

  I2C_TransferHandling(I2C2, TSL2561_Add, 1, I2C_AutoEnd_Mode, I2C_No_StartStop);

 

 

  while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TXIS) == RESET);

 

  I2C_SendData(I2C2, Val);

 

 

  while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF) == RESET);

 

  I2C_ClearFlag(I2C2, I2C_FLAG_STOPF);

 

}

 

 

uint32_t IIC_read(I2C_TypeDef* I2Cx, uint8_t I2C_Register)

 

{

 

  int8_t  SingleData = 0;

 

 

 

  while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) == SET);

 

  I2C_TransferHandling(I2C2, TSL2561_Add, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);

 

 

 

  while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TXIS) == RESET);

 

  I2C_SendData(I2C2, I2C_Register);

 

 

 

  while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TC) == RESET);

 

  I2C_TransferHandling(I2C2, TSL2561_Add, 1 , I2C_AutoEnd_Mode, I2C_Generate_Start_Read);

 

 

 

 

  while(I2C_GetFlagStatus(I2C2, I2C_FLAG_RXNE) == RESET);

 

  SingleData = I2C_ReceiveData(I2C2);

 

 

 

  while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF) == RESET);

 

  I2C_ClearFlag(I2C2, I2C_FLAG_STOPF);

 

 

  return SingleData;

 

}

 

 

void LED_init(void)

 

{

 

  GPIO_InitTypeDef GPIO_InitStruct;

 

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);

 

  GPIO_InitStruct.GPIO_Pin =LED1_Pin | LED3_Pin |LED2_Pin | LED4_Pin;

 

  GPIO_InitStruct.GPIO_Mode =GPIO_Mode_OUT;

 

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_2;

 

  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

 

  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;

 

  GPIO_Init(GPIOC , &GPIO_InitStruct);

 

}

 

 

int main(void)

 

{

 

  SystemInit();

 

  SystemCoreClockUpdate();

 

  SysTick_Config(SystemCoreClock/MillisecondsIT);

 

  IIC_init();

 

  LED_init();

 

  GPIO_SetBits(LED_GPIO,LED2_Pin);

 

  E=0;

 

 

  IIC_write(Control, powerup);

 

  DelayMil(10);

 

 

  while(1)

 

  {

 

    DataLow0 = IIC_read(I2C2, DATA0LOW);

 

    DataHigh0 = IIC_read(I2C2,DATA0HIGH);

 

    Channel0 = 256*DataHigh0 + DataLow0;

 

    DataLow1 =  IIC_read(I2C2,DATA1LOW);

 

    DataHigh1 =  IIC_read(I2C2,DATA1HIGH);

 

    Channel1 = 256*DataHigh1 + DataLow1;

 

    

 

    DelayMil(100);

 

    {

 

      //Calculating Lux

 

      if(0.0< Channel1/Channel0 && Channel1/Channel0 <=0.50)

 

        E=(0.0304*Channel0-0.062*Channel0*pow(Channel1/Channel0, 1.4));

 

      

 

      if(0.50<Channel1/Channel0&&Channel1/Channel0<=0.61)

 

        E=(0.0224*Channel0-0.031*Channel1);

 

      

 

      if(0.61<Channel1/Channel0&&Channel1/Channel0<=0.80)

 

        E=(0.0128*Channel0-0.0153*Channel1);

 

      

 

      if(0.80<Channel1/Channel0&&Channel1/Channel0<=1.30)

 

        E=(0.00146*Channel0-0.00112*Channel1);

 

      

 

      if(Channel1/Channel0>1.30)

 

        E=0;

 

    }

 

    

 

    if (E>1)

 

    {

 

      DelaySec(1);

 

      GPIO_SetBits(LED_GPIO,LED1_Pin);

 

    }

 

    else

 

      GPIO_ResetBits(LED_GPIO,LED1_Pin);  

 

  }       

 

}

#i2c-stm32f072b
0 REPLIES 0