Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using CodeWarrior to program a HCS12 Microcontroller A. Write a program to do th

ID: 3683785 • Letter: U

Question

Using CodeWarrior to program a HCS12 Microcontroller

A. Write a program to do the following, in both ASM and C. Do not use MINA, MINM, MAXA, and MAXM instructions.

1. Load an array of eight 8-bit unsigned numbers into RAM.

2. Find the largest of these numbers and store it.

3. Find the smallest of these numbers and store it.

4. Wait for the pushbuttons (S1 or S2) to be pressed.

5. The first time S1 is pressed, display the high nibble of the largest number; the second time S1 is pressed, display the low nibble of the largest number. For example, if the largest number is 0x3F, the 7-segment display should show “3” the first time S1 is pressed and “F” the second time it is pressed.

6. Do the same for S2, except use the smallest number.

B. Explain why debouncing is used with switches. Optional: Implement software debouncing for switch used above.

Explanation / Answer

C:

#include <hidef.h> /* common defines and macros */
#include <mc9s12dp256.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"

/* This is used in the 7-seg decoder function */
unsigned char segs[16];
unsigned char num[8];
unsigned char max_value;
unsigned char min_value;

void seg7_out(short int);

void main(void) {
EnableInterrupts;
DDRM = 0x00; /* Buttons on */
DDRH = 0xFF; /* Display on */

/* These are required to set up the 7-seg display function (Common Cathode) */
/* CodeWarrior does not like declaring the following globally */
segs[0] = 0x3f;
segs[1] = 0x06;
segs[2] = 0x5b;
segs[3] = 0x4f;
segs[4] = 0x66;
segs[5] = 0x6d;
segs[6] = 0x7d;
segs[7] = 0x07;
segs[8] = 0x7f;
segs[9] = 0x6f;
segs[10] = 0x77;
segs[11] = 0x7c;
segs[12] = 0x39;
segs[13] = 0x5e;
segs[14] = 0x79;
segs[15] = 0x71;

/* Find the max and min values */


/* Now loop for the buttons */

}

/*
This function will take in an unsigned char (you can think it as
an 8-bit integer value) and display it on the 7-segment display.
This depends on the segs[] array being defined first.
*/
void seg7_out(unsigned char value) {
/* Code goes here. Please see ASM version of the routine for hints */
};

ASM:

;===================Start of CodeWarrior Setup========================
; *** Normal setup routines ***
; *** All of this is standard stuff that CodeWarrior makes ***

; export symbols
XDEF Entry ; export 'Entry' symbol
ABSENTRY Entry ; for absolute assembly: mark this as application entry point

; include derivative specific macros
INCLUDE 'mc9s12dp256.inc'

ROMStart EQU $4000 ; absolute address to place my code/constant data

; variable/data section

ifdef _HCS12_SERIALMON
ORG $3FFF - (RAMEnd - RAMStart)
else
ORG RAMStart
endif
;===================End of CodeWarrior Setup==========================


; *** Now, put your declarations here ***
; This is the 7-seg decoding array for a common cathod display (The display on the Dragon12-JR is common Anode)
segmap dc.b $3f,$06,$5b,$4f,$66,$6d,$7d,$07,$7f,$6f,$77,$7c,$39,$5e,$79,$71


;================Again Start of CodeWarrior Setup===================
; *** Executing code starts here ***
; *** But there's still some setup ***
ORG ROMStart
Entry:
; remap the RAM &amp; EEPROM here. See EB386.pdf
ifdef _HCS12_SERIALMON
; set registers at $0000
CLR $11 ; INITRG= $0
; set ram to end at $3FFF
LDAB #$39
STAB $10 ; INITRM= $39

; set eeprom to end at $0FFF
LDAA #$9
STAA $12 ; INITEE= $9


LDS #$3FFF+1 ; See EB386.pdf, initialize the stack pointer
else
LDS #RAMEnd+1 ; initialize the stack pointer
endif
CLI ; enable interrupts
;================Again End of CodeWarrior Setup=====================


; *** ***
; *** YOUR CODE STARTS HERE ***
; *** ***
Start:
movb #$00,DDRM ; Set the Pushbutton pins to input
movb #$FF,DDRH ; Set the 7-seg to output

; Part one: Find the max value
; Check each value, record the highest
; Loop until you're out of the array

; Part two: Find the min value
; Same idea as before


; Next, the display routine.
; You want to loop, looking for input.
; First, clear the display (common Anode).
dloop: movb #$FF,PTH
; Now, check for button 1 (max nybble)

; And now, check for button 2 (min nybble)

; Loop back around when done
bra dloop


; This subrountine displays the lower nybble
; of the A register on the 7-segment display.
; To use it, place a number in A and then use
; 'jsr seg7_out' (no quotes).
seg7_out:
; Push the registers into the stack for later
psha
pshb
pshx
anda #$0F ; Knock out the high nybble
tfr A,B ; Copy A into B
ldx #segmap ; Load the address of the segmap array into X
; Add B and X, store in X
; That is, X will now be incremented by the number you want to
; display.
   abx
; Move what is pointed to by X into PTH (the 7-seg display)
; This will be the part of the array with the corresponding
; code for the 7-segment display.
movb X,PTH
   ; Negate the displayed bits, because the seven segment map above is for common cathode displays.  
; Restore all the registers from the stack
pulx
pulb
pula
; Return
rts

;================Again Start of CodeWarrior Setup===================
;**************************************************************
;* Interrupt Vectors *
;**************************************************************
ORG $FFFE
DC.W Entry ; Reset Vector
;================Again End of CodeWarrior Setup=====================

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote