在线时间896 小时
UID3067179
注册时间2014-10-23
NXP金币2714
TA的每日心情 | 开心 1 小时前 |
---|
签到天数: 1506 天 连续签到: 8 天 [LV.Master]伴坛终老
版主
  
- 积分
- 8744
- 最后登录
- 2025-8-27
|
使用GPIO来驱动LED灯。
一、硬件电路
电路图中,有关RGB对应的硬件电路如下:
使用的端口有P1.9, P1.10, P0.29。
二、软件部分
程序中,主要是有GPIO配置,端口初始化和功能函数。
2.1、GPIO配置代码
- const uint32_t port0_pin29_config = (
- IOCON_PIO_FUNC0 | /* Pin is configured as PIO0_29 */
- IOCON_PIO_MODE_PULLUP | /* Selects pull-up function */
- IOCON_PIO_INV_DI | /* Input function is not inverted */
- IOCON_PIO_DIGITAL_EN | /* Enables digital function */
- IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
- IOCON_PIO_SLEW_STANDARD |
- IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
- );
- IOCON_PinMuxSet(IOCON, PORT0_IDX, PIN29_IDX, port0_pin29_config); /* PORT0 PIN29 (coords: 11) is configured as PIO0_29 */
- const uint32_t port1_pin10_config = (
- IOCON_PIO_FUNC0 | /* Pin is configured as PIO1_10 */
- IOCON_PIO_MODE_PULLUP | /* Selects pull-up function */
- IOCON_PIO_INV_DI | /* Input function is not inverted */
- IOCON_PIO_DIGITAL_EN | /* Enables digital function */
- IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
- IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
- IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
- );
- IOCON_PinMuxSet(IOCON, PORT1_IDX, PIN10_IDX, port1_pin10_config); /* PORT1 PIN10 (coords: 30) is configured as PIO1_10 */
- const uint32_t port1_pin9_config = (
- IOCON_PIO_FUNC0 | /* Pin is configured as PIO1_9 */
- IOCON_PIO_MODE_PULLUP | /* Selects pull-up function */
- IOCON_PIO_INV_DI | /* Input function is not inverted */
- IOCON_PIO_DIGITAL_EN | /* Enables digital function */
- IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
- IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
- IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
- );
- IOCON_PinMuxSet(IOCON, PORT1_IDX, PIN9_IDX, port1_pin9_config); /* PORT1 PIN9 (coords: 29) is configured as PIO1_9 */
复制代码
2.2、RGB端口初始化
- GPIO_PinInit(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_G_LED_PIN, &led_config); //G - OUTPUT
- GPIO_PinInit(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_B_LED_PIN, &led_config); //B - OUTPUT
- GPIO_PinInit(GPIO, APP_BOARD_R_LED_PORT, APP_BOARD_R_LED_PIN, &led_config); //R - OUTPUT
-
- GPIO_PinWrite(GPIO, APP_BOARD_R_LED_PORT, APP_BOARD_R_LED_PIN, 1); //R = 1
- GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_G_LED_PIN, 1); //G = 1
- GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_B_LED_PIN, 1); //B = 1
复制代码
2.3、主程序
- #include "board.h"
- #include "fsl_debug_console.h"
- #include "fsl_gpio.h"
- #include "pin_mux.h"
- #include <stdbool.h>
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- #define APP_BOARD_TEST_LED_PORT 1U
- #define APP_BOARD_R_LED_PORT 0U
- #define APP_BOARD_GB_LED_PORT 1U
- #define APP_BOARD_R_LED_PIN 29U
- #define APP_BOARD_G_LED_PIN 10U
- #define APP_BOARD_B_LED_PIN 9U
- #define APP_BOARD_TEST_LED_PIN 10U
- #define APP_SW_PORT BOARD_SW1_GPIO_PORT
- #define APP_SW_PIN BOARD_SW1_GPIO_PIN
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- /*!
- * @brief delay a while.
- */
- void delay(void);
- /*******************************************************************************
- * Variables
- ******************************************************************************/
- /*******************************************************************************
- * Code
- ******************************************************************************/
- void delay(void)
- {
- volatile uint32_t i = 0;
- for (i = 0; i < 100000; ++i)
- {
- __asm("NOP"); /* delay */
- }
- }
- /*!
- * @brief Main function
- */
- int main(void)
- {
- uint32_t i = 0;
- uint32_t port_state = 0;
- /* Define the init structure for the output LED pin*/
- gpio_pin_config_t led_config = {
- kGPIO_DigitalOutput,
- 0,
- };
- /* Board pin, clock, debug console init */
- /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
- CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
- /* enable clock for GPIO*/
- CLOCK_EnableClock(kCLOCK_Gpio0);
- CLOCK_EnableClock(kCLOCK_Gpio1);
- BOARD_InitPins();
- BOARD_BootClockFROHF48M();
- BOARD_InitDebugConsole();
- /* Init output LED GPIO. */
- //GPIO_PortInit(GPIO, APP_BOARD_TEST_LED_PORT);
- //GPIO_PortInit(GPIO, APP_SW_PORT);
- GPIO_PinInit(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_G_LED_PIN, &led_config); //G - OUTPUT
- GPIO_PinInit(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_B_LED_PIN, &led_config); //B - OUTPUT
- GPIO_PinInit(GPIO, APP_BOARD_R_LED_PORT, APP_BOARD_R_LED_PIN, &led_config); //R - OUTPUT
-
- GPIO_PinWrite(GPIO, APP_BOARD_R_LED_PORT, APP_BOARD_R_LED_PIN, 1); //R = 1
- GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_G_LED_PIN, 1); //G = 1
- GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_B_LED_PIN, 1); //B = 1
-
- while (1)
- {
- GPIO_PinWrite(GPIO, APP_BOARD_R_LED_PORT, APP_BOARD_R_LED_PIN, 0);
- for (i = 0; i < 0x5fffff; ++i);
- GPIO_PinWrite(GPIO, APP_BOARD_R_LED_PORT, APP_BOARD_R_LED_PIN, 1);
- GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_G_LED_PIN, 0);
- for (i = 0; i < 0x5fffff; ++i);
- GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_G_LED_PIN, 1);
- GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_B_LED_PIN, 0);
- for (i = 0; i < 0x5fffff; ++i);
- GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_B_LED_PIN, 1);
- }
- }
复制代码
2.4、程序源码
gpio.zip
(1.95 MB, 下载次数: 3)
|
|