|
演示PY32F030K28自带RTC并驱动LED数码显示时钟。
/**
******************************************************************************
* 文件名程: main.c
* 作 者: 东莞邓凯哥
* 功 能: 主函数
* 硬 件: PY32F030K28-StartKit-V0.1
* 说明:数码管显示RTC时钟DEMO
* 软件版本 V0.0.1(2024/4/16 16:02)
******************************************************************************
**/
#include "main.h"
#include "rtc.h"
#include "rcc.h"
#include "led.h"
#include "usart.h"
#include "timepare.h"
/* Private variables ---------------------------------------------------------*/
const uint8_t dispArr[] = {LL_LED_DISP_0, LL_LED_DISP_1, LL_LED_DISP_2, LL_LED_DISP_3, LL_LED_DISP_4, \
LL_LED_DISP_5, LL_LED_DISP_6, LL_LED_DISP_7, LL_LED_DISP_8, LL_LED_DISP_9
};
static uint8_t dotdis = 0 ;
/******************************************************************************
**函数信息 :System_Tick(uint32_t Ticks)
**功能描述 :系统嘀嗒时钟配置
**输入参数 :Ticks 频率
**输出参数 :无
**说 明 :
******************************************************************************/
void System_Tick(uint32_t Ticks)
{
SysTick->LOAD = (uint32_t)((SystemCoreClock / Ticks) - 1UL); /* set reload register */
SysTick->VAL = 0UL; /* Load the SysTick Counter Value */
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk; /* Enable the Systick Timer */
}
/******************************************************************************
**函数信息 :main()
**功能描述 :主函数
**输入参数 :无
**输出参数 :无
**说 明 :
******************************************************************************/
int main(void)
{
/* 配置系统时钟 */
APP_RCC_Config();
/* 配置USART1相关参数 */
APP_USART1_Config();
/* 配置LED相关参数 */
APP_LED_Config();
/* 配置RTC相关参数 */
APP_RTC_Config();
/* 设置RTC时间 */
SET_RTC_Time(8,14,23);
/* 开启系统嘀嗒定时器 */
System_Tick(1000);
while (1)
{
if( TimePare.Tim1ms_flag == 1 ) //1ms任务
{
TimePare.Tim1ms_flag = 0;
if( ++TimePare.Tim10ms_count >= 10 )
{
TimePare.Tim10ms_count = 0;
TimePare.Tim10ms_flag = 1;
}
if( ++TimePare.Tim50ms_count >= 50 )
{
TimePare.Tim50ms_count = 0;
TimePare.Tim50ms_flag = 1;
}
}
if( TimePare.Tim10ms_flag == 1 ) //10ms 任务
{
TimePare.Tim10ms_flag = 0;
if( ++TimePare.Tim100ms_count >= 10 )
{
TimePare.Tim100ms_count = 0;
TimePare.Tim100ms_flag = 1;
}
if( ++TimePare.Tim200ms_count >= 20 )
{
TimePare.Tim200ms_count = 0;
TimePare.Tim200ms_flag = 1;
}
}
if( TimePare.Tim50ms_flag == 1 ) //50ms 任务
{
TimePare.Tim50ms_flag = 0;
}
if( TimePare.Tim100ms_flag == 1 ) //100ms 任务
{
TimePare.Tim100ms_flag = 0;
if( ++TimePare.Tim500ms_count >= 5 )
{
TimePare.Tim500ms_count = 0;
TimePare.Tim500ms_flag = 1;
}
}
if( TimePare.Tim200ms_flag == 1 ) //200ms 任务
{
TimePare.Tim200ms_flag = 0;
}
if( TimePare.Tim500ms_flag == 1 ) //500ms 任务
{
dotdis ++;
APP_UpadateRtcTimeStruct();
//显示RTC时钟
LL_LED_SetDisplayValue(LED, LL_LED_COM0, dispArr[ RTC_TimeStruct.hour /10]);
// if( dotdis / 2 ) //常规的方法,用除法
if( dotdis >> 1 ) //更高效的方法,用移位
{
dotdis = 0;
LL_LED_SetDisplayValue(LED, LL_LED_COM1, dispArr[ RTC_TimeStruct.hour %10] | LL_LED_DISP_DOT ); //时钟点闪烁
}else{
LL_LED_SetDisplayValue(LED, LL_LED_COM1, dispArr[ RTC_TimeStruct.hour %10] );
}
LL_LED_SetDisplayValue(LED, LL_LED_COM2, dispArr[ RTC_TimeStruct.min /10]);
LL_LED_SetDisplayValue(LED, LL_LED_COM3, dispArr[ RTC_TimeStruct.min %10]);
TimePare.Tim500ms_flag = 0;
if( ++TimePare.Tim1s_count >= 2 )
{
TimePare.Tim1s_count = 0;
TimePare.Tim1s_flag = 1;
}
}
if( TimePare.Tim1s_flag == 1 ) //1000ms 任务
{
printf("Hours = %02d, Min = %02d, Sec = %02d\r\n",\
RTC_TimeStruct.hour, RTC_TimeStruct.min, RTC_TimeStruct.sec); //串口打印 时,分,秒
TimePare.Tim1s_flag = 0;
}
}
}
/**
* @brief 错误执行函数
* @param 无
* @retval 无
*/
void APP_ErrorHandler(void)
{
/* 无限循环 */
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief 输出产生断言错误的源文件名及行号
* @param file:源文件名指针
* @param line:发生断言错误的行号
* @retval 无
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* 用户可以根据需要添加自己的打印信息,
例如: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* 无限循环 */
while (1)
{
}
}
#endif /* USE_FULL_ASSERT */
程序和PDF格式的原理图:
01.7z
(1.07 MB, 下载次数: 5)
|
|