● 概述
前面也已经分享了开箱贴与过程贴,本期来分享一下调试成果贴。按照活动要求,通过使用NXP FRDM-MCXA153核心开发板与扩展板,实现旋转编码器顺时针旋转,则增大两路PWM的占空比输出,步进长为2%,提高电机转速;旋转编码器逆时针旋转,则减少两路PWM的占空比输出,步进长为2%,降低电机转速。LCD彩屏显示当前占空比、电机状态、运行转向等信息。长按旋转编码器则停止电机转动,短按旋转编码器则恢复电机转动。短按一下NXP FRDM-MCXA153核心开发板上的SW2则使两个电机逆时针方向转动;短按一下NXP FRDM-MCXA153核心开发板上的SW3则使两个电机顺时针方向转动。初始默认状态:占空比为0,电机使能状态开启,运行转向为逆时针。
● 硬件使用接口
TB6612驱动器
P3_8(PWM0_A1) PWMA
P3_10(PWM0_A2) PWMB
P1_10 AIN1
P1_12 AIN2
P3_30 BIN1
P3_31 BIN2
根据上期分享的过程贴可知,AIN1/AIN2同时为低电平或者为高电平,则紧急制动刹车MOT.A;BIN1/BIN2同时为低电平或者为高电平,则紧急制动刹车MOT.B;STBY脚是用于快速使能MOT.A与MOT.B两个电机的运转,可上拉至Vcc电压通过10K电阻连接。STBY脚直接接地时则表示禁用。项目中暂未使用STBY信号脚,也没必要开启紧急制动电机,只需关闭PWM的占空比输出,亦可达到关闭电机运行效果。
SPI LCD彩屏
P1_0 MOSI
P1_1 SCLK
P1_3 CS
P3_12 DC
P3_27 RES
P3_28 BLK
旋钮编码器
P2_1 旋转A相信号(ECODE_A)
P2_2 旋转B相信号(ECODE_B)
P2_3 按键信号(ECODE_D)
按键
P3_29 SW2(控制电机逆时针方向转动)
P1_7 SW3(控制电机顺时针方向转动)
声光响应部分
P1_11 BEEP蜂鸣器
P3_13 GREEN指示灯
● 硬件结构框图

● 硬件原理图
核心开发板上的接口原理图:



扩展板上有关的接口原理图:


● 硬件连接
实物硬件连接如下图所示:

开机默认状态SPI屏显示如下:

占空比可调范围为0~100%,旋钮编码器每步进2%:

● 项目代码
由于帖子篇幅受限,这里只分享部分关键源码,如有需求,请回帖。
pin_mux.c
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 PORT2: Peripheral clock is enabled */
CLOCK_EnableClock(kCLOCK_GatePORT2);
/* 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);
/* PORT2 peripheral is released from reset */
RESET_ReleasePeripheralReset(kPORT2_RST_SHIFT_RSTn);
/* PORT3 peripheral is released from reset */
RESET_ReleasePeripheralReset(kPORT3_RST_SHIFT_RSTn);
/* FLEXPWM0 peripheral is released from reset */
RESET_ReleasePeripheralReset(kFLEXPWM0_RST_SHIFT_RSTn);
/* Write to GPIO1: Peripheral clock is enabled */
CLOCK_EnableClock(kCLOCK_GateGPIO1);
/* Write to GPIO2: Peripheral clock is enabled */
CLOCK_EnableClock(kCLOCK_GateGPIO2);
/* Write to GPIO3: Peripheral clock is enabled */
CLOCK_EnableClock(kCLOCK_GateGPIO3);
/* GPIO1 peripheral is released from reset */
RESET_ReleasePeripheralReset(kGPIO1_RST_SHIFT_RSTn);
/* GPIO2 peripheral is released from reset */
RESET_ReleasePeripheralReset(kGPIO2_RST_SHIFT_RSTn);
/* GPIO3 peripheral is released from reset */
RESET_ReleasePeripheralReset(kGPIO3_RST_SHIFT_RSTn);
/* PIO2_1, PIO2_2, PIO2_3 配置为GPIO输入 */
PORT_SetPinMux(PORT2, 1U, kPORT_MuxAlt0);
PORT_SetPinMux(PORT2, 2U, kPORT_MuxAlt0);
PORT_SetPinMux(PORT2, 3U, kPORT_MuxAlt0);
/* 使能上拉电阻 */
PORT2->PCR[1] |= PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
PORT2->PCR[2] |= PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
PORT2->PCR[3] |= PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
gpio_pin_config_t LED_GREEN_config = {
.pinDirection = kGPIO_DigitalOutput,
.outputLogic = 1U
};
/* Initialize GPIO functionality on pin PIO3_13 (pin 37) */
GPIO_PinInit(BOARD_LED_GREEN_GPIO, BOARD_LED_GREEN_GPIO_PIN, &LED_GREEN_config);
gpio_pin_config_t BEEP_OUT_config = {
.pinDirection = kGPIO_DigitalOutput,
.outputLogic = 0U
};
/* Initialize GPIO functionality on pin PIO1_11 (pin 5) */
GPIO_PinInit(BEEP_OUT_GPIO, BEEP_OUT_GPIO_PIN, &BEEP_OUT_config);
gpio_pin_config_t DIR_GPIO_config = {
.pinDirection = kGPIO_DigitalOutput,
.outputLogic = 0U
};
/* Initialize GPIO functionality on pin PIO1_10 (pin 4) */
GPIO_PinInit(DIR_GPIO_A1_GPIO, DIR_GPIO_A1_GPIO_PIN, &DIR_GPIO_config);
/* Initialize GPIO functionality on pin PIO1_12 (pin 6) */
GPIO_PinInit(DIR_GPIO_A2_GPIO, DIR_GPIO_A2_GPIO_PIN, &DIR_GPIO_config);
/* Initialize GPIO functionality on pin PIO3_30 (pin 31) */
GPIO_PinInit(DIR_GPIO_B1_GPIO, DIR_GPIO_B1_GPIO_PIN, &DIR_GPIO_config);
/* Initialize GPIO functionality on pin PIO3_31 (pin 30) */
GPIO_PinInit(DIR_GPIO_B2_GPIO, DIR_GPIO_B2_GPIO_PIN, &DIR_GPIO_config);
gpio_pin_config_t SPI_LCD_config = {
kGPIO_DigitalOutput,
1,
};
GPIO_PinInit(GPIO1, 0U, &SPI_LCD_config); // MOSI
GPIO_PinInit(GPIO1, 1U, &SPI_LCD_config); // SCLK
GPIO_PinInit(GPIO1, 3U, &SPI_LCD_config); // CS
GPIO_PinInit(GPIO3, 12U,&SPI_LCD_config); // DC
GPIO_PinInit(GPIO3, 27U, &SPI_LCD_config); // RES
GPIO_PinInit(GPIO3, 28U, &SPI_LCD_config); // BLK
gpio_pin_config_t SW_config = {
kGPIO_DigitalInput,
0U,
};
GPIO_PinInit(BOARD_S2_GPIO, BOARD_S2_GPIO_PIN, &SW_config);
GPIO_PinInit(BOARD_S3_GPIO, BOARD_S3_GPIO_PIN, &SW_config);
gpio_pin_config_t ecode_gpio_config = {
kGPIO_DigitalInput,
0U
};
GPIO_PinInit(ENCODER_GPIO, ECODE_A_PIN, &ecode_gpio_config);
GPIO_PinInit(ENCODER_GPIO, ECODE_B_PIN, &ecode_gpio_config);
GPIO_PinInit(ENCODER_GPIO, ECODE_D_PIN, &ecode_gpio_config);
const port_pin_config_t LED_GREEN = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as P3_13 */
kPORT_MuxAlt0,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT3_13 (pin 37) is configured as P3_13 */
PORT_SetPinConfig(PORT3, BOARD_LED_GREEN_GPIO_PIN, &LED_GREEN);
const port_pin_config_t BEEP_OUT = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as P1_11 */
kPORT_MuxAlt0,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT1_11 (pin 5) is configured as P1_11 */
PORT_SetPinConfig(PORT1, BEEP_OUT_GPIO_PIN, &BEEP_OUT);
const port_pin_config_t DIR_GPIO = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as P1_12/P1_13/P3_30/P3_31 */
kPORT_MuxAlt0,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT1_10 (pin 4) is configured as P1_10 */
PORT_SetPinConfig(PORT1, DIR_GPIO_A1_GPIO_PIN, &DIR_GPIO);
/* PORT1_12 (pin 6) is configured as P1_12 */
PORT_SetPinConfig(PORT1, DIR_GPIO_A2_GPIO_PIN, &DIR_GPIO);
/* PORT3_30 (pin 31) is configured as P3_30 */
PORT_SetPinConfig(PORT3, DIR_GPIO_B1_GPIO_PIN, &DIR_GPIO);
/* PORT3_31 (pin 30) is configured as P3_31 */
PORT_SetPinConfig(PORT3, DIR_GPIO_B2_GPIO_PIN, &DIR_GPIO);
const port_pin_config_t SPI_LCD = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as P3_12 */
kPORT_MuxAlt0,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
PORT_SetPinConfig(PORT1, BOARD_LCD_GPIO_CS_PIN, &SPI_LCD);
PORT_SetPinConfig(PORT1, BOARD_LCD_GPIO_SCLK_PIN, &SPI_LCD);
PORT_SetPinConfig(PORT3, BOARD_LCD_GPIO_BLK_PIN, &SPI_LCD);
PORT_SetPinConfig(PORT1, BOARD_LCD_GPIO_MOSI_PIN, &SPI_LCD);
PORT_SetPinConfig(PORT3, BOARD_LCD_GPIO_DC_PIN, &SPI_LCD);
PORT_SetPinConfig(PORT3, BOARD_LCD_GPIO_RES_PIN, &SPI_LCD);
const port_pin_config_t port0_2_pin51_config = {/* Internal pull-up resistor is enabled */
kPORT_PullUp,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as LPUART0_RXD */
kPORT_MuxAlt2,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
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 */
kPORT_PullUp,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as LPUART0_TXD */
kPORT_MuxAlt2,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
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 */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as TRIG_IN3 */
kPORT_MuxAlt1,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
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 */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as TRIG_IN2 */
kPORT_MuxAlt1,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT1_6 (pin 64) is configured as TRIG_IN2 */
PORT_SetPinConfig(PORT1, 6U, &port1_6_pin64_config);
const port_pin_config_t port3_0_pin46_config = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as TRIG_IN0 */
kPORT_MuxAlt1,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT3_0 (pin 46) is configured as TRIG_IN0 */
PORT_SetPinConfig(PORT3, 0U, &port3_0_pin46_config);
const port_pin_config_t port3_1_pin45_config = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as TRIG_IN1 */
kPORT_MuxAlt1,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT3_1 (pin 45) is configured as TRIG_IN1 */
PORT_SetPinConfig(PORT3, 1U, &port3_1_pin45_config);
const port_pin_config_t port3_10_pin40_config = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as PWM0_A2 */
kPORT_MuxAlt5,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT3_10 (pin 40) is configured as PWM0_A2 */
PORT_SetPinConfig(PORT3, 10U, &port3_10_pin40_config);
const port_pin_config_t port3_6_pin44_config = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as PWM0_A0 */
kPORT_MuxAlt5,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT3_6 (pin 44) is configured as PWM0_A0 */
PORT_SetPinConfig(PORT3, 6U, &port3_6_pin44_config);
const port_pin_config_t port3_7_pin43_config = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as PWM0_B0 */
kPORT_MuxAlt5,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT3_7 (pin 43) is configured as PWM0_B0 */
PORT_SetPinConfig(PORT3, 7U, &port3_7_pin43_config);
const port_pin_config_t port3_8_pin42_config = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as PWM0_A1 */
kPORT_MuxAlt5,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT3_8 (pin 42) is configured as PWM0_A1 */
PORT_SetPinConfig(PORT3, 8U, &port3_8_pin42_config);
const port_pin_config_t port3_29_pin32_config = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as P3_29 */
kPORT_MuxAlt0,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT3_29 (pin 32) is configured as P3_29 */
PORT_SetPinConfig(PORT3, 29U, &port3_29_pin32_config); /* 初始化按钮(SW2)引脚 */
const port_pin_config_t port1_7_pin1_config = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as P1_7 */
kPORT_MuxAlt0,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT1_7 (pin 1) is configured as P1_7 */
PORT_SetPinConfig(PORT1, 7U, &port1_7_pin1_config); /* 初始化按钮(SW3)引脚 */
const port_pin_config_t port2_1_pin1_config = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as P2_1 */
kPORT_MuxAlt0,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT2_1 (pin 1) is configured as P2_1 */
PORT_SetPinConfig(PORT2, 1U, &port2_1_pin1_config); /* 初始化ECODE_A(A相)引脚 */
const port_pin_config_t port2_2_pin2_config = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as P2_2 */
kPORT_MuxAlt0,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT2_2 (pin 2) is configured as P2_2 */
PORT_SetPinConfig(PORT2, 2U, &port2_2_pin2_config); /* 初始化ECODE_B(B相)引脚 */
const port_pin_config_t port2_3_pin3_config = {/* Internal pull-up/down resistor is disabled */
kPORT_PullDisable,
/* Low internal pull resistor value is selected. */
kPORT_LowPullResistor,
/* Fast slew rate is configured */
kPORT_FastSlewRate,
/* Passive input filter is disabled */
kPORT_PassiveFilterDisable,
/* Open drain output is disabled */
kPORT_OpenDrainDisable,
/* Low drive strength is configured */
kPORT_LowDriveStrength,
/* Normal drive strength is configured */
kPORT_NormalDriveStrength,
/* Pin is configured as P2_3 */
kPORT_MuxAlt0,
/* Digital input enabled */
kPORT_InputBufferEnable,
/* Digital input is not inverted */
kPORT_InputNormal,
/* Pin Control Register fields [15:0] are not locked */
kPORT_UnlockRegister};
/* PORT2_3 (pin 3) is configured as P2_3 */
PORT_SetPinConfig(PORT2, 3U, &port2_3_pin3_config); /* 初始化ECODE_D(按键)引脚 */
}rotary_encoder_switch.c
#include "fsl_common.h"
#include "fsl_gpio.h"
#include "fsl_port.h"
#include "fsl_debug_console.h"
#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "rotary_encoder_switch.h"
#include <stdbool.h>
/* 全局变量 */
extern encoder_state_t g_encoder;
extern volatile uint32_t g_systick_count;
/* 获取当前时间(毫秒) */
uint32_t get_current_time(void)
{
return g_systick_count;
}
/* 旋转编码器A相(CLK)中断处理 */
static void handle_ecode_a_interrupt(void)
{
uint32_t current_time = get_current_time();
/* 旋转防抖处理 */
if ((current_time - g_encoder.last_rotary_time) < ROTARY_DEBOUNCE_MS) {
GPIO_GpioClearInterruptFlags(ENCODER_GPIO, 1U << ECODE_A_PIN);
return;
}
/* 读取当前A相和B相状态 */
uint32_t a_state = GPIO_PinRead(ENCODER_GPIO, ECODE_A_PIN);
uint32_t b_state = GPIO_PinRead(ENCODER_GPIO, ECODE_B_PIN);
/* 检测A相边沿变化并判断方向 */
if (a_state != g_encoder.last_clk_state) {
if (a_state == 0) { /* A相下降沿 */
if (b_state == 0) {
/* A下降沿,B为高:逆时针旋转 */
g_encoder.count--;
if(g_encoder.count <= 1 || g_encoder.count >= 100){
g_encoder.count = 1;
}
g_encoder.last_dir = ROTARY_DIR_CCW;
g_encoder.event = ENCODER_EVENT_CCW_ROTATION;
} else {
/* A下降沿,B为低:顺时针旋转 */
g_encoder.count++;
if(g_encoder.count <= 1 || g_encoder.count >= 100){
g_encoder.count = 100;
}
g_encoder.last_dir = ROTARY_DIR_CW;
g_encoder.event = ENCODER_EVENT_CW_ROTATION;
}
} else { /* A相上升沿 */
if (b_state == 1) {
/* A上升沿,B为低:逆时针旋转 */
g_encoder.count--;
if(g_encoder.count <= 1 || g_encoder.count >= 100){
g_encoder.count = 1;
}
g_encoder.last_dir = ROTARY_DIR_CCW;
g_encoder.event = ENCODER_EVENT_CCW_ROTATION;
} else {
/* A上升沿,B为高:顺时针旋转 */
g_encoder.count++;
if(g_encoder.count <= 1 || g_encoder.count >= 100){
g_encoder.count = 100;
}
g_encoder.last_dir = ROTARY_DIR_CW;
g_encoder.event = ENCODER_EVENT_CW_ROTATION;
}
}
g_encoder.last_rotary_time = current_time;
}
g_encoder.last_clk_state = a_state;
GPIO_GpioClearInterruptFlags(ENCODER_GPIO, 1U << ECODE_A_PIN);
}
static void handle_ecode_b_interrupt(void)
{
GPIO_GpioClearInterruptFlags(ENCODER_GPIO, 1U << ECODE_B_PIN);
}
/* 按键(D相)中断处理 */
static void handle_ecode_d_interrupt(void)
{
uint32_t current_time = get_current_time();
static uint32_t last_debounce_time = 0;
/* 按键防抖处理 */
if ((current_time - last_debounce_time) < BUTTON_DEBOUNCE_MS) {
GPIO_GpioClearInterruptFlags(ENCODER_GPIO, 1U << ECODE_D_PIN);
return;
}
uint32_t btn_state = GPIO_PinRead(ENCODER_GPIO, ECODE_D_PIN);
/* 检测按键状态变化 */
if (btn_state != g_encoder.last_btn_state) {
if (btn_state == 0) { /* 按键按下(下降沿) */
g_encoder.button_pressed = true;
g_encoder.button_press_time = current_time;
g_encoder.event = ENCODER_EVENT_BUTTON_PRESS;
} else { /* 按键释放(上升沿) */
g_encoder.button_pressed = false;
g_encoder.event = ENCODER_EVENT_BUTTON_RELEASE;
}
g_encoder.last_btn_state = btn_state;
last_debounce_time = current_time;
}
GPIO_GpioClearInterruptFlags(ENCODER_GPIO, 1U << ECODE_D_PIN);
}
/* 检查长按事件(在主循环中调用) */
void encoder_check_long_press(void)
{
if (g_encoder.button_pressed) {
uint32_t press_duration = get_current_time() - g_encoder.button_press_time;
if (press_duration > 1000) { /* 长按超过1秒 */
g_encoder.button_long_press = true;
g_encoder.event = ENCODER_EVENT_BUTTON_LONG_PRESS;
g_encoder.button_pressed = false; /* 重置按下状态,避免重复检测 */
}
}
}
/* 获取编码器事件 */
encoder_event_t encoder_get_event(bool clear_event)
{
encoder_event_t event = g_encoder.event;
if (clear_event && event != ENCODER_EVENT_NONE) {
g_encoder.event = ENCODER_EVENT_NONE;
}
return event;
}
/* 清除长按标志 */
void encoder_clear_long_press(void)
{
g_encoder.button_long_press = false;
}
/* 获取旋转计数值 */
int32_t encoder_get_count(void)
{
return g_encoder.count;
}
/* 获取按键状态 */
bool encoder_is_button_pressed(void)
{
return g_encoder.button_pressed;
}
/* 重置计数值 */
void encoder_reset_count(void)
{
g_encoder.count = 0;
}
/* 获取旋转方向 */
rotary_direction_t encoder_get_last_direction(void)
{
return g_encoder.last_dir;
}
/* 初始化旋转编码器 */
void Encoder_Init(void)
{
/* 配置ECODE_A引脚中断(双边沿触发,检测旋转) */
GPIO_SetPinInterruptConfig(ENCODER_GPIO, ECODE_A_PIN,kGPIO_InterruptEitherEdge);
/* 配置ECODE_B引脚中断(可选,双边沿触发) */
GPIO_SetPinInterruptConfig(ENCODER_GPIO, ECODE_B_PIN,kGPIO_InterruptEitherEdge);
/* 配置ECODE_D引脚中断(双边沿触发,检测按键按下和释放) */
GPIO_SetPinInterruptConfig(ENCODER_GPIO, ECODE_D_PIN,kGPIO_InterruptEitherEdge);
/* 使能PORT0中断并设置优先级 */
NVIC_SetPriority(ENCODER_BUTTON_IRQ, 5);
EnableIRQ(ENCODER_BUTTON_IRQ);
/* 初始化状态 */
g_encoder.last_clk_state = GPIO_PinRead(ENCODER_GPIO, ECODE_A_PIN);
g_encoder.last_btn_state = GPIO_PinRead(ENCODER_GPIO, ECODE_D_PIN);
PRINTF("Rotary Encoder Initialization\r\n");
}
/* ENCODER中断服务函数 */
void ENCODER_BUTTON_IRQ_HANDLER(void)
{
uint32_t flags = GPIO_GpioGetInterruptFlags(ENCODER_GPIO);
if (flags & (1U << ECODE_A_PIN)) {
handle_ecode_a_interrupt();
}
if (flags & (1U << ECODE_B_PIN)) {
handle_ecode_b_interrupt();
}
if (flags & (1U << ECODE_D_PIN)) {
handle_ecode_d_interrupt();
}
__DSB();
}
/* SysTick中断处理函数(1ms中断) */
void SysTick_Handler(void)
{
g_systick_count++;
}pwm.c
#include "fsl_debug_console.h"
#include "board.h"
#include "app.h"
#include "fsl_pwm.h"
#include "math.h"
#include "lcd_init.h"
#include "lcd.h"
#include "pic.h"
#include "rotary_encoder_switch.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/* Definition for default PWM frequence in hz. */
#ifndef APP_DEFAULT_PWM_FREQUENCY
#define APP_DEFAULT_PWM_FREQUENCY (1000UL)
#endif
/* DEMO_PWM_DISABLE_MAP_OP: Operator, it can be define as '~') in app.h */
#ifndef DEMO_PWM_DISABLE_MAP_OP
#define DEMO_PWM_DISABLE_MAP_OP
#endif
/*******************************************************************************
* Variables
******************************************************************************/
typedef struct
{
unsigned char Index[3];
unsigned char Msk[32];
}typFNT_GB16;
extern const typFNT_GB16 tfont16[];
typedef struct
{
unsigned char Index[3];
unsigned char Msk[24];
}typFNT_GB12;
extern const typFNT_GB12 tfont12[];
encoder_state_t g_encoder = {0};
volatile uint32_t g_systick_count = 0;
//默认电机初始转向(逆时针)
volatile bool S2_ButtonPress = true;
volatile bool S3_ButtonPress = false;
/*******************************************************************************
* Code
******************************************************************************/
void delay_us(uint16_t us)
{
SDK_DelayAtLeastUs((1000U / APP_DEFAULT_PWM_FREQUENCY) * us, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);
}
void delay_ms(uint16_t ms)
{
SDK_DelayAtLeastUs((1000000U / APP_DEFAULT_PWM_FREQUENCY) * ms, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);
}
void Display_text(void)
{
LCD_ShowIntNum(8,10,2026,4,BLUE,GREEN,16);
LCD_ShowString(42,10,(uint8_t *)"DigiKey",BLUE,GREEN,16,0);
LCD_ShowChinese(8,30,(uint8_t *)&tfont16[0],BLUE,GREEN,16,0); //基
LCD_ShowChinese(24,30,(uint8_t *)&tfont16[1],BLUE,GREEN,16,0); //于
LCD_ShowChinese(40,30,(uint8_t *)&tfont16[2],BLUE,GREEN,16,0); //单
LCD_ShowChinese(56,30,(uint8_t *)&tfont16[3],BLUE,GREEN,16,0); //片
LCD_ShowChinese(72,30,(uint8_t *)&tfont16[4],BLUE,GREEN,16,0); //机
LCD_ShowString(8,50,(uint8_t *)"FRDM-MCXA153",RED,GREEN,16,0);
LCD_ShowChinese(8,70,(uint8_t *)&tfont16[5],RED,GREEN,16,0); //直
LCD_ShowChinese(24,70,(uint8_t *)&tfont16[6],RED,GREEN,16,0); //流
LCD_ShowChinese(40,70,(uint8_t *)&tfont16[7],RED,GREEN,16,0); //电
LCD_ShowChinese(56,70,(uint8_t *)&tfont16[8],RED,GREEN,16,0); //机
LCD_ShowChinese(72,70,(uint8_t *)&tfont16[9],RED,GREEN,16,0); //控
LCD_ShowChinese(88,70,(uint8_t *)&tfont16[10],RED,GREEN,16,0); //制
LCD_ShowChinese(8,90,(uint8_t *)&tfont12[0],RED,WHITE,12,0); //当
LCD_ShowChinese(20,90,(uint8_t *)&tfont12[1],RED,WHITE,12,0); //前
LCD_ShowChinese(32,90,(uint8_t *)&tfont12[2],RED,WHITE,12,0); //占
LCD_ShowChinese(44,90,(uint8_t *)&tfont12[3],RED,WHITE,12,0); //空
LCD_ShowChinese(56,90,(uint8_t *)&tfont12[4],RED,WHITE,12,0); //比
LCD_ShowString(68,90,(uint8_t *)"(%)",RED,WHITE,12,0);
LCD_ShowChinese(86,90,(uint8_t *)&tfont12[5],RED,WHITE,12,0); //:
LCD_ShowIntNum(92,90,0,3,RED,WHITE,12);
LCD_ShowChinese(8,105,(uint8_t *)&tfont12[6],RED,WHITE,12,0); //电
LCD_ShowChinese(20,105,(uint8_t *)&tfont12[7],RED,WHITE,12,0); //机
LCD_ShowChinese(32,105,(uint8_t *)&tfont12[8],RED,WHITE,12,0); //状
LCD_ShowChinese(44,105,(uint8_t *)&tfont12[9],RED,WHITE,12,0); //态
LCD_ShowChinese(56,105,(uint8_t *)&tfont12[5],RED,WHITE,12,0); //:
LCD_ShowString(68,105,(uint8_t *)"Opened",RED,WHITE,12,0);
LCD_ShowChinese(8,120,(uint8_t *)&tfont12[10],RED,WHITE,12,0); //运
LCD_ShowChinese(20,120,(uint8_t *)&tfont12[11],RED,WHITE,12,0); //行
LCD_ShowChinese(32,120,(uint8_t *)&tfont12[12],RED,WHITE,12,0); //转
LCD_ShowChinese(44,120,(uint8_t *)&tfont12[13],RED,WHITE,12,0); //向
LCD_ShowChinese(56,120,(uint8_t *)&tfont12[5],RED,WHITE,12,0); //:
LCD_ShowString(68,120,(uint8_t *)"<<<<",RED,WHITE,12,0);
LCD_ShowString(46,142,(uint8_t *)"2026-01-22",BLUE,GREEN,16,0);
}
void BOARD_S2_IRQ_HANDLER(void)
{
/* Clear external interrupt flag. */
GPIO_GpioClearInterruptFlags(BOARD_S2_GPIO, 1U << BOARD_S2_GPIO_PIN);
/* Change state of button. */
S2_ButtonPress = true;
S3_ButtonPress = false;
SDK_ISR_EXIT_BARRIER;
}
void BOARD_S3_IRQ_HANDLER(void)
{
/* Clear external interrupt flag. */
GPIO_GpioClearInterruptFlags(BOARD_S3_GPIO, 1U << BOARD_S3_GPIO_PIN);
/* Change state of button. */
S3_ButtonPress = true;
S2_ButtonPress = false;
SDK_ISR_EXIT_BARRIER;
}
static void PWM_DRV_Init3PhPwm(void)
{
uint16_t deadTimeVal;
pwm_signal_param_t pwmSignal[2];
uint32_t pwmSourceClockInHz;
uint32_t pwmFrequencyInHz = APP_DEFAULT_PWM_FREQUENCY;
pwmSourceClockInHz = PWM_SRC_CLK_FREQ;
/* Set deadtime count, we set this to about 650ns */
deadTimeVal = ((uint64_t)pwmSourceClockInHz * 650) / 1000000000;
pwmSignal[0].pwmChannel = kPWM_PwmA;
pwmSignal[0].level = kPWM_HighTrue;
pwmSignal[0].dutyCyclePercent = 0; /* percent dutycycle */
pwmSignal[0].deadtimeValue = deadTimeVal;
pwmSignal[0].faultState = kPWM_PwmFaultState0;
pwmSignal[0].pwmchannelenable = true;
pwmSignal[1].pwmChannel = kPWM_PwmB;
pwmSignal[1].level = kPWM_HighTrue;
/* Dutycycle field of PWM B does not matter as we are running in PWM A complementary mode */
pwmSignal[1].dutyCyclePercent = 0;
pwmSignal[1].deadtimeValue = deadTimeVal;
pwmSignal[1].faultState = kPWM_PwmFaultState0;
pwmSignal[1].pwmchannelenable = true;
/*********** PWMA_SM0 - phase A, configuration, setup 2 channel as an example ************/
PWM_SetupPwm(BOARD_PWM_BASEADDR, kPWM_Module_0, pwmSignal, 2, kPWM_SignedCenterAligned, pwmFrequencyInHz,
pwmSourceClockInHz);
/*********** PWMA_SM1 - phase B configuration, setup PWM A channel only ************/
PWM_SetupPwm(BOARD_PWM_BASEADDR, kPWM_Module_1, pwmSignal, 1, kPWM_SignedCenterAligned, pwmFrequencyInHz,
pwmSourceClockInHz / (1 << DEMO_PWM_CLOCK_DEVIDER));
/*********** PWMA_SM2 - phase C configuration, setup PWM A channel only ************/
PWM_SetupPwm(BOARD_PWM_BASEADDR, kPWM_Module_2, pwmSignal, 1, kPWM_SignedCenterAligned, pwmFrequencyInHz,
pwmSourceClockInHz / (1 << DEMO_PWM_CLOCK_DEVIDER));
}
static status_t PWM_DRV_Config(void)
{
/* Structure of initialize PWM */
pwm_config_t pwmConfig;
pwm_fault_param_t faultConfig;
PWM_GetDefaultConfig(&pwmConfig);
pwmConfig.prescale = DEMO_PWM_CLOCK_DEVIDER;
pwmConfig.reloadLogic = kPWM_ReloadPwmFullCycle;
pwmConfig.pairOperation = kPWM_ComplementaryPwmA;
pwmConfig.enableDebugMode = true;
if (PWM_Init(BOARD_PWM_BASEADDR, kPWM_Module_0, &pwmConfig) == kStatus_Fail)
{
PRINTF("PWM initialization failed\n");
return 1;
}
pwmConfig.clockSource = kPWM_Submodule0Clock;
pwmConfig.prescale = kPWM_Prescale_Divide_1;
pwmConfig.initializationControl = kPWM_Initialize_MasterSync;
if (PWM_Init(BOARD_PWM_BASEADDR, kPWM_Module_1, &pwmConfig) == kStatus_Fail)
{
PRINTF("PWM initialization failed\n");
return 1;
}
if (PWM_Init(BOARD_PWM_BASEADDR, kPWM_Module_2, &pwmConfig) == kStatus_Fail)
{
PRINTF("PWM initialization failed\n");
return 1;
}
PWM_FaultDefaultConfig(&faultConfig);
faultConfig.faultLevel = DEMO_PWM_FAULT_LEVEL;
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_SetupFaultDisableMap(BOARD_PWM_BASEADDR, kPWM_Module_1, kPWM_PwmA, kPWM_faultchannel_0,
DEMO_PWM_DISABLE_MAP_OP(kPWM_FaultDisable_0 | kPWM_FaultDisable_1 | kPWM_FaultDisable_2 | kPWM_FaultDisable_3));
PWM_SetupFaultDisableMap(BOARD_PWM_BASEADDR, kPWM_Module_2, 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 | kPWM_Control_Module_1 | kPWM_Control_Module_2, true);
PWM_StartTimer(BOARD_PWM_BASEADDR, kPWM_Control_Module_0 | kPWM_Control_Module_1 | kPWM_Control_Module_2);
return kStatus_Success;
}
void Set_RunSpeed(uint32_t duty_cycle)
{
PWM_UpdatePwmDutycycle(BOARD_PWM_BASEADDR,kPWM_Module_1, kPWM_PwmA, kPWM_SignedCenterAligned, duty_cycle);
PWM_UpdatePwmDutycycle(BOARD_PWM_BASEADDR,kPWM_Module_2, kPWM_PwmA, kPWM_SignedCenterAligned, duty_cycle);
PWM_SetPwmLdok(BOARD_PWM_BASEADDR, kPWM_Control_Module_1 | kPWM_Control_Module_2, true);
}
void Beep_Led_Response(void)
{
BEEP_OUT_ON();
LED_GREEN_ON();
delay_ms(100);
BEEP_OUT_OFF();
LED_GREEN_OFF();
}
void Run_Nishizhen_Direction(void)
{
DIR_GPIO_A1_ON();
DIR_GPIO_A2_OFF();
DIR_GPIO_B1_ON();
DIR_GPIO_B2_OFF();
}
void Run_Shunshizhen_Direction(void)
{
DIR_GPIO_A1_OFF();
DIR_GPIO_A2_ON();
DIR_GPIO_B1_OFF();
DIR_GPIO_B2_ON();
}
/*!
* @brief Main function
*/
int main(void)
{
static uint32_t pwmVal = 0;
/* Board pin, clock, debug console init */
BOARD_InitHardware();
SysTick_Config(SystemCoreClock / 1000U);
LCD_Init();
LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
LCD_ShowPicture(0,0,128,160,gImage_1);
delay_ms(2000U);
LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
Display_text();
if (PWM_DRV_Config() == kStatus_Fail)
{
PRINTF("PWM Init Config failed!\n");
return kStatus_Fail;
}
Encoder_Init();
GPIO_SetPinInterruptConfig(BOARD_S2_GPIO, BOARD_S2_GPIO_PIN, kGPIO_InterruptFallingEdge);
GPIO_SetPinInterruptConfig(BOARD_S3_GPIO, BOARD_S3_GPIO_PIN, kGPIO_InterruptFallingEdge);
EnableIRQ(BOARD_S2_IRQ);
EnableIRQ(BOARD_S3_IRQ);
while (1)
{
delay_ms(20);
encoder_check_long_press();
/* 处理编码器事件 */
encoder_event_t event = encoder_get_event(true);
switch (event) {
case ENCODER_EVENT_CW_ROTATION:
PRINTF("CountValue++ >>>: %d\r\n", g_encoder.count);
Beep_Led_Response();
if (pwmVal > 98)pwmVal = 98;
pwmVal = pwmVal + 2;
Set_RunSpeed(pwmVal);
LCD_ShowIntNum(92,90,pwmVal,3,RED,WHITE,12);
LCD_ShowString(68,105,(uint8_t *)"Opened",RED,WHITE,12,0);
break;
case ENCODER_EVENT_CCW_ROTATION:
PRINTF("CountValue-- <<<: %d\r\n", g_encoder.count);
Beep_Led_Response();
if(pwmVal < 2)pwmVal = 2;
pwmVal = pwmVal - 2;
Set_RunSpeed(pwmVal);
LCD_ShowIntNum(92,90,pwmVal,3,RED,WHITE,12);
LCD_ShowString(68,105,(uint8_t *)"Opened",RED,WHITE,12,0);
break;
case ENCODER_EVENT_BUTTON_PRESS:
PRINTF("Button pressed\r\n");
Set_RunSpeed(pwmVal);
LCD_ShowString(68,105,(uint8_t *)"Opened",RED,WHITE,12,0);
break;
case ENCODER_EVENT_BUTTON_RELEASE:
PRINTF("Button release\r\n");
delay_ms(20);
Beep_Led_Response();
break;
case ENCODER_EVENT_BUTTON_LONG_PRESS:
PRINTF("Long press the button\r\n");
encoder_reset_count();
PRINTF("Counter reset\r\n");
encoder_clear_long_press();
Set_RunSpeed(0);
Beep_Led_Response();
LCD_ShowString(68,105,(uint8_t *)"Closed",RED,WHITE,12,0);
break;
default:
break;
}
if(S2_ButtonPress)
{
PRINTF(" %s is pressed \r\n", BOARD_S2_NAME);
Beep_Led_Response();
Set_RunSpeed(0);
delay_ms(50);
Run_Nishizhen_Direction();
S2_ButtonPress = false;
Set_RunSpeed(pwmVal);
LCD_ShowString(68,120,(uint8_t *)"<<<<",RED,WHITE,12,0);
}
if(S3_ButtonPress)
{
PRINTF(" %s is pressed \r\n", BOARD_S3_NAME);
Beep_Led_Response();
Set_RunSpeed(0);
delay_ms(50);
Run_Shunshizhen_Direction();
S3_ButtonPress = false;
Set_RunSpeed(pwmVal);
LCD_ShowString(68,120,(uint8_t *)">>>>",RED,WHITE,12,0);
}
__WFI(); /* 进入低功耗模式,等待中断唤醒 */
}
}● 效果演示
我要赚赏金
