/* * pwm1.c * $Id: pwm1.c,v 1.3 2002/03/02 04:06:10 bryce Exp $ * * Pulse width modulation example code for AVR uC * Bryce Denney * * These examples show a very simple way to do PWM. One function * called do_one_pulse() does all the work. * * Disadvantages: * - it uses all the processor's attention * - only one channel: all motors would have the same amount of power, * when you might want to control different motors separately. * * This software is distributed under the GNU public license. * See http://www.gnu.org/licenses/gpl.html for details. * */ #include /* * do_one_pulse(ON, OFF, LIMIT, PERIOD) * * ON,OFF,LIMIT,PERIOD are all 8 bit values. 0 - 255. * * This function creates just one pulse. To make a stream of pulses, you * must call it over and over. First, the 8-bit ON value is written to * port D. Then, using the variable 'i', it counts from 0 to PERIOD, * increasing i by 1 each time. When i reaches the value of LIMIT, it * changes port D to the OFF value. When i reaches the value of * PERIOD, the function returns. * * So, if LIMIT is 0, you will get the ON value for a very short time * and then the OFF value for a long time. If LIMIT is about half of * PERIOD, you get the ON value for half the time and the OFF value * for the other half. * * The PERIOD setting controls the total time the function takes. * 1.0/PERIOD is proportional to the frequency. For example, * if PERIOD=255 and your crystal is 5MHz, the PWM * frequency turns out to be 3.3khz. If PERIOD=40 with the same * crystal, the PWM frequency is 20khz. * */ void do_one_pulse ( unsigned char on, unsigned char off, unsigned char limit, unsigned char period) { unsigned char i; outp (on, PORTD); for (i=0; i