Timers on Atmega1284 (and the like)

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

So... I finally got my hands on this Atmega1284 beast 8), and am trying to get a mega32 program running on it.

I have changed uart names, and it works, but the timer seems to have changed names as well, and while I found the new names and changed them, I dont get any interrupts.

Atmega32 code.

void initTimer(void)
{
   TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
   OCR1A   = 125; // Compare value 125
   TCCR1B |= (1 << CS12); // Start timer at Fcpu/256
   
   timerCounter = 0;
	
   TIMSK |= (1 << OCIE1A); // Enable CTC interrupt
}


ISR(TIMER1_COMPA_vect)
{
  tick++;
  timerCounter++;
  
  twiDecTo();

  if (tickDiff(seconds) >= 500)
  {
  	tickS++;
	seconds = tick;
  }
}

My attempt on Atmega1284 code


void initTimer(void)
{
   TCCR0B |= (1 << WGM12); // Configure timer 1 for CTC mode
   OCR0A   = 125; // Compare value 125
   TCCR0B |= (1 << CS12); // Start timer at Fcpu/256
   
   timerCounter = 0;
	
   TIFR0 |= (1 << OCF0A); // Enable CTC interrupt
}


ISR(TIMER0_COMPA_vect)
{
  tick++;
  timerCounter++;
  
  twiDecTo();

  if (tickDiff(seconds) >= 500)
  {
  	tickS++;
	seconds = tick;
  }
}

Anyone can see whats wrong the the timer setup?
The freq is the same, in fact the board is the same, since the 1284 seems to be pin compatible with 32A :)

The goal is to have an interrupt every 2ms (like it did on atmega32).

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

Quote:
Anyone can see whats wrong the the timer setup?
You are trying to set CTC mode on Timer0 with WGMxx bits for Timer1. For CTC on Timer0 you need WGM01 in TCCR0A.

With the bits you are using, you are setting mode 4, which is "Reserved".

Regards,
Steve A.

The Board helps those that help themselves.

Last Edited: Mon. Jan 31, 2011 - 08:52 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Timer1 in a mega32 is a 16 bit timer. Timer0 in a mega1284 is an 8 bit timer (I think), but it might have another 'channel'. Why not use timer1 again?

Imagecraft compiler user

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

I changed to timer0 since I guessed the name changed, since mega32 only has 1 16bit, and 1284 has 2.

I have changed back to timer1, but still no interrupts.

void initTimer(void)
{
   TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
   OCR1A   = 125; // Compare value 125
   TCCR1B |= (1 << CS12); // Start timer at Fcpu/256
   
   timerCounter = 0;
	
   TIFR1 |= (1 << OCF1A); // Enable CTC interrupt
}


ISR(TIMER1_COMPA_vect)

As far as I can tell, all names belong to timer1 now, and the interrupt name is correct ?

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

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Well, here is working TIMER0 code for the 1284p. I don't see that you are setting TCCR0A?

#define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect
#define OCRSetup() \
  /* Select internal clock */ \
  ASSR = 0x00; 				  \
\
  /* Set counter to zero */   \
  TCNT0 = 0;				  \
\
  /*						  \
   * Set comparison register: \
   * Crystal freq. is 8000000,\
   * pre-scale factor is 1024, we want 125 ticks / sec: \
   * 8000000 = 1024 * 126.01 * 62, less 1 for CTC mode \
   */ \
  OCR0A = 61; \
\
  /* 								\
   * Set timer control register: 	\
   *  - prescale: 1024 (CS00 - CS02) \
   *  - counter reset via comparison register (WGM01) \
   */ 								\
  TCCR0A = _BV(WGM01); \
  TCCR0B =  _BV(CS00) | _BV(CS02); \
\
  /* Clear interrupt flag register */ \
  TIFR0 = TIFR0; \
\
  /* \
   * Raise interrupt when value in OCR0 is reached. Note that the \
   * counter value in TCNT0 is cleared automatically. \
   */ \
  TIMSK0 = _BV (OCIE0A);
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AND U don't get an interrupt because U don't:

TIMSK0 = _BV (OCIE0A);

OP wrote:
I changed to timer0 since I guessed the name changed, since mega32 only has 1 16bit, and 1284 has 2
My '1284 d.sht. says ONE 16 bitter, BUT I'd be HAPPY if it's wrong about that .

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

The preliminary 1284p datasheet was wrong about the absence of the second 16 bit TIMER3, so maybe TIMER4,5 are worth a try too.

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

WTH ... WHERE is this most correct d.sht 'cause I can't find it at Atmel ? There STILL sayin' ONE .

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

Thanks, but every time I try to load it, my computer just hangs ( it times out after about 2min !!! ). Would someone PM a copy to me ?

TIA

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

dak664 wrote:
[M]aybe TIMER4,5 are worth a try too.
There is no Timer4 and no Timer5. Timer0 and Timer2 are 8-bit timers virtually identical to those on the mega644P. Timer1 and Timer3 are 16-bit timers virtually identical to Timer1 on the mega644P.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

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

indianajones11 wrote:
AND U don't get an interrupt because U don't:

TIMSK0 = _BV (OCIE0A);

Thanks!

So I actually enabled the input compare, instead of CTC ?

This code works.

void initTimer(void)
{
   TCCR1B |= (1 << WGM12) | (1 << CS12); // Configure timer 1 for CTC mode
   OCR1A   = 125; // Compare value 125
   TCCR1A |= (1 << CS12); // Start timer at Fcpu/256
   
   timerCounter = 0;
	
   TIMSK1 |= (1 << OCIE1A); // Enable CTC interrupt
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The CodeVision CodeWizardAVR doesn't allow you to set up Timer3.

So is it OK for the simpletons among us to assume that there really is a 16-bit Timer3 on a 1284P?

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

Quote:

So is it OK for the simpletons among us to assume that there really is a 16-bit Timer3 on a 1284P?

Yes (and a two minute test would prove this)

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

Quote:
So I actually enabled the input compare, instead of CTC ?
No, you didn't enable anything, you cleared an interrupt flag.

Regards,
Steve A.

The Board helps those that help themselves.

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

clawson wrote:
Quote:
So is it OK for the simpletons among us to assume that there really is a 16-bit Timer3 on a 1284P?
Yes (and a two minute test would prove this)

So why does http://www.atmel.com/dyn/product...
...indicate only 1?

The datasheet from http://www.atmel.com/dyn/product...
...indicate only 1?

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

The new datasheet is a combined document covering several AVRs all of which have one 16-bit timer except for the mega1284P which has two. The person that created the datasheet (and the one that proofread it, if any) either overlooked the fact that the mega1284P has an extra 16-bit timer or was ignorant of it.

The original mega1284P datasheet (still available via the ZBasic website http://www.zbasic.net/doc/ATmega... ) covers the mega1284P only and correctly describes the second 16-bit timer.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

Last Edited: Fri. Feb 4, 2011 - 12:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
...still available via the ZBasic website...
Nope.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

indianajones11 wrote:
Nope.
Au contraire. The forum software included the closing parenthesis in the link (I've edited it above). Try this link:
http://www.zbasic.net/doc/ATmega...

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

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

Quote:

So why does

Because human authors are fallible, silicon implementation is not.

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

I wonder if someone can help - ATMEGA1284

 

I found this code to provide a couple of 16 bit Analog out functions for the Arduino.

 

/* Configure digital pins 9 and 10 as 16-bit PWM outputs. */
void setupPWM16() {
    DDRB |= _BV(PB1) | _BV(PB2);        /* set pins as outputs */
    TCCR1A = _BV(COM1A1) | _BV(COM1B1)  /* non-inverting PWM */
        | _BV(WGM11);                   /* mode 14: fast PWM, TOP=ICR1 */
    TCCR1B = _BV(WGM13) | _BV(WGM12)
        | _BV(CS10);                    /* no prescaling */
    ICR1 = 0xffff;                      /* TOP counter value */
}

 

 

/* 16-bit version of analogWrite(). Works only on pins 9 and 10. */
void analogWrite16(uint8_t pin, uint16_t val)
{
    switch (pin) {
        case  9: OCR1A = val; break;
        case 10: OCR1B = val; break;
    }
}

 

Sadly this does not work on the 1284.  I'm guessing that with two timers, it must be possible to get up to 4 16bit PWM channels on the 1284

 

Can anyone suggest the modifications needed to get the above to work on the 1284 and which pins to expect it to work on....  and would a similar thing be possible with the new timer 3 ??

 

 

 

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

How have you tried to do the "port" [pun intended] of this sample code to your app?

 

Have you looked for an Arduino example that uses this processor?

 

What does "doesn't work" mean?

 

scargill wrote:
DDRB |= _BV(PB1) | _BV(PB2); /* set pins as outputs */

On a '1284, what pins are OC1A and OC1B?

 

Have you compared all the other timer configuration settings between the two processor models to see if they correspond exactly?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Guys, this thread is 5 years old...

"Some people die at 25 and aren't buried until 75." -Benjamin Franklin

 

What is life's greatest illusion?"  "Innocence, my brother." -Skyrim

 

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

Well yeah but at least scargill showed some initiative by searching for a thread about 1284 timers to tag his question about 1284 timers onto. Has to be applauded for that.

 

I could split it (if I could trust the moderation "split" function!) but let's see where it's headed first.

 

Moderator