Controlling Servo Motors with AVR
Microcontrollers and CodeVision AVR
You can use the AVR micro controllers PWM feature to control servo motors. In this way the PWM with
automatically generate signals to lock servo and the CPU is free to do other tasks. To
understand how
you can setup and use PWM you need to have basic understanding of hardware timers and PWM
modules in AVR. The following articles may be of great help.
Here we will use AVR Timer1 Module which is a 16bit timer and has two PWM channels(A and B). The
CPU frequency is 16MHz, this frequency is the maximum frequency that most AVRs are capable of
running. And so it is used in
most development board like
Low Cost AVR Development Boar
d
and
xBoards
. We chose the prescaler as 8. So the timer will get 16MHz/8 = 2000khz. We setup Timer
Mode as Mode 8.
Timer Mode 8 features are:
PWM,
Phase and Frequency Corret
TOP Value = ICR1
So the timer will count from 0 to ICR1(TOP Value). The formula for PWM frequency and calculation for
TOP value is given below
( )
( )
( )
( )
So we set up ICR1A=19999, this gives us PWM period of 20ms (50 Hz).
The above settings clears the OC1A (or OC1B) pin on Compare Match and SET ( to high) at BOTTOM.
The OC1A and OC1B pins are the PWM out pin in ATmega16/ATmega32 chips. This settings gives us
NON inverted PWM output.
Now the duty cycle can be set by setting OCR1A and OCR1B registers. These
two register controls the
PWM high period. Since the period of timer is 1uS (16MHz/(8*2)) we can calculate values required for
following servo angles if we using
HS-311 standard servo (180 degree).
Servo Angle 0 degrees require pulse width of 600 uS so value of OCR1A = 600 us/1us = 600
Servo Angle 90 degrees require pulse width of 1500 uS so value of OCR1A = 1500us/1us = 1500
Servo Angle 180 degrees require pulse width of 2.140ms(2140uS) so value of OCR1A =
2400us/1us = 2400
USING FAST PWM AND TOP=ICR1 ( TIMER MODE 14)
The timer will count from 0 to ICR1(TOP Value). The formula for PWM frequency and calculation for
TOP value is given below
( )
( )
( )
( )
So we set up ICR1A=39999, this gives us PWM period of 20ms (50 Hz).
The above settings clears the OC1A (or OC1B) pin on Compare Match and SET ( to high) at
BOTTOM. The OC1A and OC1B pins are the PWM out pin in ATmega16/ATmega32 chips.
This settings
gives us NON inverted PWM output.
Now the duty cycle can be set by setting OCR1A and OCR1B registers. These two register controls
the PWM high period. Since the period of timer is 0.5uS (16MHz/8) we can calculate values required for
following servo angles if we using
HS-311 standard servo (180 degree).
Servo Angle 0 degrees require pulse width of 600 uS so value of OCR1A = 600 us/0.5us = 1200
Servo Angle 90 degrees require pulse width of 1500 uS so value of OCR1A = 1500us/0.5us =
3000
Servo Angle 180 degrees require pulse width of 2.140ms(2140uS) so value of OCR1A =
2400us/0.5us = 4800