[国民技术N32WB452测评] 二、PWM输出代码分析及呼吸灯实现
<p>国民技术N32WB452提供了很多的示例程序,其中涉及PWM输出的在TIM目录下,为定时器的案例之一。</p><p>1、PWM Output示例</p>
<p>示例程序路径</p>
<p> 示例代码的目录有个readme文件,说明了其功能</p>
<p> 收头没有示波器就没办法得到波形了</p>
<p>2、代码分析</p>
<p>具体的实现代码比较简单</p>
<p>首先是时钟RCC的开启及其使能</p>
<pre>
<code>void RCC_Configuration(void)
{
/* TIM3 clock enable */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_EnableAPB2PeriphClk(
RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_GPIOC | RCC_APB2_PERIPH_AFIO, ENABLE);
}</code></pre>
<p>接下来是GPIO的设置</p>
<p>由于TIM3的通道CH1-CH4分别对应PA6、PA7、PB0和PB1,GPIO也只针对这4各管脚进行</p>
<pre>
<code>void GPIO_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
/* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
}</code></pre>
<p>下面是定时器及其通道的设置,代码里做了比较详细的说明</p>
<pre>
<code>/* -----------------------------------------------------------------------
TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
The TIM3CLK frequency is set to SystemCoreClock (Hz), to get TIM3 counter
clock at 24 MHz the Prescaler is computed as following:
- Prescaler = (TIM3CLK / TIM3 counter clock) - 1
SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
and Connectivity line devices and to 24 MHz for Low-Density Value line and
Medium-Density Value line devices
The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(AR + 1)
= 24 MHz / 666 = 36 KHz
TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
----------------------------------------------------------------------- */
/* Compute the prescaler value */
PrescalerValue = (uint16_t)(SystemCoreClock / 24000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.Period = 665;
TIM_TimeBaseStructure.Prescaler = PrescalerValue;
TIM_TimeBaseStructure.ClkDiv = 0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_InitTimeBase(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
TIM_OCInitStructure.Pulse = CCR1_Val;
TIM_OCInitStructure.OcPolarity= TIM_OC_POLARITY_HIGH;
TIM_InitOc1(TIM3, &TIM_OCInitStructure);
TIM_ConfigOc1Preload(TIM3, TIM_OC_PRE_LOAD_ENABLE);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
TIM_OCInitStructure.Pulse = CCR2_Val;
TIM_InitOc2(TIM3, &TIM_OCInitStructure);
TIM_ConfigOc2Preload(TIM3, TIM_OC_PRE_LOAD_ENABLE);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
TIM_OCInitStructure.Pulse = CCR3_Val;
TIM_InitOc3(TIM3, &TIM_OCInitStructure);
TIM_ConfigOc3Preload(TIM3, TIM_OC_PRE_LOAD_ENABLE);
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
TIM_OCInitStructure.Pulse = CCR4_Val;
TIM_InitOc4(TIM3, &TIM_OCInitStructure);
TIM_ConfigOc4Preload(TIM3, TIM_OC_PRE_LOAD_ENABLE);
TIM_ConfigArPreload(TIM3, ENABLE);
/* TIM3 enable counter */
TIM_Enable(TIM3, ENABLE);</code></pre>
<p>通用定时器TIM3以24MHz的工作进行计数,其中计数周期设置为666,PWM的4个通道分别为这个计数周期的50%、37.5%、25%以及12.5%;</p>
<pre>
<code>uint16_t CCR1_Val = 333;
uint16_t CCR2_Val = 249;
uint16_t CCR3_Val = 166;
uint16_t CCR4_Val = 83;</code></pre>
<p>设置完成后就由定时器TIM3硬件输出PWM信号了</p>
<p>3、呼吸灯实现</p>
<p>在这个demo案例中,稍微加点变化,就能实现呼吸灯效果了</p>
<p>定义了两个变量,一个为递增及递减的方向u8 dir,一个为记录输出比较器的参考值u16 led0pwmval=0;</p>
<p>在main主循环中增加以下代码</p>
<pre>
<code>while (1)
{
if(dir)led0pwmval++; //dir==1 led0pwmval递增
else led0pwmval--; //dir==0 led0pwmval递减
if(led0pwmval>660)dir=0; //led0pwmval到达660后,方向为递减
if(led0pwmval==0)dir=1; //led0pwmval递减到0后,方向改为递增
TIM3->CCDAT4 = led0pwmval; //CH4 做呼吸灯
Delay(0x01FFFF); //约4ms * 600 = 2.4s一次呼吸 5 s
}</code></pre>
<p>用通道4实现呼吸灯的效果</p>
<p>a284dd39250e2b92a9de80ecc7510a5b<br />
Keil的开发环境相对比较熟悉,整起来还是蛮快的。</p>
<p>工作中使用的开发环境是keil多还是IAR多</p> 秦天qintian0303 发表于 2022-7-20 08:37
工作中使用的开发环境是keil多还是IAR多
<p>国内keil多。外面差不多一样多。</p>
秦天qintian0303 发表于 2022-7-20 08:37
工作中使用的开发环境是keil多还是IAR多
<p>很少用IAR,基本都是Keil</p>
kit7828 发表于 2022-7-21 13:38
很少用IAR,基本都是Keil
<p>这个就和工作习惯相关了,真是开始决定结尾,基本都不愿意改了</p>
页:
[1]