cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F401RE Systick issues

Petrarka
Associate II

Hello, I have an issue with my code where I have configured SysTick and it triggers on every clock cycle, but I dont know how to make things trigger inside the main while loop

 

#include "main.h"
#include "functions.h"
#include <stdbool.h>
#include <stdlib.h>

union InterruptFlag flag = { .bits_access.flag=     0,
							 .bits_access.direction=0,
							 .bits_access.debounce= 0,
							 .bits_access.press   = 0}; // Initialize to 0


void EXTI15_10_IRQHandler(void){

        flag.bits_access.flag = !flag.bits_access.flag; // Set the flag to indicate button press event
        EXTI->PR |= (1<<13);  // Clear PR to re-enable EXTI interrupt

};



#define MAX_DUTY 500
#define MIN_DUTY 0
#define DEBOUNCE_DELAY 500
volatile uint32_t duty_cycle;
volatile uint16_t incrementer = 1;

int main(void){

  TIM2_INIT();
  TIM3_INIT();
  Interrupt_Init();
  SysTick->CTRL = SysTick->CTRL | (1 << 0) | (1 << 1) | (1 << 2);
  SysTick->LOAD = (84000 - 1);  // 100 millisecond
  while (1) {
	  //LED_PWM();
	  if(flag_check()==true){
		  incrementer=0;
	  }
     }
}

void SysTick_Handler(void)
{
	LED_PWM();
}

bool flag_check(){
	return flag.bits_access.flag;
}



void LED_PWM() {
    TIM2->CCR1 = duty_cycle;
    incrementer=1;
    if (duty_cycle >= MAX_DUTY) {
        flag.bits_access.direction = false; // Change direction to decrement
    } else if (duty_cycle <= MIN_DUTY) {
        flag.bits_access.direction = true;  // Change direction to increment
    }

    if (flag.bits_access.direction) {
        duty_cycle += incrementer; // Increment
    } else {
        duty_cycle -= incrementer; // Decrement
    }
}

 

1 REPLY 1
Karl Yamashita
Lead III

If you're just trying to blink an LED at a different rate based off the System tick, then look at this tutorial 

https://www.youtube.com/watch?v=o0qhmXR5LD0

 

You can keep the System tick at it's default 1 ms tick.

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.