Qorivva: Turning A Servo

Document created by GUO XIAOLI Employee on Jul 12, 2012Last modified by johnmc on Sep 18, 2012
Version 9Show Document
  • View in full screen mode



In this exercise students will utilize the sample project to turn a servo on the Qorivva TRK-MPC5604B board.



1.  Hardware Setup


  The first step of this tutorial is to gain background information on background information on servo's, timer modules, PWM signals and counters. To learn more about these topics read the MCU 101: Pulse Width Modulation for Servos article.

You will need:

2.  Import the Servo Project


The next step is to import an existing project into your Workspace. in this case, the TRK-MPC5604B Servo project. Follow the instructions on the codewarrior project import page if you can't remember how to to import the project into CodeWarrior.

Sample Project Source Code




3. Build the Code


If you have more than one project in your project view, make sure the proper project is the focus. The most reliable way to do this is to right click the project and choose Build Project as shown below. You can also go to the Project menu and choose the same command.

If you encounter errors, look in the Problems view and resolve them. You can ignore any warnings.

4. Download/Debug/Run

5. Learning Step: Servo Code Description


What will happen:

This demo will sweep your servo from max to minimum angular position, and then back again continuously

HardwareChip Port/PinComment
Servo wire connects to J6Red Pin towards center, white on outsideMotor Drive board A
Battery Wire connects to J3Red lead towards rear, black lead towards frontMotor Drive Board A
Battery Wire connects to J3Red lead towards rear, black lead towards frontMotor Drive Board A



This file sets up the Pulse Width Modulation Timer Module for use by a servo. It is set to utilize Edge-Aligned PWM, and this file properly configures the period, and pulse width for use by the other modules

Several important functions are contained in this file:
1. SERVO () - calls the sweep function
2. initEMIOS_0ch4(void) (double duty_Cycle) - enter the desired duty cycle setting for the servo
3. Delaywait() - a short delay, 500 cycles
4. initEMIOS_0 - sets the EMIOS_0 module clock to 1 Mhz


void SERVO(void)
    TransmitData("****Steering Servo Test****\n\r");
    EMIOS_0.CH[4].CBDR.R = 1500;          /* 1500 Middle */
    EMIOS_0.CH[4].CBDR.R = 1750;        /* 1750 Right Max,*/
    EMIOS_0.CH[4].CBDR.R = 1250;        /* 1250 Left Max*/
    EMIOS_0.CH[4].CBDR.R = 1500;          /* 1500 Middle */



void initEMIOS_0ch4(void) { /* EMIOS 0 CH 4: Output Pulse Width Modulation*/
  EMIOS_0.CH[4].CADR.R = 0; /* Leading edge when channel counter bus=0*/
  EMIOS_0.CH[4].CBDR.R = 1500; /* Trailing edge when channel counter bus=1400 Middle, 1650 Right Max, 1150 Left Max*/
  EMIOS_0.CH[4].CCR.B.BSL = 0x01; /* Use counter bus B */
  EMIOS_0.CH[4].CCR.B.EDPOL = 1; /* Polarity-leading edge sets output */
  EMIOS_0.CH[4].CCR.B.MODE = 0x60; /* Mode is OPWM Buffered */
  SIU.PCR[28].R = 0x0600; /* MPC56xxS: Assign EMIOS_0 ch 6 to pad */


initEMIOS_0 ()


void initEMIOS_0(void) {  
  EMIOS_0.MCR.B.GPRE= 63; /* Divide 64 MHz sysclk by 63+1 = 64 for 1MHz eMIOS clk*/
  EMIOS_0.MCR.B.GPREN = 1; /* Enable eMIOS clock */
  EMIOS_0.MCR.B.GTBE = 1; /* Enable global time base */
  EMIOS_0.MCR.B.FRZ = 1; /* Enable stopping channels when in debug mode */


Alternative Examples:

Application Note 4251 and Software.

Other Qorivva Tutorials:

1. Qorivva: Blink LED

2. Qorivva: Drive DC Motor Tutorial

4. Qorivva: Line Scan Camera Tutorial

TRK-MPC5064B Freescale Webpage
TRK-MPC5064B Freescale Reference Manual
TRK-MPC5064B Freescale Schematic