Lab 3 - Microcontroller Module and C Programming

fiercebunΗλεκτρονική - Συσκευές

2 Νοε 2013 (πριν από 4 χρόνια και 6 μέρες)

73 εμφανίσεις

Lab
3








MENG483
L

Group#:
XX

Names:
XX, XX

Microcontroller Module

and C Programming


1

Objectives


Th
e objective
s

of this

laboratory exercise
are

to introduce
the
CSM12D microcontroller
module

and practice

writing C programs
.

Operation of the module’s

I/O ports,
LEDs
, and
switches

will be demonstrated. You will then combine this information with your knowledge of
the C control statements to write some programs.

2

Theoretical b
ackground

2.1

Microcontrollers


A microcontroller chip has many components in one package. A block diagram
for a
typical microcontroller
is given in
Figure
1
. The CPU processes instructions and data which are
stored in memory.

The ATD block reads sensor inputs
,

and the PWM block generates output
sign
als for actuators. The Timer block can time events or generate output at specified time
intervals. Communication with computers or other microcontrollers is possible with serial
communication. The I/O ports allow interaction with digital circuits.


Figure
1
: Typical microcontroller block diagram

Spring 20
12

Lab 3,
Group #
X
, Names:
XX, XX


MENG 483L


2

2.2

Digit
al I/O Ports


A port provides an interface between the micro
controller and external
digital
circuitry
.
Every
input/output
port has at least t
wo registers

which
control the operation of the port.
As an
e
xample, consider Port A

as

depicted in Figure 2.


The data direction register (DDR) configures individual port lines for either input or
output.
In the figure, t
he triangle depicts an input/output buf
fer
,

which
is basically
a set of
voltage
-
controlled switches. A ‘1’ in the data direction register
DDRA

closes

the switch for that
bit, causing that line to become an output line. A ‘0’ in the data direction register opens the
switch for that bit, breaki
ng
the connection to

PORTA output register
. The

Z
s represent the high
impedance of the open switches
.
Having a ‘0’ in the data direction register allows that line to be
used for input.
When a signal is connected to one of these inputs, the Z will be rep
laced with the
high/low level of the signal.



Here are some examples of

r
ead” and “write”

operations in the C programming
language. Whenever the port name appears to the
left

of the equal sign, it is a write operation.

The code
PORTA = 103;

will store the decimal number 103 (binary number

01100111
) at
memory address 0x0000. If the port name appears anywhere else in an expression, it is a read
operation. The code
x = PORTA;

will read the current state of the PA7..0 hardware lines. Note
tha
t in
Figure
2

the read operation does not return the same bits that were written because some
of the bits are configured for input.



Figure
2
: Registers controlling Port A

input/output

Lab 3,
Group #
X
, Names:
XX, XX


MENG 483L


3

3

Equipment






CSM12D module (MCU)



USB cable



Freescale

Project Board



8
-
wire connector

4

Procedures

4.1

I/O Port
s


In this part of the lab you will explore the operation of Port A and Po
rt B (two of the
MCU
’s input/output ports).

Hardware Setup


Do not
power
the project board until the lab
instructions indicate to do so.


Configure the module’s jumpers to match the depiction in
Figure
3
.

The VX jumper
causes power to come from the

project board
.
Configuring the
USER hardware

jumpers as
shown will disable all of the input hardware and enable the output LEDs.


Figure
3
: Jumper configuration
for
part 1

Lab 3,
Group #
X
, Names:
XX, XX


MENG 483L


4


As shown in Figure 4, use the 8
-
wire connector to ro
ute the PORT A signals to the
project board’s 8 LEDs.



CodeWarrior New Project Wizard

To open CodeWarrior, go to the Start menu and select

Programs


Freescale

CodeWarrior


CodeWarrior
Development Studio for SX12(V) v5.0



CodeWarrior IDE
.

If it asks about the license select register later


S
elect

Create

New

Project on the Startup menu


This should bring up the project wizard. The first step in

the wizard is locating the microcontroller we will be using.
This is the MC9
S12X
DT512. Start
by selecting the HCS12X
subcategory then select the
HCS12XD family and then find our
specific microcontroller unit (MCU).

Once you have selected the correct
MCU
select the P&E
Multilink/Cyclone Pro connection
from the upper right hand corner.
Then click next to proceed to the
next portion of the setup.


Figure
4
: PORTA wired to LEDs

Lab 3,
Group #
X
, Names:
XX, XX


MENG 483L


5


The default (Single Core) option is the correct one for the second page, leave it unchanged and
click next.

Th
e third page contains the language
the compiler will use, leave it on the
default value of C. And more
importantly it contains the name of the
project you are creating and the
location it is going to be saved. Name
the file descriptive
ly

and save it in f
or
this lab. (If you have not created a file
for your group do so now and save all
your work in this location)
Click next
when you have completed these steps.


The fourth page is where you would
add prebuilt header files or any other
files you might want t
o include, ignore this for now and click next to continue on.

The fifth page is where you would turn on one of the processor experts, these are not used in this
lab leave it on the default selection (None) and click continue.

The sixth page controls sever
al important
features, the startup code used to initialize the
MCU, the memory model used by the MCU and
the floating point format used during calculations.
Both the startup code and memory model have
the correct values as defaults, however you need
to en
able floating point by selecting the float
IEEE32, double is IEE64 option. Click Finish
when this is complete as the next two tabs have
the correct values as defaults

CodeWarrior will now create a project with the setting used in the wizard.

The most impo
rtant file that is created is
main.c located in sources folder.
This file is is used to
control the MCU

with associate peripherals
.
In laboratory exercises you will be m
odifying this
file to
assign

the MCU to perform
specific tasks
.

Replace the line readi
ng ‘EnableIn
terrupts’ with the following four

lines of code:


DDRA = 0xFF;

// configure all 8 bits of Port A for output


DDRB = 0xF0; // configure bits 7..4 of Port B for output


PORTA = 103;

// store a value in the Port A output register


PORTB = 0
xF0
; // store a value in the Port B output register


Compiler and Debugger

Lab 3,
Group #
X
, Names:
XX, XX


MENG 483L


6

Compile the program by clicking the

button. If there are no errors,
connect the USB cable

(make sure the LED is on)

and also turn on the prototyping board power. D
ownload the program
by clicking the

button.

When the connection manager pops up click the connect
button, if you followed all of the instruction correctly to this
point it should begin downloading the program to the
MCU.



Click OK when the Loader
Warning appears.


Wait until the program has

finished

downloading
.





When you
press the run button
, the project board LEDs
should display the bit pattern 01100111, where a ‘1’ indicates ‘on’ and ‘0’ indicates ‘off’.

You
will now use the debugger in
terface to control DDRA and PORTA. Maximize the Data:1
window. Expand the data tree so it looks like the following figure.

(right click, open module…,
mc9s12….c,

click

ok, then find

_DDRAB


in the list near bottom)


1.

Double
-
click on the 255 next to grpD
DRA. Type the hexadecimal

number 0x0F

(or
decimal 15)
and press enter.


a.

Which
Port A
bits are now inputs?


Lab 3,
Group #
X
, Names:
XX, XX


MENG 483L


7

b.

Which
Port A
bits are no
w outputs
?



c.

What
do the LEDs show
?


d.

What does grpPA read?



2.

Enter the value

102

next to grpPA
.

(Note: you will not see ‘102’ after you press enter)

a.

What do the LEDs show now?



b.

What does grpPA read

now
?



3.

Change grpDDRA back to 0xFF (255)

a.

What does grpPA read now?



b.

Explain your results.






3 pts total



On the
MCU
, bits 7 through 4 of Port B are connected to four LEDs. These LEDs are different
from the project board LEDs because a ‘1’ or +5V will turn them
off

and a ‘0’ or 0V will turn
them
on
. Because the behavior seems backwards, it is natural to use the bitwis
e NOT ‘~’ to
control these LEDs. Complete the following exercise.

4.

Set both grpPA and grpPB to 0x60

a.

What do the MCU
’s LEDs show



b.

What do the project board’s top 4 LEDs show?




5.

Set grpPB to ~0x60 (the tilde ‘~’ is located next to the 1 key)

a.

What do the microcontroller’s LEDs show?










2 pts total

Press the stop button

and close the debugger.

Power off the project board.

4.2

Switches and program flow
-
control
`

In this part of the lab you wil
l use the MCU
’s switches to control program operat
ion.
We have
created a
prebuilt project
which will load the same settings you
configured manually in part 1.
This project has several predefined files to streamline the use of the CSM12D board. To use this
project go into the
MENG 483L

folder on your
desktop and

find the folder labeled
Default

then
Lab 3,
Group #
X
, Names:
XX, XX


MENG 483L


8

copy it into your personal folder
.

Rename the folder
Lab_Three

then open the folder and
rename the .mcp file

(the icon looks like a little blue chip)

to the same name as the folder. Once
that is complete yo
u can open the .mcp file and CodeWarrior should open up the project.

T
he
MCU

has two push
-
button type switches called SW1 and SW2. These two switches connect
to Port P. The module also has four rocker type switches collectively called SW3. These four
sw
itches connect to Port B.

Before we can use these switches, we need to configure the ports to receive input. Open up the
main.c file and look at the
main()

function.
The comments indicate different sections for
defining variables, initialization, and pro
gram code. Because you will be using the switches and
LEDs in most every lab, we have defined the function
CSM12D_USER_Init()

to group together
the configuration commands.

Open up the csm12d.c file and find the definition of
CSM12D_USER_Init()
. Make sure

you
understand each DDR configuration.
Close the csm12d.c file.

Back in the
main()

function, add the command
PORTB = 0;

to the initialization section.
Compile
the project
, power the project board, and download the project
.

When it has finished
download
ing, click the run button. Now that the ports are configured properly, you may enable
the switches by moving the USER jum
pers to match
Figure
5
.

In the debugger window, maximize the Data:1 window and find the merged bits group grpPB

under

_PORTAB


after following directions from above
.

1.

Set the four SW3
switches to the ‘off’ position and then click on grpPB. (The debugger
updates information for an item when you click on that item). The value of grpPB should
be 15 because the SW3 switches generate +5V or ‘1’ in the ‘off’ position.

2.

Leave SW3
-
1 ‘off’ and
set the others to ‘on’. Click grpPB.
What does it read?




3.

Set SW3
-
2

to ‘off’ and set the others to ‘on’ . Click grpPB.
What does it read?




4.

Set SW3
-
3 to ‘off’ and set the others to ‘on’ . Click grpPB.
What does it read?


5.

Set SW3
-
4 to ‘off’ and
set the others to ‘on’ . Click grpPB.
What does it read?















1pt



6.

W
hich bit

of Port B each switch controls?










Figure
5
: Jumper configuration for part 2

Lab 3,
Group #
X
, Names:
XX, XX


MENG 483L


9

Stop the program and close the debugger. Let’s now modify the program so that SW3 controls
the LEDs. Place the
following lines of code inside the infinite for
-
loop

in the program code
section
. Open up the csm12d.h file and note how these symbolic names have been defined.


LED1 = SW3_1;


LED2 = SW3_2;


LED3 = SW3_3;


LED4 = SW3_4;


Compile and download
the project. Run the program and test each switch. The program works,
but the code is a little inefficient. We can replace the 4 l
ines with a single line of code:


LEDS = SW3 << 4;


The code ‘<< 4’ takes the value of SW3 and shifts the bits of the bi
nary representation 4 bits to
the left. This number is then written to LEDS (or PORTB). Do you understand why the bit
-
shift
was necessary?

The following program demonstrates how the switches can control program flow. Modify the
current project
code
to m
atch.

void main(void)

{


/* define variables */


int x;



/* Initialize variables and hardware here */


CSM12D_USER_Init();


x=0;


LEDS=
0x60;



for(;;) /* program code goes inside this infinite loop */


{


LEDS = ~LEDS; // invert what is currently displayed


if (SW3_1==0) // if SW3_1 is in 'on' position


{


msleep(1000); // wait 1000 milliseconds


}


else


{


msleep(500); // wait 500 milliseconds


}


}


/* please ma
ke sure that you never leave the main() function */

}


Compile, download, and run the program. Use SW3_1 to change between the two delay times.

The
while

statement loops over a block of code. One common use
of

while
-
loops is to keep a
program from moving

forward until an event occurs. The following program counts the number
of times SW1

has been pressed. It waits for a
press

and
release

before incrementing x, otherwise
x could be incremented a thousand times while your finger is on the button.


Lab 3,
Group #
X
, Names:
XX, XX


MENG 483L


10

void

main(void)

{


/* define variables */


int x;



/* Initialize variables and hardware here */


CSM12D_USER_Init();


x=0;


LEDS=0xF0;



for(;;) /* program code goes inside this infinite loop */


{


while(SW1==1)


{


//stay here until SW1==0 (button press)


}


while(SW1==0)


{


//stay here until SW1==1 (button release)


}


x++; // increment x


LEDS = ~x << 4; // display x


}


/* please make sure that you never leave the mai
n() function */

}

4.3

Exercises

Exercise 1
: Simple Calculator

Write a program which can add numbers to a running total. The number will be entered in
unsigned binary form on SW3’s four switches and the running total will be displayed on the
project board’s 8

LEDs

through Port A
. Pressing SW1 should add the number to the running
total. Pressing SW2 should clear the total.
Do not forget that the
switches on the
MCU
have
been designed such that being ‘on’ or ‘closed’ corresponds to a ‘0’ seen by the microcont
roller.

Demonstrate your working program to a TA.





TA
CHECKMARK
2pt

Make sure to include the final code for your calculator in your report
,

provide line
-
by
-
line
comments
and discuss how your program works.


1 pt