1

I want to toggle each LED one aT 4Hz and one at 3Hz, so far i can only toggle 2 at the same frequency.So far i can do them separately only but i dont know how to write to code to combine them so i can run it all at the same time.

// THIS CODE IS FOR BOTH

int main (void){
//Enable clock for GPIO A and Gpio B
RCC->AHB2ENR |= 0x3UL;                             

//Configure PA_0 and PA_1

GPIOA->MODER &= ~0xFUL ;  
GPIOA->MODER |= 0x5UL;   

GPIOA-> PUPDR &= ~0XFUL;    
GPIOA-> PUPDR |= 0xAUL;   

//FOR LED GREEN

SysTick ->LOAD = 1000000-1 ;   
SysTick-> VAL = 0;
SysTick->CTRL |= 0x5UL;

    while (1)
{
 if (SysTick -> CTRL & SysTick_CTRL_COUNTFLAG_Msk)
  { 
      GPIOA->ODR ^= 0x2UL;
    }   
}

}

//THEN deleting LED GREEN TO WRITE LED orange

SysTick ->LOAD = 666667-1 ;   
SysTick-> VAL = 0;
SysTick->CTRL |= 0x5UL;

    while (1)
{
 if (SysTick -> CTRL & SysTick_CTRL_COUNTFLAG_Msk)
  { 
      GPIOA->ODR ^= 0x1UL;
    }   
}

}

i just need help to combine them mainly the systick->load for each led.

SHK
  • 11
  • 3

1 Answers1

1

Do not use systick this way. Set the systick interrupt to be triggered lets say 1000 times per second (standard STM startup files do it this way)

Then toggle LEDs in the interrupt handler

volatile uint32_t count = 0;

void SysTick_Handler(void)
{
    count++;

    if(!(count % (1000 / 8))) GPIOA -> ODR ^= 1;   // 4 blinks per secons
    if(!(count % (1000 / 6))) GPIOA -> ODR ^= 2;   // 3 blinks per second
}
0___________
  • 60,014
  • 4
  • 34
  • 74
  • I think this solution is fine, but I would like to mention one detail. Since `count` is running freely, it will eventually (after about 50 days) wrap around and we will see an anomaly in the blinks since the modulo constants are not divisors of 2^32. Of course, it does not matter in this case, but it is important to be aware of such things because an error that shows after 50 days of operation is practically impossible to catch by testing. – nielsen Mar 04 '21 at 19:42