【RTT大賽作品連載】AB32VG1開發板——按鍵掃描
1. 實驗目的
讓板載 三個用戶按鍵,通過掃描按鍵,打印按鍵值。
2. 開發板硬件平臺

按鍵硬件電路,可知,使用 按鍵要使用
PF1 —— S2
PF0 —— S3
PAS —— S4

3. 軟件編寫
在 application 目錄下,新建 key.c 和 key.h
//key.c
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-11-06 Administrator the first version
*/
#include "key.h"
#include
#include "board.h"
struct key_s
{
uint8_t k0;
uint8_t k1;
uint8_t k2;
};
struct key_s button;
//PF0 PF1 PA2
static int key_init(void)
{
button.k0 = rt_pin_get("PF.0");
button.k1 = rt_pin_get("PF.1");
rt_pin_mode(button.k0, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(button.k1, PIN_MODE_INPUT_PULLUP);
rt_kprintf("key init\n");
return 0;
}
#define KEY0_PRES 1
#define KEY1_PRES 2
static uint8_t btn_scan(uint8_t mode)
{
static uint8_t key_up = 1;
if(mode)
key_up = 1;
if(key_up && ((rt_pin_read(button.k0) == PIN_LOW) || (rt_pin_read(button.k1) == PIN_LOW)))
{
rt_thread_mdelay(10);
key_up = 0;
if(rt_pin_read(button.k0) == PIN_LOW)
return KEY0_PRES;
else if(rt_pin_read(button.k1) == PIN_LOW)
return KEY1_PRES;
}else if((rt_pin_read(button.k0) == PIN_HIGH) && (rt_pin_read(button.k1) == PIN_HIGH))
key_up = 1;
return 0;
}
static void btn_thread_entry(void* p)
{
uint8_t byn_value = 0;
while(1)
{
byn_value = btn_scan(0);
switch(byn_value)
{
case KEY0_PRES:
rt_kprintf("key0 pushed\n");
break;
case KEY1_PRES:
rt_kprintf("key1 pushed\n");
break;
default:
break;
}
rt_thread_mdelay(100);
}
}
static int Thread_btn(void)
{
rt_thread_t thread = RT_NULL;
key_init();
thread = rt_thread_create("button", btn_thread_entry, RT_NULL, 512, 11, 10);
if(thread == RT_NULL)
{
rt_kprintf("Thread_btn Init ERROR");
return RT_ERROR;
}
rt_thread_startup(thread);
}
INIT_APP_EXPORT(Thread_btn);
//key.h
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-11-06 Administrator the first version
*/
#ifndef APPLICATIONS_KEY_H_
#define APPLICATIONS_KEY_H_
#endif /* APPLICATIONS_KEY_H_ */
修改main.c
/*
* Copyright (c) 2020-2021, Bluetrum Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020/12/10 greedyhao The first version
*/
/**
* Notice!
* All functions or data that are called during an interrupt need to be in RAM.
* You can do it the way exception_isr() does.
*/
#include
#include "board.h"
int main(void)
{
rt_kprintf("Hello, world\n");
while (1)
{
rt_thread_mdelay(500);
rt_thread_mdelay(500);
}
}
4. 程序編譯下載
編譯程序,點擊小錘子即可。

下載程序
打開我們下載的Downloader軟件,雙擊Downloader.exe
配置連接串口,千萬不要搞錯串口號哦。
選擇工程目錄下的\Debug\rtthread.dcf,這里我的完整目錄是
D:\RT-ThreadStudio\workspace\ab32vg1_demo\Debug\rtthread.dcf
點擊開始即可下載成功
5. 實驗現象:
download 串口打印 msh 控制臺信息。
按鍵觸發打印,不支持連按。

6. 總結
目前是通過引腳輪詢讀取來實現按鍵掃描,后面可以試試IO中斷的方式實現,哈哈。
編輯:fqj
-
單片機
+關注
關注
6067文章
44983瀏覽量
650320 -
開發板
+關注
關注
25文章
5676瀏覽量
104576 -
RTT
+關注
關注
0文章
66瀏覽量
17649 -
中科藍訊
+關注
關注
9文章
61瀏覽量
10247
發布評論請先 登錄
上手體驗安信可小安派AI開發板,如何接入Home Assistant?

有ARM,NPU,FPGA三種核心的開發板 — 米爾安路飛龍派開發板

【新品】遠距離圖傳數傳模塊開發板、藍牙模塊開發板、無線模塊開發板

【免費試用】倒計時六天!RISC-V、OH、RK開發板等你來申請!

【免費試用】開發板評測大賽開啟!OH 、RISC-V、Rockchip頂級開發板等你試用~

T527開發板+OpenHarmony評測大賽邀你來戰!0元參與,贏百元禮品

一個不錯的活動:電子發燒友開發板評測大賽

2025電子發燒友開發板評測大賽開啟!三大賽道火力全開,頂級開發板等你來戰!

免費送30套開發板!米爾-安路飛龍派創意秀限時活動

正點原子fpga開發板不同型號
【干貨】性價比拉滿!HK32F407VG開發板介紹

評論