STM32 使用 SHT2x 溫溼度傳感器

簡介

  1. 這TM的是個超級坑的通訊過程,雖然符合SPI通訊流程,但要是你直接用SPI通訊會發現完全不是這麼回事。這個是在STM32F0系列單片機上使用的範例,可以正常讀取溫度和溼度,只寫了主機模式。
  2. database.h 裏面沒啥東西,可以直接用#include "stm32f0xx.h"代替

代碼

SHT2x.h

#ifndef SHT2x_H
#define SHT2x_H
//---------- Includes ----------------------------------------------------------
#include "I2C_HAL.h"
#include "database.h"
//---------- Defines -----------------------------------------------------------
typedef union {
  uint16_t u16;               // element specifier for accessing whole u16
  int16_t i16;               // element specifier for accessing whole i16
  struct {
    #ifdef LITTLE_ENDIAN  // Byte-order is little endian
    uint8_t u8L;              // element specifier for accessing low u8
    uint8_t u8H;              // element specifier for accessing high u8
    #else                 // Byte-order is big endian
    uint8_t u8H;              // element specifier for accessing low u8
    uint8_t u8L;              // element specifier for accessing high u8
    #endif
  } s16;                  // element spec. for acc. struct with low or high u8
} nt16;
// Error codes
typedef enum{
  ACK_ERROR                = 0x01,
  TIME_OUT_ERROR           = 0x02,
  CHECKSUM_ERROR           = 0x04,
  UNIT_ERROR               = 0x08
}etError;

//  CRC
#define SHT2xPOLYNOMIAL 0x131  //P(x)=x^8+x^5+x^4+1 = 100110001

// sensor command
typedef enum{
  TRIG_T_MEASUREMENT_HM    = 0xE3, // command trig. temp meas. hold master
  TRIG_RH_MEASUREMENT_HM   = 0xE5, // command trig. humidity meas. hold master
  TRIG_T_MEASUREMENT_POLL  = 0xF3, // command trig. temp meas. no hold master
  TRIG_RH_MEASUREMENT_POLL = 0xF5, // command trig. humidity meas. no hold master
  USER_REG_W               = 0xE6, // command writing user register
  USER_REG_R               = 0xE7, // command reading user register
  SOFT_RESET               = 0xFE  // command soft reset
}etSHT2xCommand;

typedef enum {
  SHT2x_RES_12_14BIT       = 0x00, // RH=12bit, T=14bit
  SHT2x_RES_8_12BIT        = 0x01, // RH= 8bit, T=12bit
  SHT2x_RES_10_13BIT       = 0x80, // RH=10bit, T=13bit
  SHT2x_RES_11_11BIT       = 0x81, // RH=11bit, T=11bit
  SHT2x_RES_MASK           = 0x81  // Mask for res. bits (7,0) in user reg.
} etSHT2xResolution;

typedef enum {
  SHT2x_EOB_ON             = 0x40, // end of battery
  SHT2x_EOB_MASK           = 0x40, // Mask for EOB bit(6) in user reg.
} etSHT2xEob;

typedef enum {
  SHT2x_HEATER_ON          = 0x04, // heater on
  SHT2x_HEATER_OFF         = 0x00, // heater off
  SHT2x_HEATER_MASK        = 0x04, // Mask for Heater bit(2) in user reg.
} etSHT2xHeater;

// measurement signal selection
typedef enum{
  HUMIDITY,
  TEMP
}etSHT2xMeasureType;

typedef enum{
  I2C_ADR_W                = 128,   // sensor I2C address + write bit
  I2C_ADR_R                = 129    // sensor I2C address + read bit
}etI2cHeader;


uint8_t SHT2x_MeasureHM(etSHT2xMeasureType eSHT2xMeasureType, nt16 *pMeasurand);
float SHT2x_CalcRH(uint16_t u16sRH);
float SHT2x_CalcTemperatureC(uint16_t u16sT);

void SHT2x_Process();
#endif

SHT2x.c

//==============================================================================
//   SH2x 溫溼度採集程序
//==============================================================================
// File      :  SHT2x.c
// Author    :  MC
// Compiler  :  Keil 5
//==============================================================================

//---------- Includes ----------------------------------------------------------
#include "SHT2x.h"
#include <assert.h>                // assert functions
/*
校驗
data 校驗的數據
nbrOfBytes 數據長度
checksum 校驗對比數
返回 0 成功 CHECKSUM_ERROR 4 失敗
*/
//==============================================================================
uint8_t SHT2x_CheckCrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum)
//==============================================================================
{
  uint8_t crc = 0;	
  uint8_t byteCtr;
  //calculates 8-Bit checksum with given polynomial
  for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr)
  { crc ^= (data[byteCtr]);
    for (uint8_t bit = 8; bit > 0; --bit)
    { if (crc & 0x80) crc = (crc << 1) ^ SHT2xPOLYNOMIAL;
      else crc = (crc << 1);
    }
  }
  if (crc != checksum) return CHECKSUM_ERROR;
  else return 0;
}
/*
主機模式採集溫度或適度
eSHT2xMeasureType 需要採集的項,  HUMIDITY溼度 TEMP溫度
pMeasurand 採集回來的原始值
*/
//===========================================================================
uint8_t SHT2x_MeasureHM(etSHT2xMeasureType eSHT2xMeasureType, nt16 *pMeasurand)
//===========================================================================
{
  uint8_t  checksum;   //checksum
  uint8_t  data[2];    //data array for checksum verification
  uint8_t  error=0;    //error variable
  uint16_t i;          //counting variable

  //-- write I2C sensor address and command --
  I2c_StartCondition();
  error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr
	IIC_Wait_Ack();
	switch(eSHT2xMeasureType)
  { case HUMIDITY: I2c_WriteByte(TRIG_RH_MEASUREMENT_HM); break;
    case TEMP    : I2c_WriteByte(TRIG_T_MEASUREMENT_HM);  break;
    default: assert(0);
  }
	IIC_Wait_Ack();
	
	I2c_StartCondition();
	I2c_WriteByte (I2C_ADR_R);
	IIC_Wait_Ack();
	
  //-- read two data bytes and one checksum byte --
  pMeasurand->s16.u8H = data[0] = I2c_SHT21WithSCLReadByte(ACK);
  pMeasurand->s16.u8L = data[1] = I2c_ReadByte(ACK);
  checksum=I2c_ReadByte(NO_ACK);

//  //-- verify checksum --
  error |= SHT2x_CheckCrc (data,2,checksum);
  I2c_StopCondition();
  return error;
}
/*
原始值轉換爲溼度
u16sRH 原始值
返回 溼度
*/
//==============================================================================
float SHT2x_CalcRH(uint16_t u16sRH)
//==============================================================================
{
  float humidityRH;              // variable for result

  u16sRH &= ~0x0003;          // clear bits [1..0] (status bits)
  //-- calculate relative humidity [%RH] --

  humidityRH = -6.0 + 125.0/65536 * (float)u16sRH; // RH= -6 + 125 * SRH/2^16
  return humidityRH;
}
/*
原始值轉換爲溫度
u16sRH 原始值
返回 溫度
*/
//==============================================================================
float SHT2x_CalcTemperatureC(uint16_t u16sT)
//==============================================================================
{
  float temperatureC;            // variable for result

  u16sT &= ~0x0003;           // clear bits [1..0] (status bits)

  //-- calculate temperature  --
  temperatureC= -46.85 + 175.72/65536 *(float)u16sT; //T= -46.85 + 175.72 * ST/2^16
  return temperatureC;
}

/*
用戶線程
*/
nt16 sRH={0};                    //variable for raw humidity ticks
nt16 sT={0};                     //variable for raw temperature ticks
void SHT2x_Process(){
	// --- measure humidity with "Hold Master Mode (HM)"  ---
	SHT2x_MeasureHM(HUMIDITY, &sRH);
}

I2C_HAL.h

#ifndef I2C_HAL_H
#define I2C_HAL_H

//---------- Includes ----------------------------------------------------------
#include "database.h"

//---------- Defines -----------------------------------------------------------
//IO口操作宏定義
#define GPIOSCL GPIOA
#define PinSCL GPIO_Pin_2
#define GPIOSDA GPIOA
#define PinSDA  GPIO_Pin_3

//IO方向設置
#define SDA_IN()  {GPIOA->MODER&=0XFFFFFF3F;}
#define SDA_OUT() {uint32_t Temp=GPIOA->MODER;\
									Temp&=0XFFFFFF3F;\
									Temp|=0x00000040;\
									GPIOA->MODER=Temp;}

#define SCL_IN()  {GPIOA->MODER&=0XFFFFFFCF;}
#define SCL_OUT() {uint32_t Temp=GPIOA->MODER;\
									Temp&=0XFFFFFFCF;\
									Temp|=0x00000010;\
									GPIOA->MODER=Temp;}

//IO操作函數	 
#define IIC_SCL1    GPIO_SetBits(GPIOSCL,PinSCL)
#define IIC_SCL0    GPIO_ResetBits(GPIOSCL,PinSCL) //SCL

#define IIC_SDAx(x) GPIO_WriteBit(GPIOSDA,PinSDA,x)
#define IIC_SDA1    GPIO_SetBits(GPIOSDA,PinSDA) //SDA	 
#define IIC_SDA0    GPIO_ResetBits(GPIOSDA,PinSDA) //SDA

#define READ_SDA    GPIO_ReadInputDataBit(GPIOSDA,PinSDA)  //輸入SDA 
#define READ_SCL    GPIO_ReadInputDataBit(GPIOSDA,PinSCL)  //輸入SCL 

//---------- Enumerations ------------------------------------------------------
//  I2C level
typedef enum{
  LOW                      = 0,
  HIGH                     = 1,
}etI2cLevel;

// I2C acknowledge
typedef enum{
  ACK                      = 0,
  NO_ACK                   = 1,
}etI2cAck;

//==============================================================================
void I2c_Init ();
//==============================================================================
//Initializes the ports for I2C interface

//==============================================================================
void I2c_StartCondition ();
//==============================================================================
// writes a start condition on I2C-bus
// input : -
// output: -
// return: -
// note  : timing (delay) may have to be changed for different microcontroller
//       _____
// SDA:       |_____
//       _______
// SCL :        |___

//==============================================================================
void I2c_StopCondition ();
//==============================================================================
// writes a stop condition on I2C-bus
// input : -
// output: -
// return: -
// note  : timing (delay) may have to be changed for different microcontroller
//              _____
// SDA:   _____|
//            _______
// SCL :  ___|

//===========================================================================
uint8_t I2c_WriteByte (uint8_t txByte);
//===========================================================================
// writes a byte to I2C-bus and checks acknowledge
// input:  txByte  transmit byte
// output: -
// return: error
// note: timing (delay) may have to be changed for different microcontroller

//===========================================================================
uint8_t I2c_ReadByte (etI2cAck ack);
//===========================================================================
// reads a byte on I2C-bus
// input:  rxByte  receive byte
// output: rxByte
// note: timing (delay) may have to be changed for different microcontroller
u8 IIC_Wait_Ack(void);
uint8_t I2c_SHT21WithSCLReadByte(etI2cAck ack);
#endif

I2C_HAL.c

//---------- Includes ----------------------------------------------------------
#include "I2C_HAL.h"

//==============================================================================
void I2c_Init ()
//==============================================================================
{
	GPIO_InitTypeDef       GPIO_InitStructure; 
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2|GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
  IIC_SDA0;                // Set port as output for configuration
  IIC_SCL0;                // Set port as output for configuration

  SDA_OUT();           // Set SDA level as low for output mode
	SCL_OUT();

  IIC_SDA1;               // I2C-bus idle mode SDA released (input)
  IIC_SCL1;               // I2C-bus idle mode SCL released (input)
}

//==============================================================================
void I2c_StartCondition ()
//==============================================================================
{
	SDA_OUT();
	SCL_OUT();
  IIC_SDA1;
  IIC_SCL1;
	delay_us(20);  // hold time start condition (t_HD;STA)
  IIC_SDA0;
  delay_us(20);  // hold time start condition (t_HD;STA)
  IIC_SCL0;
  delay_us(20);
}

//==============================================================================
void I2c_StopCondition ()
//==============================================================================
{
	SDA_OUT();
	SCL_OUT();
  IIC_SDA0;
  IIC_SCL0;
	delay_us(20);  // set-up time stop condition (t_SU;STO)
  IIC_SCL1;
  delay_us(20);  // set-up time stop condition (t_SU;STO)
  IIC_SDA1;
  delay_us(20);
}
//不產生ACK應答		    
void IIC_NAck(void)
{
	SCL_OUT();
	SDA_OUT();
	IIC_SCL0;
	IIC_SDA1;
	delay_us(10);
	IIC_SCL1;
	delay_us(10);
	IIC_SCL0;
}		
//產生ACK應答
void IIC_Ack(void)
{
	SCL_OUT();
	SDA_OUT();
	IIC_SCL0;
	IIC_SDA0;
	delay_us(10);
	IIC_SCL1;
	delay_us(10);
	IIC_SCL0;
} 
//等待應答信號到來
//返回值:1,接收應答失敗
//        0,接收應答成功
u8 IIC_Wait_Ack(void)
{
	u8 ucErrTime=0;
	SDA_IN();      //SDA設置爲輸入   
	IIC_SCL1;delay_us(5);	 
	while(READ_SDA)
	{
		ucErrTime++;
		if(ucErrTime>250)
		{
			I2c_StopCondition();
			return 1;
		}
		delay_us(100);	
	}
	IIC_SCL0;//時鐘輸出0 	   
	return 0;  
} 
//==============================================================================
uint8_t I2c_WriteByte (uint8_t txByte)
//==============================================================================
{
  uint8_t mask,error=0;
	u8 t;
	SCL_OUT();	
	SDA_OUT(); 	    
	IIC_SCL0;//拉低時鐘開始數據傳輸
	for(t=0;t<8;t++)
	{              
			IIC_SDAx((txByte&0x80)>>7);
			txByte<<=1; 	  
			delay_us(10);   //對TEA5767這三個延時都是必須的
			IIC_SCL1;
			delay_us(10); 
			IIC_SCL0;	
			delay_us(10);
	}
  return error;                       //return error code
}

//==============================================================================
uint8_t I2c_ReadByte (etI2cAck ack)
//==============================================================================
{
  unsigned char i,receive=0;
	SCL_OUT();
	SDA_IN();//SDA設置爲輸入
	for(i=0;i<8;i++ )
	{
			IIC_SCL0; 
			delay_us(5);
			IIC_SCL1;
			receive<<=1;
			if(READ_SDA)receive++;   
			delay_us(5); 
	}					 
	if (ack==ACK)
			IIC_Ack(); //發送ACK 
	else
			IIC_NAck();//發送nACK
	return receive;
}

//==============================================================================
uint8_t I2c_SHT21WithSCLReadByte(etI2cAck ack)
//==============================================================================
{
	uint16_t i;
	unsigned char receive=0;
	SCL_IN();                     // set SCL I/O port as input
	SDA_IN();
  for(i=0; i<10000; i++)         // wait until master hold is released or
  { delay_us(10);    // a timeout (~1s) is reached
    if (READ_SCL==1){
			if(READ_SDA)receive++; 
			break;
		}
  }
	
	SCL_OUT();
	SDA_IN();//SDA設置爲輸入
	for(i=0;i<7;i++ )
	{
			IIC_SCL0; 
			delay_us(5);
			IIC_SCL1;
			receive<<=1;
			if(READ_SDA)receive++;   
			delay_us(5); 
	}					 
	if (ack==ACK)
			IIC_Ack(); //發送ACK 
	else
			IIC_NAck();//發送nACK
	return receive;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章