鴻蒙 Hi3861 GPIO操作 點(diǎn)燈和按鍵實(shí)驗(yàn)
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
Hi3861也提供了相關(guān)得GPIO口操作
先看最簡(jiǎn)單得LED燈閃爍操作
源碼結(jié)構(gòu)如下:

BUILD.gn文件內(nèi)容:
- static_library("led_demo") {
- sources = [
- "led_demo.c"
- ]
- include_dirs = [
- "//utils/native/lite/include",
- "//kernel/liteos_m/components/cmsis/2.0",
- "//base/iot_hardware/interfaces/kits/wifiiot_lite"
- ]
- }
led_demo.c內(nèi)容:
- #include <unistd.h>
- #include "stdio.h"
- #include "ohos_init.h"
- #include "cmsis_os2.h"
- #include "wifiiot_gpio.h"
- #include "wifiiot_gpio_ex.h"
- #include <hi_types_base.h>
- #include <hi_i2c.h>
- #include <hi_early_debug.h>
- #include <hi_stdlib.h>
- void *LedTask(const char *arg)
- {
- (void)arg;
- while (1)
- {
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 0);
- usleep(300000);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 1);
- usleep(300000);
- }
- return NULL;
- }
- void led_demo(void)
- {
- osThreadAttr_t attr;
- GpioInit();
- //復(fù)用引腳為 GPIO
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_GPIO);
- //設(shè)置為輸出
- 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 = 512;
- attr.priority = 26;
- if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
- printf("[LedExample] Falied to create LedTask!\n");
- }
- }
- //SYS_RUN(led_demo);
- void oled_test(void)
- {
- }
- SYS_RUN(oled_test);
另外GPIO口還可以作為輸入,然后使用中斷,示例代碼如下:
這段示例代碼用的開發(fā)板上面的user按鍵。

通過(guò)查閱原理圖,我們可以看到Hi3861在type-C口附近有一個(gè)user按鈕,如圖,主要不要和復(fù)位按鈕搞錯(cuò)了。user按鈕對(duì)應(yīng)的是GPIO5引腳。
- /* gpio callback func */
- hi_void my_gpio_isr_func(hi_void *arg)
- {
- hi_unref_param(arg);
- printf("----- gpio isr success -----\r\n");
- }
- /* 設(shè)置 按鍵中斷響應(yīng) */
- hi_void my_gpio_isr_demo(hi_void)
- {
- hi_u32 ret;
- printf("----- gpio isr demo -----\r\n");
- (hi_void)hi_gpio_init();
- hi_io_set_func(HI_IO_NAME_GPIO_5, HI_IO_FUNC_GPIO_5_GPIO); /* uart1 rx */
- ret = hi_gpio_set_dir(HI_GPIO_IDX_5, HI_GPIO_DIR_IN);
- if (ret != HI_ERR_SUCCESS) {
- printf("===== ERROR ======gpio -> hi_gpio_set_dir1 ret:%d\r\n", ret);
- return;
- }
- ret = hi_gpio_register_isr_function(HI_GPIO_IDX_5, HI_INT_TYPE_EDGE,
- HI_GPIO_EDGE_RISE_LEVEL_HIGH, my_gpio_isr_func, HI_NULL);
- if (ret != HI_ERR_SUCCESS) {
- printf("===== ERROR ======gpio -> hi_gpio_register_isr_function ret:%d\r\n", ret);
- }
- }
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
【編輯推薦】