Download 27.02 Kb.
Size27.02 Kb.

Tufts University EE14 Lab

EECS Department Fall 2002

Super Mario Bros on the Motorola 68HC12


The practical objective of this lab is to further advance your knowledge of the 68HC12 microprocessor by first gaining familiarity with the timing registers, and second applying that knowledge to write an assembly program that will play a song through a little PC speaker attached to the 68HC12 evaluation board. This will be accomplished by completing a number of Pre-Laboratory exercises described in Part 2, below. The Motorola CPU12 Reference Manual (located at each lab station or online1) is an excellent source of information on the internal registers of the 68HC12. It is necessary to read at least Section 12: Standard Timer Module.

The code for the Super Mario Bros theme song is attached to this exercise. Either use this song in your program or create your own song based on the notes and lengths used in the Super Mario Bros code.

This laboratory exercise assumes prior knowledge of basic programming, but the use of subroutines and loops will be introduced. This exercise can also be done with interrupts, but it is not necessary. The following is the code for interrupt vector definitions stored in RAM on the 68HC12 evaluation board:

BDLC equ $0B10

ATD equ $0B12

SCI equ $0B16

SPI equ $0B18

Pulse_Edge equ $0B1A

Pulse_Overflow equ $0B1C

Timer_Overflow equ $0B1E

Timer_Ch7 equ $0B20

Timer_Ch6 equ $0B22

Timer_Ch5 equ $0B24

Timer_Ch4 equ $0B26

Timer_Ch3 equ $0B28

Timer_Ch2 equ $0B2A

Timer_Ch1 equ $0B2C

Timer_Ch0 equ $0B2E

Real_Time equ $0B30

IRQ equ $0B32

XIRQ equ $0B3F

intSWI equ $0B36

COP_fail equ $0B3A

COP_clk_fail equ $0B3C

Reset equ $0B3E

The interrupt vectors that could be used to complete this lab are Timer Overflow, Timer Channel 0-7 and the Real time interrupt.

is the online manual for the board.

2Pre-Laboratory Exercises

Read Section 12: ‘Standard Timer Module’ from the 68HC12 manual. Answer the following questions to hand in with your laboratory report.

  1. Briefly define the function of the following registers: TMSK1, TMSK2, TCTL2, TIOS, Timer Input/Output Compare registers, TSCR, TCNT, and TFLG1.

  2. How many bytes of memory does the Super Mario Bros theme song use? What is the maximum number of bytes that your song can use?

  3. How is the frequency of a specific note translated into assembly language for the Motorola 68HC12?

3Lab Exercise

There are a few subroutines that your program could have:

Initialize: Initialize the counters, set the prescale factor, and clear all flags.
GetNote: Get the notes one by one and increment the song array.
PlayNote: This subroutine should play each note, monitor the length of the note, and call the subroutine that will get the next note. It also needs to control the beat of the song. Each note (including rest notes) needs to be followed by silence of a reasonable length chosen by the programmer. This length will have an affect on the tempo of the song. A couple suggestions for a reasonable length of the “beat” are 1/32nd or 1/64th of a second. These lengths are based on the whole note being equivalent to one second.
In the loops that play and test the note it is important to optimize your code for efficiency. Having optimized code does not necessarily mean fewer lines of code, but rather less cycles to complete a task. This is why it is important to know the number of cycles for each instruction.
In the laboratory, you will be given a PC speaker to test if your song plays. One of the two wires of the PC speaker should be connected to Vcc pin-out and the other can be connected to a port of your choice, such as the output compare 2 port which corresponds to bit 2 of port B. When you successfully run your program, play your song for the TA.

If you choose to use the Super Mario Bros theme in place of your own song, it must be loaded into the EEPROM. The start of the user code/data section of the EEPROM is at $0D00. If you have trouble loading the data into the EEPROM you can load half of the song into the RAM ($0800-$0900) and then use the “move” command in the terminal screen, such as move 0800 09ff 0d00. If you composed your own song and it is short enough you can just load it into RAM. However if both your code and the song are more than 512 bytes then you will have to store the song in the EEPROM.


  1. Why is it important to optimize your code for efficiency? Explain.

  2. What flags in what registers did you set and clear in your program? Why?

  3. Why is the prescale factor especially important for this program? How does it affect the sound?

  4. How did you make sure that the program ended when the song was over?

5What to Hand In

The student is required to hand in a formal, typed write-up for this lab. The report should include a cover page, a typed version of the Pre-Laboratory exercises in Part 2 as well as a printout of the .lst and .asm files from Part 3 with a TA’s signature, problems you encountered, and answers to questions from Part 4 of the lab. Your reports will be graded based on content, organization, neatness, and tardiness.



*| |*

*| |*

*| SUPER MARIOS BROS: Playing a song on 68HC12 |*

*| |*

*| |*



* *

* For long songs, load the data into the EEPROM *

* by storing it in a separate file, data.asm, org'ed *

* at $0800, then in HyperTerminal, type "move 0800 0XXX 0d00" *

* where 0XXX is the last address of your data. *

* *



* Port Definitions *


TIOS equ $80 ; Timer Input Capture/Output Compare Select

TCNT equ $84 ; Timer Counter

TSCR equ $86 ; Timer System Control

TCTL2 equ $89 ; Timer Control 2

TMSK1 equ $8C ; Timer Interrupt Mask 1

TMSK2 equ $8D ; Timer Interrupt Mask 2

TFLG1 equ $8E ; Timer Interrupt Flag 1

TC0 equ $90 ; TIC/TOC 0

TC2 equ $94 ; TIC/TOC 2

* Notes Lengths *


nWHOLE equ 128 ;whole note is 1 second

nHALF equ 64

n4TH equ 32

n8TH equ 16

n16TH equ 8

n32ND equ 4

n64TH equ 2

n128TH equ 1

nSTEP equ n64TH


* Notes Frequencies *


REST equ 0 ; No sound

C0 equ 31312 ; Freq is in Hz 65.40639133

Db0 equ 29555 ; Freq is in Hz 69.29565774

D0 equ 27896 ; Freq is in Hz 73.41619198

Eb0 equ 26330 ; Freq is in Hz 77.78174593

E0 equ 24852 ; Freq is in Hz 82.40688923

F0 equ 23457 ; Freq is in Hz 87.30705786

Gb0 equ 22141 ; Freq is in Hz 92.49860568

G0 equ 20898 ; Freq is in Hz 97.998859

Ab0 equ 19725 ; Freq is in Hz 103.8261744

A0 equ 18618 ; Freq is in Hz 110

Bb0 equ 17573 ; Freq is in Hz 116.5409404

B0 equ 16587 ; Freq is in Hz 123.4708253

C equ 15656 ; Freq is in Hz 130.8127827

Db equ 14777 ; Freq is in Hz 138.5913155

D equ 13948 ; Freq is in Hz 146.832384

Eb equ 13165 ; Freq is in Hz 155.5634919

E equ 12426 ; Freq is in Hz 164.8137785

F equ 11729 ; Freq is in Hz 174.6141157

Gb equ 11070 ; Freq is in Hz 184.9972114

G equ 10449 ; Freq is in Hz 195.997718

Ab equ 9863 ; Freq is in Hz 207.6523488

A equ 9309 ; Freq is in Hz 220

Bb equ 8787 ; Freq is in Hz 233.0818808

B equ 8293 ; Freq is in Hz 246.9416506

C1 equ 7828 ; Freq is in Hz 261.6255653 !!!!MIDDLE C!!!!

Db1 equ 7389 ; Freq is in Hz 277.182631

D1 equ 6974 ; Freq is in Hz 293.6647679

Eb1 equ 6583 ; Freq is in Hz 311.1269837

E1 equ 6213 ; Freq is in Hz 329.6275569

F1 equ 5864 ; Freq is in Hz 349.2282314

Gb1 equ 5535 ; Freq is in Hz 369.9944227

G1 equ 5225 ; Freq is in Hz 391.995436

Ab1 equ 4931 ; Freq is in Hz 415.3046976

A1 equ 4655 ; Freq is in Hz 440

Bb1 equ 4393 ; Freq is in Hz 466.1637615

B1 equ 4147 ; Freq is in Hz 493.8833013

C2 equ 3914 ; Freq is in Hz 523.2511306

Db2 equ 3694 ; Freq is in Hz 554.365262

D2 equ 3487 ; Freq is in Hz 587.3295358

Eb2 equ 3291 ; Freq is in Hz 622.2539674

E2 equ 3107 ; Freq is in Hz 659.2551138

F2 equ 2932 ; Freq is in Hz 698.4564629

Gb2 equ 2768 ; Freq is in Hz 739.9888454

G2 equ 2612 ; Freq is in Hz 783.990872

Ab2 equ 2466 ; Freq is in Hz 830.6093952

A2 equ 2327 ; Freq is in Hz 880

Bb2 equ 2197 ; Freq is in Hz 932.327523

B2 equ 2073 ; Freq is in Hz 987.7666025

C3 equ 1957 ; Freq is in Hz 1046.502261

Db3 equ 1847 ; Freq is in Hz 1108.730524

D3 equ 1743 ; Freq is in Hz 1174.659072

Eb3 equ 1646 ; Freq is in Hz 1244.507935

E3 equ 1553 ; Freq is in Hz 1318.510228

F3 equ 1466 ; Freq is in Hz 1396.912926

Gb3 equ 1384 ; Freq is in Hz 1479.977691

G3 equ 1306 ; Freq is in Hz 1567.981744

Ab3 equ 1233 ; Freq is in Hz 1661.21879

A3 equ 1164 ; Freq is in Hz 1760

Bb3 equ 1098 ; Freq is in Hz 1864.655046

B3 equ 1037 ; Freq is in Hz 1975.533205

C4 equ 978 ; Freq is in Hz 2093.004522

Db4 equ 924 ; Freq is in Hz 2217.461048

D4 equ 872 ; Freq is in Hz 2349.318143

Eb4 equ 823 ; Freq is in Hz 2489.01587

E4 equ 777 ; Freq is in Hz 2637.020455

F4 equ 733 ; Freq is in Hz 2793.825851

Gb4 equ 692 ; Freq is in Hz 2959.955382

G4 equ 653 ; Freq is in Hz 3135.963488

Ab4 equ 616 ; Freq is in Hz 3322.437581

A4 equ 582 ; Freq is in Hz 3520

Bb4 equ 549 ; Freq is in Hz 3729.310092

B4 equ 518 ; Freq is in Hz 3951.06641

C5 equ 489 ; Freq is in Hz 4186.009045

Db5 equ 462 ; Freq is in Hz 4434.922096

D5 equ 436 ; Freq is in Hz 4698.636287

Eb5 equ 411 ; Freq is in Hz 4978.03174

E5 equ 388 ; Freq is in Hz 5274.040911

F5 equ 367 ; Freq is in Hz 5587.651703

Gb5 equ 346 ; Freq is in Hz 5919.910763

G5 equ 327 ; Freq is in Hz 6271.926976

Ab5 equ 308 ; Freq is in Hz 6644.875161

A5 equ 291 ; Freq is in Hz 7040

Bb5 equ 275 ; Freq is in Hz 7458.620184

B5 equ 259 ; Freq is in Hz 7902.13282

;!!!!!!!!!!! to find note freq its 2048000/freq = number of counts


;1. initialize Timer Registers

;2. main program should play a note, then get the next note,

; and repeat

;3. to play a note:

; -enable the timer

; -set the prescalar (if you need to)

; -tell the HC12 you want to use Bit x of PORTT for output compare

; -tell the HC12 what you want to do when the compare is true (hint: Table 12-1)

; -tell the HC12 what time you want the event to occur

* Data Storage - Note and Length for Super Mario Bros Tune *


org $0800 ; Place Data for Song in EEPROM


fdb E2,n8TH

fdb REST,n128TH

fdb E2,n8TH

fdb REST,n8TH

fdb E2,n8TH

fdb REST,n8TH

fdb C2,n8TH

fdb E2,n4TH
fdb G2,n4TH

fdb REST,n4TH

fdb G,n4TH


EE14 Microprocessor Architecture and Applications Prof. Karen Panetta

Download 27.02 Kb.

Share with your friends:

The database is protected by copyright ©ininet.org 2023
send message

    Main page