开发平台:KEIL
开发板:STM32H5
目地:本篇是利用STM32H5通过IIC读取AHT10的值,并显示在OLED上。
1.连接方式:
PC2--SCL
PC3--SDA
OLED看上一个连接。
3.核心代码:
#define AHT10_Delay_us(time) HAL_Delay(time) //delay_us(time)
#define AHT10_Delay_ms(time) HAL_Delay(time) //delay_ms(time)
#define AHT10_I2C_SCL_PIN GPIO_PIN_2
#define AHT10_I2C_SCL_GPIO_PORT GPIOC
#define AHT10_I2C_SCL_GPIO_RCC __HAL_RCC_GPIOC_CLK_ENABLE()
#define AHT10_I2C_SDA_PIN GPIO_PIN_3
#define AHT10_I2C_SDA_GPIO_PORT GPIOC
#define AHT10_I2C_SDA_GPIO_RCC __HAL_RCC_GPIOC_CLK_ENABLE()
#define AHT10_SCL_H HAL_GPIO_WritePin(AHT10_I2C_SCL_GPIO_PORT, AHT10_I2C_SCL_PIN, GPIO_PIN_SET)
#define AHT10_SCL_L HAL_GPIO_WritePin(AHT10_I2C_SCL_GPIO_PORT, AHT10_I2C_SCL_PIN, GPIO_PIN_RESET)
#define AHT10_SDA_H HAL_GPIO_WritePin(AHT10_I2C_SDA_GPIO_PORT, AHT10_I2C_SDA_PIN, GPIO_PIN_SET)
#define AHT10_SDA_L HAL_GPIO_WritePin(AHT10_I2C_SDA_GPIO_PORT, AHT10_I2C_SDA_PIN, GPIO_PIN_RESET)
#define AHT10_READ_SDA HAL_GPIO_ReadPin(AHT10_I2C_SDA_GPIO_PORT,AHT10_I2C_SDA_PIN)
初始化:
AHT10_I2C_SCL_GPIO_RCC;
AHT10_I2C_SDA_GPIO_RCC;//??GPIO??
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = AHT10_I2C_SCL_PIN;
HAL_GPIO_Init(AHT10_I2C_SCL_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = AHT10_I2C_SDA_PIN;
HAL_GPIO_Init(AHT10_I2C_SDA_GPIO_PORT, &GPIO_InitStruct);
AHT10_SDA_H;
AHT10_SCL_H;
AHT10_IIC_Init();
AHT10_IIC_Start();
AHT10_IIC_Send_Byte(AHT10_ADDRESS);
AHT10_IIC_Send_Byte(0xe1);
AHT10_IIC_Send_Byte(0x08);
AHT10_IIC_Send_Byte(0x00);
AHT10_IIC_Stop();
AHT10_Delay_ms(40);//??20ms??????
ret=AHT10ReadData(&temperature,&humidity);
if(ret==0)
{
GUI_ShowString(0,0,(unsigned char*)"recv temp fail!",8,1);
}
else
{
float temp=temperature*0.1;
float hum=humidity*0.1;
//printf("temp:%2.1f,sd:%2.1f\r\n",temp,hum);
char print_temp[64];
char print_hum[64];
sprintf(print_temp,"temp:%2.1f du",temp);
sprintf(print_hum,"hum:%2.1f%%",hum);
GUI_ShowString(0,0,(unsigned char*)print_temp,16,1);
GUI_ShowString(0,2,(unsigned char*)print_hum,16,1);
GUI_ShowString(0,4,(unsigned char*)"hello word!",16,1);
}
4、视频
引用: 火辣西米秀 发表于 2023-6-23 20:44 用STM32H5通过IIC读取AHT10的值,并显示在OLED上,这个好好跟着楼主学一下哈
我也是摸了好几天才玩通的
引用: freebsder 发表于 2023-6-24 22:34 谢谢分享,期待后续!
在进行中呢