I wanted to read a pair of quadrature encoders with my atmega32, so I needed to enable the INT0 and INT1 pins. Incidentally, if you don't mind throwing away half the resolution of your encoder, you can get away with using only one interrupt per encoder. (You put one of the pair of sensors on an interrupt, and the other one on a regular GPIO pin. You get interrupts half as often, and the other sensor tells you which way you moved).
Here's the code I used. The compiler complained about using obsolete avr-libc identifiers, but it seems to work fine.
#include <avr/interrupt.h> #include <avr/signal.h> // Just count the number of interrupts we see int interrupt_counter = 0; SIGNAL(SIG_INTERRUPT0) { interrupt_counter++; } // I got lots of random resets until I added this handler. So apparently you shouldn't enable // an interrupt unless you have a handler installed. SIGNAL(SIG_INTERRUPT1) { interrupt_counter++; } And then in my init code: #define _BV(x) (1 << (x)) // setup for servicing INT0, INT1 interrupt pins // generate an interrupt on any logical change MCUCR |= _BV(ISC10); MCUCR &= ~_BV(ISC11); MCUCR |= _BV(ISC00); MCUCR &= ~_BV(ISC01); // enable the interrupt pins GICR |= _BV(INT1) | _BV(INT0); // Set d2 and d3 to be inputs DDRD &= ~(_BV(PD2) | _BV(PD3)); // Pullups off PORTD &= ~(_BV(PD2) | _BV(PD3));
No comments:
Post a Comment