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.
Read Section 12: ‘Standard Timer Module’ from the 68HC12 manual. Answer the following questions to hand in with your laboratory report.
Briefly define the function of the following registers: TMSK1, TMSK2, TCTL2, TIOS, Timer Input/Output Compare registers, TSCR, TCNT, and TFLG1.
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?
How is the frequency of a specific note translated into assembly language for the Motorola 68HC12?
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.
Why is it important to optimize your code for efficiency? Explain.
What flags in what registers did you set and clear in your program? Why?
Why is the prescale factor especially important for this program? How does it affect the sound?
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.