GXHT-30温湿度传感器是一个国产的温湿度传感器,使用I2C方式通讯。 典型应用电路: 由测量结果数据计算相对湿度转换公式(%RH):
我这里实际使用的是GXHT-30的模块,提供VDD、GND、SDA、SCL接口。
开发板提供的GPIO中,我使用GPIO口模拟方式,连接GXHT-30温湿度传感器,其中PC9作为SCL使用,PC7作为SDA使用。 主程序在初始化完各关联设备后,每间隔1秒钟,温湿度启动传感器一次,并读取结果显示到TFT显示屏上。 int main(void) {
float temperature; // temperature [°C]
float humidity; // relative humidity [%RH]
char val[32]={'\0'};
etError error; // error code
system_clock_config();
at32_board_init();
uart_print_init(115200);
printf("Start main ...\r\n");
// 初始化TFT显示屏
Lcd_Init();
// 测试
Lcd_Clear(BLACK);
Gui_DrawFont_GBK16(8,10,WHITE, BLACK, (unsigned char *)"Artery & 21IC");
Gui_DrawFont_GBK16(30,36,WHITE, BLACK, (unsigned char *)"AT32F405");
// 初始化水位传感器、抽水马达控制口
at32_led_on(LED2);
mymac_Init();
Gui_DrawFont_GBK16(0, 60,WHITE, BLACK, (unsigned char *)"初始化:");
Gui_DrawFont_GBK16(10, 76,WHITE, BLACK, (unsigned char *)"水位控制接口");
at32_led_off(LED2);
at32_led_on(LED3);
// 初始化温湿度传感器GXHT-30
Gui_DrawFont_GBK16(10, 92,WHITE, BLACK, (unsigned char *)"传感器接口");
gxht30_Init(GXHT30_ADDRESS);
at32_led_off(LED3);
Gui_DrawFont_GBK16(0, 110, WHITE, BLACK, (unsigned char *)"温度");
Gui_DrawFont_GBK16(32,110, WHITE, BLACK, (unsigned char *)"=");
Gui_DrawFont_GBK16(0, 126,WHITE, BLACK, (unsigned char *)"湿度");
Gui_DrawFont_GBK16(32,126,WHITE, BLACK, (unsigned char *)"=");
while(1) {
// Gui_DrawFont_GBK16(58,84, WHITE, BLACK, (unsigned char *)" ");
// Gui_DrawFont_GBK16(58,100, WHITE, BLACK, (unsigned char *)" ");
//at32_led_toggle(LED2);
//delay_ms(200);
//at32_led_toggle(LED3);
//delay_ms(200);
//at32_led_toggle(LED4);
//delay_ms(200);
// error = gxht30_GetTempAndHumi(&temperature, &humidity, REPEATAB_HIGH, MODE_CLKSTRETCH, 50);
// sprintf(val, "Result=0x%02x", error);
// Gui_DrawFont_GBK16(0,142,WHITE, BLACK, (unsigned char *)val);
SHT30_read_result(GXHT30_ADDRESS, &temperature, &humidity);
sprintf(val, "%5.1f", temperature);
Gui_DrawFont_GBK16(48,110,WHITE, BLACK, (unsigned char *)val);
sprintf(val, "%5.1f", humidity);
Gui_DrawFont_GBK16(48,126, WHITE, BLACK, (unsigned char *)val);
delay_ms(1000);
}
}
目前的处理结果不对,应该卡在了传感器的读写时序上,正在检查中。
|