Vous êtes sur la page 1sur 29

3/2/2015

Department of Electrical Engineering


and Computer Science

EGN 3211: Engineering Analysis and Computation


Microcontroller Programming

Instructor: Zakhia (Zak) Abichar

Computer
What are the main parts of the computer?
1) Central Processing Unit (CPU)
The CPU will does the computations
Arithmetic (addition, subtraction, multiplication, division)
Decision making (if- else)

2) Memory
The memory stores the program and the data (eg: variables and arrays)
The memory is supplemented by the hard disk (in small computers, theres no hard
disk, only the memory)

3) Input/Output (I/O) Support


This is how the computer interacts with outside devices (keyboard, mouse, etc)
2

3/2/2015

Computer
If you open the computer box, you can identify these three parts
In a desktop computer, these parts are distinct components
This means you can replace the CPU only, or replace the memory only, or replace
the motherboard only to get better I/O performance
The motherboard is the large board of the desktop computer that connects
everything together

What if you want to build your own computer?


You need to get: a motherboard, a CPU, memory chips
Assembly them together
Disadvantage: You have to get many parts and put them together
The microcontroller solves the program
3

Microcontroller
What is a microcontroller?
The microcontroller is a small computer chip that contains all
of these in one package:
A CPU (Central Processing Unit)
Memory
I/O interfaces

Therefore, if we have a project that needs a small computer,


its convenient to get a microcontroller chip

3/2/2015

Microcontroller
Microcontrollers are used in a multitude of products that have computing
power
A simple calculator uses a basic microcontroller
Such a microcontroller could cost a few cents

TV, DVD player, fridge, AC system, washing machine, microwave


All of these use microcontrollers

A car uses around 30 microcontrollers


For engine control, stability system, fuel economy, etc.

A large airplane uses around 1000 microcontrollers


Microcontrollers cost anywhere from few cents per unit to 100s of dollars
per unit depending on computation power, memory and I/O capability

Microcontroller
What are the available microcontrollers?
They are made by these companies:
Texas Instruments (TI)
Atmel
Microchip (they produce the PIC microcontrollers)
Renesas (owned by Hitachi, NEC, Mitsubishi)
Freescale (spun off Motorola)
STMicroelectronics
NXP Semiconductors
Silicon Labs
Zilog

3/2/2015

Texas Instruments MSP430


The microcontroller that well use is called the Texas
Instruments MSP430
Its called TI MSP430, for short

Texas Instruments MSP430


This is the development
board called Launchpad
that contains a TI MSP430
This is the TI MSP430 chip
These are push buttons that
can be incorporated in the
program
There are also LEDs that
can be programmed
8

3/2/2015

Blinking LED Project


Lets program the MSP430 to blink the LED
This project is described at the link below
What you need:
MSP430 launchpad board
Code Composer Studio
This software tool allows us to write the C code, compile it, and download it to the
MSP430 and run it
This software can be downloaded from TIs website; its also available in some
engineering labs at UCF

Blinking LED project:


http://processors.wiki.ti.com/index.php/Blink_your_first_LED
Code Composer Studio for Launchpad (download info):
http://processors.wiki.ti.com/index.php/CCS_for_LaunchPad

Blinking LED Project


#1. Create a workspace in Code
Composer Studio
A workspace is a folder that
contains all your codes
Click on: File Switch
Workspace Other
Select a folder that will have your
codes
You can use your flash disk folder
if youre working in the lab

10

3/2/2015

Blinking LED Project

#2. Create a new project (specify the chip number)


Click on: File New CCS Project
In the window, fill the four items that are highlighted in the figure below
This creates a new project with an empty main function

11

Blinking LED Project

Notice, in the window below, we specified which chip were using


MSP430 comes in many editions
Below, were specifying the one were using
If you look at the chip in your launchpad board, youll se on it the label
MSP430G2553

12

3/2/2015

Blinking LED Project


Copy and paste this code in the main function
#include <msp430g2553.h> // header for MSP430 G2553 chip
unsigned int i = 0; // counter as a global variable
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // stop the watchdog timer
P1DIR |= 0x41; // set the direction register for LED1 and LED2
P1OUT &= 0xBE; // initialize LED1 and LED2 to off
for (;;) {
P1OUT ^= 0x01;

//empty for loop is an infinite loop


// invert the state of LED1

for(i=0; i< 20000; i++) // create a delay between toggles


;
// empty statement, do nothing
}
}

13

Blinking LED Project


After you paste the code in the main function, click on the
Debug button, circled in the figure below
This will compile the code and shift to the debug mode
Now, were ready to download the code to the microcontroller
and run it

14

3/2/2015

Blinking LED Project


Now, click on the Resume button (left circled button in the figure
below) and the code will start running on the microcontroller
The red LED should start blinking
To go back to editing the code, click on the Terminate button (right
circled button in the figure)
Notice, when you click on the Terminate button, our program continues
to run on the microcontroller, so the LED continues to blink

15

Blinking LED Project


You can see the blinking LED of the MSP430 at the link
below

Blinking LED video:


http://www.youtube.com/watch?v=rmMkyIzpvNc

16

3/2/2015

Blinking LED Project


Before we go over the code, lets look at binary and
hexadecimal numbers that are used in the code
Well also see some logic design concepts that are used in the
code
These concepts will help us to see what the lines of code below
are doing

P1DIR |= 0x41;
P1DIR &= 0xBE;

P1OUT ^= 0x01;

17

Decimal Numbers
Lets look at a number below thats in base 10
Each digit position has a weight

3,427
103

102

101

100

This is how we can get the value of this number:


3*103 + 4*102 + 2*101 + 7*100
= 3000 + 400 + 20 + 7
= 3,427

18

3/2/2015

Binary Numbers
Lets look at a number below thats in binary
In binary, the bits can be only 0 or 1
Each bit position has a weight

1101
23

22

21

20

This is how we can get the value of this number:


= 8 + 4 + 1
= 13

19

Binary Numbers
In general, these are the weights of a binary number:

1011 1101

128

64

32

16

This is the value of this number:


= 128 + 32 + 16 + 8 + 4 + 1
= 189

20

10

3/2/2015

Hexadecimal Numbers
Hexadecimal numbers are in base 16

In base 10, a digit can have 10 different values (0 to 9)


In binary, a bit can have one of two values (0 or 1)
In base 16, a digit can have 16 different values
In base 16 a digit can be:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
10

11

12

13

14

15

These are the numerical weights of the values A to F


21

Hexadecimal Numbers
These are the weights of each digit position

C2A7
163

162

161

160

This is how we can get the value of this number:


Remember, C is 12 and A is 10
12*163 + 2*162 + 10*161 + 7*160
= 12*4096 + 2*256 + 10*16 + 7
= 49,831

22

11

3/2/2015

Converting to Binary
To convert a decimal number to binary, keep dividing it by 2 until the
quotient becomes 0
The binary number will be all the remainders taken from the last to the
first
Example: Convert 26 to binary
26 / 2 = 13
remainder: 0
13 / 2 = 6
remainder: 1
6/2=3
remainder: 0
3/2=1
remainder: 1
1/2=0
remainder: 1
Taking the remainders from last to first, the binary number is: 11010
Therefore, we can write: 2610 = 110102
Base 10

Base 2

23

Converting to Binary
Another way to convert a decimal number to binary is to write down a
few binary positions and their weights

32

16

Lets convert the number 26 to binary


The number 26 is smaller than 32, therefore, we can put 0 in the
position of weight 32
However, the number 26 is larger than 16, so we can put 1 in the
position of weight 16
0

32

16

1
24

12

3/2/2015

Converting to Binary
0

32

16

After we put 1 in the position of weight 16, therefore, the remaining


part to represent is 2616 = 10
So, in the remaining bit positions, we want to represent 10
The number 10 is larger than 8, so we put 1 in the bit of weight 8
The remaining part is to represent is 108 = 2
So the bit of weight 4 gets 0, the bit of weight 2 gets 1 and the bit of
weight 1 gets 0
Therefore, we get the answer 11010, the same as from the method of
dividing by 2
0

32

16

1
2

0
1

25

Converting to Hexadecimal
To convert a decimal number to hexadecimal, we keep dividing it by 16
until the quotient becomes 0
The hexadecimal number will be all the remainders taken from the last
to the first (the remainder range from 0 to 15, represented by 0 to F)
Example: Convert 49,831 to hexadecimal
49,831 / 16 = 3114
remainder: 7
3114 / 16 = 194
remainder: 10
194 / 16 = 12
remainder: 2
12 / 16 = 0
remainder: 12

A
C

Taking the remainders from last to first, the hex number is: C2A7
Therefore, we can write: 49,83110 = C2A716 = C2A7 (hex)
26

13

3/2/2015

Converting between Binary and Hexadecimal


One way to convert from binary to hex: convert the binary to decimal,
then convert the decimal to hex
However, it turned out there is an easy way to convert between binary
and hexadecimal
Converting from hexadecimal to binary:
Each hex digit becomes four bits
A hex digit has a value between 0 and 15; on four bits, it will be from
0000 to 1111
Example: lets convert C2A7 to binary
1100 0010 1010 0111

27

Converting between Binary and Hexadecimal


Converting from binary to hexadecimal:
Starting from the right side, take every four bits and convert them into a
hex digit
A group of four bits will range from 0000 to 1111
Theyre assigned values from 0 to F
Example: lets convert the binary number below to hexadecimal
10 0110 1101 1001

26D9
We should start grouping the bits in four from the right side; this way, the
leftmost two bits are taken together
28

14

3/2/2015

AND Function
Lets start with the AND function
This symbol is called the AND gate
It takes two inputs (A, B) and gives one output (Y)
A, B and Y are binary; it means each of them can be 0 or 1

This is the truth table of the AND gate


The truth table defines the output for all the cases of the inputs
A
0
0
1
1

B
0
1
0
1

AND
0
0
0
1

From the truth table, we can say that the output of AND is 1 only when
its two inputs are 1
29

AND Function
This is an example of the AND operation
Here, the numbers are binary:

AND

0011 1010
0101 1110
0001 1010

Lets write these numbers in hexadecimal:


The first number:
0011 1010

The second number:


0101 1110

The result:
0001 1010

3A (in hex)
5E (in hex)
1A (in hex)

In C coding, we can enter a number in hex by using 0x before the


number
Example:
int number = 0x3A;
30

15

3/2/2015

AND Function
How can we do this operation in a C code?
AND

0011 1010
0101 1110
0001 1010

(3A)
(5E)
(1A)

In C coding, we can enter a number in hex by using 0x prefix to enter


hexadecimal numbers
Use one ampersand & to do an AND operation
The conversion specifiers %x and %X are used to print hex numbers
%x gives the number in lower case, for example: 1a
%X gives the number in upper case, for example: 1A
int num1, num2, result;
num1 = 0x3A;
num2 = 0x5E;
result = num1 & num2;
31

AND Function
Lets see the output of this code:
int num1, num2, result;
num1 = 0x3A;
num2 = 0x5E;
result = num1 & num2;
printf("This is the operation in hexadecimal...\n");
printf("%X & %X = %X\n\n", num1, num2, result);
printf("This is the operation in decimal...\n");
printf("%d & %d = %d\n", num1, num2, result);
This is the operation in hexadecimal...
3A & 5E = 1A
This is the operation in decimal...
58 & 94 = 26

32

16

3/2/2015

AND Function
Lets look at the truth table of the AND gate one more time
What does the AND gate do?
A
0
If we AND something with 1, we get the same thing
0
If we AND something with 0, we get 0
1
1

B
0
1
0
1

AND
0
0
0
1

Lets consider the 8-bit word abcd efgh (each letter is 1 bit)
How can we make the leftmost and rightmost bits 0 and keep the other
bits unchanged? We can use the AND operation
AND

abcd efgh
0111 1110
0bcd efg0

This number is
called the mask

Therefore, the AND operation can be used to zero some bits in a word
33

OR Function
Another function is the OR function thats implemented by the OR gate
This is the symbol of the OR gate
It takes two inputs (A, B) and gives one output (Y)
This is the truth table of the OR gate
The truth table defines the output for all the cases of the inputs
A
0
0
1
1

B
0
1
0
1

OR
0
1
1
1

From the truth table, we can say that the output of OR is 1 if any of
the inputs is 1
34

17

3/2/2015

OR Function
This is an example of the OR operation
Here, the numbers are binary:

OR

0011 1010
0101 1110
0111 1110

(3A)
(5E)
(7E)

35

OR Function
How can we do this operation in a C code?

OR

0011 1010
0101 1110
0111 1110

(3A)
(5E)
(7E)

In C, the bar | operator is used to do an OR operation


int num1, num2, result;
num1 = 0x3A;
num2 = 0x5E;
result = num1 | num2;

36

18

3/2/2015

OR Function
Lets look at the truth table of the OR gate one more time
What does the OR gate do?
If we OR something with 0, we get the same thing
If we OR something with 1, we get 1

A
0
0
1
1

B
0
1
0
1

OR
0
1
1
1

Lets consider the 8-bit word abcd efgh (each letter is 1 bit)
How can we make the leftmost 4 bits equal to 1 and leave the remaining
bits unchanged? We can use the OR operation
abcd efgh
1111 0000
1111 efgh

OR

This number is
called the mask

Therefore, the OR operation can be used to make some bits equal to 1


37

XOR Function
Lets look now at the exclusive OR operation, called XOR in short
This is the symbol of the XOR gate
It takes two inputs (A, B) and gives one output (Y)
This is the truth table of the XOR gate
The truth table defines the output for all the cases of the inputs
A
0
0
1
1

B
0
1
0
1

XOR
0
1
1
0

The output of XOR is 1 if only one of its inputs is 1


38

19

3/2/2015

XOR Function
This is an example of the XOR operation
Here, the numbers are binary:

XOR

0011 1010
0101 1110
0110 0100

(3A)
(5E)
(64)

39

XOR Function
How can we do this operation in a C code?

OR

0011 1010
0101 1110
0110 0100

(3A)
(5E)
(64)

In C, the hat ^ operator is used to do an XOR operation


int num1, num2, result;
num1 = 0x3A;
num2 = 0x5E;
result = num1 ^ num2;

40

20

3/2/2015

XOR Function
Lets look at the truth table of the XOR gate one more time
If we XOR something with 0, we get the same thing
If we XOR something with 1, its inverted

A
0
0
1
1

B
0
1
0
1

XOR
0
1
1
0

Lets consider the 8-bit word 0110 0101


How can we invert the rightmost 4 bits and leave the remaining bits
unchanged? We can use the XOR operation

XOR

0110 0101
0000 1111
0110 1010

This number is
called the mask

Therefore, the XOR operation can be used to invert some bits in a word
41

Blinking LED Project


Lets get back to the blinking LED code

42

21

3/2/2015

Blinking LED Project


Well go over the code to see how its making the LED blink
#include <msp430g2553.h> // header for MSP430 G2553 chip
unsigned int i = 0; // counter as a global variable
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // stop the watchdog timer
P1DIR |= 0x41; // set the direction register for LED1 and LED2
P1OUT &= 0xBE; // initialize LED1 and LED2 to off
for (;;) {
//empty for loop is an infinite loop
P1OUT ^= 0x01; // invert the state of LED1
for(i=0; i< 20000; i++) // create a delay between toggles
;
// empty statement, do nothing
}
}

43

Blinking LED Project


The code starts by inserting the msp430g2553 header file
This file contains addresses on the MSP430 board that well
use in the code
In addition, a global variable i thats used in the code is
declared
#include <msp430g2553.h> // header for MSP430 G2553 chip
unsigned int i = 0; // counter as a global variable

44

22

3/2/2015

Blinking LED Project


In the main function, the line below disables a mechanism called
watchdog timer
The watchdog timer is used to detect if the software has gone to a bad
state (infinite loop, call a function thats not supposed to run, etc.)
If the correct software is running normally, every duration, it will reset
the watchdog timer
If the watchdog timer was not reset, the microcontroller will reboot so the
program has the chance of running correctly
In our code, were not using the watchdog timer mechanism, thats why
we disable it
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // stop the watchdog timer
45

Blinking LED Project


The microcontroller contains several input and output (I/O) ports that
allow transferring data into and out of the microcontroller
Some I/O ports are bidirectional; they can be used as input or output
A bidirectional port usually has a direction register associated with it
The direction register tells if were using the port as input or output
The red LED (called LED1) is associated to Port 1, Pin0 (see next slide)
Therefore, we have to make Port 1, Pin 0 an output so we can write 1 to
it to turn it on
We can also write 0 to it to turn it off (and alternate between the two)

P1DIR |= 0x41; // set the direction register for LED1 and LED2
46

23

3/2/2015

Blinking LED Project


LED1 is assigned to Port 1 pin 0

LED2 is assigned to Port 1 pin 6

47

Blinking LED Project


Port 1 contains 8 bits on the MSP430
They are: <Pin7><Pin6><Pin5><Pin4><Pin3><Pin2><Pin1><Pin0>
Pin 6 is mapped to LED 2 (green LED) and Pin 0 is LED 1 (red LED)
Each pin can be set as input or output separately
The direction register P1DIR is used to set the direction of Port 1 pins
For example, if we write 11111111 (binary) to P1DIR, all the pins in
Port 1 are configured are output pins
This can be done by writing the number in hex:

P1DIR = 0xFF;

Otherwise, we can write 00000000 to P1DIR to set all the pins as input
This can be done with:

P1DIR = 0x00;

P1DIR |= 0x41; // set the direction register for LED1 and LED2
48

24

3/2/2015

Blinking LED Project


In this code, we want to blink LED1 (Port1 Pin0) and make sure LED2
(Port 1, Pin 6) is off
Therefore, well configure Pin0 and Pin6 of Port 1 as outputs
To do this, well make Pin6 and Pin0 in P1DIR equal to 1; the other
pins in P1DIR should be unchanged
We can use the OR operation to set a bit to 1
This mask 0100 0001 used with the OR operation makes bit 6 and bit 0
equal to 1
In C, this can be done with:
P1DIR = P1DIR | 0x41;
This can be written in the short notation in the line below
P1DIR |= 0x41; // set the direction register for LED1 and LED2
49

Blinking LED Project


Before we start the infinite loop, well initialize LED1 and LED2 to off
This will ensure that LED2 will be off
We can send the data on the pins by writing to the register P1OUT
This register also contains 8 bits
We want to make bit 6 and bit 0 of P1OUT equal to 0; the other bits
in this register should be unchanged
This can be done by using an AND operation and the mask: 1011 1110
This is the C code:
P1OUT = P1OUT & 0xBE;
This is the code in short notation:
P1OUT &= 0xBE; // initialize LED1 and LED2 to off
50

25

3/2/2015

Blinking LED Project


To switch LED1 between on and off, we want to switch bit 0 of Port 1
and keep the remaining bits unchanged
Inverting a bit in a word can be done with the XOR operation
This mask 0000 0001 can be used with the XOR operation to switch
LED1 between on and off
Therefore, this is the line of code:
P1OUT = P1OUT ^ 0x01;
This line of code can be written in the short notation below
Well put this line of code in the loop so that the LED alternates
repeatedly between on and off
P1OUT ^= 0x01; // invert the state of LED1

51

Blinking LED Project


The for-loop below doesnt have the usual elements (initializations,
termination condition, incrementation/decrementation)
This is actually an infinite loop
As a result, the LED will blink infinitely
for (;;) {
//empty for loop is an infinite loop
P1OUT ^= 0x01; // invert the state of LED1
for(i=0; i< 20000; i++) // create a delay between toggles
;
// empty statement, do nothing
}

52

26

3/2/2015

Blinking LED Project


Every time we switch the state of the LED, we want to hold it for a small
delay
If the LED switches between on and off as fast as it could, it will appear
to be continually on!
Therefore, we need to introduce a small delay so the cycle will be:
<turn on><small delay><turn off><small delay>
for (;;) {
//empty for loop is an infinite loop
P1OUT ^= 0x01; // invert the state of LED1
for(i=0; i< 20000; i++) // create a delay between toggles
;
// empty statement, do nothing
}

53

Blinking LED Project


The inner for-loop creates a small delay
It makes the counter i count up to 20,000
The body of the loop contains a semi-column, also called an empty
statement
This means the loop will do nothing (it simply creates a small delay)
The delay created here depends on the speed of the microcontroller
If the microcontroller can count up to 20,000 very fast, the delay created
is small
If we want a larger delay, we can replace 20,000 by a larger number
for (;;) {
//empty for loop is an infinite loop
P1OUT ^= 0x01; // invert the state of LED1
for(i=0; i< 20000; i++) // create a delay between toggles
;
// empty statement, do nothing
}

54

27

3/2/2015

Blinking LED Project


Using a count-up code like below to create a delay is a rough estimate
Usually, the microcontroller provides a timer function that can be more
precise and predictable in creating a delay

for (;;) {
//empty for loop is an infinite loop
P1OUT ^= 0x01; // invert the state of LED1
for(i=0; i< 20000; i++) // create a delay between toggles
;
// empty statement, do nothing
}

55

Blinking LED Project


This is the full code
#include <msp430g2553.h> // header for MSP430 G2553 chip
unsigned int i = 0; // counter as a global variable
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // stop the watchdog timer
P1DIR |= 0x41; // set the direction register for LED1 and LED2
P1OUT &= 0xBE; // initialize LED1 and LED2 to off
for (;;) {
P1OUT ^= 0x01;

//empty for loop is an infinite loop


// invert the state of LED1

for(i=0; i< 20000; i++) // create a delay between toggles


;
// empty statement, do nothing
}
}

56

28

3/2/2015

Resources
New to MSP430 webpage on TIs website
http://www.ti.com/lsds/ti/microcontroller/16-bit_msp430/newtomsp430.page

MSP430 Launchpad
http://processors.wiki.ti.com/index.php/MSP430_LaunchPad_%28MSPEXP430G2%29?DCMP=launchpad&HQS=Other+OT+launchpadwiki

MSP-EXP430G2 Out of the box


http://processors.wiki.ti.com/index.php/MSP-EXP430G2_Out_of_the_box

Code Composer Studio for Launchpad (download info)


http://processors.wiki.ti.com/index.php/CCS_for_LaunchPad

You can find more resources on TIs wiki page of MSP430


57

Resources
Additional boards that can be connected to the MSP430 board that were
using
http://processors.wiki.ti.com/index.php/BoosterPacks

58

29

Vous aimerez peut-être aussi