CENG 412 Embedded Systems Lab 11 Servo Motor Angular Position Control Name Signed Introduction In this lab you will control the angular position of a Servo motor using the PWM module with the HCS12 board. Equipment HCS12 workstation, standard bench equipment, Servo Motor Procedure Part A On the course web site locate and read the Servo Motor Control handout and answer the following questions. Use the Lab 11 Work Sheet to configure the program code. 1. What is the period and frequency of the pulse train needed to be sent to the Servo motor to control its position? Period Frequency 2. What are the pulse width (time high) values needed to position the motor at the 0 degree position 90 (or neutral) position 180 position 3. What are the pulse train duty cycle values needed to position the motor at the 0 degree position 90 (or neutral) position 180 position 4. What is the pin number for PWM channel 0 on the Demo Board header connector? Pin number Procedure Part B Create a CodeWarrior C and Relocatable Assembler project. 1
Assembler Code For the Assembly code write a program that outputs a square wave at the frequency determined above on PWM channel 0 on the Demo Board at the pin determined above. The C code will pass a parameter to the Assembler code to set the Duty Cycle of the square wave. In the Assembler code save this value in memory. Later in the code you will need to load this value into the PWMDTY0 register. Because of the low frequency requirements for the square wave, the Assembler code will need to use a pre-scale value for both ClockA (divide by 16) and ClockSA (divide by 150). C Code For the Assembly code write a program that reads the values from the Pushbutton switches on the Demo Board, processes this data and does the following: When SW2 is pressed the motor will move to the 0 degree position When SW3 is pressed the motor will move to the 90 (or neutral) position When SW4 is pressed the motor will move to the 180 position Processing the Data When writing the C code to process your data you will need to take into account the following information: The Pushbuttons are connected to PORTAD0. Information on how the switches are connected is shown below. The switches are active low. The logic level of any of the switches goes from a High to a Low when the switch is pressed. Switches SW2, SW3 and SW4 are connected to bits 4, 5, and 6 of PORTADO. These will need to be shifted to the lower 3 bits for the masking operation. Use a Switch/Case statement in the C code framework and call a userprogrammed function called SetPosition that passes an integer value that represents Duty Cycle to the Assembler code to control the motor position. PORTAD0 The upper 4 bits of PORTAD0 are connected to the push button switches on the HC9S12 Demo Board. You must configure the bits in PORTAD0 by writing 1 s to the appropriate bits. 2
PORTAD0: equ $8f ;port AD0 data register (input only) ATD0DIEN: equ $8d ;port AD0 Digital Input Enable register ;(0-disable digital input, 1-enable digital input) Figure 1: Basic Port AD0 Registers CPU Board Demo Board H1 Pin No. PAD7 26 SW5 SW2 SW3 SW4 SW5 PAD6 27 SW4 PAD5 28 SW3 PAD4 29 SW2 PAD3 PAD2 PAD1 25 24 23 LIGHT POT TEMP PAD0 22 n/c Figure 2: Pushbutton switches on HCS12 Workstation Circuit Set-Up The Servo motor has 3 wires, Red, Black and White. Connect the Red wire to + 5V on a DC power supply, the Back wire to ground of the power supply and the White wire to required pin on the Demo board Header. Be sure that the ground of the power supply is connected to the ground of the Servo motor and the ground of the HC9S12 board. Measurements Connect a scope to the Demo Board pin to view the PWM waveform created. Measure the Period T of the PWM waveform 3
PWM waveform Period Press SW2. Measure the Time High of the PWM waveform Press SW3. Measure the Time High of the PWM waveform Press SW4. Measure the Time High of the PWM waveform Do these measured values agree with those in Part A step 2? C Code ************************************* Servo Motor Position Control Lab1 - CENG 412 DGL March 26/12 This programs controls the angular position of a Servo motor. Input signal from Push Button switches. Program passes parameter for duty cycle value to the Assembler program that uses PWM function Be sure that the ground of the power supply is connected to the ground of the Servo motor and the ground of the HC9S12 board. ************************************** #include "derivative.h" #include <hidef.h> #include <stdio.h> void SetPosition(int); derivative-specific definitions common defines and macros function prototype void main(void) { ATD0DIEN = 0x70; enable PortAD0 for digital input while(1) { switch ((~PORTAD0>>4)&0x07) process bits 0-2 of modified PortAD0 contents { case 1: SetPosition(x); break; 0 deg position case 2: SetPosition(y); break; 90 deg position case 4: SetPosition(z); 180 deg position asm("swi"); 4
Assembly Code ;***************************************** ;* ;* Assembler function using PWM to control ;* Servo motor position. Position ;* control value is passed as a parameter. ;* Servo requires a 20 msec pulse train ;* Duty cycle parameter is passed from C code ;* Uses PWM channel 0 ;* ;***************************************** XDEF SetPosition include mc9s12dp256.inc ;register addresses defined in the file mc9s12dp256.inc PWME0: equ $?? ;mask to enable channel 0 dutyc: ds.b 01 ;reserve 1 byte in memory for duty cycle value SetPosition: stab dutyc movb #01, PWMCLK ;select ClockSA movb #$??, PWMPRCLK ;select Clock A prescale to 16 movb #$??, PWMSCLA ;select ClockSA prescale to 150 movb #1, PWMPOL ;channel 0 O/P high at start movb #0, PWMCAE ;select left aligned mode movb #$0C, PWMCTL ;8 bit mode, stop PWM in wait and freeze mode movb #???, PWMPER0 ;set PWM period as 20 msec movb dutyc,pwmdty0 ;load duty cycle value from memory movb #0, PWMCNT0 ;reset PWM0 counter movb #PWME0,PWME ;enable PWM channel 0 rts Lab Exercise Written by David Lloyd Computer Engineering Program Humber College 5