Pages: [1]   Go Down
Author Topic: Timer overflow interrtups - how ?  (Read 639 times)
0 Members and 1 Guest are viewing this topic.
Glasgow, scotland
Offline Offline
Newbie
*
Karma: 5
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all

I'm making a tachometer and I have some code that's working quite well, It

1. counts the number of times a photodiode is tripped
2. every set amount of time it works out counts/microseconds since last checked.

the 'set amount of time' though is determined by a pretty crude method, so i was wondering how do I set up a timer overflow interrupt ? 
I've seen a couple of examples but really can't work out what anything means! Here's my code just in case:

Quote
// this updates RPM at least every 50000 runs, up to a max rate
// of every 4000 runs.  This is about to 22 to 289ms
// it starts timing from the moment it finishes doing maths to 
// the moment the next blade passes (after min time has elapsed)

// if u wonder why I don't just use a timer instead of 'runs', it's 
// because i'm a noob smiley-grin

unsigned long time=0;
unsigned long time2=0;
unsigned long rpm;
unsigned long period;
volatile unsigned long counts=0;
int potpin=0;   // <---- change to suit your motor and pot pin
int motorpin=5;
int motor;
byte last=0;
byte finished=0;
byte first=1;
byte started=0;
int blades=6;   //   <--- change this, it's number of blades
unsigned long divisor;
unsigned long runs=0;
void setup(){
  Serial.begin(9600);
  attachInterrupt(0,ping,RISING);
  divisor=60000000/blades;
}

void loop(){
  if(runs>4000){
    last=1;
  }
  if(finished || runs>50000){
    time2=micros();  /* putting noInterrupts after reading the time
    means for some reason the time is more accurate, although you run 
    the risk of interrupt tripping between reading time and 
    calling noInterrupts, but srsly how likely is that ? */
    noInterrupts();
    period=(time2-time); 
    rpm=(counts*divisor/period);
    Serial.println(rpm);
    counts=0;
    runs=0;
    last=0;
    finished=0;
    interrupts();
    time=micros();
    
  }
  runs++;
}
void ping(){
  counts++;
  if(last){
    finished=1;
  }
}

Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The Arduino-based interrupts do not support the Timers.  Your code and your description don't really seem to match.

I'm finding it difficult to understand why you need to keep track of time.  How critical is the timing?  Do you really need a time based interrupt or can you just check your variable in the loop (as looks like you are doing now)?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Glasgow, scotland
Offline Offline
Newbie
*
Karma: 5
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's not timing critical, but as it is now it takes trial and error to 'set' the time interval.  I want an easier way to set the interval

Plus i've always wanted to know how to do a timer interrupt.  So far i know it starts with

ISR(TIMER0_OVF_vect  . .. .


but how can i set how often it will overflow ?


thanks smiley-grin
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 362
Posts: 17307
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's not timing critical, but as it is now it takes trial and error to 'set' the time interval.  I want an easier way to set the interval

Plus i've always wanted to know how to do a timer interrupt.  So far i know it starts with

ISR(TIMER0_OVF_vect  . .. .


but how can i set how often it will overflow ?


thanks smiley-grin

You could always download the MsTimer2 library and read their source code: http://www.arduino.cc/playground/Main/MsTimer2
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Tutorial on AVR timers.
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=50106
Logged


Pages: [1]   Go Up
Jump to: