【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
评论