【GD32L233C-START评测】四、定时器PWM应用之驱动RGB灯
<p>定时器PWM是MCU最常用的外设之一,GD32L233C具有多个定时器和PWM输出通道,定时器资源如下所示:</p><p> </p>
<p></p>
<p>通过管脚复用可以知道哪些IO具有PWM功能:</p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p> </p>
<p><span style="color:#e74c3c;"><strong><span style="font-size:20px;">GD32L233C-START开发板说是兼容arduino uno接口(arduino uno接口的好处不就是有大量arduino生态的扩展板能够即插即用嘛),但是并没有完全支持,只能说这个开发板设计的</span></strong></span><span style="color:#2ecc71;"><strong><span style="font-size:20px;">很水</span></strong></span><span style="color:#e74c3c;"><strong><span style="font-size:20px;">,GD32L233C能输出PWM的IO远多于6个,可是在开发板上并没有按照arduino uno接口的标准将具有PWM功能的IO连接到D3、D5、D6、D9、D10、D11。。。只支持了D3、D9、D11。。。这意味着当arduino扩展板需要3路以上PWM控制信号时,无法即插即用。</span></strong></span></p>
<p><strong><span style="font-size:20px;"><span style="color:#e74c3c;">此外,串口有好几个,没有必要将开发板上的USB调试串口和arduino uno接口的D0、D1复用。</span></span></strong></p>
<p>标准arduino uno r3接口:</p>
<p></p>
<p>为了测试PWM功能,使用涂鸦的兼容arduino uno r3的RGB扩展板:</p>
<p>RGB扩展板插在GD32L233C-START上的效果</p>
<p>根据GD32L233C-START开发板的原理图,能够使用PWM的只有PB15、PB0、PB5,遗憾:</p>
<p></p>
<p>涂鸦三明治(PWM)照明功能板是一款支持彩灯五路调光的 LED 控制器。照明功能板(PWM)可实现五路照明功能,带有暖光,冷白及 RGB 灯珠和相应的控制芯片。</p>
<ul>
<li>冷白,暖白控制采用 SLM211A DC-DC 降压型 PWM 线性恒流调光控制芯片。</li>
<li>RGB 控制采用 BP168CJ DC-DC 降压型 PWM 线性恒流控制芯片。</li>
</ul>
<table>
<thead>
<tr>
<th>I/O</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>PWMC</td>
<td>冷白 PWM 控制信号,高电平有效</td>
</tr>
<tr>
<td>PWMW</td>
<td>暖白 PWM 控制信号,高电平有效</td>
</tr>
<tr>
<td>PWMR</td>
<td>RGB 红光控制信号,高电平有效</td>
</tr>
<tr>
<td>PWMG</td>
<td>RGB 绿光控制信号,高电平有效</td>
</tr>
<tr>
<td>PWMB</td>
<td>RGB 蓝光控制信号,高电平有效</td>
</tr>
</tbody>
</table>
<h2> </h2>
<h2 id="title-3-%E7%94%B5%E6%BA%90%E6%8A%80%E6%9C%AF%E8%A6%81%E6%B1%82"><span style="font-size:16px;">3个控制管脚对应的定时器和PWM通道如下:</span></h2>
<p>PB15 --- timer11-ch1 pwmR<br />
PB5 --- timer2-ch1 pwmB<br />
PB0 --- timer2-ch2 pwmW</p>
<p> </p>
<p>gd32的API设计相比stm32用起来要舒服的多,很简洁,易于使用:</p>
<p>main.c测试代码</p>
<pre>
<code class="language-cpp">#include "gd32l23x.h"
#include "systick.h"
// PB15 --- timer11-ch1 pwmR
// PB5--- timer2-ch1pwmB
// PB0--- timer2-ch2pwmW
void gpio_config(void)
{
rcu_periph_clock_enable(RCU_GPIOB);
/* TIMER2 & TIMER11 GPIO */
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0 | GPIO_PIN_5 | GPIO_PIN_15);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0 | GPIO_PIN_5 | GPIO_PIN_15);
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_0);
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_5);
gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_15);
}
void timer2_config(void)
{
timer_oc_parameter_struct timer_ocinitpara;
timer_parameter_struct timer_initpara;
/* enable the peripherals clock */
rcu_periph_clock_enable(RCU_TIMER2);
/* deinit a TIMER */
timer_deinit(TIMER2);
/* initialize TIMER init parameter struct */
timer_struct_para_init(&timer_initpara);
/* TIMER2 configuration */
timer_initpara.prescaler = 63;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 9999;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER2, &timer_initpara);
/* initialize TIMER channel output parameter struct */
timer_channel_output_struct_para_init(&timer_ocinitpara);
/* configure TIMER channel output function */
timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_channel_output_config(TIMER2, TIMER_CH_1, &timer_ocinitpara);
timer_channel_output_config(TIMER2, TIMER_CH_2, &timer_ocinitpara);
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, 0);
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_2, 0);
/* CH0 configuration in OC timing mode */
timer_channel_output_mode_config(TIMER2, TIMER_CH_1, TIMER_OC_MODE_PWM0);
timer_channel_output_mode_config(TIMER2, TIMER_CH_2, TIMER_OC_MODE_PWM0);
}
void timer11_config(void)
{
timer_oc_parameter_struct timer_ocinitpara;
timer_parameter_struct timer_initpara;
/* enable the peripherals clock */
rcu_periph_clock_enable(RCU_TIMER11);
/* deinit a TIMER */
timer_deinit(TIMER11);
/* initialize TIMER init parameter struct */
timer_struct_para_init(&timer_initpara);
/* TIMER2 configuration */
timer_initpara.prescaler = 63;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 9999;
timer_initpara.clockdivision = TIMER_CKDIV_DIV2;
timer_init(TIMER11, &timer_initpara);
/* initialize TIMER channel output parameter struct */
timer_channel_output_struct_para_init(&timer_ocinitpara);
/* configure TIMER channel output function */
timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_channel_output_config(TIMER11, TIMER_CH_1, &timer_ocinitpara);
timer_channel_output_pulse_value_config(TIMER11, TIMER_CH_1, 0);
/* CH0 configuration in OC timing mode */
timer_channel_output_mode_config(TIMER11, TIMER_CH_1, TIMER_OC_MODE_PWM0);
}
int main(void)
{
systick_config();
gpio_config();
timer2_config();
timer11_config();
while(1)
{
/* enable a TIMER */
timer_enable(TIMER11);
//pwmR test
for(int i =0;i<1000;i++)
{
delay_1ms(2);
timer_channel_output_pulse_value_config(TIMER11, TIMER_CH_1, i*10);
}
for(int i =999;i>0;i--)
{
delay_1ms(2);
timer_channel_output_pulse_value_config(TIMER11, TIMER_CH_1, i*10);
}
timer_channel_output_pulse_value_config(TIMER11, TIMER_CH_1, 0);
timer_disable(TIMER11);
//pwmR end
timer_enable(TIMER2);
//pwmB test
for(int i =0;i<1000;i++)
{
delay_1ms(2);
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, i*10);
}
for(int i =999;i>0;i--)
{
delay_1ms(2);
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, i*10);
}
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, 0);
//pwmB end
//pwmW test
for(int i =0;i<1000;i++)
{
delay_1ms(2);
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_2, i*10);
}
for(int i =999;i>0;i--)
{
delay_1ms(2);
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_2, i*10);
}
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_2, 0);
timer_disable(TIMER2);
//pwmW end
}
}</code></pre>
<p>驱动效果:</p>
<p></p>
<p></p>
<p></p>
<p> </p>
<p><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=1L3411L7im&page=1" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
</p>
<p> </p>
<p>GD32L233C能输出PWM的IO远多于6个,PWM的IO口不少</p>
页:
[1]