AVR Interrupt Question
March 15, 2010
Hi all, here is my dilema:
I am attempting to fashion my own rotary encoder to detect wheel rotation speeds. My approach is to use a disc with a slotted pattern cut into it and a photo interrupter.
This is the photo interrupter I have on hand: http://www.sparkfun.com/datasheets/Components/GP1A57HRJ00F.pdf
I am looking to measure the speed of 4 separate wheels. Each wheel will have 1 disc, and 1 photo-interrupter (only using a single channel as direction is not important to me). The photo-interrupters will be attached to an Arduino Mega. The way I am considering capturing the data is by simply attaching the "gate pins" (the ones that output the information) of each photo-interrupter to pins of my Arduino that support external interrupts. I would then write some sort of interrupt service routine that would simply increment a counter each time the interrupt was triggered. What I am curious is if this will take up too much CPU time and essentially deadlock my processor?
Here is my reasoning (hope it is not flawed):
Assuming I went with say 32 notches on my disc and spun it at the max speed of 500 rpm (8.33 rev/s). That would be ~267 pulses per wheel / second. That would be 1068 pulses from all 4 motors / second. That means 1068 ISRs would run every second.
Assuming an add instruction works as follows:
B+ C ---> A (where C = counter, B = 1, A = memory spot to store)
Fetch instruction (add) from memory address PC.
Increment PC to address of next instruction.
Decode the instruction and operands.
Load the operands B and C from memory.
Execute the add operation.
Store the result into memory location A.
That is 6 clock cycles for an add. Assuming each time an ISR runs I am just adding to a counter. 6*1068 = 6400 clock cycles consumed every second. This is out of a total 16,000,000 (16MHZ crystal on the Arduino Mega).
Perhaps I am over simplifiying the time it would take to run the ISR (due to overhead of backing up registers before running the ISR, etc.) and the time it takes to run the ISR is say 100 cycles. 100 * 1068 = 106,800 cycles. Out of a total 16 Million.
In either case I wouldn't be close to locking up the CPU. Does this make sense?