You are on page 1of 19

Programming with CodeWarrior IDE

29/10/2007

Tutorial 1
Programming with CodeWarrior IDE
This document gives you an introduction to the programming environment you will be
using in the course for the development of embedded software systems for the MCF5213
microcontroller. You will also be introduced to the main I/O devices that are available.
Step-by-step instructions will be provided to guide you through the development of
simple applications. It is important that you understand the motives and the activities of
each of these steps. In future laboratory exercises, it will be assumed that you are capable
of reproducing these steps, as well as modify them to suite your needs. For this reason,
you should take the opportunity to enquire about any uncertainties you meet. Try to
answer the questions posed as they will give you more insight into the environment.
The tutorial is divided into 3 sections.
• Part A introduces the laboratory hardware.
• Part B introduces the CodeWarrior programming environment, , and lets you
experiment with some C programming
• Part C introduces the basic I/O modules built-in the MCF5213. This will give
you extra opportunities to practice your programming skills.

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 1 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007

1 Part A – Hardware Setup


In this course, you will be working using the following hardware setup:

MCF5213EVB
Interface Card

Motor Card

Power
UART0
Microcontroller

Sw1 & Sw2


BDM Interface
LEDs

LCD
Switches
Microcontroller

Potentiometer
Keyboard
LEDs

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 2 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007

2 Part B – CodeWarrior IDE


First, it is important to get familiar with certain terms and concepts that will be used
through this tutorial:
• Integrated Development Environment (IDE) (also known as Integrated Design
Environment and Integrated Debugging Environment) – An IDE is a type of
computer software that assists computer programmers in developing software. IDEs
normally consist of a source code editor, a compiler and/or interpreter, build-
automation tools, and (usually) a debugger. Sometimes a version control system and
various tools to simplify the construction of a GUI are integrated as well.1 Example
IDEs that you might have already encountered are Microsoft Visual Studio .NET,
Tasking and Eclipse. In this course, you will be developing software using the
Freescale CodeWarrior IDE.
• Project – A large software application will normally consist of a collection of source
files, together with libraries and object codes that are collected from a variety of
locations and developed by various developers. The application may also be
configured to run on different targets, requiring different compilations. To support the
development of such large applications, the concept of Project is introduced. A
project forms the collection of files (of various types) that constitute an application,
together with configuration information on how the files are to be compiled and
linked to form the final product.
In this section, you will get familiar with the tools necessary to develop C applications for
the MCF5213 microcontroller. You will create a simple project that will only interact
with the user via standard I/O. (the PC screen and keyboard).
1. Run the Freescale CodeWarrior IDE (Integrated Development Environment)
program
a. Select startÆprogramsÆFreescale CodeWarriorÆCodeWarrior for
ColdFire V6.3Æ Freescale CodeWarrior IDE
b. For convenience, you might want to create a shortcut of the IDE onto your
desktop.
2. Once running, the IDE loads any previous projects that were left open when the
IDE was last closed. It is perfectly safe to load and work on more than one project
at one time. However, in order to eliminate confusion in this introductory tutorial,
we will close all currently open projects and start with an empty environment.
a. Close all open windows, including the so-called Project window. The IDE
will in the end look as follows:

1
Reference: Wikipedia, http://en.wikipedia.org/wiki/Integrated_development_environment
Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 3 of 19
Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007

3. We will now use the project wizard to help us create a C program, specific for the
M5213EVB board which contains a MCF5213 microcontroller.
a. Select FileÆNew from the top menu. You can also use the shortcut
Cltr+Shift+N.
b. A dialog box appears. From the Project frame, select ColdFire
Stationery.
c. Define the Project name and its Location.
1. Project name: sample
2. Location: Choose your own location! Note that the wizard
automatically names the final folder destination to match that of
the project name. This is for your convenience only, and you can
choose any folder name desired.
d. Select OK
e. You are now prompted to select a project Stationery
1. You will be developing code for the 5213 microcontroller. Click
on the + sign to the left of CF_M5213EVB to further expand its
submenu.
2. You will be developing C code. Select C from the submenu that
appears.
3. Are you sure you selected the right microcontroller?
M5213EVB?

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 4 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007

4. Select OK.
4. The wizard now creates the necessary files and configurations to help you start
with a simple application.
5. Now is a good time to see what the wizard has done to your file system.
a. Go to the file location where you specified the project to be created.
b. Browse through the various directories and files created under this
directory
c. Don’t be afraid to open any of them! Any interesting files?
1. the .mcp file is your project file containing all the necessary
information about your project. Double-clicking on this file will
open your project in CodeWarrior.
2. Under the Source directory, you will see a collection of C
source code files! (Surprise! Surprise!) Most interesting is the
main.c file that you will encounter very shortly. Double click on
it anyway and see what it contains!
3. The Include directory contains many support source code files
that you might find useful if you need to use any of the modules
of the microcontroller such as PWM, ADC, timers, etc.
6. Back to the CodeWarrior IDE! A new window (the project window) appears
showing two important components of your project: (1) The list of files
constituting your projects and (2) the list of targets onto which the application will
be built.

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 5 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007

Target list
Debug

Make

Project files
Target options

a. Files - The files are categorised into different groups such as Source,
Linker Command Files, etc. The location of the project files in these
groups does not affect the functionality of the IDE, and is only
advantageous for you to stay in control and understand the different types
and categories of files involved.
1. Under the Source group, you will find a main.c file. Open this
file by double-clicking on it.
1. Does it look familiar?
2. What do you think the program will do when it runs?
2. Under the Linker Command Files group, you will see two
linker configuration files that specify how the files are to be built
and linked together.
3. Under the Support files group, you will see a list of files of
various types such as C-source code files and assembler code
(files with the ‘.s’ extension) The files are automatically created
by the Project wizard to initialize your hardware appropriately
depending on the options you selected. In most cases, you will
not need to modify any of these files but it is important to be
aware of their presence, and ensure that they are not deleted by
mistake.
4. The Runtime, FP, C, and UART lib group contains a list of
default library files that you will most likely need during
development.
5. Try to create a new Group called Extra files!

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 6 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007
b. Targets - Four different target configurations are specified by default.
These targets can be individually configured for different usages. It is the
first two targets that will be used in this tutorial.
1. M5213EVB Console Debug - This is the very basic stationery
that outputs to the CodeWarrior's console window. The
application is loaded onto SRAM.
2. M5213EVB UART Debug - This is the very basic stationery
that outputs to the UART. User need to connect the terminal to
see the output. The application is loaded onto SRAM.
3. M5213EVB UART Release - This is the very basic stationery
that outputs to the UART. User need to connect the terminal to
see the output. The application is loaded onto SRAM.
4. M5213EVB Internal ROM - This program copies the data from
ROM to SDRAM. This is the very basic stationery that outputs
to the UART. User need to connect the terminal to see the
output.
What do you think about the difference between targets 2 and 3?
c. You will not need to change any of the configuration options for this
tutorial, but it is important that you are familiar with the various
possibilities that can be performed. In future projects, it may be necessary
to modify certain properties.

1. Click on the Target Options Setting button , and browse


through the set of options you can specify. Note that the options
you specify only apply to the target you have currently selected.
In this way, you can provide different configuration possibilities
for different targets.
2. From the left menu, select the TargetÆAccess Paths category.
In the right menu, you will see the list of paths that the system
searches to find any included header files for your applications.
(It is almost guaranteed that you will need to change this option
in the near future.)
3. From the left menu, select the DebuggerÆRemote Debugging
category. Make sure that the Connection option is set to
PEMICRO_USB.
4. Select OK.
7. Now, let us try to compile and make the project.
a. What is the difference between Compile and Make/build?
b. First, make sure that you choose the M5213EVB Console Debug target.

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 7 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007

c. Click on the Make button in the Project window. (alternatively,


select ProjectÆMake from the top menu)
8. If everything is as it should be, and the compiler didn’t find any problems, your
system will be ready to run. We are hence ready to execute the program.
a. Power your evaluation board.

b. Run the debugger by clicking on the Debug button in the Project


window. (alternatively, select ProjectÆDebug from the top menu)
c. The program will first be downloaded onto your target, and a new debug
window is loaded.
Run

Kill Thread

Step Over

Step Into

d. The debugger is now waiting for instructions to execute and monitor your
program. You can choose to execute the program to termination, or to step
through it one instruction at a time. Move your mouse over (do not click)
each of the top buttons illustrated above and read what they try to do.
e. For now, we want to run the program to termination, by clicking on the
Run button.
1. What do you observe?
2. Does the program behave like you expected?
f. Once you’re satisfied with your simple observation, terminate the
execution by clicking on the Kill Thread button.
g. If you simply try to close the debugger window, you will get a warning
that there exists a running process that is currently being controlled by the
debugger. You will be given the option to immediately Kill the process or
Resume execution. Unless it is necessary, try not to kill the process in
this way.

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 8 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007
h. Can you explain why did you have to terminate the program manually
yourself? Why didn’t the program terminate by itself once it was finished
executing?
9. Now, we will try to compile and make the project for another target M5213EVB
UART Debug
a. Choose the M5213EVB UART Debug target.
b. Repeat the above steps to make and debug the application
1. What do you observe?
2. Does the program behave like you expected?
3. Where did the output you expected go?
10. The answer to the above question is that the new target is configured so that the
standard output is sent onto UART, instead of a window console within the
CodeWarrior IDE. In order to obtain the output, we need to run a Windows
HyperTermial.
a. From the Windows Start menu, Run
ProgramsÆaccessoriesÆCommunicationsÆHyperTermial
program.
b. Give the terminal a name (for example: CodeWarrior Output)
c. Set the Connect Using property to COM1
d. Select Ok
e. Set the properties as follows:
Bits per second 19200
(baud rate)
Data bits 8
Parity None
Stop Bits 1
Flow Control None
11. Now, rerun the project in CodeWarrior and observe the hyperTerminal you setup.
Do you observer any difference?
a. Troubleshooting: You do not see any output in the terminal?
1. Make sure that the serial cable is connected to the UART0
terminal on your evaluation board
2. Make sure that the other end of same serial cable is connected to
the PC
3. Make sure that you have selected to right COM terminal
(COM1)
Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 9 of 19
Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007
b. Once you’re satisfied with your observation, terminate the execution by
clicking on the Kill Thread button.
c. You can also close the HyperTermial window. You will be prompted to
save your connection! This can be useful so that you do not need to
reconfigure your terminal every time you run HyperTermial. You can
simply load your saved connection.
12. C programming in Codewarrior is simply like any other C programming! In order
to refresh your C programming skills, you need now to modify the program to
help the user guess a random number you choose between 1 and 100
a. Choose the M5213EVB Console Debug target.
b. Use the fgets C-function to obtain the secret number from you. (Make
sure that the user is not looking over your shoulder!)
1. Tip1: Calling the instruction
fgets(line, sizeof(line), stdin)
;where line variable is defined as “char line[100]”, waits
until the user enters text into standard input (the keyboard), and
saves the text entered into the line variable.
2. Tip2: Calling the instruction
sscanf(line, "%d", &value);
where value is defined as “int value”, scans the line array until
it meets a non-digit and reformats the result as a digital number,
placing the result in the value variable.
c. Now, prompt the user to enter their guess of the number.
d. If their guess is correct, write a “Congratulation!” message on standard
output telling them of the result. Terminate the program.
e. If their guess is larger or smaller than your random number, write a polite
message telling them of their mistake (indicate whether it was larger or
smaller) and give them an opportunity to guess again. Repeat steps C to E
until the guess is correct.
Hopefully, by the end of this section, you have become familiar with the CodeWarrior
IDE and can use it to create simple C applications that can interact with the user.
So far, the application user has been interacting with the embedded system through the
PC standard input/output devices. This is satisfactory in certain cases such as during
application development and debugging. However, in most embedded applications, there
will be no attached PC to which the microcontroller can communicate. User interaction
will hence be limited to I/O units such as a small keyboard, switches, LEDs and a simple
LCD display. More importantly, I/O interactions with the microcontroller are not limited
to user interactions. Another kind of relevant I/O devices that the microcontroller needs

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 10 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007
to interact with is the mechanical/electrical system that ought to be controlled. This
interaction occurs via sensors and actuators.

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 11 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007

3 Part C – MCF5213 Modules Introductions


In this section, we will introduce some of the relevant modules of the MCF5213 that you
will encounter in this course. We will simply focus on three main modules that come with
the microcontroller to illustrate its fundamental mechanisms:
• General purpose I/O (GPIO)
• External interrupts (EPORT)
• Programmable Interrupt Timers (PIT)
After this tutorial, you will not have to directly work with these modules at the register
level, since drivers will be provided to control the I/O devices. It is however extremely
important to be aware of the mechanisms behind these drivers, in order to be aware of
their limitations and capabilities, and to know how handle any problems you encounter
with them. (You will have problems with them. That we can promise!)
We will further develop on the generic project developed in part B of this tutorial. For
each of the subsections below, you would need to:
1. Create a new project as illustrated above. (An alternative is to simply copy the
COMPLETE project directory onto another desired location. It is hence also
convenient to rename the .mcp file under the top directory to reflect your new
project description.)
2. Modify the main.c file as required.
Please refer to the MCF5213 documentation
http://www.freescale.com/files/32bit/doc/ref_manual/MCF5213RM.pdf if you need to
know more details about the microcontroller or the pin descriptions which are not
covered in the following text.

3.1 General Purpose I/O


Many of the pins of the microcontroller may be used for several different functions.
When not used for their primary function, many of the pins may be used as general-
purpose digital I/O pins.
The digital I/O pins are grouped into 8-bit ports. (Note however that some ports do not
use all 8 bits.) Each port has registers that configure, monitor and control the port pins.
The figure below is a block diagram of the MCF5213 ports showing each port/pin’s
primary as well as alternate functions.

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 12 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007

To experiment with general purpose I/O functionality we will experiment with the 4
LEDs that are setup on the evaluation board. These 4 pins are connected to the TC port of
the microcontroller, and we will hence develop the code necessary to configure this TC
port.
1. On the microcontroller, first ensure that the LED_EN jumper is ON
2. Before we can output on these pins, we need to configure them for I/O output.
a. The register MCF_GPIO_PTCPAR (Port Assignment Register), shown
below, allows each pin to be configured between the GPIO function and
its primary function (in this case, the TC port’s primary function is a
Digital Timer Input)

i. Setting MCF_GPIO_PTCPAR[0,1] to the bit values 00


configures PTC[0] as GPIO. (A setting of 01 is used for the
primary function)
ii. Setting MCF_GPIO_PTCPAR[2,3] to the bit values 00
configures PTC[1] as GPIO. (A setting of 01 is used for the
primary function)
iii. ...

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 13 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007
b. In the main function of your project, write a simple instruction to set the
register MCF_GPIO_PTCPAR such that the pins PTC[0], PTC[1],
PTC[2] & PTC[3] act as GPIO.
i. Don’t forget to add #include “common.h” (present in the
project directory) on the top of the file in order to obtain the macro
definitions of MCF_GPIO_PTCPAR, as well as the other
registers.
c. We now need to set the pins’ directions to output. The register
MCF_GPIO_DDRTC (Data Direction Register), shown below, controls the
direction of the port TC when configured for GPIO functionality.

i. Setting MCF_GPIO_DDRTC[0] to the bit value 1 configures


PTC[0] as GPIO output.
ii. Setting MCF_GPIO_DDRTC[1] to the bit value 1 configures
PTC[1] as GPIO output.
iii. …
d. In the main function of your project, write a simple instruction to set the
register MCF_GPIO_DDRTC such that the pins PTC[0], PTC[1], PTC[2]
& PTC[3] act as output pins.
3. We are now ready to output the desired value on the output port TC. This value
will then be reflected in the LEDs of the evaluation board.
a. Outputting data on a GPIO output port is performed by simply writing the
desired value onto the MCF_GPIO_PORTTC register.
b. In the main function of your project, write a simple instruction to set the
register MCF_GPIO_PORTTC such that the TC port (and hence the LEDs)
represent the value, say, 4.
c. What do you observe on the LEDs? Do they reflect the expected value?
d. Now experiment with outputting different values such as: 0, 15, 23, etc.
Can you make any interesting observations?
e. You have so far outputted a data value on the complete TC port. Can you
modify your code so that you can output on a specific pin of the TC port
(for example PTC[2]) the bit value 1,without affecting the existing output
on the remaining pins?
i. Tip1: How do you set a single pin of a register to 1, without
affecting the remaining bits?

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 14 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007
1. If R is an 8 bit register with a binary value R=hgfedcba and
I simply want to replace the e with 1, then R=R|00010000
will do the trick. Can you see why?
ii. Tip2: How do you set a single pin of a register to 0, without
affecting the remaining bits?
1. If R is an 8 bit register with a binary value R=hgfedcba and
I simply want to replace the e with 0, then R=R&11101111
will do the trick. Can you see why?

3.2 External Interrupts


The MCF5213 edge port module (EPORT) supports 7 external interrupt sources on the
NQ port. The 7 pins of this port can be used to handle external interrupts from various
sources such as push buttons. The MCF5213EVB evaluation board comes with two user
switches that are available for application as needed: SW1 is connected to the PNQ[4]
pin and SW2 is connected to the PNQ[5] pin.
1. First, we need to configure these pins as external interrupt pins. Luckily, this has
already been configured as part of the default project created for us.
a. Open the file sysinit.c and look at the function
mcf5213_allow_interrupts. Can you understand what this
function does? That is the MCF_GPIO_PNQPAR and MCF_INTC_IMRL
registers.
i. Refer to the Reference manual and try to understand what each of
the registers (MCF_GPIO_PNQPAR, MCF_INTC_IMRL, etc) do.
ii. Maybe this is also the right opportunity to experiment with the
debugging facilities of the system.
1. Create a breakpoint somewhere early in the main function
of your project.
2. Start the debugger and wait until the program stops at your
breakpoint.
3. Select ViewÆregisters from the top menu. A new window
appears with a hierarchical list of the complete set of
system registers.
4. Search for the registers used in the
mcf5213_allow_interrupts function.
5. Observe how these registers are modified. Do these register
get the values you expect them to?
b. Now we need to specify what functions to execute whenever the external
interrupts PNQ[4] and PNQ[5] occur.

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 15 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007
i. The following code is used to specify that function
sw1_handler is to execute whenever the external interrupt
PNQ[4] occurs:
mcf5xxx_set_handler(64 + 4, sw1_handler);
ii. Note that 64 is an offset necessary to place the function handler in
the correct location in the interrupt vector table.
iii. We also need to enable the interrupt request handlers using the
following code segment:
mcf5xxx_irq_enable();
iv. In the main function of your project, write the above code
instructions.
v. Finally, we also need to provide the sw1_handler function, and
this function needs to look as follows:
__interrupt__ void sw1_handler (void) {
printf("You pressed SW1 button\n\r");
fflush(stdout);
MCF_EPORT_EPFR = MCF_EPORT_EPFR_EPF4;
//Acknowledge that the interrupt is handled
}
vi. Can you guess what this function will do when you press the SW1
button?
vii. Write the above function in your project.
viii. Make and execute your program.
1. You will receive errors complaining that the function
mcf5xxx_set_handler is not defined! This is due to a
bug in the project wizard.
a. If you open the file mcf5xxx.c, you will see at the
bottom of the file that the function
mcf5xxx_set_handler is defined! Why does
CodeWarrior not find it?
b. In the Project window, select the Link Order frame.
You will notice that not all files included in your
project are actually defined to be also linked during
Make to form the final executable. Compare the
files listed under Link Order to those listed under
the Files frame! Makes sense?
c. To handle this problem
i. In the Files frame, click on the target icon
next to the following files:
1. main.c
2. mcf5213.c
Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 16 of 19
Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007
3. mcf5xxx.c
4. sysinit.c
5. UART_unity.c
6. mcf5213_lo.s
7. mcf5xxx_lo.s
8. clock.c
9. uart.c
d. One more problem need to be fixed.
i. In the project directory, under the cfg
subdirectory replace the existing
M5213EVB_SRAM.lcf file with the
following:
http://www.md.kth.se/mmk/gru/mda/mf200
8/Downloads/M5213EVB_SRAM.lcf
e. You program should now be error free!!!!!!
2. Does the program behave as expected when you press the
SW1 button?
ix. Modify the code to setup the interrupt handler for PNQ[5]
x. Make and execute the progam
1. Still operating as desired?
xi. Can you modify the program so that the LED we developed earlier
do something interesting whenever you switch the buttons SW1 &
SW2?

3.3 Programmable Interrupt Timers


As well as external interrupts, the MCF5213 microcontroller has 50 fully programmable
interrupt sources from modules such as different timers, ADC units, and PWM. Of most
relevance to this course are timer interrupts. Such interrupts can be used to provide us
with an easy approach to perform periodic functionalities.
We will use the Programmable Interrupt Timer (PIT) module to setup a single periodic
function, which simply toggles the LEDs of the evaluation board with a predetermined
frequency. We will here not discuss the specific details of the registers necessary to
configure the PITs. Instead, you can download the necessary code segments (discussed
below) from:
http://www.md.kth.se/mmk/gru/mda/mf2008/Downloads/PITCodeSegments.c
1. The first step is to configure a PIT to run with a specific period. For this, we use
the function PITInit(int clockPrescaler, int modulus).
void PITInit(int clockPrescaler, int modulus) {
MCF_PIT_PCSR(0) = MCF_PIT_PCSR_PRE(clockPrescaler)
Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 17 of 19
Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007
| MCF_PIT_PCSR_PIE
| MCF_PIT_PCSR_RLD
| MCF_PIT_PCSR_OVW;
MCF_PIT_PMR(0) = MCF_PIT_PMR_PM(modulus);
//Setup the interrupt registers for PIT
MCF_INTC_IMRH &= ~ MCF_INTC_IMRH_MASK55;
MCF_INTC_ICR(55) = MCF_INTC_ICR_IP(4) |
MCF_INTC_ICR_IL(4);
mcf5xxx_set_handler(64 + 55, pit_handler);
mcf5xxx_irq_enable();
}
a. The parameters clockPrescaler & modulus define the period – P
– of the timer interrupt according to the equation:

mod ulus × 2clock Pr escaler + 1


P=
80 × 106
b. So, if one chooses a clockPrescaler=5, what is the necessary value
of modulus to obtain a period of 1 ms?
c. In the main.c file, include the function PITInit at the top of your file.
d. In the main function of your program, use the function PITInit to
initialise your timer, with a period of 1 ms.
2. The above configuration does not activate the PIT. We now define the function
PITStart that will do so when called.
void PITStart() {
MCF_PIT_PCSR(0) |= MCF_PIT_PCSR_EN; //enable
}
a. In the main.c file, include the function PITStart at the top of your file.
b. In the main function of your program, use the function PITStart to
start your timer, after it has been configured.
3. The final step is to define the function handler that will execute every time the
timer interrupt is triggered. This is done by defining a function with the name
pit_handler, as specified in the PITInit function above. The function
below shows a template of pit_handler. It remains to write the code segment
to be executed when an interrupt occurs.
__interrupt__ void pit_handler(void) {
//Write your code between this point....
//...and this point
MCF_PIT0_PCSR |= MCF_PIT_PCSR_PIF;
}
a. In the main.c file, include the function pit_handler at the top of your
file.
b. Modify this function, so as the LEDs of the evaluation board output the
value of a counter being incremented every time the interrupt is executed.

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 18 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/
Programming with CodeWarrior IDE
29/10/2007
4. You are now ready to make and run your project. Do so, and make sure that the
program behaves as expected.
a. Having problems compiling/making your project?
i. Tip1: Make sure you have #include “common.h” at the top
of your main.c file.
ii. Tip2: In the C language, the order with which you define your
functions is very important. The basic rule is that a function cannot
make a function-call to another function, unless the called function
is already defined earlier. For example, if function A calls function
B, then you need to define function B before the call in function A.
Can you see any potential dependencies between the functions you
have just defined?
5. You may want to save the project to reuse it in the next tutorial.

Embedded Control Systems course, Autumn 2007. (05/11/2007) Page 19 of 19


Course home page: http://www.md.kth.se/mmk/gru/mda/mf2008/

You might also like