README.md
1# BearPi-HM_Nano开发板基础外设开发——GPIO输出
2本示例将演示如何在BearPi-HM_Nano开发板上使用GPIO输出功能去点亮LED灯。
3
4## GPIO API分析
5本案例主要使用了以下几个API完成GPIO输出功能。
6### IoTGpioInit()
7```c
8unsigned int IoTGpioInit(unsigned int id);
9```
10 **描述:**
11
12初始化GPIO外设。
13
14**参数:**
15
16|参数名|描述|
17|:--|:------|
18| id | 表示GPIO引脚号。 |
19
20
21### IoTGpioSetDir()
22```c
23unsigned int IoTGpioSetDir(unsigned int id, IotGpioDir dir);
24```
25**描述:**
26
27设置GPIO输出方向。
28
29**参数:**
30
31|参数名|描述|
32|:--|:------|
33| id | 表示GPIO引脚号。 |
34| dir | 表示GPIO输出方向。 |
35
36
37## 硬件设计
38本案例将使用板载的LED来验证GPIO的输出功能,在BearPi-HM_Nano开发板上LED的连接电路图如下图所示,LED的控制引脚与主控芯片的GPIO_2连接,所以需要编写软件去控制GPIO_2输出高低电平实现LED灯的亮灭。
39
40
41
42## 软件设计
43
44**主要代码分析**
45
46LedTask()为LED灯测试主任务,该任务先调用 IoTGpioInit()初始化GPIO,因为LED灯的控制引脚接在GPIO_2上,所以通过IoTGpioSetDir()将GPIO_2设置为普通GPIO的输出模式。最后在死循环里面间隔 1s 输出GPIO_2的高低电平,实现LED灯的闪烁功能。
47```c
48/**
49 * @brief led task output high and low levels to turn on and off LED
50 *
51 */
52static void LedTask(void)
53{
54 //init gpio of LED
55 IoTGpioInit(LED_GPIO);
56
57 //set GPIO_2 is output mode
58 IoTGpioSetDir(LED_GPIO, IOT_GPIO_DIR_OUT);
59
60 while (1) {
61 //set GPIO_2 output high levels to turn on LED
62 IoTGpioSetOutputVal(LED_GPIO, 1);
63
64 //delay 1s
65 sleep(1);
66
67 //set GPIO_2 output low levels to turn off LED
68 IoTGpioSetOutputVal(LED_GPIO, 0);
69
70 //delay 1s
71 sleep(1);
72 }
73}
74```
75
76## 编译调试
77
78### 修改 BUILD.gn 文件
79
80修改`device\board\bearpi\bearpi_hm_nano\app` 路径下 BUILD.gn 文件,指定 `led_example` 参与编译。
81
82```r
83"B1_basic_led_blink:led_example",
84#"B2_basic_button:button_example",
85#"B3_basic_pwm_led:pwm_example",
86#"B4_basic_adc:adc_example",
87#"B5_basic_i2c_nfc:i2c_example",
88#"B6_basic_uart:uart_example",
89```
90
91### 运行结果
92
93示例代码编译烧录后,按下开发板的RESET按键,开发板的LED灯开始闪烁。
94
95
96