在上一篇我们测试了基于Ctimer定时的PWM输出,相对来说功能还是比较少的,只能边沿对齐,这次我们使用SCtimer定时器,让PWM有更多可能!
从零配置SCtimer的PWM输出
创建组件:
定时器基础配置:
需要配置的内容不多,只要配置时钟就好;
通道配置:
可以看到配置你想要的若干通道,在这里遇到了两个问题,一个是配置SCTIMER out 0时连接到P1_4引脚无输出,连接到P2_2则输出正常;另一个就是上图的感叹号问题,要求PWM信号必须被分配到一个初始化的状态,可是并没有相关设置项,我们带着问题进行“更新源代码”,可以看到初始化内容("peripherals.C")中:
const sctimer_config_t SCT0_initConfig = { .enableCounterUnify = true, .clockMode = kSCTIMER_System_ClockMode, .clockSelect = kSCTIMER_Clock_On_Rise_Input_0, .enableBidirection_l = false, .enableBidirection_h = false, .prescale_l = 0U, .prescale_h = 0U, .outInitState = (uint8_t)(SCT0_OUTPUT_1 | SCT0_OUTPUT_3), .inputsync = 0U }; const sctimer_pwm_signal_param_t SCT0_pwmSignalsConfig[2] = { { .output = kSCTIMER_Out_0, .level = kSCTIMER_HighTrue, .dutyCyclePercent = 50U }, { .output = kSCTIMER_Out_1, .level = kSCTIMER_LowTrue, .dutyCyclePercent = 20U } }; uint32_t SCT0_pwmEvent[2]; static void SCT0_init(void) { SCTIMER_Init(SCT0_PERIPHERAL, &SCT0_initConfig); /* Initialization of state 0 */ SCTIMER_SetupPwm(SCT0_PERIPHERAL, &SCT0_pwmSignalsConfig[0], kSCTIMER_EdgeAlignedPwm, 20000U, SCT0_CLOCK_FREQ, &SCT0_pwmEvent[0]); SCTIMER_StartTimer(SCT0_PERIPHERAL, kSCTIMER_Counter_U); }
发现相关配置文件已经创建,只是在初始化(SCT0_init(void))中未对有问题的通达进行初始化配置,我们可以通过在外面添加相应的SCTIMER_SetupPwm()重新配置对应的PWM通道。
接下来我们在外部进行对PWM通道进行从新配置:
sctimer_config_t sctimerInfo; sctimer_pwm_signal_param_t pwmParam; uint32_t sctimerClock; uint32_t event; sctimerClock = CLOCK_GetFreq(kCLOCK_BusClk); SCTIMER_GetDefaultConfig(&sctimerInfo); SCTIMER_Init(SCT0, &sctimerInfo); /* Configure first PWM with frequency 24kHZ from first output */ pwmParam.output = kSCTIMER_Out_0; pwmParam.level = kSCTIMER_HighTrue; pwmParam.dutyCyclePercent = 50; if (SCTIMER_SetupPwm(SCT0, &pwmParam, kSCTIMER_CenterAlignedPwm, 20000U, sctimerClock, &event) == kStatus_Fail) { return -1; } /* Configure second PWM with different duty cycle but same frequency as before */ pwmParam.output = kSCTIMER_Out_1; pwmParam.level = kSCTIMER_HighTrue; pwmParam.dutyCyclePercent = 20; if (SCTIMER_SetupPwm(SCT0, &pwmParam, kSCTIMER_CenterAlignedPwm, 20000U, sctimerClock, &event) == kStatus_Fail) { return -1; } SCTIMER_StartTimer(SCT0, kSCTIMER_Counter_U);
效果如下:
由上图可以看出当前PWM输出处于中心对称模式,也可以配置为边缘对齐。