Software > Software

Interupt Question

(1/2) > >>

SomeSaba:
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

bens:
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.

--- End quote ---

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).

SomeSaba:
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

Admin:
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

SomeSaba:
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!

Navigation

[0] Message Index

[#] Next page

Go to full version