查看: 1099|回复: 0

[分享] 【MCX先手尝鲜】创建工程并点亮RGB灯

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

    连续签到: 1 天

    [LV.10]以坛为家III

    152

    主题

    3146

    帖子

    31

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    8652
    最后登录
    2025-7-27
    发表于 2024-1-23 09:19:23 | 显示全部楼层 |阅读模式
    使用官网的SDK创建项目工程,并点亮开发板上的RGB全彩LED灯。


    一、硬件部分


    开发板上RGB灯连接到芯片的P3_2~P3_4引脚
    001.png


    二、程序部分


    2.1、led.c
    1. #include "main.h"

    2. /* Define the init structure for the output LED pin*/
    3. gpio_pin_config_t led_config = {
    4.                 kGPIO_DigitalOutput,
    5.                 0,
    6. };


    7. void init_led(void)
    8. {
    9.         
    10.         
    11.         CLOCK_EnableClock(kCLOCK_Port3);
    12.         
    13.         //LED_GREEN
    14.         const port_pin_config_t port3_2_pinF14_config = {/* Internal pull-up/down resistor is disabled */
    15.                                                      kPORT_PullDisable,
    16.                                                      /* Low internal pull resistor value is selected. */
    17.                                                      kPORT_LowPullResistor,
    18.                                                      /* Fast slew rate is configured */
    19.                                                      kPORT_FastSlewRate,
    20.                                                      /* Passive input filter is disabled */
    21.                                                      kPORT_PassiveFilterDisable,
    22.                                                      /* Open drain output is disabled */
    23.                                                      kPORT_OpenDrainDisable,
    24.                                                      /* Low drive strength is configured */
    25.                                                      kPORT_LowDriveStrength,
    26.                                                      /* Pin is configured as PIO3_4 */
    27.                                                      kPORT_MuxAlt0,
    28.                                                      /* Digital input enabled */
    29.                                                      kPORT_InputBufferEnable,
    30.                                                      /* Digital input is not inverted */
    31.                                                      kPORT_InputNormal,
    32.                                                      /* Pin Control Register fields [15:0] are not locked */
    33.                                                      kPORT_UnlockRegister};
    34.         /* PORT3_2 (pin F14) is configured as PIO3_2 */
    35.         PORT_SetPinConfig(PORT3, 2U, &port3_2_pinF14_config);
    36.         
    37.         //LED_BLUE
    38.         const port_pin_config_t port3_3_pinF14_config = {/* Internal pull-up/down resistor is disabled */
    39.                                                      kPORT_PullDisable,
    40.                                                      /* Low internal pull resistor value is selected. */
    41.                                                      kPORT_LowPullResistor,
    42.                                                      /* Fast slew rate is configured */
    43.                                                      kPORT_FastSlewRate,
    44.                                                      /* Passive input filter is disabled */
    45.                                                      kPORT_PassiveFilterDisable,
    46.                                                      /* Open drain output is disabled */
    47.                                                      kPORT_OpenDrainDisable,
    48.                                                      /* Low drive strength is configured */
    49.                                                      kPORT_LowDriveStrength,
    50.                                                      /* Pin is configured as PIO3_4 */
    51.                                                      kPORT_MuxAlt0,
    52.                                                      /* Digital input enabled */
    53.                                                      kPORT_InputBufferEnable,
    54.                                                      /* Digital input is not inverted */
    55.                                                      kPORT_InputNormal,
    56.                                                      /* Pin Control Register fields [15:0] are not locked */
    57.                                                      kPORT_UnlockRegister};
    58.         /* PORT3_3 (pin F14) is configured as PIO3_3 */
    59.         PORT_SetPinConfig(PORT3, 3U, &port3_3_pinF14_config);
    60.                                                                                                                                                                                                                  
    61.         //LED_RED
    62.         const port_pin_config_t port3_4_pinF14_config = {/* Internal pull-up/down resistor is disabled */
    63.                                                      kPORT_PullDisable,
    64.                                                      /* Low internal pull resistor value is selected. */
    65.                                                      kPORT_LowPullResistor,
    66.                                                      /* Fast slew rate is configured */
    67.                                                      kPORT_FastSlewRate,
    68.                                                      /* Passive input filter is disabled */
    69.                                                      kPORT_PassiveFilterDisable,
    70.                                                      /* Open drain output is disabled */
    71.                                                      kPORT_OpenDrainDisable,
    72.                                                      /* Low drive strength is configured */
    73.                                                      kPORT_LowDriveStrength,
    74.                                                      /* Pin is configured as PIO3_4 */
    75.                                                      kPORT_MuxAlt0,
    76.                                                      /* Digital input enabled */
    77.                                                      kPORT_InputBufferEnable,
    78.                                                      /* Digital input is not inverted */
    79.                                                      kPORT_InputNormal,
    80.                                                      /* Pin Control Register fields [15:0] are not locked */
    81.                                                      kPORT_UnlockRegister};
    82.         /* PORT3_4 (pin F14) is configured as PIO3_4 */
    83.         PORT_SetPinConfig(PORT3, 4U, &port3_4_pinF14_config);
    84.                                                                                                                                                                                                                  
    85.         GPIO_PinInit(LED_RED_GPIO, LED_RED_GPIO_PIN, &led_config);
    86.   GPIO_PinInit(LED_GREEN_GPIO, LED_GREEN_GPIO_PIN, &led_config);
    87.   GPIO_PinInit(LED_BLUE_GPIO, LED_BLUE_GPIO_PIN, &led_config);
    88.                                                                                                                                                                                                                  
    89.         led_red_off();
    90.         led_green_off();
    91.         led_blue_off();                                                                                                                                                                                                         
    92.                                                                                                                                        
    93. }
    复制代码


    2.2、led.h
    1. #ifndef __LED_H
    2. #define __LED_H


    3. #define LED_RED_GPIO                                         GPIO3
    4. #define LED_RED_GPIO_PIN                         4U

    5. #define LED_BLUE_GPIO                                 GPIO3
    6. #define LED_BLUE_GPIO_PIN                 3U

    7. #define LED_GREEN_GPIO                                 GPIO3
    8. #define LED_GREEN_GPIO_PIN                 2U



    9. #define led_red_on()                                                                                        GPIO_PortClear(LED_RED_GPIO, 1u << LED_RED_GPIO_PIN)
    10. #define led_red_off()                                                                                 GPIO_PortSet(LED_RED_GPIO, 1u << LED_RED_GPIO_PIN)
    11. #define led_red_tog()                                                                                        GPIO_PortToggle(LED_RED_GPIO, 1u << LED_RED_GPIO_PIN)

    12. #define led_green_on()                                                                                GPIO_PortClear(LED_GREEN_GPIO, 1u << LED_GREEN_GPIO_PIN)
    13. #define led_green_off()                                                                         GPIO_PortSet(LED_GREEN_GPIO, 1u << LED_GREEN_GPIO_PIN)
    14. #define led_green_tog()                                                                                GPIO_PortToggle(LED_GREEN_GPIO, 1u << LED_GREEN_GPIO_PIN)

    15. #define led_blue_on()                                                                                        GPIO_PortClear(LED_BLUE_GPIO, 1u << LED_BLUE_GPIO_PIN)
    16. #define led_blue_off()                                                                                 GPIO_PortSet(LED_BLUE_GPIO, 1u << LED_BLUE_GPIO_PIN)
    17. #define led_blue_tog()                                                                                GPIO_PortToggle(LED_BLUE_GPIO, 1u << LED_BLUE_GPIO_PIN)

    18. void init_led(void);                    

    19. #endif


    复制代码


    2.3、main.c

    1. #include "main.h"

    2. /*******************************************************************************
    3. * Definitions
    4. ******************************************************************************/
    5. #define BOARD_LED_GPIO     BOARD_LED_RED_GPIO
    6. #define BOARD_LED_GPIO_PIN BOARD_LED_RED_GPIO_PIN

    7. /*******************************************************************************
    8. * Prototypes
    9. ******************************************************************************/
    10. /*!
    11. * @brief delay a while.
    12. */
    13. void delay(void);

    14. /*******************************************************************************
    15. * Variables
    16. ******************************************************************************/

    17. /*******************************************************************************
    18. * Code
    19. ******************************************************************************/
    20. void delay(void)
    21. {
    22.     volatile uint32_t i = 0;
    23.     for (i = 0; i < 0x3fffff; ++i)
    24.     {
    25.         __asm("NOP"); /* delay */
    26.     }
    27. }

    28. /*!
    29. * @brief Main function
    30. */
    31. int main(void)
    32. {

    33.     /* Board pin, clock, debug console init */
    34.     /* attach FRO 12M to FLEXCOMM4 (debug console) */
    35.     CLOCK_SetClkDiv(kCLOCK_DivFlexcom4Clk, 1u);
    36.     CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);

    37.     /* enable clock for GPIO*/
    38.     CLOCK_EnableClock(kCLOCK_Gpio3);

    39.     BOARD_InitPins();
    40.     BOARD_PowerMode_OD();
    41.     BOARD_InitBootClocks();
    42.     BOARD_InitDebugConsole();
    43.         
    44.                 init_led();

    45.     /* Print a note to terminal. */
    46.     PRINTF("\r\n GPIO Driver example\r\n");
    47.     PRINTF("\r\n The LED is blinking.\r\n");


    48.     while (1)
    49.     {
    50.         
    51.                         led_red_on();
    52.                         led_green_off();
    53.                         led_blue_off();
    54.                         delay();                        
    55.                         led_red_off();
    56.                         led_green_on();
    57.                         led_blue_off();
    58.                         delay();
    59.                         led_red_off();
    60.                         led_green_off();
    61.                         led_blue_on();
    62.                         delay();
    63.     }
    64. }
    复制代码


    2.4、项目文件
    mcx_n947_prj.rar (5.97 MB, 下载次数: 6)
    哎...今天够累的,签到来了~
    回复

    使用道具 举报

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

    本版积分规则

    关闭

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

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

    GMT+8, 2025-7-27 12:29 , Processed in 0.082429 second(s), 20 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

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