聚豐項目 > 基于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();
}
}
}