Using Both A&B Output Compare on the ATMega16

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

Hi
I'm wondering how to use both OCE1EA and OCE1EB
Output compare. I want one to be 5ms long and the other run at 1920us. I was running in mode 4 but notice that the timer cleared on compare messing things up.
The crystal I have installed is 6Mhz.

I'm playing with the timers to see what kind of neat things I could do with the ATMega16. I have a AVR-JTAGICE. works much better than my old ICE200.
Thank you!
Don

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

You'll have to enable compare interrupts and don't run the CTC mode. In the ISRs for the compares, add the number of ticks for half the required frequency to the OCR registers. Set the output to toggle on compare.

For example:

ISR_OCR1A:
{
OCR1A += 15060; //add 2.5mS worth of ticks
}

do something similar for the other compare channel.

[edit]
I should've said "half the required period" or twice the required frequency - as you have a low period and a high period to make one cycle. So for 5mS, you get 2.5mS low,2.5mS high.

Last Edited: Wed. Aug 13, 2008 - 09:03 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If your in mode 4, the timer should clear on match for OCR1A. That's what the "top" column means in the datasheet. I think you'll need to use mode 0.

Also, you can't really use the output function to generate two frequencies off of the same timer unless you change the OCR register each time you have a compare interrupt (which is definitely doable, however).

edit: whoops, guess I type too slow.

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

Quote:
I'm wondering how to use both OCE1EA and OCE1EB
Output compare. I want one to be 5ms long and the other run at 1920us. I was running in mode 4 but notice that the timer cleared on compare messing things up.

Exactly what do you mean by this? Do you indeed want two different frequencies or do you mean two different pulse width (but one frequency)?

Regards,
Steve A.

The Board helps those that help themselves.

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

Hello
Thank you for all the responces.
I'm looking to generate interrupts to measure time.
Say read the input pin every 5ms and debounce them for 50ms more by test the pin again. Also I wanted to use the 1920us oc interrrupt to do some other work. I try mode0 with the simulator in the avr studio.

I'm would like run both oce1a and b at the same time.
Each will have its own interrupt routine.
Thank you
Don

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

Simply set a flag in your 2ms (1920us) ISR; perhaps count 2 to 5 of them to get the few ms debounce interval. Then, act on the flag in the mainline by clearing it, fetching the input states, and enter the sample int the debounce sequence.

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.