Arduino Mega2560 16bit timer problem

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using an arduino mega2560 and I want to set up timer5 in CTC mode to generate an interrupt every 250ms

The arduino board is running at 16MHz and I am using the arduino libraries (I have to) but I want to set up this timer manually. Here is how I set up the timer:

TCCR5A = 0x00;
TCCR5B = (1<<WGM52)|(1<<CS52)
OCR5A  = 15624;
TIMSK5 = 0x02;

I'm setting the prescaler to 256, enabling ctc mode, setting OCR5A to my desired value, and enabling the compare match A interrupt for timer5.

However it's running way too fast. On closer inspection of TCNT5 it seems the counter is only counting up to 0xFF (making it behave like an 8 bit timer)

I've spent hours just trying to figure this out. Any help would be appreciated.

Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try to use BIT_NAMEs. It makes life easier.

   TCCR5A = 0x00;
   TCCR5B = (1<<WGM52)|(1<<CS52);     //CTC div256
   OCR5A  = (F_CPU/256) * 0.25 - 1;   //15624;
   TIMSK5 = (1<<OCIE5);               //0x02;

Personally, I would have added the above comments (except the 0x02)

They result in trivial wear and tear to your fingers. They save a lot of brain-ache.

David.

Edit. I corrected my erroneous 'WGM51'. If your timing is wrong, you must either have the F_CPU wrong or a problem with your ISR()

Last Edited: Tue. Jun 18, 2013 - 08:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

but the datasheet says that for CTC mode I need to enable WGM52 not WGM51. Does that mean there's an error in the datasheet?

Re. bit names, I usually use them, But when the program didn't work, I opened up the datasheet for the 2560, and checked everything bit by bit (hence the "magic" constants in that sample)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oops. I was looking at the data sheet:

Quote:
Table 20-8. Waveform Generation Mode Bit Description

It says WGM1 for CTC mode.

I should have looked at 16-bit timers:

Quote:
Table 17-2. Waveform Generation Mode Bit Description

My apologies. If you still have a problem, I can dig out an arduino to try it on.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you have an ISR to handle the interrupt?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yeah I have set an isr using this code (I included avr/interrupt.h)

ISR(TIMER5_COMPA_vect)
{
    counter++;
}

So I have set up everything correctly then? It's still only counting up to 0xFF

The same code works fine on an atmega328p (programmed directly, not through arduino, and using timer1 instead of 5 for obvious reasons)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I get 4Hz interrupt rate with this on an ArduinoMega (mega1280)

#include 
#include 

volatile uint8_t counter;

ISR(TIMER5_COMPA_vect)
{
    counter++;
}

int main(void)
{
    uint8_t i = 0;

    DDRB = 1<<7;
    
    TCCR5A = 0x00;
    TCCR5B = (1<<WGM52)|(1<<CS52);     //CTC div256
    OCR5A  = (F_CPU/256) * 0.25 - 1;   //15624;
    TIMSK5 = (1<<OCIE5A);              //0x02; 

    sei();
    while(1)
    {
        if (i != counter)
        {
            i = counter;
            PORTB ^= 1<<7; // Toggle a pin
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Doing it the hard way. Got the arduino libraries working with atmel studio, so I can view a disassembly and figure out what the hell is going on. Thanks everyone for your help