查看: 3020|回复: 1

[原创] LPC51U68开发板评测 -- 3、GPIO 驱动RGB灯

[复制链接]
  • TA的每日心情
    开心
    1 小时前
  • 签到天数: 1506 天

    连续签到: 8 天

    [LV.Master]伴坛终老

    152

    主题

    3185

    帖子

    31

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    8744
    最后登录
    2025-8-27
    发表于 2019-12-10 22:06:15 | 显示全部楼层 |阅读模式
       
        使用GPIO来驱动LED灯。


        一、硬件电路


        电路图中,有关RGB对应的硬件电路如下:
        1.png
        使用的端口有P1.9, P1.10, P0.29。
       
       二、软件部分
      
       程序中,主要是有GPIO配置,端口初始化和功能函数。
      
       2.1、GPIO配置代码
       
    1. const uint32_t port0_pin29_config = (
    2.     IOCON_PIO_FUNC0 |                                        /* Pin is configured as PIO0_29 */
    3.     IOCON_PIO_MODE_PULLUP |                                  /* Selects pull-up function */
    4.     IOCON_PIO_INV_DI |                                       /* Input function is not inverted */
    5.     IOCON_PIO_DIGITAL_EN |                                   /* Enables digital function */
    6.     IOCON_PIO_INPFILT_OFF |                                  /* Input filter disabled */
    7.     IOCON_PIO_SLEW_STANDARD |
    8.                 IOCON_PIO_OPENDRAIN_DI                                   /* Open drain is disabled */
    9.   );
    10.   IOCON_PinMuxSet(IOCON, PORT0_IDX, PIN29_IDX, port0_pin29_config); /* PORT0 PIN29 (coords: 11) is configured as PIO0_29 */
    11.   const uint32_t port1_pin10_config = (
    12.     IOCON_PIO_FUNC0 |                                        /* Pin is configured as PIO1_10 */
    13.     IOCON_PIO_MODE_PULLUP |                                  /* Selects pull-up function */
    14.     IOCON_PIO_INV_DI |                                       /* Input function is not inverted */
    15.     IOCON_PIO_DIGITAL_EN |                                   /* Enables digital function */
    16.     IOCON_PIO_INPFILT_OFF |                                  /* Input filter disabled */
    17.     IOCON_PIO_SLEW_STANDARD |                                /* Standard mode, output slew rate control is enabled */
    18.     IOCON_PIO_OPENDRAIN_DI                                   /* Open drain is disabled */
    19.   );
    20.   IOCON_PinMuxSet(IOCON, PORT1_IDX, PIN10_IDX, port1_pin10_config); /* PORT1 PIN10 (coords: 30) is configured as PIO1_10 */
    21.   const uint32_t port1_pin9_config = (
    22.     IOCON_PIO_FUNC0 |                                        /* Pin is configured as PIO1_9 */
    23.     IOCON_PIO_MODE_PULLUP |                                  /* Selects pull-up function */
    24.     IOCON_PIO_INV_DI |                                       /* Input function is not inverted */
    25.     IOCON_PIO_DIGITAL_EN |                                   /* Enables digital function */
    26.     IOCON_PIO_INPFILT_OFF |                                  /* Input filter disabled */
    27.     IOCON_PIO_SLEW_STANDARD |                                /* Standard mode, output slew rate control is enabled */
    28.     IOCON_PIO_OPENDRAIN_DI                                   /* Open drain is disabled */
    29.   );
    30.   IOCON_PinMuxSet(IOCON, PORT1_IDX, PIN9_IDX, port1_pin9_config); /* PORT1 PIN9 (coords: 29) is configured as PIO1_9 */
    复制代码

        2.2、RGB端口初始化
       
    1.     GPIO_PinInit(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_G_LED_PIN, &led_config);  //G - OUTPUT
    2.                 GPIO_PinInit(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_B_LED_PIN, &led_config);  //B - OUTPUT
    3.                 GPIO_PinInit(GPIO,  APP_BOARD_R_LED_PORT, APP_BOARD_R_LED_PIN, &led_config);  //R - OUTPUT
    4.                
    5.                 GPIO_PinWrite(GPIO,  APP_BOARD_R_LED_PORT, APP_BOARD_R_LED_PIN, 1);  //R = 1
    6.     GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_G_LED_PIN, 1);  //G = 1
    7.                 GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_B_LED_PIN, 1);  //B = 1
    复制代码

        2.3、主程序
       
    1. #include "board.h"
    2. #include "fsl_debug_console.h"
    3. #include "fsl_gpio.h"

    4. #include "pin_mux.h"
    5. #include <stdbool.h>
    6. /*******************************************************************************
    7. * Definitions
    8. ******************************************************************************/
    9. #define APP_BOARD_TEST_LED_PORT 1U

    10. #define APP_BOARD_R_LED_PORT 0U
    11. #define APP_BOARD_GB_LED_PORT 1U

    12. #define APP_BOARD_R_LED_PIN 29U
    13. #define APP_BOARD_G_LED_PIN 10U
    14. #define APP_BOARD_B_LED_PIN 9U



    15. #define APP_BOARD_TEST_LED_PIN 10U
    16. #define APP_SW_PORT BOARD_SW1_GPIO_PORT
    17. #define APP_SW_PIN BOARD_SW1_GPIO_PIN


    18. /*******************************************************************************
    19. * Prototypes
    20. ******************************************************************************/
    21. /*!
    22. * @brief delay a while.
    23. */
    24. void delay(void);

    25. /*******************************************************************************
    26. * Variables
    27. ******************************************************************************/

    28. /*******************************************************************************
    29. * Code
    30. ******************************************************************************/
    31. void delay(void)
    32. {
    33.     volatile uint32_t i = 0;
    34.     for (i = 0; i < 100000; ++i)
    35.     {
    36.         __asm("NOP"); /* delay */
    37.     }
    38. }

    39. /*!
    40. * @brief Main function
    41. */
    42. int main(void)
    43. {
    44.         uint32_t i = 0;
    45.     uint32_t port_state = 0;

    46.     /* Define the init structure for the output LED pin*/
    47.     gpio_pin_config_t led_config = {
    48.         kGPIO_DigitalOutput,
    49.         0,
    50.     };

    51.     /* Board pin, clock, debug console init */
    52.     /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
    53.     CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
    54.     /* enable clock for GPIO*/
    55.     CLOCK_EnableClock(kCLOCK_Gpio0);
    56.     CLOCK_EnableClock(kCLOCK_Gpio1);

    57.     BOARD_InitPins();
    58.     BOARD_BootClockFROHF48M();
    59.     BOARD_InitDebugConsole();

    60.     /* Init output LED GPIO. */
    61.     //GPIO_PortInit(GPIO, APP_BOARD_TEST_LED_PORT);
    62.     //GPIO_PortInit(GPIO, APP_SW_PORT);
    63.     GPIO_PinInit(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_G_LED_PIN, &led_config);  //G - OUTPUT
    64.                 GPIO_PinInit(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_B_LED_PIN, &led_config);  //B - OUTPUT
    65.                 GPIO_PinInit(GPIO,  APP_BOARD_R_LED_PORT, APP_BOARD_R_LED_PIN, &led_config);  //R - OUTPUT
    66.                
    67.                 GPIO_PinWrite(GPIO,  APP_BOARD_R_LED_PORT, APP_BOARD_R_LED_PIN, 1);  //R = 1
    68.     GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_G_LED_PIN, 1);  //G = 1
    69.                 GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_B_LED_PIN, 1);  //B = 1
    70.    
    71.     while (1)
    72.     {
    73.         GPIO_PinWrite(GPIO, APP_BOARD_R_LED_PORT, APP_BOARD_R_LED_PIN, 0);   
    74.                                 for (i = 0; i < 0x5fffff; ++i);
    75.                           GPIO_PinWrite(GPIO, APP_BOARD_R_LED_PORT, APP_BOARD_R_LED_PIN, 1);   
    76.                                 GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_G_LED_PIN, 0);   
    77.                                 for (i = 0; i < 0x5fffff; ++i);
    78.                           GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_G_LED_PIN, 1);   
    79.                                 GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_B_LED_PIN, 0);       
    80.                                 for (i = 0; i < 0x5fffff; ++i);
    81.         GPIO_PinWrite(GPIO, APP_BOARD_GB_LED_PORT, APP_BOARD_B_LED_PIN, 1);                       
    82.     }
    83. }
    复制代码


        2.4、程序源码  
        gpio.zip (1.95 MB, 下载次数: 3)
    哎...今天够累的,签到来了~
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2025-8-8 16:43
  • 签到天数: 1504 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    97

    主题

    4692

    帖子

    12

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    10093
    最后登录
    2025-8-8
    发表于 2019-12-11 10:30:53 | 显示全部楼层
    楼主的gif图还是真棒。
    话说您的这个延时……
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-8-27 18:40 , Processed in 0.085605 second(s), 21 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表