cancel
Showing results for 
Search instead for 
Did you mean: 

Read pressure sensor with I2C in master receiver mode

philipgillespie
Associate II
Posted on June 08, 2011 at 10:12

Hi All,

Need some advise here with I2C.  I'm trying to use I2C in master receiver mode to read two bytes of data out of a pressure sensor.  The required command sequence is pg 8 of attached data sheet.

Here is the code:

       GPIO_InitTypeDef  GPIO_InitStructure;

       /* SDA */

      GPIO_InitStructure.GPIO_Pin =     GPIO_Pin_7;

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

      GPIO_InitStructure.GPIO_Mode =   GPIO_Mode_AF_OD;

      GPIO_Init(GPIOB, &GPIO_InitStructure);

      GPIO_InitStructure.GPIO_Pin       =   GPIO_Pin_6;

      GPIO_InitStructure.GPIO_Speed   =  GPIO_Speed_50MHz;          

      GPIO_InitStructure.GPIO_Mode    =  GPIO_Mode_AF_OD;              

      GPIO_Init(GPIOB, &GPIO_InitStructure);

       I2C_InitTypeDef  I2C_InitStructure;

     /* I2C configuration */

     I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

     I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

     I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;

     I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

     I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

     I2C_InitStructure.I2C_ClockSpeed = I2C_Speed;

     I2C_Cmd(I2C1, ENABLE);

     I2C_Init(I2C1, &I2C_InitStructure);

        int sensor_data[2];

        sensor_data[0] = 0x00;

        sensor_data[1] = 0x00;

        /* Enable I2C1 acknowledgement if it is already disabled by other function */

        I2C_AcknowledgeConfig(I2C1, ENABLE);

        /* While the bus is busy */

        while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));

        /*----- Transmission Phase -----*/

        /* Send START condition */

        I2C_GenerateSTART(I2C1, ENABLE);

        /* Test on EV5 and clear it */

        while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

                       

        /* Send sensor address */

        I2C_SendData(I2C1,0xF1);       

        /* Test on EV6 and clear it */

        while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

                        

        /* Read a byte from the sensor*/

          sensor_data[0] = I2C_ReceiveData(I2C1);

       

        /* Test on EV7 and clear it */

        while(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));

       

        sensor_data[1] = I2C_ReceiveData(I2C1);

       

        /* Test on EV7 and clear it */

        while(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));

           

        /* Send I2C1 STOP Condition */

        I2C_GenerateSTOP(I2C1, ENABLE);

After i send out the address of the sensor, i'm seeing (on the scope) two byte of data with 18 clock cycles.  I assumed this data is only read out using I2C_ReceiveData? Also is a NACK command required before stop commmand?

Any suggestions about what I'm not doing correctly would be greatly appreciated.

--

Thanks  

#embedded-c
0 REPLIES 0