[Fudan Micro FM33LC046N Review] + GPTIM_PWM
[Copy link]
This post was last edited by chrisrh on 2021-3-10 10:05
Learn about timers. The timers of FM33LC046N are divided into four categories:
Advanced timer ATIM, general timer GPTIM, basic timer BSTIM32, and low power timer LPTIM32
Use the general timer GPTIM to complete the PWM control of simple four-way LED lights
Connect the flying wire of LED4 to PB11
Configure GPTIM initialization parameters in mf_config.c:
/**
* @brief GPTIM0_TimerBase Initialization function
* @param void
* @retval None
*/
void MF_GPTIM0_TimerBase_Init(uint16_t psc,uint16_t arr)
{
/*IO CONFIG*/
FL_GPTIM_InitTypeDef TimerBaseInitStruct;
TimerBaseInitStruct.prescaler = (uint16_t)psc;//预分频系数
TimerBaseInitStruct.counterMode = FL_GPTIM_COUNTER_DIR_UP;//计数模式
TimerBaseInitStruct.autoReload = arr;//自动重装载值
TimerBaseInitStruct.autoReloadState = DISABLE;//预装载使能
TimerBaseInitStruct.clockDivision = FL_GPTIM_CLK_DIVISION_DIV1;
//定时器分频系数与数字滤波器所使用的采样时钟分频比
FL_GPTIM_Init(GPTIM0,&TimerBaseInitStruct );
}
/**
* @brief GPTIM0_Channel1 Initialization function
_____PB10_____GPTIM0_CH1_____LED3_____
_____PB11_____GPTIM0_CH2_____LED4_____
* @param void
* @retval None
*/
void MF_GPTIM0_Channel1_Channel2_Init(void)
{
FL_GPIO_InitTypeDef GPIO_InitStruct;
FL_GPTIM_OC_InitTypeDef defaultInitStruct;
GPIO_InitStruct.pin = FL_GPIO_PIN_10|FL_GPIO_PIN_11;
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.pull = DISABLE;
GPIO_InitStruct.remapPin = ENABLE;
FL_GPIO_Init( GPIOB, &GPIO_InitStruct );
defaultInitStruct.OCMode = FL_GPTIM_OC_MODE_PWM1;//比较输出模式
defaultInitStruct.OCFastMode = DISABLE;//比较输出通道快速模式使能
defaultInitStruct.OCPreload = DISABLE;//输出比较预装载
defaultInitStruct.compareValue = 0x1F4U;//通道比较值
defaultInitStruct.OCPolarity = FL_GPTIM_OC_POLARITY_NORMAL;//比较输出极性
defaultInitStruct.OCETRFStatus = DISABLE;//ETR清0使能
FL_GPTIM_OC_Init(GPTIM0,FL_GPTIM_CHANNEL_1,&defaultInitStruct );
FL_GPTIM_OC_Init(GPTIM0,FL_GPTIM_CHANNEL_2,&defaultInitStruct );
}
/**
* @brief GPTIM1_TimerBase Initialization function
* @param void
* @retval None
*/
void MF_GPTIM1_TimerBase_Init(uint16_t psc,uint16_t arr)
{
/*IO CONFIG*/
FL_GPTIM_InitTypeDef TimerBaseInitStruct;
TimerBaseInitStruct.prescaler = (uint16_t)psc;//预分频系数
TimerBaseInitStruct.counterMode = FL_GPTIM_COUNTER_DIR_UP;//计数模式
TimerBaseInitStruct.autoReload = arr;//自动重装载值
TimerBaseInitStruct.autoReloadState = DISABLE;//预装载使能
TimerBaseInitStruct.clockDivision = FL_GPTIM_CLK_DIVISION_DIV1;
//定时器分频系数与数字滤波器所使用的采样时钟分频比
FL_GPTIM_Init(GPTIM1,&TimerBaseInitStruct );
}
/**
* @brief GPTIM1_Channel1_Channel2 Initialization function
_____PC0_____GPTIM1_CH1_____LED1______
_____PC1_____GPTIM1_CH2_____LED2______
* @param void
* @retval None
*/
void MF_GPTIM1_Channel1_Channel2_Init(void)
{
FL_GPIO_InitTypeDef GPIO_InitStruct;
FL_GPTIM_OC_InitTypeDef defaultInitStruct;
GPIO_InitStruct.pin = FL_GPIO_PIN_0|FL_GPIO_PIN_1;
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.pull = DISABLE;
GPIO_InitStruct.remapPin = ENABLE;
FL_GPIO_Init( GPIOC, &GPIO_InitStruct );
defaultInitStruct.OCMode = FL_GPTIM_OC_MODE_PWM1;//比较输出模式
defaultInitStruct.OCFastMode = DISABLE;//比较输出通道快速模式使能
defaultInitStruct.OCPreload = DISABLE;//输出比较预装载
defaultInitStruct.compareValue = 0x1F4U;//通道比较值
defaultInitStruct.OCPolarity = FL_GPTIM_OC_POLARITY_NORMAL;//比较输出极性
defaultInitStruct.OCETRFStatus = DISABLE;//ETR清0使能
FL_GPTIM_OC_Init(GPTIM1,FL_GPTIM_CHANNEL_1,&defaultInitStruct );
FL_GPTIM_OC_Init(GPTIM1,FL_GPTIM_CHANNEL_2,&defaultInitStruct );
}
After writing the GPTIM initialization parameters, modify the mf_config.h header file and call it in MF_Config_Init
void MF_Config_Init(void)
{
/*FUNCTION CALL*/
MF_GPTIM0_TimerBase_Init(999,200);
MF_GPTIM0_Channel1_Channel2_Init();
MF_GPTIM1_TimerBase_Init(999,200);
MF_GPTIM1_Channel1_Channel2_Init();
}
Start GPTIM0 and GPTIM1 in the main function and set the comparison parameter value for comparison
int main(void)
{
uint16_t led1pwmval=0,led2pwmval=50,led3pwmval=100,led4pwmval=150;
uint8_t dir1=1,dir2=1,dir3=1,dir4=1;
MF_Clock_Init();
MF_SystemClock_Config();
UserInit();
MF_Config_Init();
FL_GPTIM_Enable(GPTIM0);
FL_GPTIM_Enable(GPTIM1);
while(1)
{
DelayMs(10);
if(dir1)led1pwmval++;
else led1pwmval--;
if(dir2)led2pwmval++;
else led2pwmval--;
if(dir3)led3pwmval++;
else led3pwmval--;
if(dir4)led4pwmval++;
else led4pwmval--;
if(led1pwmval>200)dir1=0;
if(led1pwmval==0)dir1=1;
if(led1pwmval>200)dir2=0;
if(led1pwmval==0)dir2=1;
if(led1pwmval>200)dir3=0;
if(led1pwmval==0)dir3=1;
if(led1pwmval>200)dir4=0;
if(led1pwmval==0)dir4=1;
FL_GPTIM_WriteCompareCH1(GPTIM1,led1pwmval);//LED1
FL_GPTIM_WriteCompareCH2(GPTIM1,led2pwmval);//LED2
FL_GPTIM_WriteCompareCH1(GPTIM0,led3pwmval);//LED3
FL_GPTIM_WriteCompareCH2(GPTIM0,led4pwmval);//LED4
}
}
The effects of controlling LED1~4 through PWM of GPTIM0 and GPTIM1 are as follows:
|