DOC PREVIEW
NMT EE 308 - Lab 3 – Part 1

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

EE 308 New Mexico Tech Spring 2011 Lab 3 – Part 1 C Language Programming, Interrupts and Timer Hardware In this sequence of three labs, you will learn how to write simple C language programs for the MC9S12 microcontroller, and how to use interrupts and timers. Introduction and Objectives The C programming language is used extensively in programming microprocessors. In this lab you will write some simple C programs which do the things you did in assembly language in Lab2. For example, Program 1 displays a counting pattern on the LEDs connected to Port B 1. Prelab For the prelab write the program for Part 4 of this lab. 2. The Lab 1. Start a new project. (a) This time, in Project Parameters, select C. (b) On the C/C++ Options menu, select ANSI startup code, Small memory model and None for the floating point format. (c) Select Edit - Standard Settings, Select Target - Compiler for HC12, then click on Options. Click on the Output tab, and select the Generate Listing File. The listing file will be called main.lst in the bin subdirectory of the CodeWarrior project. The listing file includes the C statements as well as the assembly language which was generated. CodeWarrior uses a linker file called Project.prm that tells the compiler where to put the program and data. In the window which lists the project files, select Project Settings - Linker Files - Project.prm Find the following lines RAM = READWRITE 0x1000 TO 0x3FFF ; and change it toEE 308 New Mexico Tech Spring 2011 RAM = READWRITE 0x1000 TO 0x1FFF ; PROG = READ ONLY 0x2000 TO 0x3FFF ; Next, find the line INTO ROM C000 /* , ROM 4000 */ ; and change it to INTO PROG /* , ROM 4000 */ ; Save and close Project.prm 2. Type in Program 1 and click Project - Make. 3. Look at the file main.lst and try to understand what it does. Note that there may be some things which do not make sense to you. At the very least, find the assembly language code which increments Port B. CodeWarrior generates a file Project.map in the bin subdirectory. The file Project.map shows the addresses of the C functions and of any global variables. The Project.map file also shows entry point to the program and the sizes of the functions (in both hex and decimal) in the OBJECT-ALLOCATION SECTION. Load the file Project.abs.s19 into your MC9S12 and run it. Verify that the LEDs increments. Where is the entry point to the program in memory. 4. Using Program 1 as a model, write a C program to implement the program from Lab 2-Part 3. Compile and run your program. Have an instructor verify that it works. 5. Look at the Project.map and determine how many bytes the program takes (the length of the .text segment). Compare this to the length of last weeks program written in assembly. 6. Put your program in the EEPROM at address 0x0400. Remember, when you put code into EEPROM you need to do some setup which DBug12 normally does for you. You need to convert the assemblylanguage code (which multiplies the clock by 6) from Lab2 into C code, and add it as the first lines of you program. There is no C statement to implement the assembly-language instruction sei. You can use the asm function to insert this (or any other assembly language instruction) into you program: asm( sei ) ;EE 308 New Mexico Tech Spring 2011 You will want the array which stores the turn signal patterns into the EEPROM (so the array will not disappear when you turn off power). You will want variables which will change as the program is executed to be placed in RAM. You can tell the compiler to put constant data (such as an array of patterns to be display on LEDs) immediately following the code (so the data will be loaded into EEPROM) by defining the data as type const. An example of setting up an array of type const is: cons t char table [ ] = {0xaa , 0 xbb , 0 xcc } ; Now you need to tell the compiler to put the program into EEPROM. You can do that by using the Project.prm file as follows: Find the following lines INTO PROG /* , ROM 4000 */ ; and change it to INTO EEPROM; Finally, change the address pointed to by EEPROM to 0x410. Save and close Project.prm. When you upload the program and try to start it from EEPROM, it will try to start from address 0x0400. To fix that add a BRA 0x0439. This is the address that your program starts at.EE 308 New Mexico Tech Spring 2011 Program 1 A C program to increment LEDs connected to Port B. #include <hidef.h> /* common defines and macros */ #include ”derivative.h” /* derivative−specific definitions */ #define D_1MS (24000/12) // Inner loop take s 12 c y c l e s // Need 24 ,000 cycles for 1 ms void de lay ( uns igned sho r t num) ; main ( ) { DDRB = 0 x f f; /* Make PORTB output */ PORTB = 0; /* Start with all off */ whi l e ( 1 ) { PORTB = PORTB + 1; de lay (50) ; } } void delay ( unsigned short num) { volatile unsigned short i ; /* volatile so compiler does not optimize */ whi l e (num > 0) { i = D_1MS; whi l e ( i > 0) { i = i − 1; } num = num − 1; }


View Full Document

NMT EE 308 - Lab 3 – Part 1

Documents in this Course
Load more
Download Lab 3 – Part 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lab 3 – Part 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lab 3 – Part 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?