本帖最后由 lugl4313820 于 2023-5-2 17:09 编辑
目的
驱动hs3003温湿度计
步骤
- 在原来墨水屏的工程基础上添加HS3003的驱动。
-
新建hs3003.c/hs3003.h,并打hs3003.c加入工程。
-
- 我偿试用硬件驱动i2c,但scl即使使用了上位电阻,也不起振,用逻辑分析仪检测如下图:
-
- 周转了半天,还得改回来用软件i2c来驱动。hs3003.c驱动程序如下:
#include "hs3003.h"
#include "software_i2c.h"
#include "log.h"
#define I2CT_FLAG_TIMEOUT ((uint32_t)0x1000)
#define I2CT_LONG_TIMEOUT ((uint32_t)(20 * I2CT_FLAG_TIMEOUT))
#define SI2C_SDA_GPIOx GPIOB
#define SI2C_SCL_GPIOx GPIOB
#define SI2C_SDA_Pin GPIO_PIN_8
#define SI2C_SCL_Pin GPIO_PIN_9
static SI2C_HANDLE SI2C_handle;
void i2c_master_init(){
log_info("n this is a software i2c master demorn");
if(SI2C_Init(&SI2C_handle,SI2C_SDA_GPIOx, SI2C_SCL_GPIOx, SI2C_SDA_Pin, SI2C_SCL_Pin, 5) == false)
{
log_info("software i2c init fail rn");
while(1);
}
}
uint32_t Humiture_HS3003_ReadData_Raw(void )
{
uint8_t rx_buf[4] = {0};
uint16_t humi, temp;
float tmp_f = 0.0;
float humi_f,temp_f;
SI2C_MasterWrite(&SI2C_handle,I2C_SLAVE_ADDR, NULL, 0);
//8bit->1.2ms
//10bit->2.72ms
//12bit->9.10ms
//14bit->33.90ms
Delay_ms(40);
__retry:
SI2C_MasterRead(&SI2C_handle, I2C_SLAVE_ADDR, rx_buf, 4);
// log_info("%x,%x,%x,%xn", rx_buf[0], rx_buf[1], rx_buf[2], rx_buf[3]);
// log_info("state:%xn", rx_buf[0] & RM_HS300X_MASK_STATUS_0XC0);
if ((rx_buf[0] & RM_HS300X_MASK_STATUS_0XC0) != RM_HS300X_DATA_STATUS_VALID)
{
Delay_ms(100);
goto __retry;
}
humi = (rx_buf[0] & RM_HS300X_MASK_HUMIDITY_UPPER_0X3F) << 8 | rx_buf[1];
temp = (rx_buf[2] << 8 | (rx_buf[3] & RM_HS300X_MASK_TEMPERATURE_LOWER_0XFC)) >> 2;
humi_f = (float)humi;
humi_f = (humi_f * RM_HS300X_CALC_HUMD_VALUE_100) / RM_HS300X_CALC_STATIC_VALUE;
temp_f = (float)temp;
temp_f = ((temp_f * RM_HS300X_CALC_TEMP_C_VALUE_165) / RM_HS300X_CALC_STATIC_VALUE) - RM_HS300X_CALC_TEMP_C_VALUE_40;
log_info("Temp:%d.%d ",(int)temp_f,(int)(temp_f*100)%100);
log_info("HUMI:%d.%drn",(int)humi_f,(int)(humi_f*100)%100);
return 1;
}
bool Humiture_HS3003_init(void)
{
i2c_master_init();
}
- hs3003.h:
#ifndef __HS3003_H__
#define __HS3003_H__
#include "n32wb03x.h"
#define I2C_MASTER_ADDR 0x30
#define I2C_SLAVE_ADDR 0x44<<1
#define HS300X_HUMD_MULTY 0.00006163516
#define HS300X_TEMP_MULTY 0.00006163516
/* Definitions of Mask Data for A/D data */
#define RM_HS300X_MASK_HUMIDITY_UPPER_0X3F (0x3F)
#define RM_HS300X_MASK_TEMPERATURE_LOWER_0XFC (0xFC)
#define RM_HS300X_MASK_STATUS_0XC0 (0xC0)
/* Definitions for Status Bits of A/D Data */
#define RM_HS300X_DATA_STATUS_VALID (0x00) // Status-bit: Valid data
/* Definitions for Calculation */
#define RM_HS300X_CALC_STATIC_VALUE (16383.0F)
#define RM_HS300X_CALC_HUMD_VALUE_100 (100.0F)
#define RM_HS300X_CALC_TEMP_C_VALUE_165 (165.0F)
#define RM_HS300X_CALC_TEMP_C_VALUE_40 (40.0F)
#define RM_HS300X_CALC_DECIMAL_VALUE_100 (100.0F)
bool Humiture_HS3003_init(void);
uint32_t Humiture_HS3003_ReadData_Raw(void );
#endif
7、在主程序中调用一下:
#include "main.h"
#include "led.h"
#include "key.h"
#include "log.h"
#include "hs3003.h"
unsigned char BlackImage[4736];//Define canvas space
int main(void)
{
unsigned char fen_L,fen_H,miao_L,miao_H;
log_init();
Humiture_HS3003_init();
Humiture_HS3003_ReadData_Raw();
while (1)
{
Humiture_HS3003_ReadData_Raw();
Delay_ms(1000);
}
}
实验效果
总结
不知道是不是硬件i2c的驱动是不是有问题,反正我的浪费了半天的时间,各种工具用上,最后还是得用软件才搞定。