2

Author Topic: Need some help on interrupt code (axon)  (Read 2071 times)

0 Members and 1 Guest are viewing this topic.

Offline TomasTopic starter

  • Full Member
  • ***
  • Posts: 111
  • Helpful? 0
Need some help on interrupt code (axon)
« on: February 11, 2009, 11:45:03 AM »
Hi. Im not sure how to use interrupts yet, I've read around on this site and others, but none has given me much information on how to do the thing.

Anyway, I want to use interrupt to update my IR sensors at a constant rate, 20 times a second (50ms delay).

This is three global variables which I want to update at the specified rate.
Code: [Select]
//global variables:
int Left;
int Front;
int Right;
//code
void UpdateSensors(void)
{
Left = sharp_IR_interpret_GP2Y0A02YK(a2dConvert8bit(0));
Front = sharp_IR_interpret_GP2Y0A02YK(a2dConvert8bit(1));
Right =sharp_IR_interpret_GP2Y0A02YK(a2dConvert8bit(2));
}

Anyone care to explain to me how I can use interrupt to update these variables 20 times a second? Is it even recommended? :) Thanks!

Thanks for any help :)

Offline yerbie

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 3
Re: Need some help on interrupt code (axon)
« Reply #1 on: February 11, 2009, 02:04:19 PM »
you'll need to use timers from the axon.
1. initialize the specific timer for the correct resolution.  In other words, figure out the DIV with FCPU and figure out what the count should be to equal 50ms.
2. enable the timer
3. you can use the timerAttach function to attach your own method you want to run
4. change your variables to volatile int Left, etc...
5. your code might look like this:
Code: [Select]
volatile int Left;
volatile int Front;
volatile int Right;

void updateSensors(void) {
  if ((timer1overflow * 255 + timer1count) >= <some number calculated in  #1>) {
    Left = sharp_IR_interpret_GP2Y0A02YK(a2dConvert8bit(0));
    Front = sharp_IR_interpret_GP2Y0A02YK(a2dConvert8bit(1));
    Right =sharp_IR_interpret_GP2Y0A02YK(a2dConvert8bit(2)); 
    reset_timer1_overflow & TCNT1 = 0
  }
}

timerAttach(updateSensors);

the code above is just pseudo code and I don't remember if timer1 is 8-bit or 16-bit, hence the 255 (for 8-bit).

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: Need some help on interrupt code (axon)
« Reply #2 on: February 11, 2009, 11:10:10 PM »
Quote
Anyone care to explain to me how I can use interrupt to update these variables 20 times a second? Is it even recommended?
The best time to read sensors is just before you are going to use sensor data. At 50ms delay, your sensor data will be outdated by up to 50ms. I wouldn't recommend using interrupts for this sensor unless you're doing something special I don't know about :P

 


Get Your Ad Here