【NUCLEO-WB09KE测评】三、IIC获取温度
<div class='showpostmsg'> 本帖最后由 dvacos 于 2024-9-26 16:29 编辑<div><span style="font-size:18px;"><strong>一、引脚确认</strong></span></div>
<div></div>
<div></div>
<div> </div>
<div><span style="font-size:18px;"><strong>二、Cube Mx配置</strong></span></div>
<div>选定IIC IO</div>
<div></div>
<div>使能后直接使用默认配置即可(传感器的IIC速率是100-400K均可)</div>
<div></div>
<div> </div>
<div><span style="font-size:18px;"><strong>三、 初步验证</strong></span></div>
<div>生成代码后IIC已经初始化完毕,所以添加代码测试即可。</div>
<div>添加以下代码</div>
<div></div>
<div>不接传感器,严重IIC波形可以正常发送</div>
<div></div>
<div>接上传感器后,整个发送数据,并且每次都有ACK</div>
<div></div>
<div>添加读代码</div>
<div></div>
<div>如图,地址会自动加一,传感器已经有ACK了</div>
<div></div>
<div>以上读写驱动验证完毕</div>
<div> </div>
<div><span style="font-size:18px;"><strong>四、 传感器简介</strong></span></div>
<div>AHT30是一款高性能的温湿度传感器,由广州奥松电子股份有限公司生产。它具有以下特点:</div>
<div>低功耗:设计用于低功耗运行,有助于延长设备的电池寿命。</div>
<div>高精度:能够提供准确的温度和湿度数据。</div>
<div>数字输出:通过I²C接口进行数字信号输出,标准I²C格式。</div>
<div>宽电压支持:工作电压范围为2.2-5.5VDC。</div>
<div>优异的长期稳定性:即使在恶劣的环境下也能保持稳定性能。</div>
<div>响应迅速:能够快速响应环境中温湿度的变化。</div>
<div>抗干扰能力强:采用SMD封装适于回流焊,具有良好的抗干扰性能。</div>
<div>AHT30传感器广泛应用于智能家居、消费电子、医疗、汽车、工业、气象等领域,例如暖通空调、除湿器和冰箱等家电产品,测试和检测设备及其他相关温湿度检测控制产品。</div>
<div><a href="mailto:技术规格方面,AHT30的供电电压为DC2.2-5.5V,测量范围是温度-40至120℃、湿度0-100%RH,典型精度为温度±0.5℃、湿度±3%RH(@25℃),分辨率为温度0.01℃、湿度0.024%RH。">技术规格方面,AHT30的供电电压为DC2.2-5.5V,测量范围是温度-40至120℃、湿度0-100%RH,典型精度为温度±0.5℃、湿度±3%RH(25℃时),分辨率为温度0.01℃、湿度0.024%RH。</a></div>
<div> </div>
<div><span style="font-size:18px;"><strong>五 、驱动传感器</strong></span></div>
<div>计算出来的温度出错</div>
<div></div>
<div>C语言实验后,温度正确,修改代码</div>
<div></div>
<div>修改代码后,读到正确温度!!</div>
<div></div>
<div> </div>
<div>代码</div>
<div>
<pre>
<code>#define aht30_addr 0x38<<1
void write_aht30(uint8_t *data_buf,uint8_t len){
HAL_I2C_Master_Transmit(&hi2c1,aht30_addr,data_buf,len,100);
}
void read_aht30(uint8_t *data_buf,uint8_t len){
HAL_I2C_Master_Receive(&hi2c1,aht30_addr,data_buf,len,100);
}
void aht30_get_data(void){
uint8_t read_back_data={0x0};
read_aht30(read_back_data,sizeof(read_back_data));
LOG("\r");
for(int i=0;i<8;i++){
LOG(",0x%02x",read_back_data);
}
LOG("\r");
double temp=0;
unsigned long data32;
data32 =((int32_t)(read_back_data&0x0F) << 16) | ((int32_t)read_back_data << 8) | ((int32_t)read_back_data);
temp=(data32*200)/1048576-50;
LOG("temp %X%.3f",data32,temp);
}
void aht30_init(void){
uint8_t init_data[]={0xAC,0x33,0x00};
write_aht30(&init_data,3);
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* Configure the peripherals common clocks */
PeriphCommonClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
uint8_t str[] = "Hello eeworld! Hello NUCLEO-WB09KE!\r\n";
HAL_UART_Transmit( &huart1, ( uint8_t * )str, sizeof( str ) - 1, 100 );
//uint8_t init_data[]={0xAC,0x33,0x00};
//HAL_I2C_Master_Transmit(&hi2c1,aht30_addr,init_data,3,100);
//HAL_Delay(150);
//
//uint8_t read_back_data={0x0};
//HAL_I2C_Master_Receive(&hi2c1,aht30_addr,read_back_data,8,100);
aht30_init();
HAL_Delay(150);
aht30_get_data();
}
</code></pre>
<p> </p>
</div>
<div> </div>
<div>开发板+传感器</div>
<div></div>
<p><!--importdoc--></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>计算出来的温度出错后面修改就完全读出正确的,看来是已经掌握掌握了</p>
页:
[1]