【MCXA156开发板测评】+PWM调节功能测试
[复制链接]
MCXA156开发板提供了PWM调节功能,利用它可实现LED灯的亮度调节。
实现功能测试的主程序为:
int main(void)
{
uint8_t duty = 0;
uint8_t idleState = 0;
uint32_t dutyCycleValue = 0;
uint32_t idleStateValue = 0;
flexio_config_t fxioUserConfig;
CLOCK_SetClockDiv(kCLOCK_DivFLEXIO0, 2u);
CLOCK_AttachClk(kFRO_HF_to_FLEXIO0);
RESET_ReleasePeripheralReset(kFLEXIO0_RST_SHIFT_RSTn);
BOARD_InitPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
FLEXIO_GetDefaultConfig(&fxioUserConfig);
FLEXIO_Init(DEMO_FLEXIO_BASEADDR, &fxioUserConfig);
PRINTF("\r\nFLEXIO_PWM demo start.\r\n");
while (1)
{
duty = 0;
dutyCycleValue = 0;
idleState = 0;
idleStateValue = 0;
PRINTF("\r\nPlease input a value (0 - 100) to set duty cycle: ");
while (duty != 0x0D)
{
duty = GETCHAR();
if ((duty >= '0') && (duty <= '9'))
{
PUTCHAR(duty);
dutyCycleValue = dutyCycleValue * 10 + (duty - 0x30U);
}
}
PRINTF("\r\nInput value is %d\r\n", dutyCycleValue);
if (dutyCycleValue > 0x64U)
{
PRINTF("Your value is output of range.\r\n");
PRINTF("Set pwm output to IDLE.\r\n");
PRINTF("\r\nPlease input pwm idle status (0 or 1): ");
while (idleState != 0x0D)
{
idleState = GETCHAR();
if ((idleState >= '0') && (idleState <= '9'))
{
PUTCHAR(idleState);
idleStateValue = idleStateValue * 10 + (idleState - 0x30U);
}
}
PRINTF("\r\nInput IDLE state value is %d\r\n", idleStateValue);
if (idleStateValue > 0x1U)
{
PRINTF("\r\nYour value is output of range.\r\n");
continue;
}
FLEXIO_SetPwmOutputToIdle(DEMO_FLEXIO_BASEADDR, DEMO_FLEXIO_TIMER_CH, idleStateValue);
#if defined(FSL_FEATURE_FLEXIO_HAS_PIN_STATUS) && FSL_FEATURE_FLEXIO_HAS_PIN_STATUS
PRINTF("\r\nPWM leave is: %d \r\n",
PWM_GetPwmOutputState(DEMO_FLEXIO_BASEADDR, DEMO_FLEXIO_TIMER_CH, DEMO_FLEXIO_OUTPUTPIN));
#endif
}
else
{
if (flexio_pwm_init(DEMO_FLEXIO_FREQUENCY, dutyCycleValue) == kStatus_Fail)
{
PRINTF("FLEXIO PWM initialization failed\n");
return -1;
}
PRINTF("\r\nPWM duty cycle is: %d\r\n", s_flexioGetPwmDutyCycle[DEMO_FLEXIO_TIMER_CH]);
#if defined(FSL_FEATURE_FLEXIO_HAS_PIN_STATUS) && FSL_FEATURE_FLEXIO_HAS_PIN_STATUS
PRINTF("\r\nPWM leave is: %d \r\n", PWM_GetPwmOutputState(DEMO_FLEXIO_BASEADDR, DEMO_FLEXIO_TIMER_CH, DEMO_FLEXIO_OUTPUTPIN));
#endif
}
}
}
该程序的功能是将PWM的工作频率设置为100KHz,占空比可通过串口从0至100自主设置。PWM脉冲的输出端为J8端口的第13号引脚,见图1所示。
图1 端口J8
经程序的编译及下载,其设置占空比的效果如图2所示。
图2 设置占空比
在连接LED模块的情况下,其调节效果如图3和图4所示。稍感不足的是,用LED模块来观察调节效果其调节范围比较窄,一般在占空比为75%至100%的期间内,否则LED就基本处于不可观察状态了。
另外,就是程序的频率调节空间有限定。本想把频率设置为50Hz,以调节占空比的方式来控制舵机,但没能成功。
图3 占空比80%
图4 占空比75%
|