Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2021 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "stdio.h"
- #include "string.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- RTC_HandleTypeDef hrtc;
- UART_HandleTypeDef huart2;
- /* USER CODE BEGIN PV */
- char time[10];
- char date[10];
- uint8_t alarm = 0;
- char TxData[] = "Buttom is pressed!\r\n";
- char TxData2[] = "Redy!\r\n";
- char trans_str[64] = { 0, };
- RTC_TimeTypeDef sTime = { 0 };
- RTC_DateTypeDef DateToUpdate = { 0 };
- RTC_AlarmTypeDef sAlarm = { 0 };
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_RTC_Init(void);
- static void MX_USART2_UART_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- void set_time(void) {
- RTC_TimeTypeDef sTime = { 0 };
- RTC_DateTypeDef sDate = { 0 };
- /** Initialize RTC and set the Time and Date */
- sTime.Hours = 0x12; // 12h.
- sTime.Minutes = 0x45; // 45m.
- sTime.Seconds = 0x50; // 50c.
- sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
- sTime.StoreOperation = RTC_STOREOPERATION_RESET;
- if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) {
- Error_Handler();
- }
- sDate.WeekDay = RTC_WEEKDAY_TUESDAY;
- sDate.Month = RTC_MONTH_OCTOBER;
- sDate.Date = 0x19;
- sDate.Year = 0x21;
- if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK) {
- Error_Handler();
- }
- /* Backup register to store TIME and DATE! */
- //HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2); // backup register
- }
- void set_alarm(void) {
- RTC_AlarmTypeDef sAlarm = { 0 };
- /** Enable the Alarm A
- */
- sAlarm.AlarmTime.Hours = 0x12;
- sAlarm.AlarmTime.Minutes = 0x46;
- sAlarm.AlarmTime.Seconds = 0x0;
- sAlarm.AlarmTime.SubSeconds = 0x0;
- sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
- sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
- sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
- sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
- sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
- sAlarm.AlarmDateWeekDay = 0x13;
- sAlarm.Alarm = RTC_ALARM_A;
- if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK) {
- Error_Handler();
- }
- }
- void get_time(void) {
- RTC_TimeTypeDef sTime = { 0 };
- RTC_DateTypeDef sDate = { 0 };
- /* Get the RTC current Time */
- HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
- /* Get the RTC current Date */
- HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
- /* Display time Format: hh:mm:ss */
- sprintf((char*) time, "%02d:%02d:%02d", sTime.Hours, sTime.Minutes,
- sTime.Seconds);
- /* Display date Format: mm-dd-yy */
- sprintf((char*) date, "%02d-%02d-%2d", sDate.Date, sDate.Month,
- 2000 + sDate.Year); // I like the date first
- }
- void display_time(void) { //Let's display the time and date on lcd
- //lcd_send_cmd (0x80);
- //lcd_send_string (time);
- //lcd_send_cmd (0xc0);
- //lcd_send_string (date);
- HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN); // RTC_FORMAT_BIN , RTC_FORMAT_BCD
- //snprintf(trans_str, 63, "Time %d:%d:%d\n", sTime.Hours, sTime.Minutes, sTime.Seconds);
- snprintf(trans_str, 63, "Time %d:%d:%d:%lu\n", sTime.Hours, sTime.Minutes,
- sTime.Seconds, sTime.SubSeconds);
- HAL_UART_Transmit(&huart2, (uint8_t*) trans_str, strlen(trans_str), 1000);
- HAL_RTC_GetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN);
- snprintf(trans_str, 63, "Date %d-%d-20%d\n", DateToUpdate.Date,
- DateToUpdate.Month, DateToUpdate.Year);
- HAL_UART_Transmit(&huart2, (uint8_t*) trans_str, strlen(trans_str), 1000);
- HAL_RTC_GetAlarm(&hrtc, &sAlarm, RTC_ALARM_A, RTC_FORMAT_BIN);
- snprintf(trans_str, 63, "Alarm %d:%d:%d\n", sAlarm.AlarmTime.Hours,
- sAlarm.AlarmTime.Minutes, sAlarm.AlarmTime.Seconds);
- HAL_UART_Transmit(&huart2, (uint8_t*) trans_str, strlen(trans_str), 1000);
- HAL_UART_Abort(&huart2); // Обрываем связь UART
- }
- void to_do_on_alarm(void)
- {
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 0); // set led ON
- }
- }
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- 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();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_RTC_Init();
- MX_USART2_UART_Init();
- /* USER CODE BEGIN 2 */
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, 1);
- /* Backup register to store TIME and DATE! */
- if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0x32F2) // Time is not restting now!
- {
- set_time(); // Set the time
- }
- set_alarm();
- HAL_UART_Transmit(&huart2, (uint8_t*) TxData2, sizeof(TxData2), 500);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1) {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET) {
- display_time();
- HAL_Delay(1000);
- }
- //display_time();
- get_time();
- HAL_Delay(500);
- if (alarm) {
- to_do_on_alarm();
- alarm = 0;
- }
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void) {
- RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
- RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
- /** Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI
- | RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.LSIState = RCC_LSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 4;
- RCC_OscInitStruct.PLL.PLLN = 168;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 4;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
- | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
- Error_Handler();
- }
- }
- /**
- * @brief RTC Initialization Function
- * @param None
- * @retval None
- */
- static void MX_RTC_Init(void) {
- /* USER CODE BEGIN RTC_Init 0 */
- /* USER CODE END RTC_Init 0 */
- // RTC_TimeTypeDef sTime = {0};
- // RTC_DateTypeDef sDate = {0};
- // RTC_AlarmTypeDef sAlarm = {0};
- /* USER CODE BEGIN RTC_Init 1 */
- /* USER CODE END RTC_Init 1 */
- /** Initialize RTC Only
- */
- 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;
- if (HAL_RTC_Init(&hrtc) != HAL_OK) {
- Error_Handler();
- }
- /* USER CODE BEGIN Check_RTC_BKUP */
- /* USER CODE END Check_RTC_BKUP */
- /** Initialize RTC and set the Time and Date
- */
- // sTime.Hours = 0x12;
- // sTime.Minutes = 0x45;
- // sTime.Seconds = 0x50;
- // sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
- // sTime.StoreOperation = RTC_STOREOPERATION_RESET;
- // if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
- // {
- // Error_Handler();
- // }
- // sDate.WeekDay = RTC_WEEKDAY_MONDAY;
- // sDate.Month = RTC_MONTH_JANUARY;
- // sDate.Date = 0x19;
- // sDate.Year = 0x21;
- //
- // if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
- // {
- // Error_Handler();
- // }
- // /** Enable the Alarm A
- // */
- // sAlarm.AlarmTime.Hours = 0x12;
- // sAlarm.AlarmTime.Minutes = 0x46;
- // sAlarm.AlarmTime.Seconds = 0x0;
- // sAlarm.AlarmTime.SubSeconds = 0x0;
- // sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
- // sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
- // sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
- // sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
- // sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
- // sAlarm.AlarmDateWeekDay = 0x1;
- // sAlarm.Alarm = RTC_ALARM_A;
- // if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
- // {
- // Error_Handler();
- // }
- /* USER CODE BEGIN RTC_Init 2 */
- /* USER CODE END RTC_Init 2 */
- }
- /**
- * @brief USART2 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_USART2_UART_Init(void) {
- /* USER CODE BEGIN USART2_Init 0 */
- /* USER CODE END USART2_Init 0 */
- /* USER CODE BEGIN USART2_Init 1 */
- /* USER CODE END USART2_Init 1 */
- huart2.Instance = USART2;
- huart2.Init.BaudRate = 115200;
- huart2.Init.WordLength = UART_WORDLENGTH_8B;
- huart2.Init.StopBits = UART_STOPBITS_1;
- huart2.Init.Parity = UART_PARITY_NONE;
- huart2.Init.Mode = UART_MODE_TX_RX;
- huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart2) != HAL_OK) {
- Error_Handler();
- }
- /* USER CODE BEGIN USART2_Init 2 */
- /* USER CODE END USART2_Init 2 */
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void) {
- GPIO_InitTypeDef GPIO_InitStruct = { 0 };
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOH_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin : BTN_Pin */
- GPIO_InitStruct.Pin = BTN_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_PULLDOWN;
- HAL_GPIO_Init(BTN_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pin : LED_Pin */
- GPIO_InitStruct.Pin = LED_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- /*** Callback будилькина ***/
- void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) {
- snprintf(trans_str, 63, "ALARM\n");
- HAL_UART_Transmit(&huart2, (uint8_t*) trans_str, strlen(trans_str), 1000);
- alarm = 1;
- }
- /* 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 */
- __disable_irq();
- while (1) {
- }
- /* 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(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 /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement