2015-11-07 09:12 AM
hello,
I'm trying to create delay of 1 second and toggle an LED. I'm using SysTick timer for this task. I have STM32F103C8T6 mcu. The following code does not work. Looks like the control never reaches the SysTick_Handler.#include ''stm32f10x.h''
int main()
{
RCC->APB2ENR|=RCC_APB2ENR_IOPBEN; //enable gpioB clock
GPIOB->CRH=0x01; //pin B.8 output mode with push pull(LED pin)
SysTick->LOAD|=0x895440; //9000000 in decimal for 1 second delay with 72MHz system clock. CALIB val is 9000(1 ms)
SysTick->CTRL|=0x02; //enable interrupt trigger
SysTick->CTRL|=0x01; // enable clock
}
void SysTick_Handler(void)
{
GPIOB->ODR^=0x100; //toggle LED
}
However when I use the following code, it works!!
#include ''stm32f10x.h''
int main()
{
RCC->APB2ENR|=RCC_APB2ENR_IOPBEN; //enable gpioB clock
GPIOB->CRH=0x01; //pin B.8 output mode with push pull(LED pin)
SysTick->LOAD|=0x895440; //9000000 in decimal for 1 second delay with 72MHz system clock. CALIB val is 9000(1 ms)
SysTick->CTRL|=0x02; //enable interrupt trigger
SysTick->CTRL|=0x01; // enable clock
while(1)
{
}
}
void SysTick_Handler(void)
{
GPIOB->ODR^=0x100; //toggle LED
}
I just included an infinite loop at the end of main(). Why is it not working without the endless loop??
#stm32f103 #timer #systick
2015-11-07 11:59 AM
You should have while(1) loop somewhere in the main function, I suppose - after initialization of peripherals.
Why wouldn't you toggle LED with dedicated timer in PWM mode? Then, you don't occupy CPU at all. Don't need to have an interrupt for toggling.2015-11-07 05:26 PM
Where do you suppose it goes when it leaves main()? There's no OS, so it probably crashes the processor. If you have a debugger you could try stepping into it and see.
Is there some value to ORing the LOAD register, instead of just setting it?2015-11-07 10:07 PM
@obid.matic yes, it doesn't require interrupt. I was just trying to understand how SysTick works. Thanks for help!!
2015-11-07 10:16 PM
@clive1 checked with STlink debugger on Keil. After the end of main() function, the debugger gets stuck. It never goes to the SysTick handler. I think you are right, the processor crashes. And, No, there is no significance of ORing value to LOAD register. I might as well just set it. Will save me few machine cycles. Thanks for help!!