This humidity temperature sensor is easy to setup.Make sure to send the slave address as 0x80 since we are using 8bit data bus.
Stm32-Discovery pins are wired to Si7021 as follows
Stm32-Discovery Si7021
====================================
PB6 SCL
PB7 SDA
GND GND
3V3 3V3
My Code
main.c
#include "stm32f10x.h"
#include "STM32vldiscovery.h"
/* Private typedef -----------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
TIM_TimeBaseInitTypeDef timerInitStructure;
/* Private variables ---------------------------------------------------------*/
float humidityValue;
float temperatureValue;
/* Private function prototypes -----------------------------------------------*/
void Delay(__IO uint32_t nCount);
void Enable_Clock(void);
void Config_GPIO(void);
void Init_I2C(void);
float ReadHumidity(void);
float readRegisterTwoBytes(int8_t, int8_t);
float ReadTemparature(void);
int main(void){
Enable_Clock();
Config_GPIO();
I2C_DeInit(I2C1);
Init_I2C();
while (1)
{
//Reading Humidity and Temperature
humidityValue=ReadHumidity();
temperatureValue=ReadTemperature();
}
}
void Enable_Clock(){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
}
void Config_GPIO(){
//Config SCL
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//Config SDA
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void Init_I2C(void){
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE);
I2C_Cmd(I2C1, ENABLE);
}
float ReadHumidity(){
uint16_t RH;
float humid;
RH=readRegisterTwoBytes(0xE5,0x80);
humid=(125*RH/65536)-6;
return humid;
}
float ReadTemparature(){
uint16_t tmp;
float temparature ;
tmp=readRegisterTwoBytes(0xE3,0x80);
temparature=(175.72*tmp/65536)-46.85;
temparature=(temparature*9/5)+32;
return temparature;
}
float readRegisterTwoBytes(int8_t reg, int8_t dAddr){
int8_t n=0;
uint16_t RegVal = 0;
float humid=0;
uint8_t BufferRX[2] ={0,0};
//send START condition a first time
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)){
}
I2C_Send7bitAddress(I2C1, dAddr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) {
}
I2C_SendData(I2C1, reg);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) {
}
//Send START condition a second time
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)){
}
// Send address for read
I2C_Send7bitAddress(I2C1, dAddr|0x01, I2C_Direction_Receiver);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)){
}
//read first byte
while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)){
}
BufferRX[n] = I2C_ReceiveData(I2C1);
n++;
//read second byte
while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)){
}
BufferRX[n] = I2C_ReceiveData(I2C1);
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1,ENABLE);
while (I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)){
}
n=0;
RegVal = (uint16_t)(BufferRX[0] << 8);
RegVal |= BufferRX[1];
return RegVal
}
Opmerkingen