银杏科技有限公司旗下技术文档发布平台 | |||
技术支持电话 | 0379-69926675-801 | ||
技术支持邮件 | Gingko@vip.163.com | ||
版本 | 日期 | 作者 | 修改内容 |
---|---|---|---|
V1.0 | 2020-11-06 | gingko | 初次建立 |
1.在主界面选择File–>New Project或者直接点击ACCEE TO MCU SELECTOR 2.出现芯片型号选择,搜索自己芯片的型号,双击型号,或者点击Start Project进入配置 在搜索栏的下面,提供的各 种查找方式,可以选择芯片内核,型号等等,可以帮助你查找芯片。本实验选取的芯片型号为:STM32F429IGHx。 3.配置RCC,使用外部时钟源 4.时基源选择SysTick 5.将LED对应的三个引脚PH14以及PI3和PI4设置为GPIO_Output 6.引脚模式配置 7.配置RTC 8.设置串口
9.时钟源设置 10.工程文件的设置, 这里就是工程的各种配置,我们只用到有限几个,其他的默认即可。IDE我们使用的是 MDK V5.27。 11.点击Code Generator,进行进一步配置
STM32F429 RTC时钟简介
RTC主要特性
RTC框图 在STM32F429设备上,RTC_AF1和RTC_AF2备用功能分别地连接到PC13和PI8。
预分频器
闹钟
时间戳
入侵检测
实时时钟和日历
int main(void) { RTC_TimeTypeDef sTime; RTC_DateTypeDef sDate; int second_bak = 0; charweekday[7][9]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}; HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_RTC_Init(); MX_USART1_UART_Init(); usart1.initialize(115200); //串口波特设置 usart1.printf("\x0c"); //清屏 usart1.printf("\033[1;32;40m"); //设置终端字体为绿色 usart1.printf(" Hello, I am iCore3L!\r\n"); LED_GREEN_ON; while (1) { HAL_Delay(100); HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN); HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN); if(second_bak != sTime.Seconds){ usart1.printf("%02d:%02d:%02d”,sTime.Hours,sTime.Minutes, sTime.Seconds); usart1.printf("20%02d-%02d-%02d:%s\r",sDate.Year,sDate.Month, sDate.Date, weekday[sDate.WeekDay-1]); second_bak = sTime.Seconds; } } }
void MX_RTC_Init(void) { RTC_TimeTypeDef sTime = {0}; RTC_DateTypeDef sDate = {0}; 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(); } if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) == 0x1234) { return; } HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x1234); sTime.Hours = 0x9; sTime.Minutes = 0x45; sTime.Seconds = 0x0; 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_NOVEMBER; sDate.Date = 0x21; sDate.Year = 0x20; if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK) { Error_Handler(); } if (HAL_RTCEx_SetWakeUpTimer(&hrtc, 0, RTC_WAKEUPCLOCK_RTCCLK_DIV16) != HAL_OK) { Error_Handler(); } }
static int rtc_set_time(unsigned char hour,unsigned char min,unsigned char sec) { RTC_TimeTypeDef sTime; sTime.Hours = hour; sTime.Minutes = min; sTime.Seconds = sec; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_RESET; if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK) { while(1); } return 0; } static int rtc_set_date(unsigned char year,unsigned char month,unsigned char date,unsigned char week) { RTC_DateTypeDef sDate; sDate.WeekDay = week; sDate.Month = month; sDate.Date = date; sDate.Year = year; if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK) { while(1); } return 0; }
HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format) { uint32_t datetmpreg = 0U; assert_param(IS_RTC_FORMAT(Format)); datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK); sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16U); sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8U); sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU)); sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13U); if(Format == RTC_FORMAT_BIN) { sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year); sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month); sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date); } return HAL_OK; } HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format) { uint32_t tmpreg = 0U; assert_param(IS_RTC_FORMAT(Format)); sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR); sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S); tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK); sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16U); sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U); sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU)); sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16U); if(Format == RTC_FORMAT_BIN) { /* Convert the time structure parameters to Binary format */ sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours); sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes); sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds); } return HAL_OK; }
__HAL_RCC_USART1_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); GPIO_InitStruct.Pin = GPIO_PIN_3; //ARM上的PB3脚为USART1_RX GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_15; //ARM上的PA15脚为USART1_TX GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);