【FM33LG0系列开发板测评】04.PWM、RTC
[复制链接]
1、简介
PWM(Pulse Width Modulation)是脉冲宽度调制缩写,我们可以使用GPIO加延时的方式来实现,也可以通过TIM定时器的PWM功能来实现;来达到不同周期、不同占空比的波形输出。
RTC(Real Time Clock)是实时时钟,在FM33LG0系列芯片中带有2个RTC外设,分别是RTCA和RTCB;RTCB只能使用外部时钟晶振,输出固定的1Hz频率,进行RTC计时;而RTCA则可以选择不同时的时钟源:XTLF或者是RCLP,可以配置产生不同频率的周期中断(1KHz、256Hz、64Hz、16Hz、4Hz、1Hz);但不管是RTCA还是RTCB,在数字调校后,精确度都能够达到+/-0.477ppm;并且是BCD时间,具有完整的万年历功能,使用相当方便。
2、功能实现
使用LPTIM16定时器的TIM_CH3通道,即PC13引脚来输出频率为1KHz,占空比为50%的波形;使用RTCB时间时间初始化配置,每间隔1秒钟打印出来当前RTCB的运行日期和时间;
3、代码实现
LPTIM16 PWM
/*******************************************************************************
* [url=home.php?mod=space&uid=159083]@brief[/url] * @param
* @retval
* [url=home.php?mod=space&uid=1020061]@attention[/url] *******************************************************************************/
void LPTIM16_Init(void)
{
FL_GPIO_InitTypeDef GPIO_InitStruct;
FL_LPTIM16_InitTypeDef LPTIM16_InitStruct;
FL_LPTIM16_OC_InitTypeDef LPTIM16_OC_InitStruct;
FL_LPTIM16_StructInit(&LPTIM16_InitStruct);
LPTIM16_InitStruct.clockSource = FL_CMU_LPTIM16_CLK_SOURCE_APBCLK;
LPTIM16_InitStruct.mode = FL_LPTIM16_OPERATION_MODE_NORMAL;
LPTIM16_InitStruct.prescalerClockSource = FL_LPTIM16_CLK_SOURCE_INTERNAL;
LPTIM16_InitStruct.prescaler = FL_LPTIM16_PSC_DIV8;
LPTIM16_InitStruct.autoReload = 1000 - 1;
LPTIM16_InitStruct.encoderMode = FL_LPTIM16_ENCODER_MODE_DISABLE;
LPTIM16_InitStruct.onePulseMode = FL_LPTIM16_ONE_PULSE_MODE_CONTINUOUS;
LPTIM16_InitStruct.triggerEdge = FL_LPTIM16_ETR_TRIGGER_EDGE_RISING;
LPTIM16_InitStruct.countEdge = FL_LPTIM16_ETR_COUNT_EDGE_RISING;
FL_LPTIM16_Init(LPTIM16, &LPTIM16_InitStruct);
FL_GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.pin = FL_GPIO_PIN_13;
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.pull = FL_DISABLE;
GPIO_InitStruct.remapPin = FL_ENABLE;
FL_GPIO_Init(GPIOC, &GPIO_InitStruct);
LPTIM16_OC_InitStruct.OCPolarity = FL_LPTIM16_OC_POLARITY_NORMAL;
LPTIM16_OC_InitStruct.compareValue = 500 - 1;
FL_LPTIM16_OC_Init(LPTIM16, FL_LPTIM16_CHANNEL_1, &LPTIM16_OC_InitStruct);
FL_LPTIM16_Enable(LPTIM16);
}
RTCB:需要注意的是RTC配置和读取的值都是以BCD码形式表示的,所以在获取日期时间时,打印日期和时间需要将BCD码转换成16进制数;在设置日期和时间时,需要将其转换成BCD码再时行设置;
在配置星期时,如果不确定是星期几,你可以随便设置一个值;在获取日期时间后,会自动返回正确的星期几,NICE!!!
/*******************************************************************************
* @file RTC.c
* @author King
* [url=home.php?mod=space&uid=252314]@version[/url] V1.00
* [url=home.php?mod=space&uid=311857]@date[/url] 27-Nov-2021
* @brief ......
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#define __RTC_C__
/* Includes ------------------------------------------------------------------*/
#include "RTC.h"
#if RTC_ENABLE
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported function prototypes ----------------------------------------------*/
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
uint8_t RTC_HEXToBCD(uint8_t Data)
{
return (Data / 10) * 16 + (Data % 10);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
uint8_t RTC_BCDToHEX(uint8_t Data)
{
return (Data / 16) * 10 + (Data % 16);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void RTCB_Configure(void)
{
FL_RTCB_InitTypeDef RTCB_InitStruct = {0x21, 0x12, 0x12, 0x00, 0x19, 0x18, 0x25};
FL_RTCB_Init(RTCB, &RTCB_InitStruct);
FL_RTCB_Enable(RTCB);
NVIC_DisableIRQ(RTCx_IRQn);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void RTC_Configure(void)
{
RTCB_Configure();
TASK_Append(TASK_ID_RTC, RTC_Handler, 1000);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void RTC_Handler(void)
{
FL_RTCB_InitTypeDef RTCB_InitStruct;
FL_RTCB_GetTime(RTCB, &RTCB_InitStruct);
printf("\r\n20%02d-%02d-%02d",
RTC_BCDToHEX(RTCB_InitStruct.year ),
RTC_BCDToHEX(RTCB_InitStruct.month),
RTC_BCDToHEX(RTCB_InitStruct.day ));
switch(RTCB_InitStruct.week)
{
case 0 : printf(" SUN "); break;
case 1 : printf(" MON "); break;
case 2 : printf(" TUE "); break;
case 3 : printf(" WED "); break;
case 4 : printf(" THU "); break;
case 5 : printf(" FRI "); break;
case 6 : printf(" SAT "); break;
default:break;
}
printf("%02d:%02d:%02d",
RTC_BCDToHEX(RTCB_InitStruct.hour ),
RTC_BCDToHEX(RTCB_InitStruct.minute),
RTC_BCDToHEX(RTCB_InitStruct.second));
}
#endif
/******************* (C) COPYRIGHT 2021 *************************END OF FILE***/
4、运行结果
在做RTC功能验证时,需要将开发板上的VBAT与VCC进行短接,给MCU的RTC外设进行供电;使用逻辑分析仪,连接开发板上的PC13排针引脚和GND,对PWM输出波形的频率和占空比进行测量,运行结果如下图所示:
5、工程源码
Project_PWM_RTCB.zip
(359.1 KB, 下载次数: 13)
|