本帖最后由 TL-LED 于 2024-12-6 18:57 编辑
一、资料下载
1.1、开发板官网链接:
https://www.nxp.com.cn/design/design-center/development-boards-and-designs/general-purpose-mcus/frdm-development-board-for-mcx-n94-n54-mcus:FRDM-MCXN947
1.2、SDK下载
https://mcuxpresso.nxp.com/en/select
根据SDK的工程文件,创建测试项目工程。
二、硬件部分
2.1、按键部分电路
2.2、LED灯部分电路
三、程序部分
3.1、key.c
#include "pin_mux.h"
#include "gpio.h"
void init_key_pins(void)
{
/* Enables the clock for PORT0 controller: Enables clock */
CLOCK_EnableClock(kCLOCK_Port0);
const port_pin_config_t SW2 = {/* Internal pull-up/down resistor is disabled */
.pullSelect = kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
.pullValueSelect = kPORT_LowPullResistor,
/* Fast slew rate is configured */
.slewRate = kPORT_FastSlewRate,
/* Passive input filter is disabled */
.passiveFilterEnable = kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
.openDrainEnable = kPORT_OpenDrainDisable,
/* Low drive strength is configured */
.driveStrength = kPORT_LowDriveStrength,
/* Pin is configured as PIO0_23 */
.mux = kPORT_MuxAlt0,
/* Digital input enabled */
.inputBuffer = kPORT_InputBufferEnable,
/* Digital input is not inverted */
.invertInput = kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
.lockRegister = kPORT_UnlockRegister};
/* PORT0_23 (pin B7) is configured as PIO0_23 */
PORT_SetPinConfig(INIT_KEY_PINS_SW2_PORT, INIT_KEY_PINS_SW2_PIN, &SW2);
const port_pin_config_t SW3 = {/* Internal pull-up resistor is enabled */
.pullSelect = kPORT_PullUp,
/* Low internal pull resistor value is selected. */
.pullValueSelect = kPORT_LowPullResistor,
/* Fast slew rate is configured */
.slewRate = kPORT_FastSlewRate,
/* Passive input filter is disabled */
.passiveFilterEnable = kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
.openDrainEnable = kPORT_OpenDrainDisable,
/* Low drive strength is configured */
.driveStrength = kPORT_LowDriveStrength,
/* Pin is configured as PIO0_6 */
.mux = kPORT_MuxAlt0,
/* Digital input enabled */
.inputBuffer = kPORT_InputBufferEnable,
/* Digital input is not inverted */
.invertInput = kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
.lockRegister = kPORT_UnlockRegister};
/* PORT0_6 (pin C14) is configured as PIO0_6 */
PORT_SetPinConfig(INIT_KEY_PINS_SW3_PORT, INIT_KEY_PINS_SW3_PIN, &SW3);
}
void init_key(void)
{
init_key_pins();
}
3.2、key.h
#ifndef __KEY_H
#define __KEY_H
#define KEY_SW2_GPIO GPIO0
#define KEY_SW2_GPIO_PIN 23U
#define KEY_SW3_GPIO GPIO0
#define KEY_SW3_GPIO_PIN 6U
#define read_key_sw2() GPIO_PinRead(KEY_SW2_GPIO, KEY_SW2_GPIO_PIN)
#define read_key_sw3() GPIO_PinRead(KEY_SW3_GPIO, KEY_SW3_GPIO_PIN)
void init_key(void);
#endif
3.3、led.c
#include "pin_mux.h"
#include "fsl_gpio.h"
#include "led.h"
void init_led_pins(void)
{
gpio_pin_config_t LED_RED_config = {
.pinDirection = kGPIO_DigitalOutput,
.outputLogic = 0U
};
/* Initialize GPIO functionality on pin PIO0_10 (pin B12) */
GPIO_PinInit(INIT_LED_PINS_LED_RED_GPIO, INIT_LED_PINS_LED_RED_PIN, &LED_RED_config);
gpio_pin_config_t LED_GREEN_config = {
.pinDirection = kGPIO_DigitalOutput,
.outputLogic = 0U
};
/* Initialize GPIO functionality on pin PIO0_27 (pin E10) */
GPIO_PinInit(INIT_LED_PINS_LED_GREEN_GPIO, INIT_LED_PINS_LED_GREEN_PIN, &LED_GREEN_config);
gpio_pin_config_t LED_BLUE_config = {
.pinDirection = kGPIO_DigitalOutput,
.outputLogic = 0U
};
/* Initialize GPIO functionality on pin PIO1_2 (pin C4) */
GPIO_PinInit(INIT_LED_PINS_LED_BLUE_GPIO, INIT_LED_PINS_LED_BLUE_PIN, &LED_BLUE_config);
const port_pin_config_t LED_RED = {/* Internal pull-up/down resistor is disabled */
.pullSelect = kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
.pullValueSelect = kPORT_LowPullResistor,
/* Fast slew rate is configured */
.slewRate = kPORT_FastSlewRate,
/* Passive input filter is disabled */
.passiveFilterEnable = kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
.openDrainEnable = kPORT_OpenDrainDisable,
/* Low drive strength is configured */
.driveStrength = kPORT_LowDriveStrength,
/* Pin is configured as PIO0_10 */
.mux = kPORT_MuxAlt0,
/* Digital input enabled */
.inputBuffer = kPORT_InputBufferEnable,
/* Digital input is not inverted */
.invertInput = kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
.lockRegister = kPORT_UnlockRegister};
/* PORT0_10 (pin B12) is configured as PIO0_10 */
PORT_SetPinConfig(INIT_LED_PINS_LED_RED_PORT, INIT_LED_PINS_LED_RED_PIN, &LED_RED);
const port_pin_config_t LED_GREEN = {/* Internal pull-up/down resistor is disabled */
.pullSelect = kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
.pullValueSelect = kPORT_LowPullResistor,
/* Fast slew rate is configured */
.slewRate = kPORT_FastSlewRate,
/* Passive input filter is disabled */
.passiveFilterEnable = kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
.openDrainEnable = kPORT_OpenDrainDisable,
/* Low drive strength is configured */
.driveStrength = kPORT_LowDriveStrength,
/* Pin is configured as PIO0_27 */
.mux = kPORT_MuxAlt0,
/* Digital input enabled */
.inputBuffer = kPORT_InputBufferEnable,
/* Digital input is not inverted */
.invertInput = kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
.lockRegister = kPORT_UnlockRegister};
/* PORT0_27 (pin E10) is configured as PIO0_27 */
PORT_SetPinConfig(INIT_LED_PINS_LED_GREEN_PORT, INIT_LED_PINS_LED_GREEN_PIN, &LED_GREEN);
const port_pin_config_t LED_BLUE = {/* Internal pull-up/down resistor is disabled */
.pullSelect = kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
.pullValueSelect = kPORT_LowPullResistor,
/* Fast slew rate is configured */
.slewRate = kPORT_FastSlewRate,
/* Passive input filter is disabled */
.passiveFilterEnable = kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
.openDrainEnable = kPORT_OpenDrainDisable,
/* Low drive strength is configured */
.driveStrength = kPORT_LowDriveStrength,
/* Pin is configured as PIO1_2 */
.mux = kPORT_MuxAlt0,
/* Digital input enabled */
.inputBuffer = kPORT_InputBufferEnable,
/* Digital input is not inverted */
.invertInput = kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
.lockRegister = kPORT_UnlockRegister};
/* PORT1_2 (pin C4) is configured as PIO1_2 */
PORT_SetPinConfig(INIT_LED_PINS_LED_BLUE_PORT, INIT_LED_PINS_LED_BLUE_PIN, &LED_BLUE);
}
void init_led(void)
{
CLOCK_EnableClock(kCLOCK_Port0);
CLOCK_EnableClock(kCLOCK_Gpio1);
init_led_pins();
led_red_off();
led_green_off();
led_blue_off();
}
3.4、led.h
#ifndef __LED_H
#define __LED_H
#define LED_RED_GPIO GPIO0
#define LED_RED_GPIO_PIN 10U
#define LED_BLUE_GPIO GPIO1
#define LED_BLUE_GPIO_PIN 2U
#define LED_GREEN_GPIO GPIO0
#define LED_GREEN_GPIO_PIN 27U
#define led_red_on() GPIO_PortClear(LED_RED_GPIO, 1u << LED_RED_GPIO_PIN)
#define led_red_off() GPIO_PortSet(LED_RED_GPIO, 1u << LED_RED_GPIO_PIN)
#define led_red_tog() GPIO_PortToggle(LED_RED_GPIO, 1u << LED_RED_GPIO_PIN)
#define led_green_on() GPIO_PortClear(LED_GREEN_GPIO, 1u << LED_GREEN_GPIO_PIN)
#define led_green_off() GPIO_PortSet(LED_GREEN_GPIO, 1u << LED_GREEN_GPIO_PIN)
#define led_green_tog() GPIO_PortToggle(LED_GREEN_GPIO, 1u << LED_GREEN_GPIO_PIN)
#define led_blue_on() GPIO_PortClear(LED_BLUE_GPIO, 1u << LED_BLUE_GPIO_PIN)
#define led_blue_off() GPIO_PortSet(LED_BLUE_GPIO, 1u << LED_BLUE_GPIO_PIN)
#define led_blue_tog() GPIO_PortToggle(LED_BLUE_GPIO, 1u << LED_BLUE_GPIO_PIN)
void init_led(void);
#endif
3.5、main.c
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "fsl_debug_console.h"
#include "led/led.h"
#include "key/key.h"
int main(void)
{
uint8_t i;
CLOCK_EnableClock(kCLOCK_Gpio0);
BOARD_InitDEBUG_UARTPins();
BOARD_PowerMode_OD();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
init_led();
init_key();
while (1)
{
if(read_key_sw2()==0)
{
led_red_on();
}
else
{
led_red_off();
}
if(read_key_sw3()==0)
{
led_green_on();
}
else
{
led_green_off();
}
}
}
四、程序运行
下载程序后,开发板运行结果
key
|