Você está na página 1de 10

EENG3288 - Homework Set #6 Solution

Exercise 1 (a): Suppose you want to generate a 24-MHz E-clock. Propose a set of values for the
SYNR and REFDV registers to achieve this goal using a 4-MHz crystal oscillator generating
OSCCLK and the on-chip PLL circuit.
[5 points]
HINT: Use Equation (6.1) from Unit 7.
SOLUTION:
The 24-MHz E clock is derived from a 48-MHz PLLCLK.
48 x 106 = 2 x 4 x 106 x (SYNR + 1) / (REFDV + 1)
6 x REFDV + 6 = SYNR + 1
SYNR = 6 x REFDV + 5
Set REFDV to 1, then SYNR is 11.
Exercise 1 (b): Using the same 4-MHz crystal oscillator as in part (a), set clock reset generator
(CRG) real-time interrupt (RTI) control register (RTICTL) properly to generate an interrupt
approximately every 1 ms.
[5 points]
HINT: Use Table 6.4.
SOLUTION: Since the period of 4 MHz OSCCLK is 1/4000 ms, we need 4000 cycles of OSCCLK to
generate RTI every 1 ms.
4000 cycles of OSCCLK is approximately equal to 4 x 210. This leads to setting (using Table 6.4)
RTICTL = 0001 0011 = 0x13.
Another likely answer will be corresponding to 212 OSCCLK cycles leading to RTICTL = 00110000
= 0x30
Exercise 1 (c): Assume that the same 24-MHz E clock prescaled by 8 is being used as input to
the timer-counter TCNT for measuring the period of an input signal via input-capture. What is
the maximum period of the input signal that can be measured without overflow. [5 points]
SOLUTION: The timer-counter clock period is 8/24000 = 1/3000 ms. Since TCNT is 16-bits long,
it can count a maximum of 65535 cycles without overflow.
That means the longest input signal period that can be measured using this setup will be
65535/3000 = 21.845 ms.

3/25/2016 - GHSCSE - FDU

Page 1

EENG3288 - Homework Set #6 Solution

Lab Exercise 1: Develop a real-time interrupt (RTI) driven change of LED light pattern display on
the Dragon 12-Plus demo board. You need to write a C program to light up one LED at a time
starting from the one driven by PORT B pin 7 till the one driven by PORT B pin 0 and then
reverse the order of LED lighting. Repeat this operation forever. Each LED should be lighted for
about 500 ms assuming that the HCS12 uses an 8-MHz crystal oscillator to generate a system
clock.

Make sure you show your results to your Teaching Assistant and have him initial a screen dump
of your code and that of your debug window. The signed screen dumps must be attached to
your work. You must document your code fully, explain variable settings, and write down your
observations.
[20+5+5 points]
HINT: See Example 6.5 from your textbook and use information from Tables 6.1 and 6.4 of Unit
7.
SOLUTION: If we load the value of 0x7F into the RTICTL register, the time out period of RTI is
about 16 x 216/(8 x 106) = 1048576/8000000 ~ 1/8 s. Therefore we need to allow the RTI to
interrupt four times (which gives 4*(1/8 s) = 500 ms ON period) before we change the LED
pattern. The variable int cnt counts the number of interrupts.
Note that ledPat[16] array includes 8 LED lighting sequence (from left to right) and another 8
LED lighting sequence (from right to left). For example, setting PTB = ledPat[0] = 0x80 will turn
ON the leftmost LED. Similarly PTB = ledPat[8] = 0x01 will turn ON the rightmost LED to start the
right to left LED lighting sequence. The variable int ix is used to index the ledPat[] array.

3/25/2016 - GHSCSE - FDU

Page 2

EENG3288 - Homework Set #6 Solution

In the main() routine, apart from enabling the LEDs, the RTI is enabled with proper interval
and the variables ix and cnt are both initialized to 0.
The interrupt service routine rtiISR increments the cnt and checks for its overflow. When
cnt equals 4, cnt gets reset to 0, ix is incremented and proper LED gets lit. The variable
ix is also checked for overflow and after reaching a value of 16 gets reset to 0.
The following program implements the specified LED pattern shifting.
The vectors.c files with the following content must be included in the Sources folder of the
project:
extern void near rtiISR(void);
#pragma CODE_SEG __NEAR_SEG NON_BANKED
#pragma CODE_SEG DEFAULT
// Change code
// section to DEFAULT.
typedef void (*near tIsrFunc)(void);
const tIsrFunc _vect[] @0xFFF0 = { // Real Time Interrupt vector table address
rtiISR
};

3/25/2016 - GHSCSE - FDU

Page 3

EENG3288 - Homework Set #6 Solution

3/25/2016 - GHSCSE - FDU

Page 4

EENG3288 - Homework Set #6 Solution

Lab Exercise 2: Develop a program on Dragon 12-Plus demo board to blink the Morse code SOS
on the rightmost 7-segment display. The program should first blink the letter S for three short
blinks (dot-dot-dot), then blink the letter O for three long blinks (dash-dash-dash), and then
blink the S again for three short blinks (dot-dot-dot). Repeat this sequence endlessly after 0.5
sec pause between two consecutive SOS sequences. You may allow on and off delay time for
the dot be 154 ms and the on and off delay time for the dash to be 256 ms.

Make sure you show your results to your Teaching Assistant and have him initial a screen dump
of your code and that of your debug window. The signed screen dumps must be attached to
your work. You must document your code fully, explain variable settings, and write down your
observations.
[20+5+5 points]
HINT: Modify LBE Example 14 code in your class notes to develop the program. Modify
half_sec_delay() function from your class notes to develop proper delays.
SOLUTION:

3/25/2016 - GHSCSE - FDU

Page 5

EENG3288 - Homework Set #6 Solution

In the code above, the function frac_sec_delay(int cycles) offers a delay given by cycles x
10.24 ms by using RTI. When cycles = 49, the function provides a delay of 49 x 10.24 ~ 500 ms.
For a delay of 154 ms for the dot, we need to set cycles = 154/10.24 = 15. Similarly the dash
delay of 256 ms is obtained by setting cycles = 256/10.24 = 25.
The dot-dot-dot blinking of S is coded by turning the 7 seg digit #3 ON and leaving it ON for
154 ms followed by digit #3 being OFF for 154 ms, and repeating this sequence three times
using the for (ix = 0; ix < 3; ix++) { loop.
The dash-dash-dash blinking of O is coded by turning the 7 seg digit #3 ON and leaving it ON
for 256 ms followed by digit #3 being OFF for 256 ms, and repeating this sequence three times
using the for (ix = 0; ix < 3; ix++) { loop.
Rest of the code is similar to LBE Example 14.

3/25/2016 - GHSCSE - FDU

Page 6

EENG3288 - Homework Set #6 Solution

LAB EXERCISE 3: Design a Keypad data input, addition, and LCD display of user I/O on your
Dragon 12-Plus demo board by entering CodeWarrior via Load Example Project and using LBE
functions. The procedure is as follows:
STEP 1: Initialize the keypad. Keypad interface is shown below.
STEP 2: Initialize the LCD properly. LCD interface is shown below.
STEP 3: Output the message Enter an integer: on the first row of the LCD. After seeing this
message, the user enters a number (xxxx) on the keypad. Use the * or E key to terminate the
number. Your program reads in the number and saves it in a buffer.
STEP 4: Output the message Enter another integer: on the first row of the LCD. After seeing
this message, the user enters a number (yyyy) on the keypad. Use the * or E key to terminate
the number. Your program reads in the number and saves it in a buffer.
STEP 5: Compute the sum of these two numbers and display it on the LCD screen as follows:
The sum is
zzzz.
where zzzz = xxxx + yyyy.
STEP 6: Restart the addition process from STEP 3 above when # or F key gets pressed.

3/25/2016 - GHSCSE - FDU

Page 7

EENG3288 - Homework Set #6 Solution

Make sure you show your results to your Teaching Assistant and have him initial a screen dump
of your code and that of your debug window. The signed screen dumps must be attached to
your work. You must document your code fully, explain variable settings, and write down your
observations.
[15+5+5 points]
HINT: In addition to keypad and LCD initialization functions, you need to use the functions:
set_lcd_addr(), getkey(), hex2asc(), data8(), wait_keyup(), number(), and type_lcd().
Here is a fragment of the code that you need to complete and execute.
#include <hidef.h>
/* common defines and macros */
#include <mc9s12dg256.h>
/* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg256b"
#include "main_asm.h"
/* interface to the assembly module */
void main(void) {
long
op[2];
char *ptr;
char kbuf[12];
char c, a;
int
i;
3/25/2016 - GHSCSE - FDU

// 32-bit operands
// pointer to keypad buffer
// keypad buffer
// get_key data
// keypad buffer index
Page 8

EENG3288 - Homework Set #6 Solution

int

j;

// operand count

char
char
char

*msg1 = Enter an integer:;


*msg2 = Enter another integer:;
*msg3 = The sum is

ptr = kbuf;
// initialize key buffer
PLL_init();
// set system clock frequency to 24 MHz
keypad_enable():
// enable keypad
lcd_init();
// enable LCD
set_lcd_addr(0x00);
// display on 1st line
type_lcd(msg1);
// display Enter an integer:
set_lcd_addr(0x40);
// go to second line of LCD display
i = 0;
// set kbuf index to 0
j = 0;
// set operand count to 0
while(1) {
c = getkey();
// read keypad
a = hex2asc(c);
// convert to ASCII for storing
kbuf[i] = a;
// store entered key value in ASCII in kbuf
if (c < 10) {
// if pressed key is between 0 9
data8(a);
// display data on LCD second line
wait_keyup();
// wait to release key
i++;
// increment kbuf index
} else {
if((c == 0xE) && (j == 0)) {
// if * or E key is entered - end of first data entry
op[j++] = number(ptr); // convert to binary
set_lcd_addr(0x00);
// clear first line
type_lcd(msg2);
// display Enter another integer:
wait_keyup();
// wait to release key
i = 0;
// reset kbuf index for the second number
set_lcd_addr(0x40);
// display on 2nd line
} else if((c == 0xE) && (j == 1)) {
// both operands are in
op[j] = number(ptr);
// convert to binary
op[1] += op[0];
// compute sum
// add your code for results display & proper reset for starting another sum
}
}
}
}
SOLUTION:
3/25/2016 - GHSCSE - FDU

Page 9

EENG3288 - Homework Set #6 Solution

3/25/2016 - GHSCSE - FDU

Page 10

Você também pode gostar