Society of Robots - Robot Forum

Software => Software => Topic started by: SomeSaba on November 11, 2007, 08:35:55 PM

Title: Interupt Question
Post by: SomeSaba on November 11, 2007, 08:35:55 PM
Hello, i havent been able to confirm the existence of this:

I know u can have timer interupts in ur code on like an atmega168

but is it possible to have an interupt whenever a certain digital input pin goes high or low?

If so what is this called so i can consult the datasheet for more info.

thank you everyone!

***what im trying to do is connect my atmega168 to the CLK of my wheel encoder that pulses every 25us or everytime it clicks (i havent gotten that part clearified yet) :P
Title: Re: Interupt Question
Post by: bens on November 11, 2007, 10:41:30 PM
There are two types of interrupts you can consider using for this purpose: external interrupts on pins PD2 (INT0) and PD3 (INT1), and pin-change interrupts on all 23 I/O pins.  Take a look at section 12 (External Interrupts) on page 67 of the mega168 datasheet for detailed information about these.  To quote the introduction to this section:

Quote
The External Interrupts are triggered by the INT0 and INT1 pins or any of the PCINT23..0 pins.
Observe that, if enabled, the interrupts will trigger even if the INT0 and INT1 or PCINT23..0 pins
are configured as outputs. This feature provides a way of generating a software interrupt. The
pin change interrupt PCI2 will trigger if any enabled PCINT23..16 pin toggles. The pin change
interrupt PCI1 will trigger if any enabled PCINT14..8 pin toggles. The pin change interrupt PCI0
will trigger if any enabled PCINT7..0 pin toggles. The PCMSK2, PCMSK1 and PCMSK0 Registers
control which pins contribute to the pin change interrupts. Pin change interrupts on
PCINT23..0 are detected asynchronously. This implies that these interrupts can be used for
waking the part also from sleep modes other than Idle mode.

The INT0 and INT1 interrupts can be triggered by a falling or rising edge or a low level. This is
set up as indicated in the specification for the External Interrupt Control Register A ? EICRA.
When the INT0 or INT1 interrupts are enabled and are configured as level triggered, the interrupts
will trigger as long as the pin is held low.

Addendum: Be cautious when you have interrupts set up to trigger often.   They can end up taking a lot of your processing time, preventing the rest of your program from executing in a timely manner.  If you're going to set up something to interrupt every 25us, try to make the interrupt routine as short as possible (e.g. if you're comfortable doing so, you may want to write the interrupt in assembly to keep it optimally tight).  I also recommend you do the math to make sure you have enough processing time left for the rest of your program (e.g. if your ISR takes 20us and you're interrupting every 25us, you're devoting 80% of your processing time to your interrupt, which is probably not so good).
Title: Re: Interupt Question
Post by: SomeSaba on November 12, 2007, 11:57:03 AM
thank you very much!

I have a lot of room on my board and i have an extra ATtiny84 sitting here... i think ill just have my ATtiny dedicated to the encoders and it can send posistion data via serial from Tx pin to Rx of my m168. And ill use the Tx of my m168 for the motor controller.

IT all works out! Thank you for the help
Title: Re: Interupt Question
Post by: Admin on November 12, 2007, 03:35:48 PM
If you plan to use a high resolution encoder, meaning an overwhelming amount of interrupts, consider looking into something called a counter IC. It counts your encoder clicks without interrupts, and your mcu can read the counter IC only when you need the encoder count.

The avrlib has software written for those who want to use encoders/interrupts with an AVR, including one for quadrature encoders:
http://hubbard.engr.scu.edu/avr/avrlib/docs/html/files.html
Title: Re: Interupt Question
Post by: SomeSaba on November 12, 2007, 08:36:24 PM
Thank you so much everyone

I've been doing a lot of reading on counters and i would love to use one, after much debate i've decided to go with my ATtiny :)

I'll look into the avr-lib now :)

Have a great Thanksgiving!
Title: Re: Interupt Question
Post by: bens on November 12, 2007, 09:35:12 PM
You can use your ATtiny84's timer0 or timer1 fairly easily to count encoder ticks.  Just connect the encoder output to the T0 pin (PA3) or the T1 pin (PA4).  If you then configure the corresponding timer to use the Tn pin as its clock source, the TCNTn register will automatically increment every time your encoder ticks.  For more information on this you can check out the timer section of the ATtiny24/44/84 datasheet.  You can configure your timer to generate an interrupt whenever TCNTn reaches a certain value, or you can use another timer to generate interrupts at fixed intervals where you check the encoder counts by reading TCNTn.
Title: Re: Interupt Question
Post by: SomeSaba on November 12, 2007, 10:01:12 PM
woa!

ok so using the pins as a clock source is a diff approach than ext hardware interupts

i like it! i didnt know u could do that, thats aweomse thank you so much