【国民技术N32WB031_STB开发板评测】驱动HS3003
<div class='showpostmsg'> 本帖最后由 lugl4313820 于 2023-5-2 17:09 编辑<div class="parsedown-markdown">
<h1>目的</h1>
<p>驱动hs3003温湿度计</p>
<h1>步骤</h1>
<ol>
<li>在原来墨水屏的工程基础上添加HS3003的驱动。</li>
<li>
<p>新建hs3003.c/hs3003.h,并打hs3003.c加入工程。</p>
</li>
<li>
<p></p>
</li>
<li>我偿试用硬件驱动i2c,但scl即使使用了上位电阻,也不起振,用逻辑分析仪检测如下图:</li>
<li>
<p></p>
</li>
<li>周转了半天,还得改回来用软件i2c来驱动。hs3003.c驱动程序如下:</li>
</ol>
<pre>
<code>
#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_GPIOxGPIOB
#define SI2C_SCL_GPIOxGPIOB
#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 = {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, rx_buf, rx_buf, rx_buf);
// log_info("state:%xn", rx_buf & RM_HS300X_MASK_STATUS_0XC0);
if ((rx_buf & RM_HS300X_MASK_STATUS_0XC0) != RM_HS300X_DATA_STATUS_VALID)
{
Delay_ms(100);
goto __retry;
}
humi = (rx_buf & RM_HS300X_MASK_HUMIDITY_UPPER_0X3F) << 8 | rx_buf;
temp = (rx_buf << 8 | (rx_buf & 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();
}
</code></pre>
<ol start="6">
<li>hs3003.h:</li>
</ol>
<pre>
<code>#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
</code></pre>
<p>7、在主程序中调用一下:</p>
<pre>
<code>#include "main.h"
#include "led.h"
#include "key.h"
#include "log.h"
#include "hs3003.h"
unsigned char BlackImage;//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);
}
}</code></pre>
<h1>实验效果</h1>
<p></p>
<h1>总结</h1>
<p>不知道是不是硬件i2c的驱动是不是有问题,反正我的浪费了半天的时间,各种工具用上,最后还是得用软件才搞定。</p>
</div>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>N32WB031_STB开发板哪里有</p>
<p>国民技术的资料好像不多</p>
Jacktang 发表于 2023-5-2 21:49
N32WB031_STB开发板哪里有
国民技术的资料好像不多
<p>论坛里评测申请到的。国民技术官网也有卖,不过价格不便宜呀。</p>
<p>写的不错,学习了。加油使自己更优秀!!!</p>
页:
[1]