Pages: [1]   Go Down
Author Topic: Timer library  (Read 775 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i'm sharing with you my last library for controlling timer interrupts. It's really easy to use.

It's called atmega-timers and code and information is hosted at github:
https://github.com/jvalencia80/atmega-timers

Give it a try on arduino and let me know how is it working for you smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello. Do you know what the smallest resolution in microseconds that can be accurately obtained with Timer2? I am using FlexiTimer2 which says that it is based on MsTmier2. However, when I set a rate equivalent to about 40us (25KHz), I don't see the correct number of callback counts in 1 second. Here is a snippet of code to illustrate the problem. When I use FLEXI_RATE=20000 (or smaller), I do see my print statement come out once every second. However, when I set FLEXI_RATE=25000 or greater, I do not see the print statement come out every second - they come out slower (maybe every 2 seconds). My goal is to have an accurate function callback every 10useconds, which means FLEXI_RATE=100000. Any thoughts?

#include <FlexiTimer2.h>

#define FLEXI_RATE 25000
static unsigned long pwm_ct=0;
void simulatePWM() {
    if ((pwm_ct % FLEXI_RATE) == 0)
    {
        Serial.print(pwm_ct);
    }
    ++pwm_ct;
}
void setup() {
  Serial.begin(9600);
  FlexiTimer2::set(1, 1.0/FLEXI_RATE, simulatePWM);
  FlexiTimer2::start();
}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13469
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

to speed up I see two things
* Remove the modulo math, its expensive
* use higher baudrates ..

Code:
#include <FlexiTimer2.h>

#define FLEXI_RATE 25000

static unsigned long pwm_ct=0;

void simulatePWM()
{
    if (pwm_ct - FLEXI_RATE == 0)
    {
        Serial.print(pwm_ct);
        pwm_ct = 0;
    } else
    ++pwm_ct;
}

void setup()
{
  Serial.begin(115200);
  FlexiTimer2::set(1, 1.0/FLEXI_RATE, simulatePWM);
  FlexiTimer2::start();
}

does this help?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, it looks like the biggest contributor to the problem was the modulo math. I should have known! Thanks for the quick response. I see the correct number of counts (when measuring over 60 seconds) when FLEXI_RATE is 25000 and 50000. However, when I get to FLEXI_RATE=75000 I start to see the problem, e.g. with FLEXIRATE=100000, I see  83,333 ticks in 60 seconds when I expect 100,000.

So is the problem specifically caused by the simulatePWM() function taking longer to run than the base rate interval, effectively causing it to overrun the interval? If so, then it sounds like the fastest I will be able to run and execute a small amount of instructions is probably 50,000Hz, and the instructions will likely need to be simple direct port writing operations to keep it quick.
Logged

Pages: [1]   Go Up
Jump to: