Controlling Servo Motors with Various Microcontrollers March 2000 
[ Homepage ][ Send me mail ] © Kam Leang

1  Introduction

The following article is a compilation of information on how to control servo motors using various microcontrollers.  The microcontrollers considered include the Parallax Basic Stamps, Marvin Green's BotBoard (Motorola HC11), the Motorola HC12 MCU, and the MIT Handy Board Microcontroller (Motorola HC11).  Sample code for each controller is included for reference and testing.  Have fun!

2  Servomotor Basics

Servomotors, the same used in remoted controlled cars and planes (a good source for these motors is Tower Hobbies), have become a popular choice for hobby robotics enthusiasts in the past few years.  Why?  Simple.  Each motor assembly comes complete with a gear box and control electronics that make interfacing them to microcontrollers a cinch.  With the wide variety of available end attachments, it is easy to mount wheels and custom mechanisms to the motor (see Figure 1).  In contrast to standard DC motors, a servomotor has three wires,  power (usually +5V @200mA and capable of ~1A peak current), ground, and an input which accepts a pulse-width-modulated (PWM) signal controlling the servos position.  Thus, a motor driver circuit or chip is not required unlike standard DC motors.


Figure 1.  A basic servomotor with a circular end attachment for mounting wheels and mechanisms.

Servos are active devices with on-board position feedback electronics allowing them to be commanded to a position, usually between 0 and 180 degrees.  Being active, when commanded to a position, they will continuously try to minimize the error between the actual and commanded position.  So, if an external disturbance tries to move it from its commanded position, the servo will try to maintain a position of zero error. Servomotors can deliver output torque between 20 to 100+ oz-inches!

The PWM input signal that a servo requires is simply a square wave (0-5V) with the high time being variable as shown in Figure 2.  One of the important characteristics of the PWM signal is that the repeat rate must be between 20-30mSecs (i.e., 50 times a second), otherwise the on-board electronics will ignore the signal or in some cases cause the motor to exhibit poor performance such as chattering and unpredictable movements.  The pulse width, w, determines the command position, which varies from manufacturer to manufacturer, but the neutral position (90 degrees or so) is usually characterized by a 1.5uS pulse width.  Thus, any source capable of supplying this PWM signal can control a servo, which means that microcontrollers are the best canidates for controlling servos because they can easily generate PWM signals.  In addition, the pulse width can be controlled via software, making servos the ideal actuator to use in robotics in terms of locomotion and mechanism control.

Figure 2.  Standard servo PWM signal characteristics.  The repeat rate for the signal is approximately 20mSecs (i.e., 50 times a second) while the pulse width w can vary between manufacturers, but usually the netural position is approximately 1.5uSecs in width.
Although servos are easy to use and interface to microcontrollers, their limited range of motion can be useless for locomotion, unless they are used as joints in a legged robot.  Wheeled robots require continuous rotation.  Servos are not limited to only 180 degrees of travel, but can be modified to rotate continously by removing the position feedback circuit and any internal limits that may exist.  After modification, the servo then thinks that its stuck as one position and as a command position is given to the motor, it tries to reach that position, but never making because there is no feedback.  The motor then rotates continously until another command is given.  The modified servo can rotate in both directions with adequate speed control based on the command position.  For information on how to modify a servo motor for continous rotation, click here.

With the basics of servomotors covered, it's now time to control them using different microcontrollers.  The following sections assumes that you have some knowledge of programming and compiling the applicable code.  But before I begin with the code hacking process, let's talk a bit about hacking a servomotor for continuous rotation, just in case you are planning on using these motors for robot locomotion, for example.

3  Hacking the Servo Motor for Continuous Rotation

Servo motors fresh out of the package are not designed to rotation continuously (i.e., all the way around and around and around).  Instead, they are limited to about 180 degrees of rotation.  Follow the example in this artice Modifying the Tower Hobbies TS-53 Servomotor for Continuous Rotation if you plan on using the servomotor for locomotion purposes, otherwise keep the motor intact.  I would recommend that you read this entire article before modifying anything!  With that established, let's start with one of the most basic microcontrollers, the Basic Stamp.

4  Parallax Basic Stamp Microcontroller

The Parallax Basic Stamp microcontroller is a small and inexpensive controller based on Microchip's PIC line.  Parallax has made building projects and robots easy with their Pbasic programming language designed for the Stamps.  Pbasic can be download from Parallax's website (http://www.parallaxinc.com) for free.  To control a servo, the Pbasic function to use is pulsout.  For example, to generate a repeating 20mSec signal on pin 0 with 1.5us pulse width, the following commands would be used:

loop:    pulsout 0, 150
         pause 20
         goto loop
The function pulsout takes two inputs, the pin number and the number of 10-microsecond time units for generating the pulse.  Thus, the output from the Pbasic instructions shown above would result in a PWM signal on pin 0 with a 1.5uSec pulse width.  The pause 20 command produces the 20mSec separation between each pulse.  Refer to the Parallax Basic Stamp manaul and application notes available at their website for more detail.

5  BotBoard (Motorola HC11) Microcontroller

The BotBoard is a Motoral 68HC11 microcontroller based controller design by Marvin Green (refer to Marvin's website for more details about the BotBoard).  The board is very simple to operate with four servomotor output jumpers connected to the HC11's timer ports.  The assembly program below controls two servomotors connected to PA6 (J2) and PA5 (J3) of the BotBoard.  Note, the program is only a slightly modified version of the program listed in Marvin Green's BotBoard Manual V1.03.  Click here to download the assembly program or the pre-asssembled .S19 file.  Use PCBUG11 to download the .S19 file into the BotBoard microcontroller.

* SERVO_01.ASM
* The following program is a slightly modified version
* From Marvin Green's BotBoard Manual V1.03
* Written for the Motorola 68HC11E2 Microcontroller
*============= EQUATES ===============================
COPRST  EQU $103A  ; Arm/Reset COP timer circuitry
TOC2    EQU $1018  ; Timer Output Compare register 2
TOC3    EQU $101A  ; Timer Output Compare register 3
OC1M    EQU $100C  ; Output Compare 1 Mask register
OC1D    EQU $100D  ; Output Compare 1 Data register
TCTL1   EQU $1020  ; Timer Control register 1
TOC1    EQU $1016  ; Timer Output Compare register 1
CW      EQU $F800  ; Clock wise rotation value
CCW     EQU $F200  ; Counter clock wise rotation value

*============== Program ==============================
$ORG    $F800        ; Start assembling code at the
                     ; top of EEPROM
        LDAA  #$00
        STAA  COPRST
        LDD   #$0000 ; Stop the servos by clearing
        STD   TOC2   ; TOC2 and TOC3
        STD   TOC3
        LDAA  #$60   ; Setting up bits for TOC2 & TOC3
        STAA  OC1M   ; OC1M controls TOC2 and TOC3
        LDAA  #$00   ; When OC1M times out, it will clear
                     ; TOC2 and TOC3, i.e., low cycle
        STAA  OC1D
        LDAA  #$F0   ; When TOC2 & TOC3 time out, both
                     ; will output high, i.e., high cycle
        STAA  TCTL1
        LDD   #$0000 ; Get timeout value for OC1M and
        STD   TOC1   ; save in timer register

        LDD   #CW    ; Turn servo #1 CW by setting
        STD   TOC2   ; TOC2 (PA6-J2)
        STD   TOC3   ; Turn servo #2 CW by setting
                     ; TOC3 (PA5-J3)
*============= END OF PROGRAM ========================

6  Motorola HC12 Microcontroller

The Motorola HC12 microcontroller is the bigger brother of the HC11.  With an increased instruction set, more I/O pins, 16-bit data bus, and faster operating clock frequency (16Mhz compared to 8Mhz), the HC12 is a nice controller for robotics and applications alike.  The HC12 is available in two models, the A4 and B32 version.  The A4 contains 4KB of EEPROM, while the B32 version has 32KB of FLASH EEPROM in addition to some standard EEPROM.  One disadvantage about the HC12 is it's only available in a surface mount package.  Finding a good PCB to house the chip can be hard, but Kevin Ross of the Seattle Robotics Society has a very neat circuit board designed for both versions.  Soldering the chip to Kevin's boards is a snap!

The following program was written to test two servomotors connected to PORTT0 and PORTT1.  The program was written for the HC12B32 version and can easily be modified to run on the A4 version (refer to the Motorola HC12A4 User's Manual).  One thing to note.  The program is assembled in the FLASH EEPROM of the B32 model, but can rewritten to be assembled in the EEPROM instead.  Refer to the HC12B32 User's Manual.  To download the assembly code, click here.  You wil have to assemble it yourself.

* SERVO_O2.ASM
* Kam Leang 1999
* Designed for:  Motorola 68HC12B32
*----------------------------------------------------------------------------
* Test servo motors connected to PORTT[0:1]
*----------------------------------------------------------------------------

*============================================================================
#include "hc12b32.h"            ; Include header file containing all standard
                                ; registers equated to names.
MAINORG    EQU    $8000         ; Start of Flash eeprom

*===========================PROGRAM==========================================
$ORG    MAINORG                 ; Start the program assembly at the
                                ; top of the flash EEPROM.
MAIN    EQU     *               ; Equate start of MAIN program to
                                ; this location.
        CLR     COPCTL          ; Clear the watch dog timer.  This
                                ; will allow interupts.
        LDS     #$0BFF          ; Start the stack at top of RAM.
        CLI                     ; Clear interupt mask in CCR
        LDAA    #%10000111      ; Setup channels 0, 1, 2, and 7
                                ; for TOC
        ORAA    TIOS            ; %10000111 in TIOS
        STAA    TIOS
        LDAA    #%10000111
        STAA    CFORC
        LDAA    #%11000000
        ORAA    TCTL1
        STAA    TCTL1
        LDAA    #%11101010      ; Set channels 0, 1, and 2 to
                                ; clear their output pins when
                                ; the compare happens
        ANDA    TCTL2           ; %XX101010 in TCTL2
        ORAA    #%00101010
        STAA    TCTL2
        LDAA    #%11111010      ; Set %XX11X010 on TMSK2
                                ; Prescaler of 32ms periods.
        ANDA    TMSK2
        ORAA    #%00110010
        STAA    TMSK2
        LDAA    #%00000011      ; Setup TOC7 to handle the start
                                ; of the pulses by setting the
                                ; value to 1 when TCNT is zero
        ORAA    OC7M
        STAA    OC7M
        LDAA    #%00000011
        ORAA    OC7D
        STAA    OC7D
        LDX     #$0000
        STX     TC7H            ; Configure TC7 to latch on
                                ; free running
                                ; clock at value #$0000
        LDAA    #%10000000      ; Turn on Timer and disable it
                                ; during background debug mode
        STAA    TSCR

        LDX     #$8000
        STX     TC0H
        LDX     #$6000
        STX     TC1H
        LDX     #$2000
        STX     TC2H
*============================================================================
        ORG     RSTVEC          ;Reset interupt set-up.  Code
                                ; execution begins at the top of
                                ; Flash eeprom when
                                ;reset button is pressed.
        FDB     MAIN

7 MIT Handy Board Microcontroller

I don't have this or the bottom sections completed, so bare with me.  If you really want to know how to interface a servo motor to the MIT Handy Board microcontroller, then check out its website!

7.1  With the Expansion Board

Same as above.

8  Conclusion

None yet. I'll finish this article later.

9  References

Coming soon.  Just you wait!

10  Contributed

If you know how to control a servomotor using a microcontroller NOT mentioned in this article and would like to share the information, just let me know and I'll include it in this section with appropriate references of course.