//Controlling Servo Motor with PWM (pulse width modulation) on Dragon12 Plus Trainer Board //On Dragon12 board we have connections for four servo motors. They are located at the top middle of the board //They are designated as PP4, PP5, PP6, and PP7. Next to them is the blue terminal block (T7) for external power connection for servo motors //Next to the blue terminal block (T7) is the jumper to select power source for servo motor (SERVO_PWR J35) //We can use on-board power or bring in an external power source for the servo motor. //Depending on the size of the servo motor you might have to connect the blue T7 terminal block to external power (5-6V) . //and set the SERVO_PWR jumper J35 to right (EXT). If you use on-board power set the jumper J35 to left. //The servo motros have 3 leads. They are Black (ground), Red(power) and White for PWM control pulse. //See http://www.parallax.com/Store/Robots/AllRobots/tabid/755/CategoryID/2/List/0/SortField/0/Level/a/ProductID/488/Default.aspx //On Dragon12 board the PP4 has 3 pins. The left one is for PWM motor control, the middle one is the power, and the right one is for ground. //The same is true for the other three servo motro connections of PP5,PP6 and PP7 //So when you connect a servo motor to PP4 (or PP5, or PP6,..) make sure the white lead of the servo motor is on the left connection. //This program uses the PWM of chan 4 (PP4) to control servo motor. //Modified from Example 17-16 Mazidi & Causey HCS12 book. See Chapter 17 for PWM discussion //Written and tested by M. Mazidi #include /* common defines and macros */ #include "derivative.h" /* derivative-specific definitions */ void main(void) { /* put your own code here */ PWMPRCLK=0x04; //ClockA=Fbus/2**4=24MHz/16=1.5MHz PWMSCLA=125; //ClockSA=1.5MHz/2x125=6000 Hz PWMCLK=0b00010000; //ClockSA for chan 4 PWMPOL=0x10; //high then low for polarity PWMCAE=0x0; //left aligned PWMCTL=0x0; //8-bit chan, PWM during freeze and wait PWMPER4=100; //PWM_Freq=ClockSA/100=6000Hz/100=60Hz. CHANGE THIS PWMDTY4=50; //50% duty cycle AND THIS TO SEE THE EFFECT ON MOTOR. TRY LESS THAN 10% PWMCNT4=10; //clear initial counter. This is optional PWME=0x10; //Enable chan 4 PWM while(1); //stay here forever }