Name(s): your name here team or Group #(if applicable): Professor’s name

Download 30.87 Kb.
Size30.87 Kb.
DeVry University –– ECET 340

Team or Group #(if applicable):


Experiment or Assignment #: 7

Lab Meeting Day & Time:

Title of Report:

Electromechanical Peripheral Interfacing

Summary (two sentences):

We learned how to program the HCS12 timer to generate a user-selected sequence of audio waveforms each with a specific frequency and time duration. We now understand how microcontrollers interact with electromechanical devices.

Checklist (items included):

1. Introduction & Background


4. Results: Data, Tables, & Diagrams


2. Procedure


5. Analysis of Results


3. Troubleshooting & Testing


6. Conclusions


Key Results:

In this lab, we took a look at the internal speaker on the Dragon Board. We changed the output frequencies to make it play a ‘song’.

Key Conclusions (technical):

The Programmer is able to program specific frequency values that are based off of the internal clock of the microcontroller and in tern make notes. If these frequency notes are paired with a delay cycle, and played in a specific order for the correct length of time, it will sound like a song is being played.

Key Conclusions (critical thinking):

There will be labs and future problems where the programmer needs to change between different preset values. In this lab it was sound frequencies, but it could possibly be LED brightness, radio signal or a number of other things.

Date Due: DATE DUE

Date Submitted: DATE SUBMITTED

Student(s) Signature: YOUR NAME

Professor’s Signature:


  1. To learn how to program the HCS12 timer to generate a user-selected sequence of audio waveforms each with a specific frequency and time duration

  2. To understand how microcontrollers interact with electromechanical devices



IBM PC – Pentium or compatible

Codewarrior v. 5.9.0


1- Wytec EVBPlus Microcontroller Demonstration Board Kit, including:

MC9S12DG256 demonstration board
USB cable and adapter
Universal power supply and cable
MC9S12DG256 Development Boards CD


Electromechanical devices transform electrical inputs into some type of mechanical action. These devices include motors, speakers, relays, and the piezoelectric crystals found in ultrasonic transducers. The slow response makes program timing important.

Speakers use ceramic resonators or electromagnets to drive an acoustic diaphragm. The diaphragm oscillates to produce sound waves. The speaker on your demo board is connected to Port P, pin 5, so we use the channel 5 output from the PWM unit to generate audible sounds (see Figure 1). The pitch of each sound is to closely match that of a note on the 7-tone musical scale (see Table 1). By having the program periodically adjust the value in the PWMPER register, a complete melody can be played.

group 1838

Figure 1 - Speaker Interface to HCS12

The small frequency separation between adjacent notes requires a more precise resolution for the programmed waveform period than is possible with an 8-bit PWMPER register. The remedy is to divide a relatively high clock frequency of 12 MHz (after pre-scale) by a 16-bit period value to get 28 times better frequency resolution. This is done in the HCS12 by concatenating two neighboring channels of the PWM in the PWMCTL register, in this case channels 4 and 5.

The higher-indexed channel (5’s) pin is used to output the waveform while the lower-indexed channel (4’s) enable and polarity registers are used to control the functions. The 16-bits for both period and duty cycle are loaded into two consecutive PWMPER and PWMDTY register addresses, starting with the address belonging to the lower-indexed channel (PWMPER4 and PWMDTY4).

group 1792

Table 1 - Standard Musical Note Frequencies



  1. Examine the program below; it outputs a sequence of waveforms, each with its own frequency and duration, to produce ascending-descending musical scales in an acoustical resonator. The program accesses defined waveform information in a two-dimensional array through a data structure for clarity and efficiency.

  2. Enter the program below into a new Codewarrior project, 340_lab7_1 to test your speaker interface. Use F7 – F5 – F5 to run this code at the 24MHz clock speed.

// 340_lab7_1 - Part C - Codewarrior v. 5.9.0 - Tested 6-15-2009kj

// Wytec HC12 Dev Board (24MHz eclk)

// PWM generates musical waveform for speaker sound on Port P pin 5

// Ascending/descending musical (octave) scale, once every 4 sec.

/* Jumper J26 to connect speaker to pin PP5 */

#include /* common defines and macros */

#include /* derivative information */

#pragma LINK_INFO DERIVATIVE "mc9s12dg256b"
//symbols – [pitch = 12MHz/(audio frequency in Hz)]

#define C4 22934

#define D4 20431

#define E4 18202

#define F4 17181

#define G4 15306

#define A4 13636

#define B4 12149

#define C5 11467

#define Q 250

#define H 500
//structure definition, type definition

struct note{ // contains pitch and note length

unsigned short pitch; // name of note (to be converted into frequency)

unsigned int duration; // H = half note, Q = quarter note, E = eighth note


typedef struct note note_type;

//global - creates a table called song in memory

note_type song[14] = {{C4,H},{D4,Q},{E4,Q},{F4,Q},{G4,Q},{A4,Q},{B4,Q},

{C5,H},{B4,Q},{A4,Q},{G4,Q},{F4,Q},{E4,Q},{D4,Q}}; {B4,Q},{A4,Q},{G4,Q},{F4,Q},{E4,Q},{D4,Q}}; {B4,Q},{A4,Q},{G4,Q},{F4,Q},{E4,Q},{D4,Q}};
//function prototypes

void init_PWM(void);

void play(unsigned short);

void delay(int);


void main(){

int i;

init_PWM( );

while(1){ // loop to repeat the tune



delay(song[i].duration); //...keep sounding for full length of note




void init_PWM( ){

PWMCLK = 0x00; // Ch. 4 - Ch. 5 source is clock A

PWMPOL = 0x20; // initial HIGH output on ch. 5

PWMPRCLK = 0x01; // Clk A pre-scale = 2 (PWM clock = 24MHz/2 = 12.0 MHz)

PWMCTL = 0x40; // CON45 = '1': 16-bit PWM counter, period and duty regs.

PWME |= 0x20; // turn-on PWM ch. 5

void play(unsigned short notepitch){

PWMPER45 = notepitch; // 12 MHz/note = audio frequency (in Hz)

PWMDTY45 = (notepitch/2); // (squarewave -- 0.50 x notepitch)

void delay(int noteduration){ // delay for 'noteduration' msecs

int i,j;

for(i=0; i

for(j=0; j<4000; j++);

/********** end of file *******/

Design Project (18 points)

Demonstrate to your instructor (onsite students) your speaker playing a short, recognizable tune that you write (25 notes or so, such as “Mary had a Little Lamb,” “Happy Birthday,” etc.).

The tune should repeat in a loop indefinitely. Comment key lines of code in your program (.c file) and answer the question below. Online students should submit a copy of your code with your lab report.

Lab 7 – Instructor Sign-off______________________


Describe any problems encountered and how those problems were solved

The hardest part of this lab is making sure that you place the right notes in the right octave for the correct duration of time. It is not too difficult; it is just something the programmer has to be wary of when writing the code.


  1. Given C7 = 8372.0 Hz what value would C7 need to be associated with (in #define C7)? Assume all other timer values are the same as in Fig. 4 and show your work.

Using the equation from above for Symbols,

[pitch = 12MHz/(audio frequency in Hz)

//(12M/8372.0)2 = 3866.69
#define C7 3867

Mary Had a Little Lamb code

//jumper J26 to connect speaker to pin PP5
#include /* common defines and macros */

#include /* derivative-specific definitions */

#pragma LINK_INFO DERIVATIVE "mc9s12dg256b"

#define C3 45867

#define D3 40863

#define E3 36404

#define F3 34361

#define G3 30613

#define A3 27273

#define B3 24297

#define C4 22934

#define D4 20431

#define E4 18202

#define F4 17181

#define G4 15306

#define A4 13636

#define B4 12149

#define C5 11467

#define Qu 60000

#define Re 20

#define Q 250

#define H 500

#define W 1000
//structure definition

struct note{

unsigned short pitch;

unsigned int duration;

typedef struct note note_type;

note_type song[52] = {{B3,Q},{Qu,Re},{A3,Q},{Qu,Re},{G3,Q},{Qu,Re},









//funtion prototypes

void init_PWM(void);

void play(unsigned short);

void delay(int);


void main(){

int i;



for (;;){









void init_PWM(){

PWMCLK = 0x00;

PWMPOL = 0x20;

PWMPRCLK = 0x01;

PWMCTL = 0x40;

PWME |= 0x20;

void play(unsigned short notepitch){

PWMPER45 = notepitch;

PWMDTY45 = (notepitch/2);

void delay(int noteduration){

int i,j;

for(i=0; ifor(j=0; j<4000; j++);


ECET-340 DeVry University 7-17-2009 kj

Download 30.87 Kb.

Share with your friends:

The database is protected by copyright © 2024
send message

    Main page