avr-chat
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-chat] PWM, to fast!


From: Matthew MacClary
Subject: Re: [avr-chat] PWM, to fast!
Date: Mon, 6 Jun 2005 09:45:09 -0700
User-agent: Mutt/1.4.2.1i

On Mon, Jun 06, 2005 at 10:42:50AM -0500, gecko gecko wrote:
>    I've just got the hang of PWM (i think) but it turns out that its just
>    to fast!

    Glad, to hear it. I am lost though on how you are doing PWM
without using timers or a software PWM...

>    I need about 30Hz frequency and to vary the duty cycle from
>    full(ish) to about 1/3. The idea is to control the speed of a fan
>    by the PWM.

    This should work fine as long as you have a good quality motor
driver in between your uC and the fan. Keep in mind that both the motor
driver circuitry and the fan motor will have maximum and minimum
frequency requirements - though I expect 30Hz to be doable.

>    Am i right in thinking:    8MHz / 128prescalar = 62500kHz is the
>    slowest PWM I can make?

    No, you can go much much slower. That type of frequency wouldn't
be useful in many PWM applications! Note that your units seem to have
come out wrong in the above calculation 8MHz / 128 = 62.5KHz.

>    If so I need to write a software PWM, and so any pointers in the
>    right direction would be appreciated.

    No, you should just let a timer do it. Here is some example
code. Note that I have a 16 MHz crystal on a ATmega128 but your chip
has a two channel 16bit timer also. There is of course no reason to
use both channels in your design, that is just what I needed
in my code.

-Matt


  /* Set up PWM  http://members.shaw.ca/climber/avrpwm.html*/
  /* set up counter 1 (16 bit) to act as a dual channel PWM generator */
  TCCR1A = (1<<COM1A1) // set OC1A/B at TOP
    | (1<<COM1B1) // clear OC1A/B when match
    | (1<<WGM11); // mode 14 (fast PWM, clear TCNT1 on match ICR1)
  TCCR1B = (1<<WGM13)
    | (1<<WGM12)
    | (1<<CS11); // timer uses main system clock with 1/8 prescale
  ICR1 = 40000; // used for TOP, makes for 50 hz PWM, period= 20ms

  OCR1A = 2000; // servo at -max degrees 
  OCR1B = 2000; // servo at +max degrees





reply via email to

[Prev in Thread] Current Thread [Next in Thread]