cancel
Showing results for 
Search instead for 
Did you mean: 

code for blink an led with systick timer

AKhan.17
Associate II

i made this code to blink an led with systick time but it does not work can you please correct this. i am using stm32l152rc discovery board.

#include "stm32l1xx.h"

while {

  if (SysTick->CTRL |= SysTick_CTRL_COUNTFLAG == 0)

   {

   GPIOB->BSRRH |= GPIO_BSRR_BS_6; 

   }

  else {

   GPIOB->BSRR |= GPIO_BSRR_BR_6;

   }

}

 int main(void)

 {

 RCC->AHBENR |= RCC_AHBENR_GPIOBEN;

 GPIOB->MODER &= ~GPIO_MODER_MODER6;

 GPIOB->MODER |= GPIO_MODER_MODER6_0;

 GPIOB->OTYPER &= -(GPIO_OTYPER_OT_6);

 GPIOB->OSPEEDR &= ~GPIO_OSPEEDER_OSPEEDR6;

 GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR6;

 GPIOB->BSRRL = GPIO_BSRR_BR_6;

 SysTick->CTRL = 0;

 SysTick->LOAD = 1599999;

 SysTick->VAL = 0;

 SysTick->CTRL = 7;

 }

5 REPLIES 5

Your presentation style is confusing.

What's the while() loop floating above main()?

Why is the |= in the if statement?

You should just do an assignment for BSRRH/BSRR, not OR it

The main() routine should not exit

Try using a debugger

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
AKhan.17
Associate II

thank you very much Mr Clive for your hints but this time can you please rewrit the above code and send it to me, i would greatly appreciate. I am just a beginner and you are an expert.

arif from canada

k.arif4@hotmail.com

AKhan.17
Associate II

#include"stm32l1xx.h"

int main(void)

   {

 RCC->AHBENR |= RCC_AHBENR_GPIOBEN;

 GPIOB->MODER &= ~GPIO_MODER_MODER6;

 GPIOB->MODER |= GPIO_MODER_MODER6_0;

 GPIOB->OTYPER &= -(GPIO_OTYPER_OT_6);

 GPIOB->OSPEEDR &= ~GPIO_OSPEEDER_OSPEEDR6;

 GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR6;

 void delay_ticks(unsigned ticks)

 {

  SysTick->LOAD = 319999;

  SysTick->VAL = 0;

  SysTick->CTRL = 5;

   while(1)

   (( SysTick->CTRL &= SysTick_CTRL_COUNTFLAG) == 1);

    SysTick->CTRL = 0;

   }

    GPIOB -> BSRRL|= GPIO_BSRR_BS_6;// turn led on

    void delay_s(unsigned ms)

    {

      delay_ticks(1000 * (32000000 / 8) );

    }

    GPIOB ->BSRRH |= GPIO_BSRR_BR_6; // turn off delay

    void delay_s(unsigned ms)

    {

      delay_ticks(1000 * (32000000 / 8) );

    }

  }

i made this code for blinking an led with systick timer for a a delay of one second. can anyone make correction in it because led just lights up but does not turn off

henry.dick
Senior II

"can you please correct this."

your code is too far from working and reflects a complete lack of understanding, both from a hardware and software perspective.

the flow would be something like this:

  1. set the pin to output;
  2. set the systick to overflow at a given interval;
  3. while (1) {
  4. if systick overflow, reset the flag and flip the led pin;
  5. }

now you just need to implement the few blocks above.

AKhan.17
Associate II

i am doing point ( suggestion ) no 4 like this. Sir, i am just a beginner not an expert like you.

Is this correct ?

if (( SysTick->CTRL &= SysTick_CTRL_COUNTFLAG) == 1);

 SysTick->CTRL = 0;

{ GPIOB -> BSRRL|= GPIO_BSRR_BS_6;// turn led on

}