RTC部分直接在上部分代码里面增加
1.配置STM32CubeMX工程文件
(1).使能RTC时钟
(2),采用外部时钟,配置一秒产生一次中断
(3).时间的话随便设置一个就行
(4).生成keil工程文件
2.配置keil工程文件
(1).添加.c .h文件,过程省略
(2).转移资源.c .h文件里面的内容
(3).去路径
#include "main.h" ——》#include "stm32g4xx_hal.h" ——》#include "stm32g4xx_hal_conf.h"
打开RTC使能
3.在main.c编写功能函数
(1).RTC初始化
(2).定义日期、时间结构体、定义八位变量秒(注意结构体类型需要记忆)
(3). 编写获取年月日时分秒的函数 并通过LCD屏幕显示出来(此处注意,需要两个结构体都获取时间,单独获取一个会出现问题)
#include "stdio.h"
#include "main.h"
#include "led\bsp_led.h"
#include "key\bsp_key.h"
#include "lcd\bsp_lcd.h"
#include "adc\bsp_adc.h"
#include "basic_tim6\bsp_basic_tim6.h"
#include "rtc\bsp_rtc.h"
__IO uint32_t uwTick_key_set_point = 0;
unsigned char uckey_valu,uckey_old,uckey_down,uckey_up;
__IO uint32_t uwTick_lcd_set_point = 0;
unsigned int i = 0;
unsigned char Lcd_Dis_String[22];
RTC_DateTypeDef Y_M_D_Date;
RTC_TimeTypeDef H_M_S_Time;
uint8_t Second;
void SystemClock_Config(void);
void KEY_Proc(void);
void LCD_Disp(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
LED_Init();
KEY_Init();
LCD_Init();
ADC1_Init();
ADC2_Init();
LCD_Clear(White);
TIM6_Init();
RTC_Init();
HAL_TIM_Base_Start_IT(&htim6);
while (1)
{
KEY_Proc();
LCD_Disp();
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
i++;
HAL_TIM_Base_Start_IT(&htim6);
}
void LCD_Disp(void)
{
if (uwTick - uwTick_lcd_set_point < 200) return;
uwTick_lcd_set_point = uwTick;
//i++;
sprintf((char *)Lcd_Dis_String,"The time is %d",i);
LCD_DisplayStringLine(Line0, Lcd_Dis_String);
sprintf((char *)Lcd_Dis_String,"ADC1:%5.3f",getADC1()*3.3/4096);
LCD_DisplayStringLine(Line1, Lcd_Dis_String);
sprintf((char *)Lcd_Dis_String,"ADC2:%5.3f",getADC2()*3.3/4096);
LCD_DisplayStringLine(Line2, Lcd_Dis_String);
//RTC有关
HAL_RTC_GetTime(&hrtc,&H_M_S_Time,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc,&Y_M_D_Date,RTC_FORMAT_BIN);
sprintf((char *)Lcd_Dis_String,"DATE:%d/%d/%d",Y_M_D_Date.Year,Y_M_D_Date.Month,Y_M_D_Date.Date);
LCD_DisplayStringLine(Line3, Lcd_Dis_String);
sprintf((char *)Lcd_Dis_String,"TIME:%d:%d:%d",H_M_S_Time.Hours,H_M_S_Time.Minutes,H_M_S_Time.Seconds);
LCD_DisplayStringLine(Line4, Lcd_Dis_String);
}
void KEY_Proc(void)
{
if (uwTick - uwTick_key_set_point < 200) return;
uwTick_key_set_point = uwTick;
uckey_valu = KEY_Scan();
uckey_down = uckey_valu & (uckey_old^uckey_valu);
uckey_up = ~uckey_valu & (uckey_old^uckey_valu);
uckey_old = uckey_valu;
if (uckey_down == 1)
{
LED_Disp(0x11);
}
if (uckey_down == 2)
{
LED_Disp(0xff);
}
if (uckey_down == 3)
{
LED_Disp(0x44);
}
if (uckey_down == 4)
{
LED_Disp(0x00);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
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_2) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
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 */
工程代码下载链接放在文章开始,可以免费下载