找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 168|回复: 1
收起左侧

(大赛作品)STM32F072RB NUCLEO智能家居控制

[复制链接]
ID:1117093 发表于 2024-4-20 23:12 | 显示全部楼层 |阅读模式
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f0xx_hal.h"
  3. #include "adc.h"
  4. #include "dma.h"
  5. #include "rtc.h"
  6. #include "spi.h"
  7. #include "tim.h"
  8. #include "usart.h"
  9. #include "gpio.h"

  10. /* USER CODE BEGIN Includes */

  11. #include "lcd.h"
  12. #include "stdio.h"
  13. #include "calendar.h"
  14. #include "misc.h"
  15. #include "ds18b20.h"



  16. /* USER CODE END Includes */

  17. /* Private variables ---------------------------------------------------------*/

  18. /* USER CODE BEGIN PV */

  19. RTC_DateTypeDef  sdatestructure;
  20. RTC_TimeTypeDef  stimestructure;
  21. float tempreture;
  22. float humidity;

  23. volatile uint32_t ADC_Value;


  24. char sTime[20];
  25. char sDate[20];
  26. char lunar[20];
  27. char jieqi[20];
  28. char temp[20];
  29. char humi[20];
  30. char adc[20];


  31. const unsigned char gImage_ST[5760] = { /* 0X00,0X10,0X40,0X00,0X2D,0X00,0X01,0X1B, */
  32. 0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,0XFF,0XFE,0XFF,0XDF,0XFF,0XFF,0XFF,

  33. 0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
  34. };






  35. /* USER CODE END PV */

  36. /* Private function prototypes -----------------------------------------------*/
  37. void SystemClock_Config(void);

  38. /* USER CODE BEGIN PFP */
  39. void RTC_AlarmConfig(uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second);


  40. /* USER CODE END PFP */

  41. /* USER CODE BEGIN 0 */

  42. /* USER CODE END 0 */

  43. int main(void)
  44. {

  45.   /* USER CODE BEGIN 1 */

  46.   /* USER CODE END 1 */

  47.   /* MCU Configuration----------------------------------------------------------*/

  48.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  49.   HAL_Init();

  50.   /* Configure the system clock */
  51.   SystemClock_Config();

  52.   /* Initialize all configured peripherals */
  53.   MX_GPIO_Init();
  54.   MX_DMA_Init();
  55.   MX_ADC_Init();
  56.   //MX_RTC_Init();
  57.   MX_SPI1_Init();
  58.   MX_TIM1_Init();
  59.   MX_TIM2_Init();
  60.   MX_USART2_UART_Init();

  61.   /* USER CODE BEGIN 2 */
  62.         RTC_AlarmConfig(0x15, 0x01, 0x27, 0x10, 0x57, 0x00);
  63.        
  64.         HAL_TIM_Base_Start(&htim2);
  65.        
  66.         HAL_ADCEx_Calibration_Start(&hadc);
  67.        
  68.         HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
  69.        
  70.        
  71.         LCD_Init();
  72.        
  73.         LCD_Draw_String(100, 3, "HomeKits Demo", RED);
  74.         LCD_Draw_String(80, 208, "Powered by", RED);
  75.         LCD_Draw_Image(170, 192, 64, 45, (uint8_t *)(gImage_ST));
  76.        
  77. //        uint8_t r = 0;
  78. //        r = DS18B20_Check();
  79. //       
  80. //        if(r == 0)
  81. //                LCD_Draw_String(10, 110, "DS18B20 exist", RED);
  82. //        else
  83. //                LCD_Draw_String(10, 110, "DS18B20 not exist", RED);


  84.         HAL_ADC_Start_DMA(&hadc, (uint32_t *)(&ADC_Value), 1);
  85.         LCD_Draw_String(10, 110, temp, RED);
  86.        
  87.         //LCD_Set_Dir(1);
  88.        
  89. //        LCD_Clear(BLUE);
  90.        
  91.         //LCD_Draw_String(10, 100, "NUCLEO DMA刷屏测试", RED);
  92.        
  93.         //LCD_Set_Window(10, 100, 319, 239);
  94.        
  95.         //LCD_DMA_Test();

  96.   /* USER CODE END 2 */

  97.   /* USER CODE BEGIN 3 */
  98.   /* Infinite loop */
  99.   while (1)
  100.   {
  101.                
  102.                 HAL_RTC_GetTime(&hrtc, &stimestructure, FORMAT_BIN);
  103.                 //sprintf(sTime, "%02d:%02d:%02d", stimestructure.Hours, stimestructure.Minutes, stimestructure.Seconds);
  104.                
  105.                 HAL_RTC_GetDate(&hrtc, &sdatestructure, FORMAT_BIN);
  106.                
  107.                 sprintf(sDate, "20%02d-%02d-%02d  %02d:%02d:%02d", sdatestructure.Year, sdatestructure.Month, sdatestructure.Date, stimestructure.Hours, stimestructure.Minutes, stimestructure.Seconds);
  108.                
  109.                 LCD_Draw_String(10, 30, sDate, RED);
  110.                
  111.                 GetChinaCalendarStr(sdatestructure.Year + 2000, sdatestructure.Month, sdatestructure.Date, (uint8_t *)lunar);
  112.                
  113.                 LCD_Draw_String(10, 50, lunar, RED);
  114.                
  115.                 if(GetJieQiStr(sdatestructure.Year + 2000, sdatestructure.Month, sdatestructure.Date, (uint8_t *)jieqi))
  116.                 {
  117.                         LCD_Draw_String(10, 70, jieqi, RED);
  118.                 }
  119.                
  120.                 tempreture = DS18B20_GetTemp();
  121.                 sprintf(temp, "温度:%5.1f℃", tempreture);
  122.                 LCD_Draw_String(10, 90, temp, RED);
  123.                
  124.                 sprintf(adc, "ADC Vol:%5.1fV", ADC_Value * 3.3 / 4096);
  125.                 LCD_Draw_String(10, 110, adc, RED);
  126.                
  127.                 TIM1->CCR2 = (uint16_t)(ADC_Value / 4096.0 * 1000);
  128.                
  129.                 if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8) == GPIO_PIN_SET)
  130.                 {
  131.                         LCD_Draw_String(10, 130, "有人活动", RED);
  132.                 }
  133.                 else
  134.                 {
  135.                         LCD_Draw_String(10, 130, "无人活动", RED);
  136.                 }
  137. //                HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
  138. //                delay(50000);

  139.   }
  140.   /* USER CODE END 3 */

  141. }

  142. /** System Clock Configuration
  143. */
  144. void SystemClock_Config(void)
  145. {

  146.   RCC_OscInitTypeDef RCC_OscInitStruct;
  147.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  148.   RCC_PeriphCLKInitTypeDef PeriphClkInit;

  149.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
  150.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  151.   RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  152.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  153.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  154.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  155.   RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV2;
  156.   HAL_RCC_OscConfig(&RCC_OscInitStruct);

  157.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  158.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  159.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  160.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  161.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);

  162.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_RTC;
  163.   PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  164.   PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  165.   HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);

  166.   __SYSCFG_CLK_ENABLE();

  167. }

  168. /* USER CODE BEGIN 4 */

  169. void RTC_AlarmConfig(uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
  170. {
  171.         RTC_TimeTypeDef sTime;
  172.   RTC_DateTypeDef sDate;
  173.   RTC_AlarmTypeDef sAlarm;

  174.     /**Initialize RTC and set the Time and Date
  175.     */
  176.   hrtc.Instance = RTC;
  177.   hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  178.   hrtc.Init.AsynchPrediv = 127;
  179.   hrtc.Init.SynchPrediv = 255;
  180.   hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  181.   hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  182.   hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  183.   HAL_RTC_Init(&hrtc);

  184.         if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x1236)
  185.         {
  186.                 sTime.Hours = hour;
  187.                 sTime.Minutes = minute;
  188.                 sTime.Seconds = second;
  189.                 sTime.SubSeconds = 0;
  190.                 sTime.TimeFormat = RTC_HOURFORMAT12_AM;
  191.                 sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  192.                 sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  193.                 HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BCD);

  194.                 sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  195.                 sDate.Month = month;
  196.                 sDate.Date = day;
  197.                 sDate.Year = year;
  198.                 HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BCD);
  199.         }

  200.     /**Enable the Alarm A
  201.     */
  202.   sAlarm.AlarmTime.Hours = 0;
  203.   sAlarm.AlarmTime.Minutes = 0;
  204.   sAlarm.AlarmTime.Seconds = 0;
  205.   sAlarm.AlarmTime.SubSeconds = 0;
  206.   sAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
  207.   sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  208.   sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
  209.   sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
  210.   sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
  211.   sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
  212.   sAlarm.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY;
  213.   sAlarm.Alarm = RTC_ALARM_A;
  214.   HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, FORMAT_BCD);

  215.     /**Enable the WakeUp
  216.     */
  217.   HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
  218.        
  219.         HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x1236);
  220. }


  221. /* USER CODE END 4 */

  222. #ifdef USE_FULL_ASSERT

  223. /**
  224.    * @brief Reports the name of the source file and the source line number
  225.    * where the assert_param error has occurred.
  226.    * @param file: pointer to the source file name
  227.    * @param line: assert_param error line source number
  228.    * @retval None
  229.    */
  230. void assert_failed(uint8_t* file, uint32_t line)
  231. {
  232.   /* USER CODE BEGIN 6 */
  233.   /* User can add his own implementation to report the file name and line number,
  234.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  235.   /* USER CODE END 6 */

  236. }

  237. #endif

复制代码

原理图: 无
仿真: 无
代码: (大赛作品)STM32F072RB NUCLEO智能家居控制.7z (2.37 MB, 下载次数: 13)
回复

使用道具 举报

ID:276663 发表于 2024-4-22 15:54 | 显示全部楼层
都能控制哪些东西啊?都能用哪些控制方式?手机?遥控?语音?自动控制?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表