skyworth74 发表于 2022-3-12 08:46

GD32L233C TRNG 处理和PWM配置(II)

<p>&nbsp; &nbsp; &nbsp;前几天发了TRNG的生成调试,今天继续PWM使用,做智能LED灯要控制LED生成不同的颜色,离不开PWM,通过PWM控制RGBW不同占空比就可以使LED灯出现不同的颜色和等效,比如颜色渐变,灯的呼吸,甚至模拟出不同太阳的生物节律灯,让LED跟随日出日落的颜色变化成为可能。现在就讲如何实现PWM配置。PWM要占用timer,为了减少定时器的占用,尽可能选择一个timer可以控制多个GPIO的定时器,查看GD233C的SPEC,可以看到Timer2 可以支持4路PWM输出,正好符合LED灯RGBW四色LED的控制。</p>

<p></p>

<p>选择好定时器后可以开始配置定时器</p>

<p>void timer_config(void)<br />
{<br />
&nbsp;&nbsp; &nbsp;timer_parameter_struct timer_struct;<br />
&nbsp;&nbsp; &nbsp;timer_oc_parameter_struct timer_ocintpara;<br />
&nbsp;&nbsp; &nbsp;rcu_periph_clock_enable(RCU_TIMER6);<br />
&nbsp;&nbsp; &nbsp;timer_struct_para_init(&amp;timer_struct );<br />
&nbsp;&nbsp; &nbsp;timer_struct.period =999;//1000<br />
&nbsp;&nbsp; &nbsp;timer_struct.prescaler = 47;//63;//TIMER_EXT_TRI_PSC_DIV8;<br />
&nbsp;&nbsp; &nbsp;timer_struct.counterdirection = TIMER_COUNTER_UP;<br />
&nbsp;&nbsp; &nbsp;timer_struct.alignedmode = TIMER_COUNTER_EDGE;<br />
&nbsp;&nbsp; &nbsp;timer_struct.clockdivision = TIMER_CKDIV_DIV1;<br />
&nbsp;&nbsp; &nbsp;timer_deinit(TIMER6);<br />
&nbsp;&nbsp; &nbsp;timer_init(TIMER6,&amp;timer_struct);<br />
&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;//NVIC_SetPriority(TIMER6_IRQn, 0x02U);<br />
&nbsp;&nbsp; &nbsp;timer_interrupt_enable(TIMER6,TIMER_INT_UP);<br />
&nbsp;&nbsp; &nbsp;nvic_irq_enable(TIMER6_IRQn, 0x00U);<br />
&nbsp;&nbsp; &nbsp;timer_enable(TIMER6);<br />
&nbsp;&nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp;rcu_periph_clock_enable(RCU_TIMER2);<br />
&nbsp;&nbsp; &nbsp;timer_struct_para_init(&amp;timer_struct );<br />
&nbsp;&nbsp; &nbsp;timer_struct.period = 255;//255;//1000<br />
&nbsp;&nbsp; &nbsp;timer_struct.prescaler = 479;//639;//TIMER_EXT_TRI_PSC_DIV8;<br />
&nbsp;&nbsp; &nbsp;timer_struct.counterdirection = TIMER_COUNTER_UP;<br />
&nbsp;&nbsp; &nbsp;timer_struct.alignedmode = TIMER_COUNTER_EDGE;<br />
&nbsp;&nbsp; &nbsp;timer_struct.clockdivision = TIMER_CKDIV_DIV1;<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_struct_para_init(&amp;timer_ocintpara);<br />
&nbsp;&nbsp; &nbsp;timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;<br />
&nbsp;&nbsp; &nbsp;timer_ocintpara.outputstate = TIMER_CCX_ENABLE;<br />
&nbsp;&nbsp; &nbsp;timer_deinit(TIMER2);<br />
&nbsp;&nbsp; &nbsp;timer_init(TIMER2,&amp;timer_struct);<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_config(TIMER2,TIMER_CH_0,&amp;timer_ocintpara);//PA6 RED<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_config(TIMER2,TIMER_CH_1,&amp;timer_ocintpara);//PA7//GREEN LED1&nbsp;<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_config(TIMER2,TIMER_CH_2,&amp;timer_ocintpara);//PB0 BLUE<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_config(TIMER2,TIMER_CH_3,&amp;timer_ocintpara);//PB1 YELLOW<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_0, 0);<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, 0);<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_2, 0);<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_3, 0);<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_mode_config(TIMER2, TIMER_CH_0, TIMER_OC_MODE_PWM0);<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_mode_config(TIMER2, TIMER_CH_1, TIMER_OC_MODE_PWM0);<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_mode_config(TIMER2, TIMER_CH_2, TIMER_OC_MODE_PWM0);<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_mode_config(TIMER2, TIMER_CH_3, TIMER_OC_MODE_PWM0);<br />
&nbsp;&nbsp; &nbsp;timer_auto_reload_shadow_enable(TIMER2);<br />
&nbsp;&nbsp; &nbsp;timer_enable(TIMER2);<br />
&nbsp;&nbsp; &nbsp;<br />
}</p>

<p>按如上配置每路PWMperiod 设置为255,可以实现LED8bit 256级输出,理论上可以产生255X255X255=16,581,375种颜色输出</p>

<p>接下来是封装一个pwm输出接口</p>

<p>void gpio_pwm(xt_rgb duty){<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_0, duty.rgb_r_pwm);<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, duty.rgb_g_pwm);<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_2, duty.rgb_b_pwm);<br />
&nbsp;&nbsp; &nbsp;timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_3, duty.rgb_w_pwm);&nbsp;&nbsp; &nbsp;<br />
}</p>

<p>把rgbw 的数据通过一个结构体传递给定时器就可以控制4路LED输出。</p>

<p>网络上的渐变和呼吸LED使用的都是简单的在一个循环通过延时控制pwm递增递减实现,这个只适合最演示效果,要实现渐变和呼吸等效</p>

<p>单独使用一个定时器,通过这个定时器实现更加精准的渐变和呼吸等效,这里采用timer6实现</p>

<p></p>

<p>实现1u秒的延时,通过这个定时器封装一类定时功能的函数,实现定时器开启,定时器失效等功能,这个定时器不是使用简单的delay()功能,执行更高效</p>

<p></p>

<p>搭好以上基础设施后就可以编写灯的各种功能实现渐变开机渐变关机,呼吸等等</p>

<p>举个简单的栗子:</p>

<p>&nbsp;&nbsp; &nbsp; &nbsp; case LIGHT_BLUE:<br />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;if(light_state.is_start==XTRUE){<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;if (pdTRUE==result){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;target_pwm.rgb_r_pwm=0;//?????<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;target_pwm.rgb_g_pwm=0;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;target_pwm.rgb_b_pwm=PWM(255);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;XtimerStop(sg_pwm_timer);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//light_state.cct = 2700;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;gradient_time_ms=light_state.gradient_time_ms;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//light_pwm_send(light_pwm);<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//time_previous = 0; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp; &nbsp; if (XTRUE==set_pwm(&amp;light_pwm,&amp;target_pwm,&amp;gradient_time_ms,&amp;light_state, &amp;time)){<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; step =1;<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; gradient_time_ms=light_state.gradient_time_ms;<br />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; light_state_response(light_state);<br />
&nbsp;&nbsp; &nbsp; &nbsp; }</p>

<p><iframe allowfullscreen="true" frameborder="0" height="510" src="https://training.eeworld.com.cn/shareOpenCourseAPI?isauto=true&amp;lessonid=32768" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
&nbsp;</p>
页: [1]
查看完整版本: GD32L233C TRNG 处理和PWM配置(II)