标题:
(大赛作品)STM32F072RB NUCLEO智能家居控制
[打印本页]
作者:
11111111184684
时间:
2024-4-20 23:12
标题:
(大赛作品)STM32F072RB NUCLEO智能家居控制
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"
#include "adc.h"
#include "dma.h"
#include "rtc.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include "stdio.h"
#include "calendar.h"
#include "misc.h"
#include "ds18b20.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
RTC_DateTypeDef sdatestructure;
RTC_TimeTypeDef stimestructure;
float tempreture;
float humidity;
volatile uint32_t ADC_Value;
char sTime[20];
char sDate[20];
char lunar[20];
char jieqi[20];
char temp[20];
char humi[20];
char adc[20];
const unsigned char gImage_ST[5760] = { /* 0X00,0X10,0X40,0X00,0X2D,0X00,0X01,0X1B, */
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,0XFF,0XFE,0XFF,0XDF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
};
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void RTC_AlarmConfig(uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second);
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC_Init();
//MX_RTC_Init();
MX_SPI1_Init();
MX_TIM1_Init();
MX_TIM2_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
RTC_AlarmConfig(0x15, 0x01, 0x27, 0x10, 0x57, 0x00);
HAL_TIM_Base_Start(&htim2);
HAL_ADCEx_Calibration_Start(&hadc);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
LCD_Init();
LCD_Draw_String(100, 3, "HomeKits Demo", RED);
LCD_Draw_String(80, 208, "Powered by", RED);
LCD_Draw_Image(170, 192, 64, 45, (uint8_t *)(gImage_ST));
// uint8_t r = 0;
// r = DS18B20_Check();
//
// if(r == 0)
// LCD_Draw_String(10, 110, "DS18B20 exist", RED);
// else
// LCD_Draw_String(10, 110, "DS18B20 not exist", RED);
HAL_ADC_Start_DMA(&hadc, (uint32_t *)(&ADC_Value), 1);
LCD_Draw_String(10, 110, temp, RED);
//LCD_Set_Dir(1);
// LCD_Clear(BLUE);
//LCD_Draw_String(10, 100, "NUCLEO DMA刷屏测试", RED);
//LCD_Set_Window(10, 100, 319, 239);
//LCD_DMA_Test();
/* USER CODE END 2 */
/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
HAL_RTC_GetTime(&hrtc, &stimestructure, FORMAT_BIN);
//sprintf(sTime, "%02d:%02d:%02d", stimestructure.Hours, stimestructure.Minutes, stimestructure.Seconds);
HAL_RTC_GetDate(&hrtc, &sdatestructure, FORMAT_BIN);
sprintf(sDate, "20%02d-%02d-%02d %02d:%02d:%02d", sdatestructure.Year, sdatestructure.Month, sdatestructure.Date, stimestructure.Hours, stimestructure.Minutes, stimestructure.Seconds);
LCD_Draw_String(10, 30, sDate, RED);
GetChinaCalendarStr(sdatestructure.Year + 2000, sdatestructure.Month, sdatestructure.Date, (uint8_t *)lunar);
LCD_Draw_String(10, 50, lunar, RED);
if(GetJieQiStr(sdatestructure.Year + 2000, sdatestructure.Month, sdatestructure.Date, (uint8_t *)jieqi))
{
LCD_Draw_String(10, 70, jieqi, RED);
}
tempreture = DS18B20_GetTemp();
sprintf(temp, "温度:%5.1f℃", tempreture);
LCD_Draw_String(10, 90, temp, RED);
sprintf(adc, "ADC Vol:%5.1fV", ADC_Value * 3.3 / 4096);
LCD_Draw_String(10, 110, adc, RED);
TIM1->CCR2 = (uint16_t)(ADC_Value / 4096.0 * 1000);
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8) == GPIO_PIN_SET)
{
LCD_Draw_String(10, 130, "有人活动", RED);
}
else
{
LCD_Draw_String(10, 130, "无人活动", RED);
}
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
// delay(50000);
}
/* USER CODE END 3 */
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV2;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_RTC;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
__SYSCFG_CLK_ENABLE();
}
/* USER CODE BEGIN 4 */
void RTC_AlarmConfig(uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
RTC_AlarmTypeDef sAlarm;
/**Initialize RTC and set the Time and Date
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
HAL_RTC_Init(&hrtc);
if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x1236)
{
sTime.Hours = hour;
sTime.Minutes = minute;
sTime.Seconds = second;
sTime.SubSeconds = 0;
sTime.TimeFormat = RTC_HOURFORMAT12_AM;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BCD);
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = month;
sDate.Date = day;
sDate.Year = year;
HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BCD);
}
/**Enable the Alarm A
*/
sAlarm.AlarmTime.Hours = 0;
sAlarm.AlarmTime.Minutes = 0;
sAlarm.AlarmTime.Seconds = 0;
sAlarm.AlarmTime.SubSeconds = 0;
sAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
sAlarm.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY;
sAlarm.Alarm = RTC_ALARM_A;
HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, FORMAT_BCD);
/**Enable the WakeUp
*/
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x1236);
}
/* USER CODE END 4 */
#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(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif
复制代码
原理图: 无
仿真: 无
代码:
(大赛作品)STM32F072RB NUCLEO智能家居控制.7z
(2.37 MB, 下载次数: 17)
2024-4-22 00:58 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
IdeaMing
时间:
2024-4-22 15:54
都能控制哪些东西啊?都能用哪些控制方式?手机?遥控?语音?自动控制?
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1