Microcontroller Tutorials

Arduino, Raspberry Pi, PIC, Beaglebone Resources

Forum

You need to log in to create posts and topics.

stm32duino and TIM interrupts stmf407

hello guys im trying to make tim interrupt work but for some reason its not triggering my irq

#include <stm32f4xx.h>

#include <Wire.h>

#include <LcdI2C_stm32.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);//lcd 4x20

const byte LedPin = PG7;

const byte mapSensorPin = PA0;

volatile bool enableFlag = false;

unsigned long time_now = 0, time_before = 0;

volatile unsigned long timeoutTime = 1000000;

unsigned long delayTime = 500000; //time in us

uint16_t returnPeriod(uint16_t prescaler, unsigned long time, uint8_t mhz) {

return (time) / ((1000000 / mhz) * 0.00001) / prescaler;

}

void setup() {

pinMode(LedPin, OUTPUT);

pinMode(mapSensorPin, INPUT);

lcd.begin();

// Turn on the blacklight and print a message.

lcd.backlight();

lcd.clear();

lcd.print("test");

__TIM5_CLK_ENABLE();

TIM5->CR1 &= ~TIM_CR1_CEN; //disable timer count

NVIC_EnableIRQ(TIM5_IRQn);

NVIC_SetPriority(TIM5_IRQn, 0);

TIM5->DIER |= TIM_DIER_UIE;

TIM5->PSC = 10000; //prescaler

TIM5->CNT = 0;

}

void loop() {

time_before = time_now;

time_now = micros();

if (enableFlag == false) { //make sure we enable after timeout

TIM5->ARR = returnPeriod(TIM5->PSC, delayTime * 10, 84); //wait 05seconds

TIM5->CNT = 0;

TIM5->CR1 |= TIM_CR1_CEN; //enable timer here

}

while ((TIM5->CR1 & TIM_CR1_CEN)) { // wait till timeout aka timer disabled

}

lcd.setCursor(0, 0);

lcd.iprintf("Total Time: %d ", time_now - time_before);

}

void TIM5_IRQHandler(void) {

if (TIM5->SR & TIM_SR_UIF) {

TIM5->SR &= ~TIM_SR_UIF; //clear interupt flag

if (enableFlag == false) {

enableFlag = true;

digitalWrite(LedPin, HIGH);

TIM5->ARR = returnPeriod(TIM5->PSC, timeoutTime * 10, 84); //timeout after 1 seconds

TIM5->CNT = 0;

}

else {

enableFlag = false;

digitalWrite(LedPin, LOW);

TIM5->CR1 &= ~TIM_CR1_CEN; //disable timer

}

}

}

this is my program all it does it sets a delay to trigger a led on and then a timeout to keep led on for specific time.
normaly this is working if i use TimerHandleInit() and attachIntHandle() but if i just use TIM registers or hal its not working