2015-03-16 07:53 AM
Hi, I need to modify a PWM inside an interrupt, I though that It would be easy, just changing CCR2 to the new desired value but it is not working properly.
The change is not happening each pulse, it is a bit random.. not sure why. If i set 3000, it works right, if I set 17500 it works right, but if i set a condition to change from one to the other value then it doesnt work because it doesnt change in each interrupt, at it is even a bit random (image below)...My code is simple..., after each TIM5 interrupt I want a new PWM (in this example 2 values, in my real code more values, but I wanted something easy to show here)void TIM5_IRQHandler(void) { if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET) {static int a=1;
a^=1; if(a) TIM9->CCR2=3000; else TIM9->CCR2=17500; TIM9->CNT=0;TIM_ClearITPendingBit(TIM5, TIM_IT_Update);
}}Here is the initialization of the ports (that's working right)void TIM5_Configuration(void){ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); TIM_TimeBaseStructure.TIM_Period =5200; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); TIM_PrescalerConfig(TIM5, 2 , TIM_PSCReloadMode_Immediate); TIM_ITConfig(TIM5,TIM_IT_Update , ENABLE); TIM_Cmd(TIM5, ENABLE); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);}void TIM9_PWM_Configuration2(void){ GPIO_InitTypeDef GPIO_InitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE); GPIO_InitStructure.GPIO_Pin = pinO; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(portO, &GPIO_InitStructure); GPIO_PinAFConfig(portO, O_SOURCE, GPIO_AF_TIM9); TIM_TimeBaseStructure.TIM_Period =5200*6; TIM_TimeBaseStructure.TIM_RepetitionCounter =0; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM9, &TIM_TimeBaseStructure);// TIM_PrescalerConfig(TIM9, 2 , TIM_PSCReloadMode_Immediate); TIM_ITConfig(TIM9,TIM_IT_Update , ENABLE); /* Enable TIM9 Preload register on ARR */ TIM_ARRPreloadConfig(TIM9, ENABLE); /* TIM PWM1 Mode configuration */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 1000; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC2Init(TIM9, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM9, TIM_OCPreload_Enable); TIM_Cmd(TIM9, ENABLE);}Bank2 CH 8 is the PWM TIM 9 CH2 while Bank2 ch 15 is the IRQ (TIM 5)I dont know if DMA + PWM would fix this problem.. I dont know if it would even work, but I though that it would be easy, a simple thing.Any tip or help would be welcomed.Thanks in advance. #adc #pwm #dma #stm32f2 #tim52015-03-16 08:36 AM
You need two timers why? Why the different periodicity? Why not just set the pulse width in the TIM's own interrupt?
You'd probably want to set the CNT to zero first, and consider if you can get the master/slave relationship set up so one timer resets the other.