【RTT大賽作品連載】AB32VG1開發(fā)板——按鍵掃描
1. 實驗?zāi)康?/strong>
讓板載 三個用戶按鍵,通過掃描按鍵,打印按鍵值。
2. 開發(fā)板硬件平臺
按鍵硬件電路,可知,使用 按鍵要使用
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. 程序編譯下載
編譯程序,點(diǎn)擊小錘子即可。
下載程序
打開我們下載的Downloader軟件,雙擊Downloader.exe
配置連接串口,千萬不要搞錯串口號哦。
選擇工程目錄下的\Debug\rtthread.dcf,這里我的完整目錄是
D:\RT-ThreadStudio\workspace\ab32vg1_demo\Debug\rtthread.dcf
點(diǎn)擊開始即可下載成功
5. 實驗現(xiàn)象:
download 串口打印 msh 控制臺信息。
按鍵觸發(fā)打印,不支持連按。
6. 總結(jié)
目前是通過引腳輪詢讀取來實現(xiàn)按鍵掃描,后面可以試試IO中斷的方式實現(xiàn),哈哈。
編輯:fqj
-
單片機(jī)
+關(guān)注
關(guān)注
6030文章
44491瀏覽量
632031 -
開發(fā)板
+關(guān)注
關(guān)注
25文章
4900瀏覽量
97068 -
RTT
+關(guān)注
關(guān)注
0文章
65瀏覽量
17060 -
中科藍(lán)訊
+關(guān)注
關(guān)注
9文章
53瀏覽量
9841
發(fā)布評論請先 登錄
相關(guān)推薦
評論