壓縮文件內容
基于STM32 hal庫實現的RTC實時時鐘設計所有資料51hei提供下載:
RTC.rar
(5.06 MB, 下載次數: 37)
2018-1-24 16:36 上傳
點擊文件名下載附件
單片機源程序如下:
- /**
- ******************************************************************************
- * File Name : main.c
- * Description : Main program body
- ******************************************************************************
- ** This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * COPYRIGHT(c) 2018 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32l0xx_hal.h"
- #include "lcd.h"
- #include "key.h"
- #include "time_con.h"
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- RTC_HandleTypeDef hrtc;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_RTC_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- HAL_StatusTypeDef RTC_Set_Time(u8 hour,u8 min,u8 sec);
- HAL_StatusTypeDef RTC_Set_Date(u8 year,u8 month,u8 date,u8 week);
- /* USER CODE END 0 */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- RTC_TimeTypeDef rtc_timeStruct;
- RTC_DateTypeDef rtc_dateStruct;
- u8 tbuf[40];
- static u8 key,weeks;
- /* 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();
-
- /* USER CODE BEGIN 2 */
- LCD1602_Init();
- HAL_Delay(100);
- HAL_RTC_GetTime(&hrtc, &rtc_timeStruct, RTC_FORMAT_BIN);
- HAL_RTC_GetDate(&hrtc, &rtc_dateStruct, RTC_FORMAT_BIN);
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
-
- key=KEY_Scan();
- switch(key)
- {
- case 1:
- LCD1602_Show_Str(15,0," ");
- break;
- case 2://year
- LCD1602_cursor(8,0);
- if(KEY1==RESET)
- {
- rtc_dateStruct.Year=down(rtc_dateStruct.Year,100);
- RTC_Set_Date(rtc_dateStruct.Year,rtc_dateStruct.Month,rtc_dateStruct.Date,rtc_dateStruct.WeekDay);
- }
- if(KEY2==RESET)
- {
- rtc_dateStruct.Year=up(rtc_dateStruct.Year,100);
- RTC_Set_Date(rtc_dateStruct.Year,rtc_dateStruct.Month,rtc_dateStruct.Date,rtc_dateStruct.WeekDay);
- }
- break;
- case 3://month
- LCD1602_cursor(11,0);
- if(KEY1==RESET)
- {
- rtc_dateStruct.Month--;
- if((rtc_dateStruct.Month>=13)||rtc_dateStruct.Month==0)
- rtc_dateStruct.Month=12;
- RTC_Set_Date(rtc_dateStruct.Year,rtc_dateStruct.Month,rtc_dateStruct.Date,rtc_dateStruct.WeekDay);
- }
- if(KEY2==RESET)
- {
- rtc_dateStruct.Month++;
- if(rtc_dateStruct.Month==13)
- rtc_dateStruct.Month=1;
- RTC_Set_Date(rtc_dateStruct.Year,rtc_dateStruct.Month,rtc_dateStruct.Date,rtc_dateStruct.WeekDay);
- }
- break;
- case 4://date
- LCD1602_cursor(14,0);
- if(KEY1==RESET)
- {
- rtc_dateStruct.Date--;
- if((rtc_dateStruct.Date>=dates(rtc_dateStruct.Year,rtc_dateStruct.Month)+1)||rtc_dateStruct.Date==0)
- rtc_dateStruct.Date=dates(rtc_dateStruct.Year,rtc_dateStruct.Month);
- RTC_Set_Date(rtc_dateStruct.Year,rtc_dateStruct.Month,rtc_dateStruct.Date,rtc_dateStruct.WeekDay);
- }
- if(KEY2==RESET)
- {
- rtc_dateStruct.Date++;
- if(rtc_dateStruct.Date==dates(rtc_dateStruct.Year,rtc_dateStruct.Month)+1)
- rtc_dateStruct.Date=1;
- RTC_Set_Date(rtc_dateStruct.Year,rtc_dateStruct.Month,rtc_dateStruct.Date,rtc_dateStruct.WeekDay);
- }
- break;
- case 5://hour
- LCD1602_cursor(1,1);
- if(KEY1==RESET)
- {
- rtc_timeStruct.Hours=down(rtc_timeStruct.Hours,24);
- RTC_Set_Time(rtc_timeStruct.Hours,rtc_timeStruct.Minutes,rtc_timeStruct.Seconds);
- }
- if(KEY2==RESET)
- {
- rtc_timeStruct.Hours=up(rtc_timeStruct.Hours,24);
- RTC_Set_Time(rtc_timeStruct.Hours,rtc_timeStruct.Minutes,rtc_timeStruct.Seconds);
- }
- break;
- case 6://min
- LCD1602_cursor(4,1);
- if(KEY1==RESET)
- {
- rtc_timeStruct.Minutes=down(rtc_timeStruct.Minutes,60);
- RTC_Set_Time(rtc_timeStruct.Hours,rtc_timeStruct.Minutes,rtc_timeStruct.Seconds);
- }
- if(KEY2==RESET)
- {
- rtc_timeStruct.Minutes=up(rtc_timeStruct.Minutes,60);
- RTC_Set_Time(rtc_timeStruct.Hours,rtc_timeStruct.Minutes,rtc_timeStruct.Seconds);
- }
- break;
- case 7://sec
- LCD1602_cursor(7,1);
- if(KEY1==RESET)
- {
- rtc_timeStruct.Seconds=down(rtc_timeStruct.Seconds,60);
- RTC_Set_Time(rtc_timeStruct.Hours,rtc_timeStruct.Minutes,rtc_timeStruct.Seconds);
- }
- if(KEY2==RESET)
- {
- rtc_timeStruct.Seconds=up(rtc_timeStruct.Seconds,60);
- RTC_Set_Time(rtc_timeStruct.Hours,rtc_timeStruct.Minutes,rtc_timeStruct.Seconds);
- }
- break;
- }
- HAL_Delay(150);
- HAL_RTC_GetTime(&hrtc, &rtc_timeStruct, RTC_FORMAT_BIN);
- HAL_RTC_GetDate(&hrtc, &rtc_dateStruct, RTC_FORMAT_BIN);
- sprintf((char*)tbuf,"%02d:%02d:%02d ",rtc_timeStruct.Hours,rtc_timeStruct.Minutes,rtc_timeStruct.Seconds);
- LCD1602_Show_Str(0, 1,tbuf);
- sprintf((char*)tbuf,"DATE:20%02d-%02d-%02d",rtc_dateStruct.Year,rtc_dateStruct.Month,rtc_dateStruct.Date);
- LCD1602_Show_Str(0, 0,tbuf);
- weeks=week_update(rtc_dateStruct.Year,rtc_dateStruct.Month,rtc_dateStruct.Date);
- switch(weeks)
- {
- case 0:
- LCD1602_Show_Str(9, 1,"Sunday ");
- break;
- case 1:
- LCD1602_Show_Str(9, 1,"Monday ");
- break;
- case 2:
- LCD1602_Show_Str(9, 1,"Tuesday ");
- break;
- case 3:
- LCD1602_Show_Str(9, 1,"Wednesday");
- break;
- case 4:
- LCD1602_Show_Str(9, 1,"Thursday ");
- break;
- case 5:
- LCD1602_Show_Str(9, 1,"Friday ");
- break;
- case 6:
- LCD1602_Show_Str(9, 1,"Saturday ");
- break;
- }
- }
- /* USER CODE END 3 */
- }
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- /**Configure the main internal regulator output voltage
- */
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
- RCC_OscInitStruct.LSEState = RCC_LSE_ON;
- RCC_OscInitStruct.HSIState = RCC_HSI_DIV4;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_16;
- RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Initializes the CPU, AHB and APB busses 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_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
- PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* RTC init function */
- static void MX_RTC_Init(void)
- {
- RTC_TimeTypeDef sTime;
- RTC_DateTypeDef sDate;
- /**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.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
- hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
- hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
- if (HAL_RTC_Init(&hrtc) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Initialize RTC and set the Time and Date
- */
- if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
- sTime.Hours = 0x20;//
- sTime.Minutes = 0x19;
- sTime.Seconds = 0x0;
- sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
- sTime.StoreOperation = RTC_STOREOPERATION_SET;
- if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- sDate.WeekDay = RTC_WEEKDAY_SUNDAY;
- sDate.Month = RTC_MONTH_JANUARY;
- sDate.Date = 0x21;
- sDate.Year = 0x18;
- if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
- }
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- PA2 ------> USART2_TX
- PA3 ------> USART2_RX
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOH_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin : B1_Pin */
- GPIO_InitStruct.Pin = B1_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
-
- /*Configure GPIO pins : PA0 PA1 PA4 */
- GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pins : USART_TX_Pin USART_RX_Pin */
- GPIO_InitStruct.Pin = USART_TX_Pin|USART_RX_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pin : LD2_Pin */
- GPIO_InitStruct.Pin = LD2_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
-
- /*USER Configure : GPIOC_Pin : LCD1602 */
- GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; //推挽輸出
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; //IO口速度為高速
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); //初始化GPIOD0~10
-
- GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; //推挽輸出
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; //IO口速度為高速
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); //初始化GPIOD0~10
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE BEGIN 0 */
- //RTC時間設置
- //hour,min,sec:小時,分鐘,秒鐘
- //ampm:@RTC_AM_PM_Definitions:RTC_HOURFORMAT12_AM/RTC_HOURFORMAT12_PM
- //返回值:SUCEE(1),成功
- // ERROR(0),進入初始化模式失敗
- HAL_StatusTypeDef RTC_Set_Time(u8 hour,u8 min,u8 sec)//unsigned char ampm
- {
- RTC_TimeTypeDef RTC_TimeStructure;
-
- RTC_TimeStructure.Hours=hour;
- RTC_TimeStructure.Minutes=min;
- RTC_TimeStructure.Seconds=sec;
- //RTC_TimeStructure.TimeFormat=ampm;
- RTC_TimeStructure.DayLightSaving=RTC_DAYLIGHTSAVING_NONE;
- RTC_TimeStructure.StoreOperation=RTC_STOREOPERATION_RESET;
- return HAL_RTC_SetTime(&hrtc,&RTC_TimeStructure,RTC_FORMAT_BIN);
- }
- //RTC日期設置
- //year,month,date:年(0~99),月(1~12),日(0~31)
- //week:星期(1~7,0,非法!)
- //返回值:SUCEE(1),成功
- // ERROR(0),進入初始化模式失敗
- HAL_StatusTypeDef RTC_Set_Date(u8 year,u8 month,u8 date,u8 week)
- {
- RTC_DateTypeDef RTC_DateStructure;
-
- RTC_DateStructure.Date=date;
- RTC_DateStructure.Month=month;
- RTC_DateStructure.WeekDay=week;
- RTC_DateStructure.Year=year;
- return HAL_RTC_SetDate(&hrtc,&RTC_DateStructure,RTC_FORMAT_BIN);
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- void _Error_Handler(char * file, int line)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- 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
- /**
- * @}
- */
- /**
- * @}
- */
復制代碼
|