聚丰项目 > 基于F401的多功能家居辅助系统
在家居环境中,离不开对环境的感知与调节。利用微控器与相应的传感器进行组合可以对环境的相应参数进行检测并加以调节和控制。从简化设计的角度出发,合理地选择开发板和传感器功能板可起到事半功倍的效果。在多功能家居辅助系统设计中,将STM32F401_NUCLEO开发板与X-NUCLEO-IKS01A1功能模块组合在一起就会有这样的效果,它可以对温湿度、大气压及6轴状态进行检测,从而为相应的控制提供数据支持。
jinglixixi
分享jinglixixi
团队成员
李静 实验师
该项目的硬件部分主要由STM32F401_NUCLEO开发板、X-NUCLEO-IKS01A1功能模块、双色0.96’OLED屏、2.4’TFT彩色屏、软按键、MP3语音模块等组成。
其中STM32F401_NUCLEO开发板承担数据处理、X-NUCLEO-IKS01A1功能模块承担环境信号采集、OLED屏用于数据显示;在连接光电隔离继电器的情况下,可进行温湿度等的控制。
结构框图
该项目的软件开发平台采样keil5.0,并由C语音进行编程,所涉及的功能模块有OLED屏显示模块、RTC读取与显示模块、温湿度及大气压的读取与显示模块。
功能框图
OLED屏的初始化函数为:
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
OLED_SCLK_Set();
OLED_SDIN_Set();
Delay_1ms(800);
OLED_WR_Byte(0xAE,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0x10,OLED_CMD);
OLED_WR_Byte(0x40,OLED_CMD);
OLED_WR_Byte(0xB0,OLED_CMD);
OLED_WR_Byte(0x81,OLED_CMD);
OLED_WR_Byte(0xFF,OLED_CMD);
OLED_WR_Byte(0xA1,OLED_CMD);
OLED_WR_Byte(0xA6,OLED_CMD);
OLED_WR_Byte(0xA8,OLED_CMD);
OLED_WR_Byte(0x3F,OLED_CMD);
OLED_WR_Byte(0xC8,OLED_CMD);
OLED_WR_Byte(0xD3,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xD5,OLED_CMD);
OLED_WR_Byte(0x80,OLED_CMD);
OLED_WR_Byte(0xD8,OLED_CMD);
OLED_WR_Byte(0x05,OLED_CMD);
OLED_WR_Byte(0xD9,OLED_CMD);
OLED_WR_Byte(0xF1,OLED_CMD);
OLED_WR_Byte(0xDA,OLED_CMD);
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xDB,OLED_CMD);
OLED_WR_Byte(0x30,OLED_CMD);
OLED_WR_Byte(0x8D,OLED_CMD);
OLED_WR_Byte(0x14,OLED_CMD);
OLED_WR_Byte(0xAF,OLED_CMD);
}
RTC时钟显示函数为:
static void RTC_Handler(TMsg *Msg)
{
uint8_t subSec = 0;
RTC_DateTypeDef sdatestructureget;
RTC_TimeTypeDef stimestructure;
if(DataLoggerActive || AutoInit)
{
HAL_RTC_GetTime(&RtcHandle, &stimestructure, FORMAT_BIN);
HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, FORMAT_BIN);
subSec = ((((((int) RTC_SYNCH_PREDIV) - ((int) stimestructure.SubSeconds)) * 100) / (RTC_SYNCH_PREDIV + 1)) & 0xff);
}
if(DataLoggerActive)
{
Msg->Data[3] = (uint8_t)stimestructure.Hours;
Msg->Data[4] = (uint8_t)stimestructure.Minutes;
Msg->Data[5] = (uint8_t)stimestructure.Seconds;
Msg->Data[6] = subSec;
}
else if(AutoInit)
{
sprintf(dataOut, "Time: %d:%d:%d.%d", stimestructure.Hours,
stimestructure.Minutes, stimestructure.Seconds, subSec);
OLED_ShowString(0,0,dataOut,16);
sprintf(dataOut, "TimeStamp: %d:%d:%d.%d\n", stimestructure.Hours,
stimestructure.Minutes, stimestructure.Seconds, subSec);
HAL_UART_Transmit(&UartHandle, (uint8_t*)dataOut, strlen(dataOut), 5000);
}
}
气压的检测与显示函数为:
static void Pressure_Sensor_Handler(TMsg *Msg)
{
int32_t d1, d2;
if(BSP_PRESSURE_isInitialized())
{
BSP_PRESSURE_GetPressure((float *)&PRESSURE_Value);
floatToInt(PRESSURE_Value, &d1, &d2, 2);
if ( DataLoggerActive )
{
if(Sensors_Enabled & PRESSURE_SENSOR)
{
Serialize(&Msg->Data[7], d1, 2);
Serialize(&Msg->Data[9], d2, 2);
}
}
else if ( AutoInit )
{
sprintf(dataOut, "PRESS: %d.%02d", (int)d1, (int)d2);
HAL_UART_Transmit(&UartHandle, (uint8_t*)dataOut, strlen(dataOut), 5000);
OLED_ShowString(0,2,dataOut,16);
}
}
else
{
if(AutoInit)
{
BSP_PRESSURE_Init();
}
}
}
温湿度的检测与显示函数为:
static void HumTemp_Sensor_Handler(TMsg *Msg)
{
int32_t d1, d2, d3, d4;
if(BSP_HUM_TEMP_isInitialized())
{
BSP_HUM_TEMP_GetHumidity((float *)&HUMIDITY_Value);
BSP_HUM_TEMP_GetTemperature((float *)&TEMPERATURE_Value);
floatToInt(HUMIDITY_Value, &d1, &d2, 2);
floatToInt(TEMPERATURE_Value, &d3, &d4, 2);
if ( DataLoggerActive )
{
if(Sensors_Enabled & TEMPERATURE_SENSOR)
{
Serialize(&Msg->Data[11], d3, 1);
Serialize(&Msg->Data[12], d4, 1);
}
if(Sensors_Enabled & HUMIDITY_SENSOR)
{
Serialize(&Msg->Data[13], d1, 1);
Serialize(&Msg->Data[14], d2, 1);
}
}
else if ( AutoInit )
{
sprintf(dataOut, "HUM: %d.%02d", (int)d1, (int)d2);
OLED_ShowString(0,4,dataOut,16);
sprintf(dataOut, "TEMP: %d.%02d", (int)d3, (int)d4);
OLED_ShowString(0,6,dataOut,16);
sprintf(dataOut, "HUM: %d.%02d TEMP: %d.%02d\n", (int)d1, (int)d2, (int)d3, (int)d4);
HAL_UART_Transmit(&UartHandle, (uint8_t*)dataOut, strlen(dataOut), 5000);
}
}
else
{
if(AutoInit)
{
BSP_HUM_TEMP_Init();
}
}
}