CENG 412 Lab 9 Embedded Systems Precision Square Wave Generation using Output Compare Name __________________________ Part A Signature ___________ Part B Signature ___________ Introduction In this lab you will use the Output Compare timer function of the HCS12 to generate square waves at various frequencies and duty cycles in Assembler and C. Procedure A - Assembler Code Implementation 1. Create an Absolute Assembly project in CodeWarrior. Calculate values for the hi_time and lo_time parameters to give a square wave at an exact frequency of 2.5 kHz and with a 60 % duty cycle. Use a prescale factor of 8. Period 2.5 kHz sq wave ____________ THIGH value ____________ TLOW value ____________ Pre-Scaled clock value _____________ hi_time parameter ____________ lo_time parameter ______________ 2. Make the project and download the .s19 file in AsmIDE. Execute the program. The square wave should be present on PT7/IOC7 channel 7. Verify the pin number needed for PT7/IOC7 and Switch 1 as before. You will need to be sure that S1 is set to the high logic level setting. You can do this with a scope or DVM. PT7/IOC7 channel 7 pin number ____________ Is there a Logic High level at Switch 1? _____________ 3. Verify using the scope that you have the correct waveform. Draw a sketch of the waveform showing voltage levels and time intervals. How accurate is the frequency of the square wave to the desired target using this method? 4. Calculate the values needed to produce a 10 kHz square wave with a 75% duty cycle. Period 10 kHz sq wave _____________ THIGH value ___________ TLOW value ____________ hi_time parameter ____________ lo_time parameter ______________ Procedure B - C Code Implementation 5. Create a C project in CodeWarrior. Calculate new values of the lo_time and hi_time parameters to give a square wave at an exact frequency of 5.0 kHz and with a 25 % duty cycle using a prescale factor of 16. Period 5.0 kHz sq wave ____________ THIGH value ___________ TLOW value ____________ Pre-Scaled clock value _____________ hi_time parameter ______________ lo_time parameter ________________ 6. Make the project and download the .s19 file to AsmIDE. Execute the program. The square wave should be present on channel 7, PT7 of PORT T. Verify using the scope that you have the correct waveform. Draw a sketch of the waveform showing voltage levels and time intervals. How accurate is the frequency of the square wave to the desired target using this method? 7. Demonstrate your programs and have your lab sheet signed. ;***************************************** ;* ;* Lab 9 - This lab generates a 2.5 kHz 60% duty cycle ;* TTL level waveform using the Output Compare ;* features of the HCS12 at channel 7, PT7 of PORT T ;* ;******************************************* ; variable/data section TCNT: TIOS: TSCR1: TSCR2: TCTL1: TFLG1: TC7: OC7: C7F: hi_time: lo_time: equ equ equ equ equ equ equ equ equ equ equ $44 $40 $46 $4D $48 $4E $5E $80 $80 XX YY ORG $2000 movb #$90,TSCR1 movb #$03,TSCR2 bset TIOS,OC7 movb #$C0,TCTL1 ldd TCNT repeat: addd #lo_time std TC7 brclr TFLG1,C7F,* movb #$80,TCTL1 ldd TC7 addd #hi_time std TC7 brclr TFLG1,C7F,* movb #$C0,TCTL1 ldd TC7 bra repeat swi end ;counter address ;Input/output compare select address ;TSCR1 address ;TSCR2 address ;Timer control register 1 address ;Timer interrupt flag register address ;address of TC7 ;enable OC7 mask ;clear C7F flag mask ;hi_time value ;lo_time value ;enable TCNT with fast timer flag clear ;disable TCNT interrupt, set prescaler to 8 ;enable output compare at OC7 ;select pull high as pin action ;start an OC0 operation with 480 us as delay ; " ; " ;wait until OC7 pin goes high ;select pull low as pin action ;start an OC operation with 720 us as delay ; " ; " ;wait until OC7 pin goes low ;select pull high as pin action C Code Implementation //****************************************** //* //* D. Lloyd Lab 9 April 2011 //* //* This lab generates a Square Wave at exactly 5.0 kHz with a //* 25 % duty cycle using the Output Compare features of the HCS12. //* //* Look at Output Capture Channel 7 PT7 of PORT T //* //************************************************ #include <hidef.h> #include "derivative.h" /* common defines and macros */ /* derivative-specific definitions */ #define #define #define #define /* enable channel 7 as output Compare */ /* mask to clear C7F bit */ hi_time XX lo_time YY OC7 0x80 C7F 0x80 void main (void) { TSCR1 = 0x90; /* enable TCNT and fast timer flag clear */ TIOS |= OC7; /* enable OC7 function */ TSCR2 = 0x04; /* disable TCNT interrupt, set prescaler to 16 */ TCTL1 = 0xC0; /* set OC7 action to be pull high */ TC7 = TCNT + lo_time; /* start an OC7 operation */ while(1) { while(!(TFLG1 & C7F)); /* wait for PT7 to go high */ TCTL1 = 0x80; /* set OC7 pin action to pull low */ TC7 += hi_time; /* start a new OC7 operation */ while(!(TFLG1 & C7F)); /* wait for PT7 pin to go low */ TCTL1 = 0xC0; /* set OC7 pin action to pull high */ TC7 += lo_time; /* start a new OC7 operation */ } asm("swi"); /* code never reaches here */ } Lab Exercise Written by David Lloyd Computer Engineering Program Humber College