Counter mode

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

I'm having a hard time figuring out which counter mode I need in my 16-bit counter in Mega16.

I am not capturing events, generating PWM or anything like that. I just want a predictable delay. Which counter mode is best suited for this:

- Init timer to clk/8, enable interrupts
- Write 0x2300 to the timer register
- Count down to 0x0000
- Receive an interrupt
- Disable interrupts and the timer

Any ideas?

Greetings,

Børge

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

Quote:

- Count down to 0x0000

Almost all AVR timer modes count up. AFAIK, the onlyones that count down are the count up-down PWM modes.

You probably either want to preload the TCNT!x registers (to 65k-0x2300) and count up and interrupt on overflow, or use Output Compare (OCR1A from memory) and set OCR1Ax to 0x2300, TNCT1x to 0, and interrupt on compare match.

In either case, turn off the timer in the interrupt routine by setting the prescaler to 0. Since the timer is stopped and the interrupt will not fire again, it is optional whether you want to unmask the interrupt.

Lee

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

Check out your datasheet of the ATM16 on page 99 at 'Modes of operation' and especially the topic 'Normal mode'. It describes what you ask for and it is the default mode after POR.
In normal mode the timer increments. So to have 0x2300 counts you should write the timer-registers with 0xFFFF-0x2300. Next, enable the timer overflow interrupt (TOIE1 in TIMSK-register). Do this before you start the timer via the prescaler bits (CS12,CS11,CS10 in TCCR1B-register)
Once the interrupt occures you can switch off the timer again.

When you do this from reset on you don't need to worry about input capture and output compare modules. Default these are not 'active'.

Good luck. Eduard

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

Thanks Lee and Eduard!

Got it up and running like that now

Cheers,
Børge