[Solved] Hardware Timer

Working libraries, libraries being ported and related hardware
Post Reply
ats3788
Posts: 4
Joined: Fri Jun 05, 2020 10:14 am

[Solved] Hardware Timer

Post by ats3788 »

Hello Guys
I use PlatformIO and fiddle with the STM32F1 Blue Pill
so far so good, but I have a Problem with the Timer Hardware Interrupts
Thats my code
It compile without any Problems but the timers do not count

Code: Select all

void handler3(void) {
    count3++;
  //  Serial.print("T3");
}

void handler4(void) {
    count4++;
   // Serial.print("T4");
}

     Timer3.setMode(TIMER_CH2, TIMER_OUTPUT_COMPARE);
     Timer4.setMode(TIMER_CH4, TIMER_OUTPUT_COMPARE);
     Timer3.pause();
     Timer4.pause();
     Timer3.setCount(0);
     Timer4.setCount(0);
     Timer3.setOverflow(30000);
     Timer4.setOverflow(30000);
     Timer3.setCompare(TIMER_CH3, 1000);
     Timer4.setCompare(TIMER_CH4, 1000);
     Timer3.attachInterrupt(TIMER_CH3, handler3);
     Timer4.attachInterrupt(TIMER_CH4, handler4);
     Timer3.refresh();
     Timer4.refresh();
     Timer3.resume();
     Timer4.resume();
     
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: Hardware Timer

Post by stevestrong »

You should set the mode after pausing the timer, so check the order of the functions:

Code: Select all

TimerX.pause();
...
TimerX.refresh();
TimerX.attachInterrupt(..);
TimerX.resume();
Btw, you set the mode for channel 2 and attach the interrupt on channel 3...
And there is no need to set compare mode if you only want to count, setting the overflow (or eventually the period) and attaching an interrupt on "channel 0" (overflow) would do the job.
ats3788
Posts: 4
Joined: Fri Jun 05, 2020 10:14 am

Re: Hardware Timer

Post by ats3788 »

Thank you
What is realy odd, I tried and tried all kind of different approaches then the bootloader crashed and I installed the bootloader fresh, and after this
the Interrupt Timer is working fine. The ghost in the machine. Lol
ats3788
Posts: 4
Joined: Fri Jun 05, 2020 10:14 am

Re: Hardware Timer

Post by ats3788 »

This is my Code what works

Code: Select all

#include <Arduino.h>


USBSerial usb;

#define TOGGLE(x) digitalWrite(x, digitalRead(x) ? LOW : HIGH)
uint32_t Counter1 = 0;
uint32_t Counter2 = 0;
uint32_t Counter3 = 0;
uint32_t Counter4 = 0;

uint16_t Array[1000];
char Buffer[40];

void SetupTimer();


void setup() {
    // Set up the LED to blink
    usb.begin(115200);
    


  for (int i = 0; i < 100; i++)
  {

    delay(100);
    usb.print('*');
    usb.print(i);
  }

  SetupTimer();

}


void loop()
{

sprintf(Buffer, " ->%u<->%u<->%u<->%u<-" , Counter1, Counter2 ,Counter3, Counter4);
usb.println(Buffer);

delay(250);

}


void __Timer1(void) {
    TOGGLE(LED_BUILTIN);
   Counter1++;
}

void __Timer2(void) {
    TOGGLE(LED_BUILTIN);
   Counter2++;
}

void __Timer3(void) {
    
   Counter3++;
}

void __Timer4(void) {
    
   Counter4++;
}

void SetupTimer()
{
    pinMode(LED_BUILTIN, OUTPUT);

//Timer 1
    Timer1.pause();
    Timer1.setPeriod(500000); // in microseconds
    Timer1.setChannel1Mode(TIMER_OUTPUT_COMPARE);
    Timer1.setCompare(TIMER_CH1, 1);  // Interrupt 1 count after each update
    Timer1.attachCompare1Interrupt(__Timer1);
    Timer1.refresh();
    Timer1.resume();

//Timer 2
    Timer2.pause();
    Timer2.setPeriod(1000000); // in microseconds
    Timer2.setChannel1Mode(TIMER_OUTPUT_COMPARE);
    Timer2.setCompare(TIMER_CH2, 2);  // Interrupt 1 count after each update
    Timer2.attachCompare1Interrupt(__Timer2);
    Timer2.refresh();
    Timer2.resume();

 //Timer 3
    Timer3.pause();
    Timer3.setPeriod(1500000); // in microseconds
    Timer3.setChannel3Mode(TIMER_OUTPUT_COMPARE);
    Timer3.setCompare(TIMER_CH3, 3);  // Interrupt 1 count after each update
    Timer3.attachCompare3Interrupt(__Timer3);
    Timer3.refresh();
    Timer3.resume();

//Timer 4
    Timer4.pause();
    Timer4.setPeriod(2000000); // in microseconds
    Timer4.setChannel4Mode(TIMER_OUTPUT_COMPARE);
    Timer4.setCompare(TIMER_CH4, 4);  // Interrupt 1 count after each update
    Timer4.attachCompare4Interrupt(__Timer4);
    Timer4.refresh();
    Timer4.resume();
}
Post Reply

Return to “Libraries & Hardware”