使用ptmr产生1S定时器,在定时器内让PTC2和PTC3交叉输出PWM(20KHZ,占空比20%)和高电平,程序出现卡死
-
static void Board_Init(void)
{
Clock_Sys_Init();
Pins_Drv_Init();
Uart_Print_Init();
ADC_DMA_Init();
Ptmr_Drv_Init();
}void Ptmr_Drv_Init(void)
{
pTMR_DRV_Init(pTMR0_INST,&PTMR_Config);
pTMR_DRV_InitChannel(pTMR0_INST,pTMR0_CH0,&ptmr_channel_0);
pTMR_DRV_InitChannel(pTMR0_INST,pTMR0_CH1,&ptmr_channel_1);
pTMR_DRV_InitChannel(pTMR0_INST,pTMR0_CH2,&ptmr_channel_2);
pTMR_DRV_InitChannel(pTMR0_INST,pTMR0_CH3,&ptmr_channel_3);
INT_SYS_EnableIRQ(pTMR0_IRQn);INT_SYS_InstallHandler(pTMR0_IRQn, ptmr0_interrupt_isr, NULL); pTMR_DRV_StartTimerChannels(pTMR0_INST, pTMR0_CH0); pTMR_DRV_StartTimerChannels(pTMR0_INST, pTMR0_CH1); pTMR_DRV_StartTimerChannels(pTMR0_INST, pTMR0_CH2); pTMR_DRV_StartTimerChannels(pTMR0_INST, pTMR0_CH3);
}
void ptmr0_interrupt_isr(void)
{
if (pTMR_DRV_GetInterruptFlagTimerChannels(pTMR0_INST, pTMR0_CH0))
{
pTMR_DRV_ClearInterruptFlagTimerChannels(pTMR0_INST, pTMR0_CH0);
PRINTF("pTMR CH0 ISR\n");
PMM_Mode_IN1();
}void PMM_Mode_IN1(void)//IN1 PWM mode
{
//引脚初始化
PRINTF("main_motor_forward_PMM_Mode_IN1 start\n");
g_pin_mux_InitConfigArr0[10].base = PCTRLC;
g_pin_mux_InitConfigArr0[10].pinPortIdx = 2U;
g_pin_mux_InitConfigArr0[10].pullConfig = PCTRL_INTERNAL_PULL_NOT_ENABLED;
g_pin_mux_InitConfigArr0[10].driveSelect = PCTRL_LOW_DRIVE_STRENGTH;
g_pin_mux_InitConfigArr0[10].passiveFilter = false;
g_pin_mux_InitConfigArr0[10].mux = PCTRL_MUX_ALT2;
g_pin_mux_InitConfigArr0[10].direction = GPIO_INPUT_DIRECTION;
g_pin_mux_InitConfigArr0[10].intConfig = PCTRL_DMA_INT_DISABLED;
g_pin_mux_InitConfigArr0[10].rateSelect = PCTRL_FAST_SLEW_RATE;
g_pin_mux_InitConfigArr0[10].clearIntFlag = false;
g_pin_mux_InitConfigArr0[10].digitalFilter = false;
g_pin_mux_InitConfigArr0[10].filterConfig.width = 0;
g_pin_mux_InitConfigArr0[10].gpioBase = GPIOC;
g_pin_mux_InitConfigArr0[10].direction = GPIO_INPUT_DIRECTION;
g_pin_mux_InitConfigArr0[10].initValue=0;
PINS_Init(&g_pin_mux_InitConfigArr0[10]);
PRINTF("000 000\n");
g_pin_mux_InitConfigArr0[11].base = PCTRLC;
g_pin_mux_InitConfigArr0[11].pinPortIdx = 3U;
g_pin_mux_InitConfigArr0[11].pullConfig = PCTRL_INTERNAL_PULL_NOT_ENABLED;
g_pin_mux_InitConfigArr0[11].driveSelect = PCTRL_LOW_DRIVE_STRENGTH;
g_pin_mux_InitConfigArr0[11].passiveFilter = false;
g_pin_mux_InitConfigArr0[11].mux = PCTRL_MUX_AS_GPIO;
g_pin_mux_InitConfigArr0[11].direction = GPIO_INPUT_DIRECTION;
g_pin_mux_InitConfigArr0[11].intConfig = PCTRL_DMA_INT_DISABLED;
g_pin_mux_InitConfigArr0[11].rateSelect = PCTRL_FAST_SLEW_RATE;
g_pin_mux_InitConfigArr0[11].clearIntFlag = false;
g_pin_mux_InitConfigArr0[11].digitalFilter = false;
g_pin_mux_InitConfigArr0[11].filterConfig.width = 0;
g_pin_mux_InitConfigArr0[11].gpioBase = GPIOC;
g_pin_mux_InitConfigArr0[11].direction = GPIO_OUTPUT_DIRECTION;
g_pin_mux_InitConfigArr0[11].initValue=0;
PINS_Init(&g_pin_mux_InitConfigArr0[11]);
PRINTF("111 111\n");
ETMR_PWM_Config0.pwmChannelConfig->hwChannelId = 2;
ETMR_PWM_Config0.pwmChannelConfig->polarity = eTMR_POLARITY_NORMAL;
ETMR_PWM_Config0.pwmChannelConfig->pwmSrcInvert = false;
ETMR_PWM_Config0.pwmChannelConfig->align = eTMR_PWM_RIGHT_EDGE_ALIGN;
ETMR_PWM_Config0.pwmChannelConfig->channelInitVal = 0;
ETMR_PWM_Config0.pwmChannelConfig->typeOfUpdate = eTMR_PWM_UPDATE_IN_DUTY_CYCLE;
ETMR_PWM_Config0.pwmChannelConfig->dutyCycle = 0x0000;
ETMR_PWM_Config0.pwmChannelConfig->offset = 0;
ETMR_PWM_Config0.pwmChannelConfig->enableSecondChannelOutput = false;
ETMR_PWM_Config0.pwmChannelConfig->secondChannelPolarity = eTMR_POLARITY_NORMAL;
PRINTF("222 222\n");
eTMR_DRV_Init(eTMR0_INST,&ETMR_CM_Config0,&ETMR_CM_Config0_State);
PRINTF("333 333\n");
eTMR_DRV_InitPwm(eTMR0_INST,&ETMR_PWM_Config0);```
PRINTF("444 444\n");
/* PWM is generated when eTMR enable, the duty cycle is configured in the initial function /
eTMR_DRV_Enable(eTMR0_INST);
OSIF_TimeDelay(10);
PRINTF("555 555\n");
/ The following code shows how to switch the different duty cycle /
/ channel 2 duty cycle is 20%(0x1000U)~80%(0x6666U) */
eTMR_DRV_UpdatePwmChannel(eTMR0_INST, 2, 0x1000U, 0);
eTMR_DRV_SetLdok(0);
OSIF_TimeDelay(10);
PRINTF("main_motor_forward_PMM_Mode_IN1 end\n");
}测试LOG:
pTMR CH0 ISR
main_motor_forward_PMM_Mode_IN1 start
000 000
111 111
222 222
333 333
444 444我这里要实现的控制逻辑:
在定时器1S内,前7S,PTC2输出PWM,PTC3输出高电平;接着5S,PTC2和PTC3全部输出高电平;再接着7S,PTC2输出高电平,PTC3输出PWM,再接着5S,PTC2和PTC3全部输出高电平,这样一个循环过程。若是要实现这个过程,在1S定时任务就涉及到引脚初始化和PWM输出初始化,现在在定时器1S初始化PTC2输出PWM,PTC3输出高电平时,程序就出现了问题,请工程师协助分析下问题原因?谢谢~
-
Diga 你好!
按照你所述,我这里做了调整,现在遇到一个新的问题:
void Etmr_Drv_Init(void)
{
eTMR_DRV_Init(eTMR0_INST,&ETMR_CM_Config0,&ETMR_CM_Config0_State);
eTMR_DRV_InitPwm(eTMR0_INST,&ETMR_PWM_Config0);
/* PWM is generated when eTMR enable, the duty cycle is configured in the initial function */
eTMR_DRV_Enable(eTMR0_INST);
OSIF_TimeDelay(10);/* The following code shows how to switch the different duty cycle */ /* channel 2 duty cycle is 20% */ eTMR_DRV_UpdatePwmChannel(eTMR0_INST, 2, 0x8000U, 0); /* channel 3 duty cycle is 20% */ eTMR_DRV_UpdatePwmChannel(eTMR0_INST, 3, 0x8000U, 0); eTMR_DRV_SetLdok(0); OSIF_TimeDelay(10);
// PRINTF("CH2 output 20KHZ duty 20, CH3 output 20KHZ duty 20\n");
}
按照当前配置,正常应该是输出高电平,但是实测全部是低电平,将两路的0x8000U更改为0x0000U,输出的也是低电平,请问怎么回事啊?谢谢~
-
Diga 参考了例程,里面有这样的说明:
/* The following code shows how to switch the different duty cycle /
/ channel 2 duty cycle is 50% /
eTMR_DRV_UpdatePwmChannel(0, 2, 0x4000U, 0);
/ channel 3 duty cycle is 50% */
eTMR_DRV_UpdatePwmChannel(0, 3, 0x4000U, 0);
eTMR_DRV_SetLdok(0);
OSIF_TimeDelay(10);**/* channel 2 100% duty cycle, here is the workaround for 100% duty cycle */ eTMR0->CH[2].VAL0 = 0; eTMR0->CH[2].VAL1 = eTMR0->MOD + 1; eTMR_DRV_SetLdok(0); OSIF_TimeDelay(10);**
按照这样的配置,通道2输出的是100%占空比,也就是高电平了。
通道3是50%占空比。
帮助没办法联网的电脑使用YCT
帮助改进和优化YT CONFIG TOOL,有机会抽取YTM32B1ME0 EVB哦...