STM32F0单片机基于Hal库温控智能风扇

一、项目概述
设计采用STM32F0系列单片机做主控芯片,通过DHT11采集温湿度,将温度显示在OLED 屏幕上。根据温度的不同,利用STM32对风扇进行调速,总体硬件设计如下图所示

STM32F0单片机基于Hal库温控智能风扇插图

1.效果展示
STM32F0单片机基于Hal库温控智能风扇插图1

2.主要功能
传感器检测外界温度和湿度并在OLED 屏幕上实时显示出来,当传感器检测到外界温度超过36摄氏度时,单片机便会控制风扇打开。

二、硬件部分
STM32F0最小系统(集成了OLED屏幕插座)、DHT11温湿度模块、带风扇的电机驱动模块
开发环境
keil5、STM32CubeMX、STM32CubeProgrammer

3.接线
DHT11
vcc---3.3v
DAT---PA0
GND---GND

风扇
G---GND
V---3V3
S---PA3

三、软件部分

  • 主要代码
    1.OLED显示界面
    将要显示的字符通过取模软件取模,将生成数据放入oledfont.h中的数组char Hzk[][32]里

2.DHT11温湿度传感器

#include "bsp_DHT11.h"

static void DHT11_Mode_IPU(void);
static void DHT11_Mode_Out_PP(void);
static uint8_t DHT11_ReadByte(void);
unsigned char   ple[]="0123456789";
extern DHT11_Data_TypeDef DHT11_Data;
#define Bit_RESET 0
#define Bit_SET   1
/* 函数体 */
/**
  * 函数功能: 
  * 输入参数: 无
  * 返 回 值: 无
  * 说    明:无
  */

void delay_us(unsigned long i)
{
  unsigned long j;
  for(i;i>0;i--)
  {
     for(j=1;j>0;j--);
  }
}
static void DHT11_Delay(uint16_t time)
{
    uint8_t i;

  while(time)
  {    
    for (i = 0; i humi_high8bit= DHT11_ReadByte();
    DHT11_Data->humi_low8bit = DHT11_ReadByte();
    DHT11_Data->temp_high8bit= DHT11_ReadByte();
    DHT11_Data->temp_low8bit = DHT11_ReadByte();
    DHT11_Data->check_sum    = DHT11_ReadByte();

    /*读取结束,引脚改为输出模式*/
    DHT11_Mode_Out_PP();
    /*主机拉高*/
    DHT11_Dout_HIGH();

    /* 对数据进行处理 */
    humi_temp=DHT11_Data->humi_high8bit*100+DHT11_Data->humi_low8bit;
    DHT11_Data->humidity =(float)humi_temp/100;

    humi_temp=DHT11_Data->temp_high8bit*100+DHT11_Data->temp_low8bit;
    DHT11_Data->temperature=(float)humi_temp/100;    

    /*检查读取的数据是否正确*/
    temp = DHT11_Data->humi_high8bit + DHT11_Data->humi_low8bit + 
    DHT11_Data->temp_high8bit+ DHT11_Data->temp_low8bit;
    if(DHT11_Data->check_sum==temp)
    { 
      return SUCCESS;
    }
    else 
      return ERROR;
    }   
    else
        return ERROR;
}
  • 主函数
#include "main.h"
#include "gpio.h"
#include "bsp_DHT11.h"

DHT11_Data_TypeDef DHT11_Data;

void SystemClock_Config(void);
int16_t Data;
uint32_t TimeCounter;

int main(void)
{

  uint8_t Temperature; //温度
  uint8_t Humidity; //湿度

  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();

  OLED_Init();//初始化OLED屏幕的一些配置  
  OLED_Clear();//控制屏幕内容清除一次 
  DHT11_Init();//初始化传感器的一些配置

  while(1)
  {

      DHT11_Read_TempAndHumidity(&DHT11_Data);
      Temperature = DHT11_Data.temperature;     //实际温度
      Humidity = DHT11_Data.humidity;        //实际湿度

      /*****显示智能家居*******/
     OLED_ShowCHinese(18,0,0);
     OLED_ShowCHinese(36,0,1);
     OLED_ShowCHinese(54,0,2);
     OLED_ShowCHinese(72,0,3);

     OLED_ShowNum(35,6,Temperature,3,16);
     OLED_ShowString(0,6,"Tem: ");
     OLED_ShowCHinese(60,6,7);

     OLED_ShowNum(35,3,Humidity,3,16);
     OLED_ShowString(0,3,"Hum: ");
     OLED_ShowString(60 ,3,"%");

      if(Temperature >= 36)
     {
    //开启风扇
        HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_SET);

    HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_4);
    HAL_Delay(500);
    //OLED_Clear();//清楚屏幕上的内容,实现闪屏效果
      }
    else
    {
      //关闭风扇
      HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET);
    }

    if(Humidity >= 70)
    {

     //led闪烁
     HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_4);
         HAL_Delay(500);//延时500ms,达到闪烁状态

    //OLED_Clear();//清楚屏幕上的内容,实现闪屏效果
    }
    else
    {
       OLED_ShowNum(35,3,Humidity,3,16);
    }               
  }

}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(char *file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %drn", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  • 代码烧录
    通过STM32CubeProgrammer烧录代码,上电前,设置Boot0=1,Boot1=0,下载完上电后按一下复位键。
    STM32F0单片机基于Hal库温控智能风扇插图2

文章来源于互联网:STM32F0单片机基于Hal库温控智能风扇

THE END
分享
二维码