jinglixixi 发表于 2021-8-21 00:44

【AB32VG1开发板测评】按键控制LED及风扇

本帖最后由 jinglixixi 于 2021-8-21 00:45 编辑

<p class="paragraph" style="margin-top:4px; margin-bottom:4px">在AB32VG1开发板上,配有LED和KEY,其原理图如图1所示。</p>

<p> &nbsp;</p>

<p> &nbsp;</p>

<p>图1&nbsp; LED与KEY 线路连接</p>

<p>&nbsp;</p>

<p>以KEY控制LED的程序如下:</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);
    uint8_t S2 = rt_pin_get("PF.1");
    rt_pin_mode(S2, PIN_MODE_INPUT_PULLUP );
    uint8_t S3 = rt_pin_get("PF.0");
    rt_pin_mode(S3, PIN_MODE_INPUT_PULLUP );
    uint8_t S4 = rt_pin_get("PA.2");
    rt_pin_mode(S4, PIN_MODE_INPUT_PULLUP );
    rt_pin_write(pin, PIN_HIGH);
    rt_pin_write(pin1, PIN_HIGH);
    rt_pin_write(pin2, PIN_HIGH);
    while (1)
    {
        if(rt_pin_read(S2)==PIN_LOW) {
                    rt_pin_write(pin, PIN_LOW);
        } else {
                    rt_pin_write(pin, PIN_HIGH);
        }
        if(rt_pin_read(S3)==PIN_LOW) {
                    rt_pin_write(pin1, PIN_LOW);
        } else {
                    rt_pin_write(pin1, PIN_HIGH);
        }
        if(rt_pin_read(S4)==PIN_LOW) {
                    rt_pin_write(pin2, PIN_LOW);
        } else {
                    rt_pin_write(pin2, PIN_HIGH);
        }
    }
}</code></pre>

<p>该程序的作用是以按键S2~S4分别控制LED_R、LED_G及LED_B的亮灭。</p>

<p>&nbsp;</p>

<p>在开发板上配有Arduino接口,利用它可以控制外挂的LED模块,在控制引脚连接PA0的情况下,其控制程序如下:</p>

<pre>
<code class="language-cpp">#include &lt;rtthread.h&gt;
#include "board.h"
int main(void)
{
    uint32_t cnt = 0;
    uint8_t pin0 = rt_pin_get("PA.0");
    rt_pin_mode(pin0, PIN_MODE_OUTPUT);
    while (1)
    {
        if (cnt % 2 == 0) {
            rt_pin_write(pin0, PIN_LOW);
        } else {
            rt_pin_write(pin0, PIN_HIGH);
        }
        cnt++;
        rt_thread_mdelay(500);
    }
    return 0;
}</code></pre>

<p>&nbsp;</p>

<p> &nbsp;</p>

<p>图2 驱动LED模块</p>

<p>&nbsp;</p>

<p>此外,若将LED模块换为风扇模块则可控制风扇的运转,并可以延时值来调节风扇转速。</p>

<p> &nbsp;</p>

<p>图3 驱动风扇模块</p>

littleshrimp 发表于 2021-8-21 08:07

<p>下边是通过延时调节I/O模拟 PWM的占空比?</p>

littleshrimp 发表于 2021-8-21 08:09

littleshrimp 发表于 2021-8-21 08:07
下边是通过延时调节I/O模拟 PWM的占空比?

<p>看错了 好像是通过延时调节频率</p>

jinglixixi 发表于 2021-8-21 10:11

littleshrimp 发表于 2021-8-21 08:07
下边是通过延时调节I/O模拟 PWM的占空比?

<p>道理相近</p>

OS001 发表于 2021-8-23 15:51

<p>了解更多智能硬件相关产品~请戳:【华为开发者联盟生态市场智能硬件专区https://developer.huawei.com/consumer/cn/market/prod-list?categoryIdL1=7e857be8d4b24a7e97ddf5d941b38ca1&amp;ha_source=mkt-thirdforum】</p>

freebsder 发表于 2021-8-23 17:13

<p>看来楼主买了不少模块啊</p>

jinglixixi 发表于 2021-8-24 00:21

freebsder 发表于 2021-8-23 17:13
看来楼主买了不少模块啊

<p>哈哈!</p>

aozima 发表于 2021-8-24 00:33

<p>调低占空比,终于可以不晃眼了!</p>

<p>470R的电阻,灯太亮</p>
页: [1]
查看完整版本: 【AB32VG1开发板测评】按键控制LED及风扇