描述
本文来源电子发烧友社区,作者:hehung, 帖子地址:
https://bbs.elecfans.com/jishu_2013493_1_1.html有一周多没有时间弄这个板子了,这段时间实在是有点忙,这周末抓紧了点时间弄了弄。资料确实有点少了,只能一点一点的摸索。弄了一个按键控制LED的程序。核心板上有一个LED以及按键,我们查看原理图可以知道,LED连接到GPIO9,按键连接到了GPIO5. 所以只需要初始化这两个引脚作为输入输出就行了,还是比较简单的,因为是学习,所以从简单的开始入手。初始化代码如下:LED初始化,初始化为输出:
-
GpioInit();
-
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_GPIO);
-
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_OUT);
复制代码
KEY初始化函数,初始化为输入:
-
GpioInit();
-
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_IO_FUNC_GPIO_5_GPIO);
-
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_GPIO_DIR_IN);
复制代码
如何调用呢?看下边:控制GPIO输出,1为输出高,0位输出低:
-
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 1);
复制代码
获取GPIO输入电平:
-
WifiIotGpioValue Key_Status = WIFI_IOT_GPIO_VALUE1;
-
GpioGetinputVal(WIFI_IOT_IO_NAME_GPIO_5, &Key_Status);
复制代码
其中:Key_Status就是获取的电平值,GpioGetInputVal()的返回值表示获取是否成功的标志。我创建了两个任务:一个任务专门控制LED,一个任务控制Key。上电之后,LED闪烁,频率是500ms。按下user按键之后,LED灭再次按下user按键,LED亮再次按下user按键,再次闪烁以此循环。完整代码如下:BUILD.gn
-
static_library("myapp") {
-
sources = [
-
"my_main.c"
-
]
-
include_dirs = [
-
"//utils/native/lite/include",
-
"//kernel/liteos_m/components/cmsis/2.0",
-
"//base/iot_hardware/interfaces/kits/wifiiot_lite",
-
"//vender/hisi/hi3861/hi3861/include"
-
]
-
}
复制代码
my_main.c函数
-
/*this is my first application*/
-
/*2020/11/08*/
-
-
#include
-
#include "ohos_init.h"
-
#include "ohos_types.h"
-
#include
-
-
#include
-
-
#include "ohos_init.h"
-
#include "cmsis_os2.h"
-
#include "wifiiot_gpio.h"
-
#include "wifiiot_gpio_ex.h"
-
#include "wifiiot_adc.h"
-
-
#include
-
#include
-
#include
-
#include
-
-
-
-
#define LED_INTERVAL_TIME_US 500000 /*500ms*/
-
#define LED_TASK_STACK_SIZE 512
-
#define LED_TASK_PRIO 25
-
-
#define KEY_INTERVAL_TIME_US 10000 /*10ms*/
-
#define KEY_TASK_STACK_SIZE 1024
-
#define KEY_TASK_PRIO 26
-
-
static void *LedTask(const char *arg);
-
static void *KeyTask(const char *arg);
-
-
/*LED Control*/
-
enum LedState {
-
LED_ON = 0,
-
LED_OFF,
-
LED_SPARK,
-
};
-
enum LedState g_ledState = LED_SPARK;
-
-
-
-
-
static void *LedTask(const char *arg)
-
{
-
(void)arg;
-
-
while (1) {
-
switch (g_ledState) {
-
case LED_ON:
-
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 1);
-
usleep(LED_INTERVAL_TIME_US);
-
break;
-
case LED_OFF:
-
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 0);
-
usleep(LED_INTERVAL_TIME_US);
-
break;
-
case LED_SPARK:
-
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 0);
-
usleep(LED_INTERVAL_TIME_US);
-
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 1);
-
usleep(LED_INTERVAL_TIME_US);
-
break;
-
default:
-
usleep(LED_INTERVAL_TIME_US);
-
break;
-
}
-
}
-
-
return NULL;
-
}
-
-
static void Led0Entry(void)
-
{
-
osThreadAttr_t attr;
-
-
GpioInit();
-
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_GPIO);
-
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_OUT);
-
-
attr.name = "LedTask";
-
attr.attr_bits = 0U;
-
attr.cb_mem = NULL;
-
attr.cb_size = 0U;
-
attr.stack_mem = NULL;
-
attr.stack_size = LED_TASK_STACK_SIZE;
-
attr.priority = LED_TASK_PRIO;
-
-
if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
-
printf("[Led0Task] Falied to create LedTask!n");
-
}
-
}
-
SYS_RUN(Led0Entry);
-
-
/*OLED board KEY Control - vased on ADC input*/
-
static void *KeyTask(const char *arg)
-
{
-
(void)arg;
-
-
WifiIotGpioValue Key_Status = WIFI_IOT_GPIO_VALUE1;
-
WifiIotGpioValue Last_key_Status = WIFI_IOT_GPIO_VALUE1;
-
uint8_t Key_press_cnt = 0u;
-
-
while (1) {
-
GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_5, &Key_Status);
-
// printf("%dn", Key_Status);
-
-
if((Key_Status == WIFI_IOT_GPIO_VALUE0) && (Last_key_Status != Key_Status))
-
{
-
// Key_press_cnt = (Key_press_cnt>2) ? 0:(++Key_press_cnt);
-
if(Key_press_cnt >= 2)
-
{
-
Key_press_cnt = 0;
-
}
-
else
-
{
-
Key_press_cnt ++;
-
}
-
switch(Key_press_cnt)
-
{
-
case 0:g_ledState = LED_ON;break;
-
case 1:g_ledState = LED_OFF;break;
-
case 2:g_ledState = LED_SPARK;break;
-
}
-
}
-
else
-
{
-
-
}
-
-
Last_key_Status = Key_Status;
-
usleep(LED_INTERVAL_TIME_US);
-
}
-
-
return NULL;
-
}
-
-
static void KeyEntry(void)
-
{
-
osThreadAttr_t attr;
-
-
GpioInit();
-
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_IO_FUNC_GPIO_5_GPIO);
-
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_GPIO_DIR_IN);
-
// AdcRead();
-
-
attr.name = "KeyTask";
-
attr.attr_bits = 0U;
-
attr.cb_mem = NULL;
-
attr.cb_size = 0U;
-
attr.stack_mem = NULL;
-
attr.stack_size = KEY_TASK_STACK_SIZE;
-
attr.priority = KEY_TASK_PRIO;
-
-
if (osThreadNew((osThreadFunc_t)KeyTask, NULL, &attr) == NULL) {
-
printf("[KeyTask] Falied to create KeyTask!n");
-
}
-
}
-
SYS_RUN(KeyEntry);
-
复制代码
我的软件目录如下:最后效果图见视频。
打开APP阅读更多精彩内容