Question
Corrupt varible within an Interrupt on STM32F103ZET6
Posted on June 06, 2011 at 08:02
I have a variable that is not appears to not being retrieved consistantly from within an interrupt.
I have the following code for the Timer3, which is configure to interrupt on a count down timer. unsigned int errorCount; void TIM3_IRQHandler(void) { static volatile unsigned int pwm_on = 0; if (TIM3->SR & 0x0001) //Check interrupt source { TIM3->SR &= ~0x0001; //Clear UIF Flag if (((pwm_on) && ((GPIOC->ODR & 0x0001) == 0)) || ((pwm_on == 0) && (GPIOC->ODR & 0x0001))) errorCount++; if (pwm_on) { GPIOC->ODR &= ~0x0001; pwm_on = 0; } else { GPIOC->ODR |= 0x0001; pwm_on = 1; } } The problem is that errorCount is not zero. This interrupt operates about 7000 times per second, and in that time we get an error count of 7 - 15 times. I am using this interrupt to toggle a pwm pin and it affects my device. If I toggle the pin directly like GPIOC-> ^= 0x0001; then everything is fine. The only reason this could occur is if pwm_on was getting corrupted or was not being retrieved. I'm really stumped. Any help would be appreciated. Thanks #corrupt-ram-interrupt-stm32-timer