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

HCS12 Microcontroller: MC9S12DG256B Please use CodeWarrior to write an assembly

ID: 2249264 • Letter: H

Question

HCS12 Microcontroller: MC9S12DG256B
Please use CodeWarrior to write an assembly program to do the following:

At the very beginning of the main.asm file, put your name as a comment. The starting address of your main program is at $4000. Your program must declare two variables called MyReading and MyNumber. Reserve one byte for MyReading without an initial value, and initialize MyNumber with the initial value of $25. Your program must read from DIP switches and put the reading in MyReading, add the value from MyNumber to this reading (not hard-coded $25, in other words, your program should still work correctly if you change the initial value of MyNumber to a different number, for example, $12), and displays the sum using the LEDs. Your program must do this continuously forever.

Explanation / Answer

;**************************************************************
;* LED Test Program *
;* Written by Sparky 31/03/14 *
;* Designed to toggle the LEDs of PORTA *
;**************************************************************

; Include derivative-specific definitions
INCLUDE 'mc9s12dg128.inc' ;uC on board

;------------------
;Memory locations

R1 EQU $1000
R2 EQU $1001
R3 EQU $1002

;------------------
;ORG directive

ORG $4000

Entry:
LDS #$4000 ;Stack
LDAA #$FF
STAA DDRA ;Set PORTA as output

BACK LDAA #$55 ;Load 0x55
STAA PORTA ;Display on PORTA LEDs
JSR DELAY ;Delay
LDAA #$AA ;Repeat above for 0xAA
STAA PORTA
JSR DELAY

BRA BACK

;------------------
;DELAY subroutine

DELAY

PSHB ;Save Reg A on Stack
LDAB #10 ;Change this value to see  
STAB R3 ;how fast LEDs Toggle
;--10 msec delay.
;Freq. for Instruction Clock Cycle is 24MHz (1/2 of 48Mhz).
;(1/24MHz) x 10 Clk x240x100=10 msec. Overheads are excluded in this calculation.   
L3 LDAB #100
STAB R2
L2 LDAB #240
STAB R1
L1 NOP ;1 Intruction Clk Cycle
NOP ;1
NOP ;1
DEC R1 ;4
BNE L1 ;3
DEC R2 ;Total Instr.Clk=10
BNE L2
DEC R3
BNE L3
;--------------   
PULA ;Restore Reg A
RTS
;-------------------
;**************************************************************
;* Interrupt Vectors *
;**************************************************************
ORG $FFFE
DC.W Entry ;Reset Vector. CPU wakes here and it is sent to start of the code at $4000