AnsweredAssumed Answered

I2C communication with humidity/temperature sensor HIH6130

Question asked by ahmed.ahlem on Mar 12, 2013
Latest reply on Jun 26, 2014 by cooke.ernest
hello every body 
i'm using STM32f103RBT6 to develop several projects. Actually i'm having a problem with the I2C communication since i've been trying for a month to make it work but no results until now.
so the main idea is to receive the value of the humidity from honeywell humidity/temperature sensor HIH6130 (according to the datasheet of this sensor the default adress is 0x27). i'm using Keil µvision as a development enviromment .
i developped some others codes beside this one posted here, but i noticed that there is no data coming from the sensor and the value displayed is always the slave address(i mean 0x27) so i decided to change it all and this is how i wrote my new code which not working and freeze after the function :I2C_WaitSensorStandbyState() :

#include <stdio.h>


#include "LCD.h"
#include "Serial.h"
#include "LED.h"
#include "ADC.h"
#include "stm32f10x_lib.h"
#include "stm32f10x_i2c.h"
#include "lcd.h"




#define I2C_TIMEOUT      100000
#define I2C1_DR_Address        0x40005410
#define I2C1_SLAVE_ADDRESS7    0x27
#define ClockSpeed             100000
#define DELAY_2N     0


/*----------variables globales-------------*/


/*-------------------------------------------------------------------------------
  delays function
*------------------------------------------------------------------------------*/
#define DELAY_2N     0
 static void delay (int cnt)
{
  cnt <<= DELAY_2N;


  while (cnt--);
}


/*******************************************************************************
* Function Name  : I2C_WaitsensorStandbyState
* Description    : Wait for the sensor to pass to the Standby state
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_WaitSensorStandbyState(void)      
{
  vu16 SR1_Tmp = 0;


  do
  {
    /* Send START condition */
    I2C_GenerateSTART(I2C1, ENABLE);
    /* Read I2C1 SR1 register */
    SR1_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1);
    /* Send EEPROM address for write */
    I2C_Send7bitAddress(I2C1,I2C1_SLAVE_ADDRESS7<<1, I2C_Direction_Transmitter);
  }while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1))); //& 0x0002));
  
  /* Clear AF flag */
  I2C_ClearFlag(I2C1, I2C_FLAG_AF);
  
  /* STOP condition */    
  I2C_GenerateSTOP(I2C1, ENABLE);  
}
/*******************************************************************************
* Function Name  : I2C_Configuration
* Description    : I2C Configuration
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_Configuration(void)
{
  I2C_InitTypeDef  I2C_InitStructure; 


  /* I2C configuration */
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  I2C_InitStructure.I2C_OwnAddress1 = 0x00;     // change it to 0x00 as it's the Master adress 
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
  
  /* I2C Peripheral Enable */
  I2C_Cmd(I2C1, ENABLE);


  /* I2C De-initialize */
  I2C_DeInit(I2C1);
  /* Apply I2C configuration after enabling it */
  I2C_Init(I2C1, &I2C_InitStructure);
}
  


/*----------------------------------------------------------------------------
 * Main Program
 *----------------------------------------------------------------------------*/


int main (void){


u8  humidity[5];
u8* pBuffer;
int NumByteToRead=2;


u16 V1=0;
u16 V2=0;
u16 valeur_finale;
char humidity_value; 
int i=1; 


/*--------------------------------------------------------------------*/


#ifdef DEBUG
  debug();
#endif
/*----------------------------------------------------------------------
 * Configurating I2C for the communication*/
/*----------------------------------------------------------------------*/


 I2C_Configuration();
 I2C


/*---------------------------------------------------------------------
 * wait until the sensor is on a standby status
/*---------------------------------------------------------------------*/


I2C_WaitSensorStandbyState();




/*----------------------------------------------------------------------


 * calculate the humidity value


/*----------------------------------------------------------------------*/


    /* While the bus is busy */
  while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
  
  /* Send START condition */
  I2C_GenerateSTART(I2C1, ENABLE);
   delay(20000);
  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
   
  /* Send EEPROM address for write */
  I2C_Send7bitAddress(I2C1,I2C1_SLAVE_ADDRESS7<<1,I2C_Direction_Transmitter);


  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
  
  /* Clear EV6 by setting again the PE bit */
  I2C_Cmd(I2C1, ENABLE);


  /* Send the EEPROM's internal address to write to */
  I2C_SendData(I2C1, 0x27);  


  /* Test on EV8 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  
  /* Send STRAT condition a second time */  
  I2C_GenerateSTART(I2C1, ENABLE);
  
  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
  
  /* Send EEPROM address for read */
  I2C_Send7bitAddress(I2C1, I2C1_SLAVE_ADDRESS7<<1, I2C_Direction_Receiver);
  
  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
  
  /* While there is data to be read */
  while(NumByteToRead)  
  {
    if(NumByteToRead == 1)
    {
      /* Disable Acknowledgement */
      I2C_AcknowledgeConfig(I2C1, DISABLE);
      
      /* Send STOP Condition */
      I2C_GenerateSTOP(I2C1, ENABLE);
    }


    /* Test on EV7 and clear it */
    if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))  
    {      
      /* Read a byte from the EEPROM */
      *pBuffer = I2C_ReceiveData(I2C1);


       humidity[i]=*pBuffer;
       lcd_init();
       lcd_clear();
       lcd_print(&humidity[i]);
      /* Point to the next location where the byte read will be saved */
      pBuffer++; 
      i++;
      /* Decrement the read bytes counter */
      NumByteToRead--;        
    }   
  }


  /* Enable Acknowledgement to be ready for another reception */
  I2C_AcknowledgeConfig(I2C1, ENABLE);


  /*Generate Stop*/


  I2C_GenerateSTOP(I2C1, ENABLE);




/*---------------------------------------------------------------------------------


 * Display humidity value


/*----------------------------------------------------------------------------------*/


  V1=humidity[1];


  V2=humidity[2];


  /*humidity value*/


 valeur_finale=(((V1&0x3f)<<8)|V2);




 /*the humidity */


  humidity_value=((valeur_finale)/((2^14) - 2))* 100;

 } 

the configuration :
 /*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;


  /* Configure I2C1 pins: SCL and SDA */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  GPIO_Init(GPIOB, &GPIO_InitStructure);


}


/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();


  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);


  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();


  if(HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer */
  //  FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);


   // /* Flash 2 wait state */
   // FLASH_SetLatency(FLASH_Latency_2);
 
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1); 
  
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1); 


    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);
 
    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);


    /* Enable PLL */ 
    RCC_PLLCmd(ENABLE);


    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }


    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);


    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }


/* Enable peripheral clocks --------------------------------------------------*/
  /* GPIOB Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  /* I2C1 Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
}
 
can anybody help me with that and take a look at the code

thank you 


 

Outcomes