Marc L

Having problem using PWM to control a futaba servo motor

Discussion created by Marc L on Feb 19, 2007
Latest reply on Mar 2, 2007 by bigmac
I am using a MC9S12DP256C with an E-clock of 8MHz, the servo motor requires a
period of 20ms(50Hz) with a minimum pulse of 0.5ms and a max pulse of
2.5ms. The problem is that when I hook it up to the motor it just kinda
squirms rapidly, I ckecked it out with a scope and found that the period
coming out is something like 2.5Hz. I can't seem to see where my code or
calculations went wrong.

I divided the 8Mhz clock by 64 to get 125kHz, then divided that by 50Hz
to get that I need a 2500 period. That can fit into the period register
if I concactinate channel 0 and 1. However it doesnt seem to be working
out for some reason.

Any help is much appreciated.

Here is my code:

//start of code


; Test program for servos

PWEN: equ $00A0 ; PWM enable register
PWPER0: equ $00B4 ; PWM period channel for channel 0
PWPER1: equ $00B5
PWPER2: equ $00B6
PWCLK: equ $00A2 ; PWM clocks and concatenate (used to
concat ch0 and ch1)
PWDTY0: equ $00BC ; PWM duty cycle for channel 1
PWDTY1: equ $00BD
PWDTY2: equ $00BE
PWPOL: equ $00A1 ; PWM clock select and polarity
PWSCAL0:equ $00A8 ; PWM Scale register 0
PWPRCLK:equ $00A3
PWCAE: equ $00A4
PWCTL: equ $00A5 ; PWM control register

org $08FA

MOVB #%00010000,PWCTL ; concate 0 and 1
MOVB #$00,PWCLK ; clock select
MOVB #$02,PWPOL ; Polarity
MOVB #%00000110,PWPRCLK; Set up an initial frequency
MOVB #$00,PWCAE ; Left-Aligned
MOVB #$00,PWSCAL0 ; Do not further scale clock
MOVB #$02,PWEN ; Enable PWM
MOVB #$09,PWPER0 ; Set channel 0 period

middle: MOVB #$49,PWDTY0 ; Set channel 0 duty cycle -(straight

ldaa #$FF
JSR delay ;32ms delay

MOVB #$00,PWEN ; Disable PWM

right: MOVB #$2B,PWDTY0 ; Set channel 0 duty cycle -(right

MOVB #$02,PWEN ; Enable PWM

ldaa #$FF
JSR delay ;32ms delay

MOVB #$00,PWEN ; Disable PWM

BRA middle

delay: LDX #$F0FF
delay1: DEX
BNE delay1
DBNE A,delay

//end of code