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