聚丰项目 > 按键控制LED

按键控制LED

基于AB32VG1的按键控制LED显示的实验,本实验材料较为简单,仅仅运用了GPIO的输出和输入功能,但是本套设备重点在于培养C语言的能力。运用本套设备可以实现嵌入式单片机开发的多种功能,比如,按键中断,定时器,GPIO输入输出,可以训练我们对C语言中各种循环和函数的使用,增强我们的C语言基础能力,为后续的开发打好基础。 本作品的功能有:1按键控制LED亮灭。2按键控制LED更换颜色。3按键控制LED在正常闪烁和PWM呼吸灯之间切换。

jf_71872534 jf_71872534

分享
0 喜欢这个项目
团队介绍

jf_71872534 jf_71872534

团队成员

jf_71872534 工程师

分享
项目简介
基于AB32VG1的按键控制LED显示的实验,本实验材料较为简单,仅仅运用了GPIO的输出和输入功能,但是本套设备重点在于培养C语言的能力。运用本套设备可以实现嵌入式单片机开发的多种功能,比如,按键中断,定时器,GPIO输入输出,可以训练我们对C语言中各种循环和函数的使用,增强我们的C语言基础能力,为后续的开发打好基础。 本作品的功能有:1按键控制LED亮灭。2按键控制LED更换颜色。3按键控制LED在正常闪烁和PWM呼吸灯之间切换。
硬件说明

硬件: AB32VG1开发板,

板上资源:CPUAB5301A;(LQFP48 封装,主频 120M,片上集成 RAM 192K, flash 8MbitADCPWMUSBUARTIIC 等资源)

一路 TF Card 接口,用来插入sd卡,存储要播放的wav音频文件

一路 USB 接口,用来下载更新代码

一路音频接口(美标 CTIA),用来外接音响设备播放声音。

六路 PWM 输出引脚端子引出,用来控制卧室灯光调光调色。

一个全彩 LED 灯模块,一个电源指示灯,三个烧录指示灯,用来做指示。

一个 Reset 按键,三个功能按键(通用版为两个功能按键),用于复位。

板子规格尺寸:6cm*9cm

I/O 口通过 2.54MM 标准间距引出,同时兼容 Arduino Uno 扩展接口

image.png

软件说明

开发环境:

开发工具及版本:RT-Thread Studio 2.0.1

平台Windows 10

RT-Thread系统:RT-Thread标准版

RT-Thread版本:RT-Thread V 4.0.3

组件部分:LED、PWM、串口、按键 


#include <rtthread.h>
#include "board.h"
int main(void)
{
    uint8_t green = rt_pin_get("PE.4");             //
定义绿色LED灯的引脚,uint8_t为数据类型
    uint8_t blue = rt_pin_get("PA.1");             //定义绿色LED灯的引脚,uint8_t为数据类型
    uint8_t red = rt_pin_get("PE.1");             //定义绿色LED灯的引脚,uint8_t为数据类型
    uint8_t key = rt_pin_get("PF.1");               //定义按键引脚
    rt_pin_mode(green, PIN_MODE_OUTPUT);            //定义LED为输出
    rt_pin_mode(red, PIN_MODE_OUTPUT);            //定义LED为输出
    rt_pin_mode(blue, PIN_MODE_OUTPUT);            //定义LED为输出
    rt_pin_mode(key, PIN_MODE_INPUT_PULLUP );       //定义按键为输入
    rt_pin_write(green, PIN_HIGH);                  //将LED置为高电平
    int16_t i;
    i = 0;
    /*while(1){
            if(rt_pin_read(key) == 1)
                {
                    rt_pin_write(green, PIN_LOW);
                }
            if(rt_pin_read(key) == 0)
               {
                    rt_pin_write(green, PIN_HIGH);
               }
    }//while循环要有,使程序一直运行*/
    while(1){
        if(rt_pin_read(key) == 0)
        {
            rt_thread_mdelay(500);
            i = 1+i;
        }
        if (i == 9) {
            i = 0;
        }
        if(i == 1)
        {
            rt_pin_write(red, PIN_LOW);
            rt_pin_write(green, PIN_HIGH);
            rt_pin_write(blue, PIN_HIGH);
        }
        if (i == 2)
        {
            rt_pin_write(red, PIN_HIGH);
            rt_pin_write(green, PIN_LOW);
            rt_pin_write(blue, PIN_HIGH);
        }
        if(i == 3)
        {
            rt_pin_write(red, PIN_HIGH);
            rt_pin_write(green, PIN_HIGH);
            rt_pin_write(blue, PIN_LOW);
        }
        if(i == 4)
        {
            rt_pin_write(red, PIN_LOW);
            rt_pin_write(green, PIN_LOW);
            rt_pin_write(blue, PIN_HIGH);
        }
        if(i == 5)
        {
            rt_pin_write(red, PIN_LOW);
            rt_pin_write(green, PIN_HIGH);
            rt_pin_write(blue, PIN_LOW);
        }
        if(i == 6)
        {
            rt_pin_write(red, PIN_HIGH);
            rt_pin_write(green, PIN_LOW);
            rt_pin_write(blue, PIN_LOW);
        }
        if(i == 7)
        {
            rt_pin_write(red, PIN_LOW);
            rt_pin_write(green, PIN_LOW);
            rt_pin_write(blue, PIN_LOW);
        }
        if(i == 8)
        {
            rt_thread_mdelay(2000);
                    rt_pin_write(red, PIN_HIGH);
                    rt_pin_write(green, PIN_HIGH);
                    rt_pin_write(blue, PIN_HIGH);
                    //红1
                    rt_pin_write(red, PIN_LOW);
                    rt_thread_mdelay(500);
                    rt_pin_write(red, PIN_HIGH);
                    rt_thread_mdelay(500);
                    //绿2
                    rt_pin_write(green, PIN_LOW);
                    rt_thread_mdelay(500);
                    rt_pin_write(green, PIN_HIGH);
                    rt_thread_mdelay(500);
                    //蓝3
                    rt_pin_write(blue, PIN_LOW);
                    rt_thread_mdelay(500);
                    rt_pin_write(blue, PIN_HIGH);
                    rt_thread_mdelay(500);
                    //黄1+3
                    rt_pin_write(blue, PIN_LOW);
                    rt_pin_write(red, PIN_LOW);
                    rt_thread_mdelay(500);
                    rt_pin_write(blue, PIN_HIGH);
                    rt_pin_write(red, PIN_HIGH);
                    rt_thread_mdelay(500);
                    //紫2+3
                    rt_pin_write(blue, PIN_LOW);
                    rt_pin_write(green, PIN_LOW);
                    rt_thread_mdelay(500);
                    rt_pin_write(blue, PIN_HIGH);
                    rt_pin_write(green, PIN_HIGH);
                    rt_thread_mdelay(500);
                    //1+2
                    rt_pin_write(red, PIN_LOW);
                    rt_pin_write(green, PIN_LOW);
                    rt_thread_mdelay(500);
                    rt_pin_write(red, PIN_HIGH);
                    rt_pin_write(green, PIN_HIGH);
                    rt_thread_mdelay(500);
                    //1+2+3
                    rt_pin_write(red, PIN_LOW);
                    rt_pin_write(green, PIN_LOW);
                    rt_pin_write(blue, PIN_LOW);
                    rt_thread_mdelay(500);
                    rt_pin_write(red, PIN_HIGH);
                    rt_pin_write(green, PIN_HIGH);
                    rt_pin_write(blue, PIN_HIGH);
                    rt_thread_mdelay(500);
        }


    }
}


演示效果

IMG_1288.JPG视频需下载

Gitee项目托管地址:https://gitee.com/im-zheng-wei/rt_-thread.git

附件

(0.00 MB)下载

评论区(0 )