Harmony OS 设备开发学习记录(九)-- 通过ADC值区分按键(中断方式)
基于hispark wifi套件采用harmony os 2.0全量代码
一、看原理图确定硬件电路
oled板上的两个按键
二、在源码中建立demo文件
在app下建立adcdemo文件夹并创建BUILD.gn和adc_botton_int.c文件
三、编写代码
在adcdemo/adc_botton_int.c中写入
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_gpio.h"
#include "hi_io.h"
#include "hi_adc.h"
#include "hi_errno.h"
#define MSGQUE_COUNT 1
#define MSG_SIZE 1
#define ACT_DOWN 0
#define ACT_UP 1
#define KEY2_BUTTON 2
#define KEY3_BUTTON 3
#define KEY4_BUTTON 4
#define NOKEY_BUTTON 0
static osMessageQueueId_t button_msgq;
void button_status_callback(uint8_t button_id, uint8_t button_action){
uint32_t rst;
rst = osMessageQueuePut(button_msgq, &button_id, 0, 0);
if (rst != osOK)
{
printf("button_status_callback error! \n");
return;
}
printf("button_status_callback %d\n", rst);
}
static void key234_isr_handler(char *arg)
{
hi_u16 data;
static uint8_t btn_record;
// static enum button_id btn_record;
hi_adc_read(HI_ADC_CHANNEL_2, &data, HI_ADC_EQU_MODEL_4, HI_ADC_CUR_BAIS_DEFAULT, 0);
if ((5<(int)data) && ((int)data<228)){
button_status_callback(KEY2_BUTTON, ACT_DOWN);
btn_record = KEY2_BUTTON;
}
else if ((228<(int)data) && ((int)data<455)){
button_status_callback(KEY3_BUTTON, ACT_DOWN);
btn_record = KEY3_BUTTON;
}
else if ((455<(int)data) && ((int)data<682)){
button_status_callback(KEY4_BUTTON, ACT_DOWN);
btn_record = KEY4_BUTTON;
}
if ((int)data > 1422)
{
btn_record = NOKEY_BUTTON;
button_status_callback(btn_record, ACT_UP);
hi_gpio_register_isr_function(HI_GPIO_IDX_5, HI_INT_TYPE_EDGE, HI_GPIO_EDGE_FALL_LEVEL_LOW, key234_isr_handler, NULL);
}
else
{
hi_gpio_register_isr_function(HI_GPIO_IDX_5, HI_INT_TYPE_EDGE, HI_GPIO_EDGE_RISE_LEVEL_HIGH, key234_isr_handler, NULL);
}
}
static void ButtonThreadTask(void *arg){
(void)arg;
key234_isr_handler(arg);
uint8_t value = 0;
osMessageQueueId_t msgq;
msgq = osMessageQueueNew(MSGQUE_COUNT, MSG_SIZE, NULL);
if (msgq == NULL){
printf("The key234_isr_handler falied to create button queue!\n");
return;
} else {
button_msgq = msgq;
}
while (1)
{
if (osMessageQueueGet(button_msgq, &value, 0, osWaitForever) == osOK){
printf("success! get button value %d\n", value);
}
}
}
static void AdcGpioEntry(void){
printf("ADC Test!\n");
osThreadAttr_t attr;
hi_gpio_init();
hi_io_set_func(HI_GPIO_IDX_5, HI_IO_FUNC_GPIO_5_GPIO);
hi_gpio_set_dir(HI_GPIO_IDX_5, HI_GPIO_DIR_IN);
hi_io_set_pull(HI_GPIO_IDX_5, HI_IO_PULL_UP);
attr.name = "ButtonThreadTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 1024;
attr.priority = 25;
if (osThreadNew(ButtonThreadTask, NULL, &attr) == NULL) {
printf("[LedExample] Falied to create LedTask!\n");
}
}
SYS_RUN(AdcGpioEntry);
在adcdemo/BUILD.gn中写入
static_library("adcdemo") {
sources = [
"adc_botton_int.c"
]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/components/cmsis/2.0",
"//base/iot_hardware/peripheral/interfaces/kits",
"//device/hisilicon/hispark_pegasus/sdk_liteos/include"
]
}
在上级目录的app/BUILD.gn中写入
import("//build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
"adcdemo",
]
}
四、在Linux下使用hb工具进行编译
root@DESKTOP-QAO2AOK:~/harmonyos/code-2.0-canary# hb set
[OHOS INFO] Input code path: .
OHOS Which product do you need? wifiiot_hispark_pegasus
root@DESKTOP-QAO2AOK:~/harmonyos/code-2.0-canary# hb build
如果曾经设置过hb set就不需要再设置了,直接这样就可以了
root@DESKTOP-QAO2AOK:~/harmonyos/code-2.0-canary# hb build
看到success字样即为编译成功
五、将编译好的固件烧录到开发板
将linux中的源码文件夹中的out拷贝到Windows下替换原有out文件夹就可以了,但是要先删除原有out文件夹
打开vscode使用DevEco Device Tool打开源码文件夹
选择对应的开发板型号
这里选择的是hi3861
然后在项目设置中按照实际端口情况进行如下设置
保存项目并打开
点击upload进行烧录,烧录时需要根据提示按下开发板的rst键,稍等片刻,看到success代表烧录成功。
烧录完成后打开串口调试助手
按下rst键重启开发板,这时候分别按下核心板上的usr按键,和oled板上的s1按键与s2按键可以看到串口中会输出对应按下的按键,按键弹起时会输出0。