go_away

Author Topic: Atmega168 Timer1 PWM  (Read 6284 times)

0 Members and 1 Guest are viewing this topic.

Offline SomeSabaTopic starter

  • Full Member
  • ***
  • Posts: 75
  • Helpful? 0
Atmega168 Timer1 PWM
« on: December 18, 2007, 09:08:18 PM »
Hello,

I am having serious trouble getting a proper PWM with my 16bit Timer1 on my mega168.
First i set it for 8-bit fast PWM
(Qustion 1: what does 8bit pwm mean? is the 16bit TCNT1 limited to 8-bits?) i found no mention in the datasheet of this

I also set the the OC1A and OC1B ports for output, and made the timer set those pins high (then low at BOTTOM) on a compare match with OCR1A/OCR1B.

My goal is to be able to send two different PWM's on the two pins, but here what i think happens:

TCNT1 increments, once it reaches OC1A, the pin is set to low (then high), and if OC1B is a diff value TCNT continues to increment until it reaches OC1B, then clears and sets OC1B, so the PWM's have very short time frames during which they are at a low state. Now my problem is, i cant really use the overflow interupt to set TCNT1 back to 0 once it reaches 255 because it overflow at 2^8, its a 16bit timer, what do i have to do here? I cant dedicate the entire proccessor time to checking TCNT1

Any clearification is appreciated thank you!

Offline DomoArigato

  • Full Member
  • ***
  • Posts: 54
  • Helpful? 1
Re: Atmega168 Timer1 PWM
« Reply #1 on: December 19, 2007, 12:47:43 AM »
Can you post the values you stored in the control registers?  I'm pretty sure 8 bit means your using the 8 bit timer, not the 16 bit timer.  What are your trying to control?  I think Phase and Frequency correct PWM may be what you want?
« Last Edit: December 19, 2007, 12:49:47 AM by DomoArigato »

Offline DomoArigato

  • Full Member
  • ***
  • Posts: 54
  • Helpful? 1
Re: Atmega168 Timer1 PWM
« Reply #2 on: December 19, 2007, 01:29:29 AM »
For phase and frequency correct mode (probably better if your using pwm to control your robot) here is what you need to do.  First you need to determine what you want your base frequency to be, how long do you want each PWM cycle to last?  Then you need to decide how long you want the pulse to last for that cycle.  For example for a servo you probably want a cycle of 50hz (a cycle of 20ms).  Then if you want the servo to center itself, you'll usually want to send a pulse of 1.5ms, so of that 20ms for 1.5 seconds the wave will be high and 17.5 the wave will be low.  To get the right base frequency you'll have to select a prescaler, use guess and check to find the one that will give you the highest resolution, and still give you the base frequency you need.  Then once you have a prescaler, you can solve for the value you need to get a 20ms PWM cycle.  For phase and frequency correct mode, for one cycle the counter will count from zero, to the TOP value, then decrease from TOP back down to zero.  So the equations you need for the first two steps are these:

Resolution = log(top value + 1)/log(2)
PWM freq. = clock freq. / (prescaler * 2 * top)
top = clock freq. / (PWM freq. * 2 * prescaler)

In the AtMega128 I set the registers to store the top value will be stored in ICR1, but in the AtMega168 it looks like you'll use the OCRA as top.

ICR1 = CLOCK_FREQUENCY / (baseFrequency * 2 * prescaler); // ICR1 is the top value.

You'll want to make sure you set both outputs A and B to set on compare, then set their values to generate the size pulse you want.  For example I set the TOP value to 20000, then I set OCR1A to 1500 and OCR1B to 1500 as a default (1.5ms pulse).  I hope this is helpful.  Let me know what your putting in the registers and I can tell you if it's right.  You shouldn't have to use any interrupts or active processing to generate your PWM signals.

Offline SomeSabaTopic starter

  • Full Member
  • ***
  • Posts: 75
  • Helpful? 0
Re: Atmega168 Timer1 PWM
« Reply #3 on: December 19, 2007, 03:25:10 PM »
I got it work (kinda), only some (seemingly random values for OCR1A and OCR1B dont work like 255)

Clock is 1Mhz / (8 prescale*256) gives me around 2000 us (im using 8bit fast PWM) not phase corrected

I set OCR1A and OCR1B to 250 and it works, a 1500 us signal means OCR1A and OCR1B are 187, and that works too.

So i guess.... everything works... but idk why 255 doesnt work

I dont understand the diff btwn phase corrected and fast PWM, does the timer just increment going up and down?

Offline DomoArigato

  • Full Member
  • ***
  • Posts: 54
  • Helpful? 1
Re: Atmega168 Timer1 PWM
« Reply #4 on: December 19, 2007, 04:18:11 PM »
Yes the only difference between the two is that phase and frequency correct mode goes up to top then backdown.  This gives you a greater resolution PWM signal.  It works almost the same.  You cant use 255 as a value because the top value is 255. 2^8 = 256, but one of those 256 values is zero.  You can set the top value to anything you want, but by default it will be FF or 255.  If your trying to control a normal hobby servo you don't want a 2ms cycle, you want a 20ms one.  You can achieve this in fast PWM by changing your prescaler to 256 and your TOP value to 78 or 79.  This will give you a 19.968ms cycle or 20.224ms cycle.  If you use phase correct you can get your numbers (cycle and pulse) closer to the values you really want, thats the only difference between the two.

Offline SomeSabaTopic starter

  • Full Member
  • ***
  • Posts: 75
  • Helpful? 0
Re: Atmega168 Timer1 PWM
« Reply #5 on: December 19, 2007, 09:15:49 PM »
Thank you for your help!

Everything works now! <3

Thank you

 


Get Your Ad Here

data_list