Demo_PWM_同一个周期内立即更新占空比
-
- 背景
特殊应用场合出现同一个周期内,更新两次占空比,SDK自带DEMO会更新不进去 - 需求
- 芯片系列:MC0,MD,ME,LE
- 功能需求描述
- 特殊应用场合出现同一个周期内,更新两次占空比,无法更新
- 代码包:MCAL x.x.x/SDK x.x.x/RegCode
- 编译器:IAR/Keil/Gcc
- 解决方案描述
1.模拟同一个周期内多次更新占空比,去掉原demo里的两次更新占空比之间的延时
[图片]
此时PWM输出为第一次占空比设置的0%占空比。
2.将原来regisiter loading Opportunity配置的synchronized with counter matching MOD改为synchronized with register loading trigger source。 - 示例说明
2.将PWM设置为软触发更新
[图片]
3.在代码中将eTMR_DRV_SetLdok()函数改为eTMR_DRV_SyncWithSoftTrigger(ETMR_INST)
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ Board_Init(); /* USER CODE BEGIN 2 */ /* PWM is generated when eTMR enable, the duty cycle is configured in the initial function */ eTMR_DRV_Enable(ETMR_INST); OSIF_TimeDelay(10); /* The following code shows how to switch the different duty cycle */ /* channel 2 duty cycle is 50% */ eTMR_DRV_UpdatePwmChannel(ETMR_INST, ETMR_CHANNEL_INDEX0, 0x4000U, 0); /* channel 3 duty cycle is 75% */ eTMR_DRV_UpdatePwmChannel(ETMR_INST, ETMR_CHANNEL_INDEX1, 0x6000U, 0); eTMR_DRV_SetLdok(ETMR_INST); OSIF_TimeDelay(10); /* channel 2 duty cycle is 100%, here is the workaround for 100% duty cycle * when channel is right align, value0 is configured as 0 */ g_etmrBase[ETMR_INST]->CH[ETMR_CHANNEL_INDEX0].VAL0 = 0; g_etmrBase[ETMR_INST]->CH[ETMR_CHANNEL_INDEX0].VAL1 = g_etmrBase[ETMR_INST]->MOD + 1; /* channel 3 duty cycle is 100% * when channel is left/center/asymmetrical align, value0 is configured as 1 */ g_etmrBase[ETMR_INST]->CH[ETMR_CHANNEL_INDEX1].VAL0 = 1; g_etmrBase[ETMR_INST]->CH[ETMR_CHANNEL_INDEX1].VAL1 = g_etmrBase[ETMR_INST]->MOD + 1; eTMR_DRV_SetLdok(ETMR_INST); OSIF_TimeDelay(10); /* channel 2 duty cycle is 0 */ eTMR_DRV_UpdatePwmChannel(ETMR_INST, ETMR_CHANNEL_INDEX0, 0, 0); /* channel 3 duty cycle is 0 */ eTMR_DRV_UpdatePwmChannel(ETMR_INST, ETMR_CHANNEL_INDEX1, 0, 0); ***eTMR_DRV_SyncWithSoftTrigger(ETMR_INST);*** //OSIF_TimeDelay(10); /* channel 2 duty cycle is 0x6000 */ eTMR_DRV_UpdatePwmChannel(ETMR_INST, ETMR_CHANNEL_INDEX0, 0x6000U, 0); /* channel 3 duty cycle is 0x4000 */ eTMR_DRV_UpdatePwmChannel(ETMR_INST, ETMR_CHANNEL_INDEX1, 0x4000U, 0); ***eTMR_DRV_SyncWithSoftTrigger(ETMR_INST);*** /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }
可以输出最新的更新的占空比值,不再需要等到COUNTER值等于MOD值更新。
输出波形:
[图片]
5. 工程代码
8b54813e-6ed9-4df8-9464-81756d2f7667-Etmr_Pwm_Demo.zip - 背景
-
通过Ozone看占空比寄存器,此方法确实可以实现在一个周期内更改两次占空比,实际输出占空比为最后一次更改的占空比。
-
The link in pdf.云途开发生态介绍.pdf
-
download link:https://account.ytmicro.com/#/login,
After registering and logging in, you can download it.
帮助没办法联网的电脑使用YCT
帮助改进和优化YT CONFIG TOOL,有机会抽取YTM32B1ME0 EVB哦...