jinglixixi 发表于 2021-8-11 22:56

【AB32VG1开发板测评】RGB_LED的色彩变换显示

<p class="paragraph" style="margin-top:4px; margin-bottom:4px; text-align:justify">AB32VG1开发板载有一个RGB_LED,其原理图见图1所示。</p>

<p> &nbsp;</p>

<p> &nbsp;</p>

<p>图1 RGB_LED电路连接</p>

<p>按照色彩叠加的原理,可以组合出黄色、亮蓝、粉色及白色。</p>

<p>实现色彩变换效果的程序如下:</p>

<pre>
<code class="language-cpp">int main(void)
{
    uint32_t cnt = 0;
    uint8_t pin = rt_pin_get("PE.1");
    rt_pin_mode(pin, PIN_MODE_OUTPUT);
    uint8_t pin1 = rt_pin_get("PE.4");
    rt_pin_mode(pin1, PIN_MODE_OUTPUT);
    uint8_t pin2 = rt_pin_get("PA.1");
    rt_pin_mode(pin2, PIN_MODE_OUTPUT);
    rt_pin_write(pin, PIN_HIGH);
    rt_pin_write(pin1, PIN_HIGH);
    rt_pin_write(pin2, PIN_HIGH);
    while (1)
    {
        if(cnt % 8 == 0)
        {
            rt_pin_write(pin, PIN_LOW);
            rt_pin_write(pin1, PIN_HIGH);
            rt_pin_write(pin2, PIN_HIGH);
        }
        if(cnt % 8 == 1)
        {
            rt_pin_write(pin, PIN_HIGH);
            rt_pin_write(pin1, PIN_LOW);
            rt_pin_write(pin2, PIN_HIGH);
        }
        if(cnt % 8 == 2)
        {
            rt_pin_write(pin, PIN_HIGH);
            rt_pin_write(pin1, PIN_HIGH);
            rt_pin_write(pin2, PIN_LOW);
        }
        if(cnt % 8 == 3)
        {
            rt_pin_write(pin, PIN_LOW);
            rt_pin_write(pin1, PIN_LOW);
            rt_pin_write(pin2, PIN_HIGH);
        }
        if(cnt % 8 == 4)
        {
            rt_pin_write(pin, PIN_HIGH);
            rt_pin_write(pin1, PIN_LOW);
            rt_pin_write(pin2, PIN_LOW);
        }
        if(cnt % 8 == 5)
        {
            rt_pin_write(pin, PIN_LOW);
            rt_pin_write(pin1, PIN_HIGH);
            rt_pin_write(pin2, PIN_LOW);
        }
        if(cnt % 8 == 6)
        {
            rt_pin_write(pin, PIN_LOW);
            rt_pin_write(pin1, PIN_LOW);
            rt_pin_write(pin2, PIN_LOW);
        }
        if(cnt % 8 == 7)
        {
            rt_pin_write(pin, PIN_HIGH);
            rt_pin_write(pin1, PIN_HIGH);
            rt_pin_write(pin2, PIN_HIGH);
        }
        cnt++;
        rt_thread_mdelay(1000);
    }
    return 0;
}</code></pre>

<p>&nbsp;</p>

<p> &nbsp;</p>

<p> &nbsp;</p>

<p> &nbsp;</p>

<p> &nbsp;</p>

<p> &nbsp;</p>

<p> &nbsp;</p>

<p> &nbsp;</p>

<p>图2 演示效果图</p>

dcexpert 发表于 2021-8-12 09:37

<p>效果不错,感觉程序可以优化一下,不用写8个if,根据cnt每个二进制位就可以设置LED状态,这样只需要3个if语句。</p>

annysky2012 发表于 2021-8-12 11:50

效果不错,感觉程序可以优化一下,不用写8个if,根据cnt每个二进制位就可以设置LED状态,这样只需要3个if语句。


有道理!

jinglixixi 发表于 2021-8-12 14:28

dcexpert 发表于 2021-8-12 09:37
效果不错,感觉程序可以优化一下,不用写8个if,根据cnt每个二进制位就可以设置LED状态,这样只需要3个if语 ...

<p>有道理,回头试一下。</p>

freebsder 发表于 2021-8-12 17:15

<p>为啥pin脚不放三个相邻的好控制呀</p>

jinglixixi 发表于 2021-8-12 18:21

freebsder 发表于 2021-8-12 17:15
为啥pin脚不放三个相邻的好控制呀

<p>这个还真说不好,我想可能与Arduino接口的设计有关,PE1和PE4都在芯片的同侧,却与CLK和SDA相邻被分开了,而PA1可能是按Arduino接口分配好引脚后,为保持这3个引脚均是PWM引脚而选择的结果。我分析有这样的因素,不一定准确。</p>
页: [1]
查看完整版本: 【AB32VG1开发板测评】RGB_LED的色彩变换显示