OpenEdv-开源电子网

 找回密码
 立即注册

扫一扫,访问微社区

正点原子新作:阿波罗STM32F767&F429&探索者STM32F4开发板&赶快来下载资料哦。

查看: 500|回复: 3

HAL写的F4定时器查询HTS211数据无法更新问题

[复制链接]

  离线 

2

主题

11

帖子

0

精华

新手上路

积分
24
金钱
24
注册时间
2018-2-6
在线时间
5 小时
发表于 2018-2-6 23:39:31 | 显示全部楼层 |阅读模式
6金钱
本帖最后由 yxzy150 于 2018-2-7 00:17 编辑

不能实时更新数据 也不知道错误出在哪 求大神相助

F411_NUC_CUBEMXZ NO7.rar

4.02 KB, 下载次数: 26

定时中断和HTS221

最佳答案

回复

使用道具 举报

  离线 

2

主题

11

帖子

0

精华

新手上路

积分
24
金钱
24
注册时间
2018-2-6
在线时间
5 小时
 楼主| 发表于 2018-2-6 23:39:32 | 显示全部楼层
回复

使用道具 举报

  离线 

2

主题

11

帖子

0

精华

新手上路

积分
24
金钱
24
注册时间
2018-2-6
在线时间
5 小时
 楼主| 发表于 2018-2-12 18:31:45 | 显示全部楼层
自己都弄完了。。。
回复

使用道具 举报

  离线 

2

主题

11

帖子

0

精华

新手上路

积分
24
金钱
24
注册时间
2018-2-6
在线时间
5 小时
 楼主| 发表于 2018-2-15 14:07:25 | 显示全部楼层
[C] 纯文本查看 复制代码
#include "HTS221.h"
#include <math.h>
float T0_degC, T1_degC;	//&Icirc;&Acirc;&para;&Egrave;&ETH;&pound;×&frac14;
int16_t T0_out, T1_out;	//&ETH;&pound;×&frac14;&Icirc;&Acirc;&para;&Egrave;&Ouml;&micro;&Ecirc;&auml;&sup3;&ouml;
float H0_rh, H1_rh;	//&Ecirc;&ordf;&para;&Egrave;&ETH;&pound;×&frac14;
int16_t H0_T0_out, H1_T0_out;//&ETH;&pound;×&frac14;&Ecirc;&ordf;&para;&Egrave;&Ouml;&micro;&Ecirc;&auml;&sup3;&ouml;
void HTS221_Init()
{
	uint8_t order[2];
	//&Aring;&auml;&Ouml;&Atilde;&micro;&Iacute;&frac14;&para;±&eth;&frac12;&Oacute;&iquest;&Uacute;
	//1&iexcl;&cent;&acute;ò&iquest;&ordf;HTS221&pound;&not;&frac12;&laquo;PD&Ouml;&Atilde;1±í&Ecirc;&frac34;&iquest;&ordf;&AElig;&ocirc; 2&iexcl;&cent;&Ecirc;&auml;&sup3;&ouml;&Ecirc;&yacute;&frac34;&Yacute;&Euml;&Ugrave;&Acirc;&Ecirc;&Ntilde;&iexcl; 
	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W, HTS221_CTRL_REG1, I2C_MEMADD_SIZE_8BIT,order,1,0xFFFF);
	order[0] |= 0x80;
	
	order[0] &= ~(HTS221_ODR_MASK);
  order[0] |= 1;//00&Ograve;&raquo;&acute;&Icirc;&ETH;&Ocirc;&pound;&not;01 1Hz&pound;&not;10 7Hz&pound;&not;11 12.5hz
	
	HAL_I2C_Mem_Write(&hi2c1,HT_ADDR_W,HTS221_CTRL_REG1, I2C_MEMADD_SIZE_8BIT,order,1,0xFFFF);
}

/**
 * @brief  Exit the shutdown mode for HTS221
 * @retval HUM_TEMP_OK in case of success, an error code otherwise
 */
void HTS221_Power_On(void)
{
  uint8_t order;
  
  /* Read the register content */
	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_CTRL_REG1, I2C_MEMADD_SIZE_8BIT,&order,1,0xFFFF);
  
  /* Set the power down bit */
  order |= HTS221_MODE_ACTIVE;
  
  /* Write register */
	HAL_I2C_Mem_Write(&hi2c1,HT_ADDR_W,HTS221_CTRL_REG1, I2C_MEMADD_SIZE_8BIT,&order,1,0xFFFF);
}

/**
 * @brief  Enter the shutdown mode for HTS221
 * @retval HUM_TEMP_OK in case of success, an error code otherwise
 */
void HTS221_Power_OFF(void)
{
  uint8_t order;
  
  /* Read the register content */
	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_CTRL_REG1, I2C_MEMADD_SIZE_8BIT,&order,1,0xFFFF);
  
  /* Reset the power down bit */
  order &= ~(HTS221_MODE_ACTIVE);
  
  /* Write register */
	HAL_I2C_Mem_Write(&hi2c1,HT_ADDR_W,HTS221_CTRL_REG1, I2C_MEMADD_SIZE_8BIT,&order,1,0xFFFF);
}



/**
 * @brief  HTS221 Calibration procedure
 * @retval HUM_TEMP_OK in case of success, an error code otherwise
 */
void HTS221_Calibration(void)
{
  /* Temperature Calibration */
  /* Temperature in degree for calibration ( "/8" to obtain float) */
  uint16_t T0_degC_x8_L, T0_degC_x8_H, T1_degC_x8_L, T1_degC_x8_H;
  uint8_t H0_rh_x2, H1_rh_x2;
  uint8_t tempReg[2] = {0, 0};

  HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_T0_degC_x8, I2C_MEMADD_SIZE_8BIT,tempReg,1,0xFFFF);
  T0_degC_x8_L = (uint16_t)tempReg[0];
  
	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_T1_T0_msb, I2C_MEMADD_SIZE_8BIT,tempReg,1,0xFFFF);
  
  T0_degC_x8_H = (uint16_t) (tempReg[0] & 0x03);
  T0_degC = ((float)((T0_degC_x8_H << 8) | (T0_degC_x8_L))) / 8;
  
	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_T1_degC_x8, I2C_MEMADD_SIZE_8BIT,tempReg,1,0xFFFF);
  
  T1_degC_x8_L = (uint16_t)tempReg[0];
	
  HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_T1_T0_msb, I2C_MEMADD_SIZE_8BIT,tempReg,1,0xFFFF);

  T1_degC_x8_H = (uint16_t) (tempReg[0] & 0x0C);
  T1_degC_x8_H = T1_degC_x8_H >> 2;
  T1_degC = ((float)((T1_degC_x8_H << 8) | (T1_degC_x8_L))) / 8;
  
	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,(HTS221_T0_OUT_Low_Half | HTS221_I2C_MULTIPLEBYTE_CMD), I2C_MEMADD_SIZE_8BIT,tempReg,2,0xFFFF);
 
  T0_out = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
  
	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,(HTS221_T1_OUT_Low_Half | HTS221_I2C_MULTIPLEBYTE_CMD), I2C_MEMADD_SIZE_8BIT,tempReg,2,0xFFFF);

  T1_out = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
  
  /* Humidity Calibration */
  /* Humidity in degree for calibration ( "/2" to obtain float) */
  HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_H0_rH_x2, I2C_MEMADD_SIZE_8BIT,&H0_rh_x2,1,0xFFFF);
	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_H1_rH_x2 , I2C_MEMADD_SIZE_8BIT,&H1_rh_x2,1,0xFFFF);
	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,(HTS221_H0_T0_OUT_Low_Half | HTS221_I2C_MULTIPLEBYTE_CMD) , I2C_MEMADD_SIZE_8BIT,&tempReg[0],2,0xFFFF);


  H0_T0_out = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
  
  HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,(HTS221_H1_T0_OUT_Low_Half  | HTS221_I2C_MULTIPLEBYTE_CMD) , I2C_MEMADD_SIZE_8BIT,&tempReg[0],2,0xFFFF);

  H1_T0_out = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
  
  H0_rh = ((float)H0_rh_x2) / 2;
  H1_rh = ((float)H1_rh_x2) / 2;
  
}

void HTS221_ReadID(void)
{
	uint8_t tempReg;

	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_WHO_AM_I , I2C_MEMADD_SIZE_8BIT,&tempReg,1,0xFFFF);
}

/**
 * @brief  Read HTS221 output register, and calculate the humidity
 * @param  pfData the pointer to data output
 * @retval HUM_TEMP_OK in case of success, an error code otherwise
 */
uint16_t HTS221_Get_Humidity(void) 
{
	float* pfData;
  int16_t H_T_out, humidity_t;
  uint8_t tempReg[2] = {0, 0};
  uint8_t tmp = 0x00;
  float H_rh;
  
	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_CTRL_REG1, I2C_MEMADD_SIZE_8BIT,&tmp,1,0xFFFF);
  
  /* Output Data Rate selection */
  tmp &= (HTS221_ODR_MASK);
  
  if(tmp == 0x00)
  {
		HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_CTRL_REG2, I2C_MEMADD_SIZE_8BIT,&tmp,1,0xFFFF);

    /* Serial Interface Mode selection */
    tmp &= ~(HTS221_ONE_SHOT_MASK);
    tmp |= HTS221_ONE_SHOT_START;
    
		HAL_I2C_Mem_Write(&hi2c1,HT_ADDR_W,HTS221_CTRL_REG2, I2C_MEMADD_SIZE_8BIT,&tmp,1,0xFFFF);

    
    do
    {
			HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_STATUS_REG, I2C_MEMADD_SIZE_8BIT,&tmp,1,0xFFFF);
    }
    while(!(tmp & 0x02));
  }
  
	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,(HTS221_HUMIDITY_OUT_L| HTS221_I2C_MULTIPLEBYTE_CMD), I2C_MEMADD_SIZE_8BIT,tempReg,2,0xFFFF);


  H_T_out = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
  
  H_rh = ( float )(((( H_T_out - H0_T0_out ) * ( H1_rh - H0_rh )) / ( H1_T0_out - H0_T0_out )) + H0_rh );
  
  // Truncate to specific number of decimal digits
  humidity_t = (uint16_t)(H_rh * pow(10, HUM_DECIMAL_DIGITS));
  *pfData = ((float)humidity_t) / pow(10, HUM_DECIMAL_DIGITS);
  
  // Prevent data going below 0% and above 100% due to linear interpolation
  if ( *pfData <   0.0f ) *pfData =   0.0f;
  if ( *pfData > 100.0f ) *pfData = 100.0f;
  
  return *pfData;
}

int16_t HTS221_Get_Temperature(void)
{
	int16_t pfData;
  int16_t T_out, temperature_t;
  uint8_t tempReg[2] = {0, 0};
  uint8_t tmp = 0x00;
  float T_degC;
  
	HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_CTRL_REG1, I2C_MEMADD_SIZE_8BIT,&tmp,1,0xFFFF);
  /* Output Data Rate selection */
  tmp &= (HTS221_ODR_MASK);
  
  if(tmp == 0x00)
  {
    HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_CTRL_REG2, I2C_MEMADD_SIZE_8BIT,&tmp,1,0xFFFF);
    /* Serial Interface Mode selection */
    tmp &= ~(HTS221_ONE_SHOT_MASK);
    tmp |= HTS221_ONE_SHOT_START;
    HAL_I2C_Mem_Write(&hi2c1,HT_ADDR_W,HTS221_CTRL_REG2, I2C_MEMADD_SIZE_8BIT,&tmp,1,0xFFFF);
    
    do
    {
			HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,HTS221_STATUS_REG, I2C_MEMADD_SIZE_8BIT,&tmp,1,0xFFFF);
    }
    while(!(tmp & 0x01));
  }
  HAL_I2C_Mem_Read(&hi2c1,HT_ADDR_W,(HTS221_TEMP_OUT_L | HTS221_I2C_MULTIPLEBYTE_CMD), I2C_MEMADD_SIZE_8BIT,tempReg,2,0xFFFF);
	
  T_out = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
  
  T_degC = ((float)(T_out - T0_out)) / (T1_out - T0_out) * (T1_degC - T0_degC) + T0_degC;
  
  temperature_t = (int16_t)(T_degC * pow(10, TEMP_DECIMAL_DIGITS));
  
  pfData = ((float)temperature_t) / pow(10, TEMP_DECIMAL_DIGITS);
  
  return pfData;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则




QQ|联系我们|手机版|官方淘宝店|新浪微博|微信公众平台|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2018-5-21 07:07

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表
/* */