在大概了解鸿蒙系统后和学习完编译环境搭建,于是我做了一个小小的实验,利用Hi3861GPIO操作完成点灯和按键实验
先看最简单得LED灯闪烁操作
源码结构如下:
Hello world.c文件内容:
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_pwm.h"
static WifiIotIoName LightGPIO[] =
{
WIFI_IOT_IO_NAME_GPIO_10,
WIFI_IOT_IO_NAME_GPIO_11,
WIFI_IOT_IO_NAME_GPIO_12
};
static int volatile BtnPressed = 0;
static void OnButtonPressed(char* arg);
static void OnButtonReleased(char* arg);
static void OnButtonPressed(char* arg)
{
(void)arg;
printf("[HelloWorld] OnButtonPressed()\n");
BtnPressed = 1;
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_8,
WIFI_IOT_INT_TYPE_EDGE,
WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH,
OnButtonReleased, NULL);
}
static void OnButtonReleased(char* arg)
{
(void)arg;
printf("[HelloWorld] OnButtonReleased()\n");
BtnPressed = 0;
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_8,
WIFI_IOT_INT_TYPE_EDGE,
WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,
OnButtonPressed, NULL);
}
static void* HelloWorld_Task(const char* arg)
{
int i = 0;
printf("[HelloWorld] HelloWorld_Task()\n");
(void)arg;
GpioInit();
for(i=0; i<3; i++)
{
//复用引脚为 GPIO
IoSetFunc(LightGPIO[i], 0);
//设置为输出
GpioSetDir(LightGPIO[i], WIFI_IOT_GPIO_DIR_OUT);
}
//复用引脚为 GPIO
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_FUNC_GPIO_8_GPIO);
//设置为输入
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_PULL_UP);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_8,
WIFI_IOT_INT_TYPE_EDGE,
WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,
OnButtonPressed, NULL);
//复用引脚为 GPIO
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT);
//设置为输入
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_OUT);
PwmInit(WIFI_IOT_PWM_PORT_PWM0);
while(1)
{
int j = 0;
while( j < 3 )
{
GpioSetOutputVal(LightGPIO[j++], 0);
}
i = (i + 1) % 3;
GpioSetOutputVal(LightGPIO[i], 1);
usleep(500000);
if( BtnPressed )
PwmStart(WIFI_IOT_PWM_PORT_PWM0, 5 * 1000, 40 * 1000);
else
PwmStop(WIFI_IOT_PWM_PORT_PWM0);
}
return NULL;
}
static void HelloWorld_Entry(void)
{
osThreadAttr_t attr = {0};
printf("[HelloWorld] HelloWorld_Entry()\n");
attr.name = "HelloWorld_Task";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 1024;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)HelloWorld_Task, NULL, &attr) == NULL)
{
printf("[HelloWorld] Falied to create LedTask!\n");
}
}
SYS_RUN(HelloWorld_Entry);
作者:wx5fb05cef6ec2d
想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com/
来源:oschina
链接:https://my.oschina.net/u/4857646/blog/4839856