Yahoo Groups archive

AVR-Chat

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

Thread

Mega16 PWM

Mega16 PWM

2008-02-24 by J C

I am trying to control 2 servos with an ATMEGA16 running off of an 8MHz crystal. I have tried to figure it out with various code examples on the internet but don't seem to be getting it. Here is my code, any ideas?


#include <avr/io.h>

#define F_CPU        8000000

// Set for Prescaler of 8 at 8MHz xtl = 1us
#define Timer1ON()   TCCR1B|=0x0D    
#define Timer1OFF()  TCCR1B&=0xF7

int main(void)
{
    Timer1OFF(); // stop timer 1

    // Set for PWM (Table 47, mode 10 page 112 
    // of Mega16 datasheet)
    TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11);
    TCCR1B = _BV(WGM13);

    // 20000 * 1us = 20ms or 50Hz
    ICR1 = 20000;

    // set the port pin and direction
    DDRD |= 0x30;
    PORTD &= (0xFF ^ 0x20);

    Timer1ON();

    while(1)
    {
        OCR1A = 1500;
        OCR1B = 1500;   
    }
} 

       
---------------------------------
Looking for last minute shopping deals?  Find them fast with Yahoo! Search.

[Non-text portions of this message have been removed]

Re: [AVR-Chat] Mega16 PWM

2008-02-25 by David VanHorn

On Sun, Feb 24, 2008 at 5:52 PM, J C <stereotyperjack@yahoo.com> wrote:
> I am trying to control 2 servos with an ATMEGA16 running off of an 8MHz crystal. I have tried to figure it out with various code examples on the internet but don't seem to be getting it. Here is my code, any ideas?


What I do, is to set up an array of bytes in memory, each holding a
0-255 value for pulse width.
I add this to a minimum pulse width, and create timer values.

I start all the servo pulses at once, then turn them off at the
appropriate time using a single timer interrupt.

So the output looks like

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

and so on.

Then the framing pulse (15mS or so) is just a do-nothing time.

Re: [AVR-Chat] Mega16 PWM

2008-02-28 by J C

Thanks for that idea, I did get the other code running but the clock wasn't set right so I wasn't seeing any pulses. I might try that idea in the future.

David VanHorn <microbrix@gmail.com> wrote:                               On Sun, Feb 24, 2008 at 5:52 PM, J C <stereotyperjack@yahoo.com> wrote:
 > I am trying to control 2 servos with an ATMEGA16 running off of an 8MHz crystal. I have tried to figure it out with various code examples on the internet but don't seem to be getting it. Here is my code, any ideas?
 
 What I do, is to set up an array of bytes in memory, each holding a
 0-255 value for pulse width.
 I add this to a minimum pulse width, and create timer values.
 
 I start all the servo pulses at once, then turn them off at the
 appropriate time using a single timer interrupt.
 
 So the output looks like
 
 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
 
 and so on.
 
 Then the framing pulse (15mS or so) is just a do-nothing time.
 
     
                               

       
---------------------------------
Looking for last minute shopping deals?  Find them fast with Yahoo! Search.

[Non-text portions of this message have been removed]

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.