PWM - Pulse Width ModulationIntroServos require PWM outputs from the microcontroller to tell them what position to turn to. Many other control devices also use PWM as their inputs from the microcontroller. These devices expect a certain base period between pulses and use the duration of the pulse to control their behavior. Many devices including most servos are pretty lenient about the base period, and will function pretty well with considerable fluctuation in the period. For those devices, simple software-based PWM works just fine. This is usually accomplished with a macro that sets the pin high, waits N cycles, then sets the pin low. This is then repeated in a loop of approximately the right period for the device. If this works for your application then it by far the easiest way to do it. There are several things that can make this approach fail:
For these situations, you will need to use the built-in interrupt driven PWM output of the atMega chip. On the Axon, you have access to 7 PWM outputs although you have to use up an additional timer to get to the 7th one, so the code below only deals with 6 outputs. The idea is to set up the appropriate timers to a 20mS base period (50 Hz). This is the standard used by most hobby servos and the Sabertooth R/C. Then enable PWM on the pins you need. Then you can change the servo setting by just changing one variable. That pulse width will continue to be sent every 20mS until the variable is changed. On the Axon, you have easy access to PWM on the following pins:
Example CodeInsert the code at the bottom of this page into your project, then you can do PWM on any or all of those 6 pins. For example if you wanted to use PWM on H3 and H4, you could do this:
// Initialize PWM functions on H3-5 with TIMER4 Download |
|