菜单路径设计:
tools->HUMI->显示温湿度值
1、移植sht30的驱动。我这里选用I2C1来显示,查找数据手册I2C1可以选用PC0(SCL)、PC1(SDA)复用配置为AF4。初始化代码如下:
- void sht3x_i2c_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- I2C_InitTypeDef I2C_InitStruct;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
- I2C_DeInit(I2C1);
- I2C_StructInit(&I2C_InitStruct);
- I2C_InitStruct.I2C_Mode = I2C_MODE_MASTER;
- I2C_InitStruct.I2C_OwnAddress = I2C_OWN_ADDRESS;
- I2C_InitStruct.I2C_ClockSpeed = 100000;
- I2C_Init(I2C1, &I2C_InitStruct);
- I2C_TargetAddressConfig(I2C1, 0x88);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource0, GPIO_AF_4);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_4);
- GPIO_StructInit(&GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;
- GPIO_Init(GPIOC, &GPIO_InitStruct);
- I2C_Cmd(I2C1, ENABLE);
-
- }
- /**
- * @brief 向SHT30发送一条指令(16bit)
- * @param cmd —— SHT30指令(在SHT30_MODE中枚举定义)
- * @retval 成功返回HAL_OK
- */
- static uint8_t SHT30_Send_Cmd(SHT30_CMD cmd)
- {
- uint8_t i;
- uint8_t cmd_buffer[2];
- cmd_buffer[0] = cmd >> 8;
- cmd_buffer[1] = cmd;
- for(i=0;i<2;i++)
- {
- I2C_SendData(I2C1, cmd_buffer[i]);
- while (RESET == I2C_GetFlagStatus(I2C1, I2C_STATUS_FLAG_TFE))
- {
- }
- }
- I2C_GenerateSTOP(I2C1, ENABLE);
- while (RESET == I2C_GetFlagStatus(I2C1, I2C_STATUS_FLAG_TFE))
- {
- }
- return 0;
- }
- /**
- * @brief 从SHT30读取一次数据
- * @param dat —— 存储读取数据的地址(6个字节数组)
- * @retval 成功 —— 返回HAL_OK
- */
- uint8_t SHT30_Read_Dat(uint8_t* dat)
- {
- uint8_t i;
- SHT30_Send_Cmd(READOUT_FOR_PERIODIC_MODE);
- for (i = 0; i < 6; i++)
- {
- I2C_ReadCmd(I2C1);
-
- while (RESET == I2C_GetFlagStatus(I2C1, I2C_STATUS_FLAG_RFNE))
- {
- }
- dat[i] = I2C_ReceiveData(I2C1);
-
- }
- I2C_GenerateSTOP(I2C1, ENABLE);
- while (RESET == I2C_GetFlagStatus(I2C1, I2C_STATUS_FLAG_TFE))
- {
- }
- return 0;
- }
- void tools_option(void)
- {
- struct option_class option[] = {
- {"<<<"},
- {"HUMI", temp_sht30}, //温湿度计
- {"IC", }, //6-6 输入捕获模式测频率
- {"PWM_Out", }, //6-3 PWM驱动LED呼吸灯
- {"PWM_In", }, //6-7 PWMI模式测频率占空比
- {"Encoder", }, //6-8 编码器接口测速
- {"ADC", }, //8-2 DMA+AD多通道
- {"Serial_SR_HEX", }, //9-3 串口收发HEX数据包
- {"Serial_SR_UTF-8", }, //9-4 串口收发文本数据包
- {"SPI_RW_W25Q64", }, //11-2 硬件SPI读写W25Q64
- {"Oscillograph", }, //示波器
- {".."}
- };
-
- run_play_option_class(option);
- }
- void temp_sht30(void)
- {
- uint8_t sht30_dat[6];
- char show_str[32];
- float temperature=15.0, humidity=80.1;
- sht3x_i2c_init();
- SHT30_Init();
- tos_task_delay(1000);
- while(1)
- {
- OLED_Clear();
- if(Key_Enter_Get()) {return;} //如果检测到按键,则返回菜单
- SHT30_Init();
- tos_task_delay(1000);
- SHT30_Read_Dat(sht30_dat);
- SHT30_Dat_To_Float(sht30_dat, &temperature, &humidity);
- sprintf(show_str,"T:%2.1f H:%.2f",temperature,humidity);
- OLED_ShowString(0, 0, show_str, OLED_8X16);
- OLED_Update();
- tos_task_delay(1000);
- }
- }