【复旦微FM33LC046N评测】+ GPTIM_PWM
本帖最后由 chrisrh 于 2021-3-10 10:05 编辑<p>学习一下定时器,FM33LC046N的定时器分四大类:</p>
<p>高级定时器ATIM、通用定时器GPTIM、基本定时器BSTIM32、和低功耗定时器LPTIM32</p>
<p>使用通用定时器GPTIM完成简单的四路LED灯的PWM控制</p>
<p></p>
<p><span style="color:#3498db;">将LED4飞线与PB11连接</span></p>
<p> </p>
<p>在mf_config.c中配置GPTIM初使化参数:</p>
<pre>
<code class="language-cpp">/**
* <a href="home.php?mod=space&uid=159083" target="_blank">@brief</a> GPTIM0_TimerBase Initialization function
* @paramvoid
* @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 );
}
/**
* <a href="home.php?mod=space&uid=159083" target="_blank">@brief</a>GPTIM0_Channel1 Initialization function
_____PB10_____GPTIM0_CH1_____LED3_____
_____PB11_____GPTIM0_CH2_____LED4_____
* @paramvoid
* @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 );
}
/**
* @briefGPTIM1_TimerBase Initialization function
* @paramvoid
* @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 );
}
/**
* @briefGPTIM1_Channel1_Channel2 Initialization function
_____PC0_____GPTIM1_CH1_____LED1______
_____PC1_____GPTIM1_CH2_____LED2______
* @paramvoid
* @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 );
}</code></pre>
<p>编写完GPTIM初使化参数同时修改mf_config.h头文件,在MF_Config_Init中进行调用</p>
<pre>
<code class="language-cpp">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();
}</code></pre>
<p>在main函数中启动GPTIM0和GPTIM1,设置比较参数值进行比较</p>
<pre>
<code class="language-cpp">
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
}
}
</code></pre>
<p>通过GPTIM0和GPTIM1的PWM控制LED1~4效果如下:</p>
<p></p>
<p> </p>
<p> </p>
<p>能把代码关键语句加上注释,大家能稍微理解一些,就更完美</p>
Jacktang 发表于 2021-3-10 11:01
能把代码关键语句加上注释,大家能稍微理解一些,就更完美
<p>ok</p>
chrisrh 发表于 2021-3-10 16:09
ok
<p>楼主很认真细心,这么快就注释了,谢谢</p>
页:
[1]