这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » Let'sdo2025年第4期-基于单片机的直流电机控制-总结贴

共1条 1/1 1 跳转至

Let'sdo2025年第4期-基于单片机的直流电机控制-总结贴

助工
2026-02-01 20:04:06     打赏

一、设计思路

参考论坛里的各位大佬的思路,采用开发板上的两个按键控制电机正反转、速度、开关;

二、硬件连接

TB6612的PWMB、BIN1、BIN2分别连接开发板上的P3_6、P3_7、P3_8,与过程贴的引脚相同,VCC和GND分别接到开发板的电源引脚上。不同的是,过程贴中使用开发板的5V电源作为TB6612的电机驱动电源VM,在本次成果贴中使用直流电源作为电机电源。


#include "fsl_common.h"
#include "fsl_port.h"
#include "pin_mux.h"

/* FUNCTION ************************************************************************************************************
 *
 * Function Name : BOARD_InitBootPins
 * Description   : Calls initialization functions.
 *
 * END ****************************************************************************************************************/
void BOARD_InitBootPins(void)
{
    BOARD_InitPins();
}

/* FUNCTION ************************************************************************************************************
 *
 * Function Name : BOARD_InitPins
 * Description   : Configures pin routing and optionally pin electrical features.
 *
 * END ****************************************************************************************************************/
void BOARD_InitPins(void)
{
    /* Write to PORT0: Peripheral clock is enabled */
    CLOCK_EnableClock(kCLOCK_GatePORT0);
    /* Write to PORT1: Peripheral clock is enabled */
    CLOCK_EnableClock(kCLOCK_GatePORT1);
    /* Write to PORT3: Peripheral clock is enabled */
    CLOCK_EnableClock(kCLOCK_GatePORT3);
    /* LPUART0 peripheral is released from reset */
    RESET_ReleasePeripheralReset(kLPUART0_RST_SHIFT_RSTn);
    /* PORT0 peripheral is released from reset */
    RESET_ReleasePeripheralReset(kPORT0_RST_SHIFT_RSTn);
    /* INPUTMUX0 peripheral is released from reset */
    RESET_ReleasePeripheralReset(kINPUTMUX0_RST_SHIFT_RSTn);
    /* PORT1 peripheral is released from reset */
    RESET_ReleasePeripheralReset(kPORT1_RST_SHIFT_RSTn);
    /* GPIO1 peripheral is released from reset */
    RESET_ReleasePeripheralReset(kGPIO1_RST_SHIFT_RSTn);
    /* PORT3 peripheral is released from reset */
    RESET_ReleasePeripheralReset(kPORT3_RST_SHIFT_RSTn);
    /* GPIO3 peripheral is released from reset */
    RESET_ReleasePeripheralReset(kGPIO3_RST_SHIFT_RSTn);
    /* FLEXPWM0 peripheral is released from reset */
    RESET_ReleasePeripheralReset(kFLEXPWM0_RST_SHIFT_RSTn);

    const port_pin_config_t port0_2_pin51_config = {/* 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,
                                                    /* Normal drive strength is configured */
                                                    .driveStrength1 = kPORT_NormalDriveStrength,
                                                    /* Pin is configured as LPUART0_RXD */
                                                    .mux = kPORT_MuxAlt2,
                                                    /* 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_2 (pin 51) is configured as LPUART0_RXD */
    PORT_SetPinConfig(PORT0, 2U, &port0_2_pin51_config);

    const port_pin_config_t port0_3_pin52_config = {/* 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,
                                                    /* Normal drive strength is configured */
                                                    .driveStrength1 = kPORT_NormalDriveStrength,
                                                    /* Pin is configured as LPUART0_TXD */
                                                    .mux = kPORT_MuxAlt2,
                                                    /* 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_3 (pin 52) is configured as LPUART0_TXD */
    PORT_SetPinConfig(PORT0, 3U, &port0_3_pin52_config);

    const port_pin_config_t port1_13_pin7_config = {/* 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,
                                                    /* Normal drive strength is configured */
                                                    .driveStrength1 = kPORT_NormalDriveStrength,
                                                    /* Pin is configured as TRIG_IN3 */
                                                    .mux = kPORT_MuxAlt1,
                                                    /* 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_13 (pin 7) is configured as TRIG_IN3 */
    PORT_SetPinConfig(PORT1, 13U, &port1_13_pin7_config);

    const port_pin_config_t port1_6_pin64_config = {/* 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,
                                                    /* Normal drive strength is configured */
                                                    .driveStrength1 = kPORT_NormalDriveStrength,
                                                    /* Pin is configured as TRIG_IN2 */
                                                    .mux = kPORT_MuxAlt1,
                                                    /* 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_6 (pin 64) is configured as TRIG_IN2 */
    PORT_SetPinConfig(PORT1, 6U, &port1_6_pin64_config);

    /* PORT1_7 (pin 1) is configured as P1_7 */
    PORT_SetPinMux(PORT1, 7U, kPORT_MuxAlt0);

    PORT1->PCR[7] = ((PORT1->PCR[7] &
                      /* Mask bits to zero which are setting */
                      (~(PORT_PCR_IBE_MASK)))

                     /* Input Buffer Enable: Enables. */
                     | PORT_PCR_IBE(PCR_IBE_ibe1));

    /* PORT3_0 (pin 46) is configured as TRIG_IN0 */
    PORT_SetPinMux(PORT3, 0U, kPORT_MuxAlt1);

    PORT3->PCR[0] =
        ((PORT3->PCR[0] &
          /* Mask bits to zero which are setting */
          (~(PORT_PCR_PS_MASK | PORT_PCR_PE_MASK | PORT_PCR_SRE_MASK | PORT_PCR_PFE_MASK | PORT_PCR_ODE_MASK | PORT_PCR_DSE_MASK | PORT_PCR_IBE_MASK | PORT_PCR_INV_MASK)))

         /* Pull Select: Enables internal pulldown resistor. */
         | PORT_PCR_PS(PCR_PS_ps0)

         /* Pull Enable: Disables. */
         | PORT_PCR_PE(PCR_PE_pe0)

         /* Slew Rate Enable: Fast. */
         | PORT_PCR_SRE(PCR_SRE_sre0)

         /* Passive Filter Enable: Disables. */
         | PORT_PCR_PFE(PCR_PFE_pfe0)

         /* Open Drain Enable: Disables. */
         | PORT_PCR_ODE(PCR_ODE_ode0)

         /* Drive Strength Enable: Low. */
         | PORT_PCR_DSE(PCR_DSE_dse0)

         /* Input Buffer Enable: Enables. */
         | PORT_PCR_IBE(PCR_IBE_ibe1)

         /* Invert Input: Does not invert. */
         | PORT_PCR_INV(PCR_INV_inv0));

    /* PORT3_1 (pin 45) is configured as TRIG_IN1 */
    PORT_SetPinMux(PORT3, 1U, kPORT_MuxAlt1);

    PORT3->PCR[1] =
        ((PORT3->PCR[1] &
          /* Mask bits to zero which are setting */
          (~(PORT_PCR_PS_MASK | PORT_PCR_PE_MASK | PORT_PCR_SRE_MASK | PORT_PCR_PFE_MASK | PORT_PCR_ODE_MASK | PORT_PCR_DSE_MASK | PORT_PCR_IBE_MASK | PORT_PCR_INV_MASK)))

         /* Pull Select: Enables internal pulldown resistor. */
         | PORT_PCR_PS(PCR_PS_ps0)

         /* Pull Enable: Disables. */
         | PORT_PCR_PE(PCR_PE_pe0)

         /* Slew Rate Enable: Fast. */
         | PORT_PCR_SRE(PCR_SRE_sre0)

         /* Passive Filter Enable: Disables. */
         | PORT_PCR_PFE(PCR_PFE_pfe0)

         /* Open Drain Enable: Disables. */
         | PORT_PCR_ODE(PCR_ODE_ode0)

         /* Drive Strength Enable: Low. */
         | PORT_PCR_DSE(PCR_DSE_dse0)

         /* Input Buffer Enable: Enables. */
         | PORT_PCR_IBE(PCR_IBE_ibe1)

         /* Invert Input: Does not invert. */
         | PORT_PCR_INV(PCR_INV_inv0));

    /* PORT3_12 (pin 38) is configured as P3_12 */
    PORT_SetPinMux(BOARD_LED_RED_PORT, BOARD_LED_RED_PIN, kPORT_MuxAlt0);

    PORT3->PCR[12] = ((PORT3->PCR[12] &
                       /* Mask bits to zero which are setting */
                       (~(PORT_PCR_IBE_MASK)))

                      /* Input Buffer Enable: Enables. */
                      | PORT_PCR_IBE(PCR_IBE_ibe1));

    PORT3->PCR[29] = ((PORT3->PCR[29] &
                       /* Mask bits to zero which are setting */
                       (~(PORT_PCR_MUX_MASK | PORT_PCR_IBE_MASK)))

                      /* Pin Multiplex Control: PORT3_29 (pin 32) is configured as P3_29. */
                      | PORT_PCR_MUX(PORT3_PCR29_MUX_mux00)

                      /* Input Buffer Enable: Enables. */
                      | PORT_PCR_IBE(PCR_IBE_ibe1));

    const port_pin_config_t port3_6_pin44_config = {/* 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,
                                                    /* Normal drive strength is configured */
                                                    .driveStrength1 = kPORT_NormalDriveStrength,
                                                    /* Pin is configured as PWM0_A0 */
                                                    .mux = kPORT_MuxAlt5,
                                                    /* 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};
    /* PORT3_6 (pin 44) is configured as PWM0_A0 */
    PORT_SetPinConfig(PORT3, 6U, &port3_6_pin44_config);

    PORT3->PCR[7] = ((PORT3->PCR[7] &
                      /* Mask bits to zero which are setting */
                      (~(PORT_PCR_MUX_MASK | PORT_PCR_IBE_MASK)))

                     /* Pin Multiplex Control: PORT3_7 (pin 43) is configured as P3_7. */
                     | PORT_PCR_MUX(PORT3_PCR7_MUX_mux00)

                     /* Input Buffer Enable: Enables. */
                     | PORT_PCR_IBE(PCR_IBE_ibe1));

    /* PORT3_8 (pin 42) is configured as P3_8 */
    PORT_SetPinMux(PORT3, 8U, kPORT_MuxAlt0);

    PORT3->PCR[8] = ((PORT3->PCR[8] &
                      /* Mask bits to zero which are setting */
                      (~(PORT_PCR_IBE_MASK)))

                     /* Input Buffer Enable: Enables. */
                     | PORT_PCR_IBE(PCR_IBE_ibe1));
}

main.c

/*
 * Copyright...
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "fsl_debug_console.h"
#include "board.h"
#include "app.h"
#include "fsl_pwm.h"
#include "fsl_port.h"
#include "fsl_gpio.h"
#include "fsl_common.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/
#ifndef APP_DEFAULT_PWM_FREQUENCY
#define APP_DEFAULT_PWM_FREQUENCY (1000UL)
#endif

#ifndef DEMO_PWM_DISABLE_MAP_OP
#define DEMO_PWM_DISABLE_MAP_OP
#endif

#define LONG_PRESS_THRESHOLD  (500U)  /* ms */
#define DUTY_STEP             (10U)
#define SDK_CLOCK_FREQ        SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY

/* 兼容不同SDK/SoC的中断清除方式 */
#if (defined(FSL_FEATURE_PORT_HAS_NO_INTERRUPT) && FSL_FEATURE_PORT_HAS_NO_INTERRUPT) || 
    (!defined(FSL_FEATURE_SOC_PORT_COUNT))
#define CLEAR_GPIO_IRQ_FLAGS(gpio, pin) GPIO_GpioClearInterruptFlags((gpio), 1U << (pin))
#else
#define CLEAR_GPIO_IRQ_FLAGS(gpio, pin) GPIO_PortClearInterruptFlags((gpio), 1U << (pin))
#endif

/*******************************************************************************
 * Globals
 ******************************************************************************/
volatile bool g_Button2Press = false;
volatile bool g_Button3Press = false;

volatile bool     g_PwmRunning      = false;
volatile uint32_t g_PwmDuty         = 50;    /* 0~100 */
volatile bool     g_DirectionState  = false; /* 0/1 */

/*******************************************************************************
 * Local helpers
 ******************************************************************************/
static inline void delay_ms(uint32_t ms)
{
    SDK_DelayAtLeastUs(ms * 1000U, SDK_CLOCK_FREQ);
}

static inline void Motor_SetDirection(bool dir)
{
    GPIO_PinWrite(BOARD_Direct_GPIO, BOARD_Direct1_GPIO_PIN, (uint8_t)dir);
    GPIO_PinWrite(BOARD_Direct_GPIO, BOARD_Direct2_GPIO_PIN, (uint8_t)!dir);
}

static inline void Motor_OutputOff(void)
{
    GPIO_PinWrite(BOARD_Direct_GPIO, BOARD_Direct1_GPIO_PIN, 0U);
    GPIO_PinWrite(BOARD_Direct_GPIO, BOARD_Direct2_GPIO_PIN, 0U);
}

static inline void PWM_ApplyDuty(uint32_t duty)
{
    PWM_UpdatePwmDutycycle(BOARD_PWM_BASEADDR, kPWM_Module_0, kPWM_PwmA, kPWM_SignedCenterAligned, duty);
    PWM_SetPwmLdok(BOARD_PWM_BASEADDR, kPWM_Control_Module_0, true);
}

static void PWM_StartWithCurrentState(void)
{
    Motor_SetDirection(g_DirectionState);
    PWM_StartTimer(BOARD_PWM_BASEADDR, kPWM_Control_Module_0);
    PWM_ApplyDuty(g_PwmDuty);
    GPIO_PinWrite(BOARD_LED_GPIO, BOARD_LED_GPIO_PIN, 0U); /* LED On */
    PRINTF("PWM Start, LED Onrn");
}

static void PWM_StopAndSafeOff(void)
{
    Motor_OutputOff();
    PWM_ApplyDuty(0U);
    delay_ms(1);
    PWM_StopTimer(BOARD_PWM_BASEADDR, kPWM_Control_Module_0);
    GPIO_PinWrite(BOARD_LED_GPIO, BOARD_LED_GPIO_PIN, 1U); /* LED Off */
    PRINTF("PWM Stop, LED Offrn");
}

static void PWM_DRV_Init3PhPwm(void)
{
    pwm_signal_param_t pwmSignal;
    uint32_t pwmSourceClockInHz = PWM_SRC_CLK_FREQ;
    uint32_t pwmFrequencyInHz   = APP_DEFAULT_PWM_FREQUENCY;

    /* deadtime ~650ns */
    uint16_t deadTimeVal = (uint16_t)(((uint64_t)pwmSourceClockInHz * 650U) / 1000000000ULL);

    pwmSignal.pwmChannel       = kPWM_PwmA;
    pwmSignal.level            = kPWM_HighTrue;
    pwmSignal.dutyCyclePercent = g_PwmDuty;
    pwmSignal.deadtimeValue    = deadTimeVal;
    pwmSignal.faultState       = kPWM_PwmFaultState0;
    pwmSignal.pwmchannelenable = true;

    PWM_SetupPwm(BOARD_PWM_BASEADDR,
                 kPWM_Module_0,
                 &pwmSignal,
                 1,
                 kPWM_SignedCenterAligned,
                 pwmFrequencyInHz,
                 pwmSourceClockInHz);
}

/* 读取按下持续时间(按键为低电平按下:0=pressed) */
static uint32_t Key_MeasurePressMs(GPIO_Type *gpio, uint32_t pin)
{
    uint32_t t = 0U;
    while (GPIO_PinRead(gpio, pin) == 0U)
    {
        delay_ms(1U);
        t++;
        if (t > LONG_PRESS_THRESHOLD)
        {
            break;
        }
    }
    return t;
}

typedef void (*key_action_t)(void);

static void Key_HandleOne(volatile bool *flag,
                          GPIO_Type *gpio,
                          uint32_t pin,
                          key_action_t onShort,
                          key_action_t onLong)
{
    if (!(*flag))
        return;

    *flag = false;

    uint32_t press_time = Key_MeasurePressMs(gpio, pin);
    if (press_time >= LONG_PRESS_THRESHOLD)
    {
        if (onLong) onLong();
    }
    else
    {
        if (onShort) onShort();
    }
}

/*******************************************************************************
 * Key actions
 ******************************************************************************/
static void SW2_Short(void)
{
    g_PwmDuty = (g_PwmDuty + DUTY_STEP) > 100U ? 100U : (g_PwmDuty + DUTY_STEP);
    PWM_ApplyDuty(g_PwmDuty);
    PRINTF("SW2 Short Press, Duty +10%% -> %d%%rn", g_PwmDuty);
}

static void SW2_Long(void)
{
    g_PwmRunning = !g_PwmRunning;
    if (g_PwmRunning)
    {
        PWM_StartWithCurrentState();
    }
    else
    {
        PWM_StopAndSafeOff();
    }
}

static void SW3_Short(void)
{
    /* 不低于10%(按你原逻辑:<11则置10) */
    g_PwmDuty = (g_PwmDuty > (DUTY_STEP + 10U)) ? (g_PwmDuty - DUTY_STEP) : 10U;
    PWM_ApplyDuty(g_PwmDuty);
    PRINTF("SW3 Short Press, Duty -10%% -> %d%%rn", g_PwmDuty);
}

static void SW3_Long(void)
{
    g_DirectionState = !g_DirectionState;
    Motor_SetDirection(g_DirectionState);
    PRINTF("SW3 Long Press, Direction State -> %drn", g_DirectionState);
}

static void Key_Process(void)
{
    Key_HandleOne(&g_Button2Press, BOARD_SW2_GPIO, BOARD_SW2_GPIO_PIN, SW2_Short, SW2_Long);
    Key_HandleOne(&g_Button3Press, BOARD_SW3_GPIO, BOARD_SW3_GPIO_PIN, SW3_Short, SW3_Long);
}

/*******************************************************************************
 * IRQ handlers
 ******************************************************************************/
void BOARD_SW2_IRQ_HANDLER(void)
{
    CLEAR_GPIO_IRQ_FLAGS(BOARD_SW2_GPIO, BOARD_SW2_GPIO_PIN);
    g_Button2Press = true;
    SDK_ISR_EXIT_BARRIER;
}

void BOARD_SW3_IRQ_HANDLER(void)
{
    CLEAR_GPIO_IRQ_FLAGS(BOARD_SW3_GPIO, BOARD_SW3_GPIO_PIN);
    g_Button3Press = true;
    SDK_ISR_EXIT_BARRIER;
}

/*******************************************************************************
 * Main
 ******************************************************************************/
int main(void)
{
    /* GPIO 配置 */
    const gpio_pin_config_t sw_in_cfg   = {kGPIO_DigitalInput,  0U};
    const gpio_pin_config_t led_out_cfg = {kGPIO_DigitalOutput, 1U}; /* 1=灭(按你原注释) */
    const gpio_pin_config_t dir_out_cfg = {kGPIO_DigitalOutput, 0U};

    pwm_config_t       pwmConfig;
    pwm_fault_param_t  faultConfig;

    BOARD_InitHardware();

    PRINTF("MCUX SDK version: %srn", MCUXSDK_VERSION_FULL_STR);
    PRINTF("FlexPWM + GPIO Key Control Examplern");

    /* GPIO init */
    GPIO_PinInit(BOARD_SW2_GPIO,   BOARD_SW2_GPIO_PIN,   &sw_in_cfg);
    GPIO_PinInit(BOARD_SW3_GPIO,   BOARD_SW3_GPIO_PIN,   &sw_in_cfg);
    GPIO_PinInit(BOARD_LED_GPIO,   BOARD_LED_GPIO_PIN,   &led_out_cfg);
    GPIO_PinInit(BOARD_Direct_GPIO, BOARD_Direct1_GPIO_PIN, &dir_out_cfg);
    GPIO_PinInit(BOARD_Direct_GPIO, BOARD_Direct2_GPIO_PIN, &dir_out_cfg);
    Motor_OutputOff();

    /* GPIO interrupt config */
#if (defined(FSL_FEATURE_PORT_HAS_NO_INTERRUPT) && FSL_FEATURE_PORT_HAS_NO_INTERRUPT) || 
    (!defined(FSL_FEATURE_SOC_PORT_COUNT))
    GPIO_SetPinInterruptConfig(BOARD_SW2_GPIO, BOARD_SW2_GPIO_PIN, kGPIO_InterruptFallingEdge);
    GPIO_SetPinInterruptConfig(BOARD_SW3_GPIO, BOARD_SW3_GPIO_PIN, kGPIO_InterruptFallingEdge);
#else
    PORT_SetPinInterruptConfig(BOARD_SW2_PORT, BOARD_SW2_GPIO_PIN, kPORT_InterruptFallingEdge);
    PORT_SetPinInterruptConfig(BOARD_SW3_PORT, BOARD_SW3_GPIO_PIN, kPORT_InterruptFallingEdge);
#endif
    EnableIRQ(BOARD_SW2_IRQ);
    EnableIRQ(BOARD_SW3_IRQ);

    /* PWM init */
    PWM_GetDefaultConfig(&pwmConfig);
#ifdef DEMO_PWM_CLOCK_DEVIDER
    pwmConfig.prescale = DEMO_PWM_CLOCK_DEVIDER;
#endif
    pwmConfig.reloadLogic      = kPWM_ReloadPwmFullCycle;
    pwmConfig.enableDebugMode  = true;

    if (PWM_Init(BOARD_PWM_BASEADDR, kPWM_Module_0, &pwmConfig) == kStatus_Fail)
    {
        PRINTF("PWM initialization failedrn");
        return 1;
    }

    /* Fault config */
    PWM_FaultDefaultConfig(&faultConfig);
#ifdef DEMO_PWM_FAULT_LEVEL
    faultConfig.faultLevel = DEMO_PWM_FAULT_LEVEL;
#endif
    PWM_SetupFaults(BOARD_PWM_BASEADDR, kPWM_Fault_0, &faultConfig);
    PWM_SetupFaults(BOARD_PWM_BASEADDR, kPWM_Fault_1, &faultConfig);
    PWM_SetupFaults(BOARD_PWM_BASEADDR, kPWM_Fault_2, &faultConfig);
    PWM_SetupFaults(BOARD_PWM_BASEADDR, kPWM_Fault_3, &faultConfig);

    PWM_SetupFaultDisableMap(BOARD_PWM_BASEADDR,
                             kPWM_Module_0,
                             kPWM_PwmA,
                             kPWM_faultchannel_0,
                             DEMO_PWM_DISABLE_MAP_OP(kPWM_FaultDisable_0 |
                                                     kPWM_FaultDisable_1 |
                                                     kPWM_FaultDisable_2 |
                                                     kPWM_FaultDisable_3));

    PWM_DRV_Init3PhPwm();
    PWM_SetPwmLdok(BOARD_PWM_BASEADDR, kPWM_Control_Module_0, true);

    /* 初始停止 */
    PWM_StopTimer(BOARD_PWM_BASEADDR, kPWM_Control_Module_0);
    GPIO_PinWrite(BOARD_LED_GPIO, BOARD_LED_GPIO_PIN, 1U);

    while (1U)
    {
        Key_Process();
        delay_ms(10U);
    }
}







关键词: Let's do 2025年第4期     直流电机控制    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]