Author
Topic: Atmega168 Timer1 PWM (Read 6453 times)

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

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?

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:

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.

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.