聚豐項(xiàng)目 > 按鍵控制LED
基于AB32VG1的按鍵控制LED顯示的實(shí)驗(yàn),本實(shí)驗(yàn)材料較為簡(jiǎn)單,僅僅運(yùn)用了GPIO的輸出和輸入功能,但是本套設(shè)備重點(diǎn)在于培養(yǎng)C語(yǔ)言的能力。運(yùn)用本套設(shè)備可以實(shí)現(xiàn)嵌入式單片機(jī)開(kāi)發(fā)的多種功能,比如,按鍵中斷,定時(shí)器,GPIO輸入輸出,可以訓(xùn)練我們對(duì)C語(yǔ)言中各種循環(huán)和函數(shù)的使用,增強(qiáng)我們的C語(yǔ)言基礎(chǔ)能力,為后續(xù)的開(kāi)發(fā)打好基礎(chǔ)。 本作品的功能有:1按鍵控制LED亮滅。2按鍵控制LED更換顏色。3按鍵控制LED在正常閃爍和PWM呼吸燈之間切換。
jf_71872534
分享jf_71872534
團(tuán)隊(duì)成員
jf_71872534 工程師
硬件: AB32VG1開(kāi)發(fā)板,
板上資源:CPU:AB5301A;(LQFP48 封裝,主頻 120M,片上集成 RAM 192K, flash 8Mbit,ADC,PWM,USB,UART,IIC 等資源)
一路 TF Card 接口,用來(lái)插入sd卡,存儲(chǔ)要播放的wav音頻文件
一路 USB 接口,用來(lái)下載更新代碼
一路音頻接口(美標(biāo) CTIA),用來(lái)外接音響設(shè)備播放聲音。
六路 PWM 輸出引腳端子引出,用來(lái)控制臥室燈光調(diào)光調(diào)色。
一個(gè)全彩 LED 燈模塊,一個(gè)電源指示燈,三個(gè)燒錄指示燈,用來(lái)做指示。
一個(gè) Reset 按鍵,三個(gè)功能按鍵(通用版為兩個(gè)功能按鍵),用于復(fù)位。
板子規(guī)格尺寸:6cm*9cm
I/O 口通過(guò) 2.54MM 標(biāo)準(zhǔn)間距引出,同時(shí)兼容 Arduino Uno 擴(kuò)展接口
開(kāi)發(fā)環(huán)境:
開(kāi)發(fā)工具及版本:RT-Thread Studio 2.0.1
平臺(tái):Windows 10
RT-Thread系統(tǒng):RT-Thread標(biāo)準(zhǔn)版
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為數(shù)據(jù)類(lèi)型
uint8_t blue = rt_pin_get("PA.1"); //定義綠色LED燈的引腳,uint8_t為數(shù)據(jù)類(lèi)型
uint8_t red = rt_pin_get("PE.1"); //定義綠色LED燈的引腳,uint8_t為數(shù)據(jù)類(lèi)型
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循環(huán)要有,使程序一直運(yùn)行*/
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);
//藍(lán)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);
}
}
}
視頻需下載
Gitee項(xiàng)目托管地址:https://gitee.com/im-zheng-wei/rt_-thread.git
(0.00 MB)下載