Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

Phase and Frequency Correct PWM on a Mega 8

Phase and Frequency Correct PWM on a Mega 8

2005-11-09 by Martin Jay McKee

I'm having what I can only classify as annoying.  I implemented a simple
servo controller with a tiny 15 ( two position ) and it worked perfectly.
I've also done some simple PWM stuff with Timer 2 on the Mega 8.  Now,
however I need to use the 16 bit timer to generate PWM.  I need a bare
minimum of 12 bits precision but more would be better ( 256 servo positions,
60 Hz update rate ).  To that end I've tried to get 16 bit phase and
frequency correct PWM working and I've had no luck.  In the simulator the
timer counts up to TOP (in my case 0xFFFE ) and promptly overflows to zero.
Now it's my understanding that it is supposed to count up then down (
certainly that's how it works in 8 bit mode ).  It wouldn't bug me so much,
it overflowing, if I was able to get something useful out of it.  However
because it is overflowing the output pin is only getting set or cleared (
depending on the setting of the COM1nX pins ).  Following is the code I use
to initialize it.     

 

ldi temp, (1<<COM1A1) + (1<<COM1B1)                         ; Initialize
Phase and Frequency Correct PWM

            out TCCR1A, temp

 

            ldi temp, (1<<WGM13) + (1<<CS10)                               ;
TOP = ICR1

            out TCCR1B, temp

 

            ldi temp, 0x01
; Initialize OCR1A

            ldi temp2, 0xFF

            out OCR1AH, temp

            out OCR1AL, temp2

 

            ldi temp, 0x01
; Initialize OCR1B

            ldi temp2, 0xFF

            out OCR1BH, temp

            out OCR1BL, temp2

 

            ldi temp, 0xFF
; Initialize TOP

            out ICR1H, temp

            ldi temp, 0xFE

            out ICR1L, temp 

 

I have tried the code in an actual circuit and it doesn't seem any better
than the simulator.  I don't have a scope ( its on the wish list ) but I
don't seem to be getting anything on the output pins.  Actually my frequency
meter is registering 60Hz with out the servo and 0 with the servo attached (
a load of  > 100k so it looks like feed through noise ).  I sure would like
to get this working but I'm not sure where to go from here.  Incidentally I
am having the same trouble with phase correct PWM.  In the simulator all the
registers will show their proper values ( according to the data sheet ) but
the operation isn't as expected ( reset of the timer at TOP ).  Thanks in
advance.

 

Martin Jay McKee

Re: [AVR-Chat] Phase and Frequency Correct PWM on a Mega 8

2005-11-09 by Dave Hylands

Hi Martin,

> I'm having what I can only classify as annoying.  I implemented a simple servo controller with a tiny 15 ( two position ) and it worked perfectly.  I've also done some simple PWM stuff with Timer 2 on the Mega 8.  Now, however I need to use the 16 bit timer to generate PWM.  I need a bare minimum of 12 bits precision but more would be better ( 256 servo positions, 60 Hz update rate ).  To that end I've tried to get 16 bit phase and frequency correct PWM working and I've had no luck.  In the simulator the timer counts up to TOP (in my case 0xFFFE ) and promptly overflows to zero.  Now it's my understanding that it is supposed to count up then down ( certainly that's how it works in 8 bit mode ).  It wouldn't bug me so much, it overflowing, if I was able to get something useful out of it.  However because it is overflowing the output pin is only getting set or cleared ( depending on the setting of the COM1nX pins ).  Following is the code I use to initialize it.

I wrote some code for the ATMega128 which uses Timer3, but it should
work on Timer1 with a minor tweak (changing the SERVO_1 and SERVO_2
definitions to be 1A and 1B rather than 3A and 3B).. It uses the Fast
PWM mode runs the timer at 2MHz, which means it will overflow every
32.7 msec (or 30.5 times per second).

The sample code can be found here:
<http://websvn.gumstix.com/listing.php?repname=Buildroot&path=%2Fbranches%2Fprojects%2Frobostix%2F&rev=0&sc=0>

You'll be prompted for a username and password, use the word gumstix for both.

To get the complete example to build, you'll need the Common
directory, the Simple-Servo directory, and the Rules.mk file.

--
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.