timer/counter problem - please help

Hi

I'm using an atmega328 to build an engine controller for a final year project and I've run into a problem I can solve.

I initially used the 16bit timer1 as a counter for the encoder, and then periodically fetched the value for TCNT1.

However, using timer1 as a counter prevents the servo function from working, which I need to actuate the engine trottle.

I've tried using timer0, but that seemed to disable the serial com, and I can't figure out how to use timer2 as a counter.

If anyone can help setting up timer2 as a counter, or knows of another method of using a high speed encoder (up to 5000 inputs per second) and servo motor together, i'd be extremely grateful.

This is the timer 2 setup thats not working:

TIMSK2 = 0;            // Disable timer2 interrupts
ASSR = 0b01000000;      // Enable external clock as source
// ASSR | 0b00100000;      // Set to asnychronous mode ??? 
// Apparently timer must be asnychronous to use the external source
TCCR2A = 0;            // Disable Waveform Generation Mode
TCCR2B = 0;       // Use clock with no prescaler
TCNT2 = 0;            // Clear the counter
TIFR2=0;            // Clear the interrupt flag register
TIMSK2 = 1;            // Enable overflow interrupts

I forgot to mention that serial communication with the computer is vital to the project, which is why i timer0 can't be used.

Setting up the timer is pretty straight forward:

        TCNT2 = 0;
        TCCR2A = 0b00000000;     // Normal Mode, free-running, no connection to pins
        TCCR2B = 0b00000010;     // Start timer with pre-scaler of 8
        TIMSK2 = B00000001;      // Overflow interrupt

Depending on what kind of interrupts you want, you might need to adjust TIMSK2. For another pre-scaler setting, adjust TCCR2B.

That was all there is and it worked for me.

Korman

I got the timer working like that, however I'm using the timer to count the outputs of the speed pickup, so it needs to be connected to one of the I/O pins.
I've got the encoder working now with plain hardware interrupts (don't know why I didn't think of it earlier), but i'm still interested to know whether it could be done with timer2.

I'm not entirely sure, but what I think the problem is, is that all write operations the timer2 control registers are timed by the asynchronous clock the moment it's activated. So if the engine is not running the control registers don't get updated, and the timer therefore won't work.