Output Toggle
该示例演示使用 GPIO 输出功能驱动LED进行翻转。
首先将GPIO配置为输出来驱动LED。然后进入while循环,每1秒翻转一次LED状态。
环境需求
该示例支持以下开发套件:
Hardware Platforms |
Board Name |
---|---|
RTL87x2G HDK |
RTL87x2G EVB |
更多信息请参考快速入门。
硬件连线
连接P1_0和LED0。 LED驱动电路如下图所示。

LED驱动电路图
编译和下载
该示例的工程路径如下:
Project file: samples\peripheral\gpio\output_toggle\proj\rtl87x2g\mdk
Project file: samples\peripheral\gpio\output_toggle\proj\rtl87x2g\gcc
请按照以下步骤操作构建并运行该示例:
打开工程文件。
按照 快速入门 中 编译APP Image 给出的步骤构建目标文件。
编译成功后,在路径
mdk\bin
或gcc\bin
下会生成 app binapp_MP_xxx.bin
文件。按下复位按键,开始运行。
测试验证
当EVB启动后,在Debug Analyzer工具内观察如下log。
Start gpio toggle test!
观察LED灯闪烁情况。
代码介绍
该章节分为以下几个部分:
源码路径
工程路径:
sdk\samples\peripheral\gpio\output_toggle\proj
源码路径:
sdk\samples\peripheral\gpio\output_toggle\src
该工程的工程文件代码结构如下:
└── Project: output_toggle
└── secure_only_app
└── Device includes startup code
├── startup_rtl.c
└── system_rtl.c
├── CMSIS includes CMSIS header files
├── CMSE Library Non-secure callable lib
├── lib includes all binary symbol files that user application is built on
└── rtl87x2g_io.lib
├── peripheral includes all peripheral drivers and module code used by the application
├── rtl_gpio.c
├── rtl_pinmux.c
└── rtl_rcc.c
└── APP includes the ble_peripheral user application implementation
├── io_gpio.c
└── main_ns.c
初始化
初始化流程包括了 board_gpio_init
和 driver_gpio_init
。
board_gpio_init
中包含了PAD与PINMUX设置:
配置PAD:设置引脚、PINMUX模式、PowerOn、无内部上拉。
配置PINMUX:分配引脚为GPIO功能。
driver_gpio_init
包含了对GPIO外设的初始化。
使能 PCC 时钟源。
设置
GPIO_Mode
为输出模式。
RCC_PeriphClockCmd(APBPERIPH_GPIO, APBPERIPH_GPIO_CLOCK, ENABLE);
......
GPIO_InitStruct.GPIO_Mode = GPIO_MODE_OUT;
功能实现
初始化后执行while循环,循环体内执行 GPIO_WriteBit()
函数,控制GPIO输出高低电平。
while (1)
{
/* Set GPIO_PIN_OUTPUT */
GPIO_WriteBit(GPIO_PORT, GPIO_PIN, (BitAction)(1));
platform_delay_ms(1000);
/* Reset GPIO_PIN_OUTPUT */
GPIO_WriteBit(GPIO_PORT, GPIO_PIN, (BitAction)(0));
platform_delay_ms(1000);
}