« Drop & Run Checkpoint Passed | Main | First Sensor Amplifier »

Sun, March 5, 2006 12:09 AM

Timer 3 Notes

Setting up the ATmega128 Timer 3 was a bit confusing, primarily because I was inadvertently enabling the wrong interrupt.

We use Timer 3 to set up a 100 Hz interrupt to drive the control loop. Speed and steering feedback control will be processed in this loop (at this time, ADC conversions will happen in their own loop, sequencing among channels and storing the values in global variables. Some kind of synchronization with the main control loop will likely be necessary, or at least, perhaps helpful).

The following is the code excerpt for setting up Timer 3 (fosc = 16 MHz):

//    Output disconnected (we only want interrupts)
//    CTC, TOP := ICR3

TCCR3A = (0 << COM3A1) | (0 << COM3A0)
            | (0 << COM3B1) | (0 << COM3B0)
            | (0 << COM3C1) | (0 << COM3C0)
            | (0 << WGM31) | (0 << WGM30);

//    Disable input capture noise canceler, edge select to negative.
//    CTC, TOP := ICR3
//    prescale:  fosc / 8

TCCR3B = (0 << ICNC3) | (0 << ICES3)
            | (1 << WGM33) | (1 << WGM32)
            | (0 << CS32) | (1 << CS31) | (0 << CS30);

ICR3 = 9999;            //    100 Hz loop
//ICR3 = 1999;          //    500 Hz loop
//ICR3 = 999;           //    1000 Hz loop
//ICR3 = 499;           //    2000 Hz loop

//    Reset the timer

TCNT3 = 0;

//    Enable Timer 3 overflow interrupt

ETIMSK |= (1 << TICIE3);

The interrupt handler is defined like this:

ISR(TIMER3_CAPT_vect)
{
    //  Blink an LED so we can
    //  measure the frequency

    PORTC ^= (1 << kPinYellowLED);
}

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


Please enter the security code you see here