cancel
Showing results for 
Search instead for 
Did you mean: 

SysTick Mystery

navintiwari08
Associate III
Posted on November 07, 2015 at 18:12

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
4 REPLIES 4
matic
Associate III
Posted on November 07, 2015 at 20:59

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.

Posted on November 08, 2015 at 02:26

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?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
navintiwari08
Associate III
Posted on November 08, 2015 at 07:07

@obid.matic yes, it doesn't require interrupt. I was just trying to understand how SysTick works. Thanks for help!!

navintiwari08
Associate III
Posted on November 08, 2015 at 07:16

@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!!