




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





In this sequence of three labs, you will learn how to write simple assembly language programs for the MC9S12 microcontroller, and how to use general purpose I/O (input/output) ports. Week 1 Introduction and Objectives This laboratory will give you more experience with the tools we will use this semester ― the MiniDRAGON+ evaluation board (EVB), the DBug12 monitor, and the as12 assembler. Be sure to read through the entire lab and do the prelab for each section before coming to lab
instruction.)
b) Trace through the program. Verify that the Z, N, V and C bits are what you expect after each instruction. c) Look at the contents of address 0x2000. Does the value agree with your answer from the prelab? d) You could change this program to add rather than subtract by changing the sba instruction to an aba instruction. Modify the program, assemble it and load the new program into the MC9S12. a. Find the address for the sba instruction. b. In the MC9S12 Core Users Guide , find the op code for the aba instruction. c. Go to the address of the sba instruction, and change the op code to that of the aba instruction. d. Run the program again, and verify that the program now adds rather than subtracts. Consider the program in Figure 2, which is a program to divide a table of ten values by 2. Figure 2. Demo program for part 2 of lab 2 a)Use the text editor to enter this program, assemble the program into an s19 file. b)How may cycles will it take to execute the program take on the MC9S12?
Introduction and Objectives The purpose of this laboratory is to write a few assembly language programs and test them on your MC9S12. PreLab Make sure you have the programs written and clearly thought out before you come to the lab. You should put all your code starting at memory location 0x2000. You are encouraged to bring the programs in on a disk. The Lab As in last week’s lab you will write some programs in assembly language and run the programs on the MC9S12. Write the program in Figure 1 and add necessary instructions to make it run. The MniDRAGON+ has a sevensegment LED display connected to the MC9S12 Port H. A sevensegment display looks like this: On the MiniDRAGON+, the a LED is connected to bit 0 of Port H, the b LED is connected to bit 1 of Port H, etc. (Bit 7 of Port H is connected to an infrared detector; it is not used to manipulate the seven segment LED display.) To display the number 3 on the LEDs, you need to turn on segments a , b , c , d and g. To do this, you need to write a 01001111 2 , or 0x4f, to the Port H data register. (A 0xCF will also display a 3 on the LEDs, because the most significant bit can be either 0 or 1.) The following program will toggle the a LED of the seven segment display:
Figure 1. Demo program for part 2 of lab 2. b) Test your program on the MC9S12. Trace through the loop to see what is happening. Note: The program should cause one of the 7segment display to toggle. c) Modify the code to make all seven segments toggle in an alphabetical order. d) Write a program to swap the last element of an array with the first element, the next-to-last element with the second element, etc. The array should have 0x20 eight-bit numbers and should start at 0x2000. Check that your program works on the MC9S12. Use the following data for your test: e) Write a program which puts the exclusive OR of the eight-bit numbers form memory locations 0x through 0x8FFF into memory location 0x2003. (This operation is often used to generate a check sum to verify data transmission. The sending computer generates and transmits the check sum along with the data. The receiving computer calculates the check sum for the received data and compares it with the check sum sent by the sending computer. If the two values do not match, then there was an error in the transmission (The last byte in a line of an S19 file is a check sum used for this purpose.)
c) A flasher: Repeat the above sequence d) Generate a message scrolling banner. Repeat the above message Figure 1. Samples of the functions to be performed using Port H as an output. Figure 2. Port B inputs to control the Port A functions. The Lab Run your program on the MC9S12. If you have difficulty getting your program to work, start by trying to implement one function only – say, the binary counter. Once this works, start working on your next functions. Set a break point at the first line of your delay subroutine. When the breakpoint is reached, check the value of the stack pointer, and the data on the stack. Make sure you understand what these mean. When you get your program to work, have your lab instructor or TA verify the program operation. The MC9S12 has EEPROM (Electrically Erasable Programmable Read Only Memory) functionality. If you put your program into EEPROM the program will remain there when you turn off power.
The EEPROM is located at address 0x400. You can just change the origin statement of your assembly language program, then reassemble, and reload your program. If you type “ G 400 ”, you will run your program out of EEPROM, and it should work the same as it did when you ran it out of RAM. (Try it.) You can power cycle your board, and then type “ G 400 ”, and again your program will run correctly. (Try it.) For some applications it would be nice if you could run your program without having to type “ G 400 ” – if your board is controlling a robot, and no computer is connected to it, it would be impossible to start the program by typing “ G 400 ”. DBug12 has a special mode to allow you to run a program out of EEPROM without having to type “ G 400 ”. If you move jumper J9 to the left , and power cycle the board, the program will run immediately out of EEPROM. (Try it.) You will notice that the program runs much slower – actually, three times slower than it did when you ran it by typing “ G 400 ”. This is because DBug12 does some system initialization which is bypassed when you move jumper J7 and run your program directly from EEPROM. In particular, the miniDRAGON+ board has a 16 MHz clock, and the MC9S12 runs at half the clock frequency, or 8 MHz. The MC9S12 has a built in phases lock loop (PLL) which allows the chip to generate a faster clock internally, and run with a 24 MHz frequency. In order to get the chip to run at the higher frequency, you must do the initialization which enables the PLL. Here is some code which will do that initialization: org $ Add the above code to your program, right after the “org $400” line and before the first line of your program. Load this new code into EEPROM. (Be sure to move jumper J7 to the right in order to get back to the DBug12 monitor so you can load new code into memory.) Now move jumper J9 to the left , power cycle your board, and your program should run at the same speed it did when starting by “ G 400 ”. Note: The document “ readme_EEPROM.pdf ” which came on the miniDRAGON+ CD says that you need to convert your S1 code (in the S19 file) to S2 code to successfully load a program into EEPROM. This is because the MC9S12 EEPROM must be programmed with an even number of bytes, and must be programmed starting at an even address. However, I have had no problem loading a program which starts on an odd address or has an odd number of bytes. I think that, when DBug12 sees that a user wants to load a program which starts on an odd address or contains an odd number of bytes, it automatically adds the bytes needed to make the programming start on an even address or to contain an even number of bytes. If you have trouble getting an EEPROM program to load correctly, you should try converting your S1 code to S2 code as discussed in the “ readme_EEPROM.pdf ” document.