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, there’s 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

1

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,
it’s convenient to get a microcontroller chip

4

2

DVD player. washing machine. fuel economy. NEC.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. fridge. • 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 5 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. microwave – All of these use microcontrollers • A car uses around 30 microcontrollers – For engine control. etc. AC system. stability system. Mitsubishi) • Freescale (spun off Motorola) • STMicroelectronics • NXP Semiconductors • Silicon Labs • Zilog 6 3 .

3/2/2015 Texas Instruments MSP430 • The microcontroller that we’ll use is called the ‘Texas Instruments MSP430’ • It’s called TI MSP430. for short 7 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 4 .

ti.com/index.wiki.ti.php/Blink_your_first_LED Code Composer Studio for Launchpad (download info): http://processors. and download it to the MSP430 and run it – This software can be downloaded from TI’s website.wiki.3/2/2015 Blinking LED Project • Let’s 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. it’s also available in some engineering labs at UCF Blinking LED project: http://processors.com/index.php/CCS_for_LaunchPad 9 Blinking LED Project • #1. compile it. 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 you’re working in the lab 10 5 .

we’re specifying the one we’re using If you look at the chip in your launchpad board. 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. Create a new project (specify the chip number) Click on: File  New  CCS Project In the window. in the window below. you’ll se on it the label ‘MSP430G2553’ 12 6 .3/2/2015 Blinking LED Project • • • • #2. we specified which chip we’re using MSP430 comes in many editions Below.

h> // header for MSP430 G2553 chip unsigned int i = 0.) { P1OUT ^= 0x01. do nothing } } 13 Blinking LED Project • After you paste the code in the ‘main’ function. // empty statement. //empty for loop is an infinite loop // invert the state of LED1 for(i=0.3/2/2015 Blinking LED Project • Copy and paste this code in the ‘main’ function #include <msp430g2553. // set the direction register for LED1 and LED2 P1OUT &= 0xBE. click on the ‘Debug’ button. i< 20000. circled in the figure below • This will compile the code and shift to the debug mode • Now. i++) // create a delay between toggles .. we’re ready to download the code to the microcontroller and run it 14 7 . // initialize LED1 and LED2 to off for (. // stop the watchdog timer P1DIR |= 0x41. // counter as a global variable void main(void) { WDTCTL = WDTPW + WDTHOLD.

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.3/2/2015 Blinking LED Project • Now. when you click on the ‘Terminate’ button.com/watch?v=rmMkyIzpvNc 16 8 . our program continues to run on the microcontroller. 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.youtube.

… 17 Decimal Numbers • Let’s look at a number below that’s in base 10 • Each digit position has a weight 3. … P1OUT ^= 0x01.427 18 9 . P1DIR &= 0xBE.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.3/2/2015 Blinking LED Project • Before we go over the code. let’s look at binary and hexadecimal numbers that are used in the code • We’ll 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.

3/2/2015 Binary Numbers • Let’s look at a number below that’s in binary • In binary. these are the weights of a binary number: 1011 1101 128 64 32 16 8 4 2 1 • This is the value of this number: = 128 + 32 + 16 + 8 + 4 + 1 = 189 20 10 . the bits can be only ‘0’ or ‘1’ • Each bit position has a weight 1101 23 22 21 20 8 4 2 1 • This is how we can get the value of this number: = 8 + 4 + 1 = 13 19 Binary Numbers • In general.

a digit can have 16 different values In base 16 a digit can be: 0. 7. A. 3. a bit can have one of two values (0 or 1) In base 16. D. 6. C is 12 and A is 10 12*163 + 2*162 + 10*161 + 7*160 = 12*4096 + 2*256 + 10*16 + 7 = 49. 8. 9. 2. E. 4.3/2/2015 Hexadecimal Numbers • Hexadecimal numbers are in base 16 • • • • In base 10. 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. 5. a digit can have 10 different values (0 to 9) In binary. 1. B.831 22 11 .

the number 26 is larger than 16. we can put ‘0’ in the position of weight 32 • However. 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. so we can put ‘1’ in the position of weight 16 0 1 32 16 8 4 2 1 24 12 . the binary number is: 11010 • Therefore. 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 8 4 2 1 • Let’s convert the number 26 to binary • The number 26 is smaller than 32.3/2/2015 Converting to Binary • To convert a decimal number to binary.

the same as from the method of dividing by 2 0 1 1 0 32 16 8 4 1 2 0 1 25 Converting to Hexadecimal • To convert a decimal number to hexadecimal.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. we get the answer 11010.3/2/2015 Converting to Binary 0 1 32 16 8 4 2 1 • After we put ‘1’ in the position of weight 16.83110 = C2A716 = C2A7 (hex) 26 13 . therefore. we can write: 49. we want to represent 10 • The number 10 is larger than 8. represented by 0 to F) • Example: Convert 49. the bit of weight 2 gets ‘1’ and the bit of weight 1 gets ‘0’ • Therefore. the hex number is: C2A7 • Therefore. so we put ‘1’ in the bit of weight 8 • The remaining part is to represent is 10–8 = 2 • So the bit of weight 4 gets ‘0’. 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. the remaining part to represent is 26–16 = 10 • So. in the remaining bit positions.831 to hexadecimal 49.

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. the leftmost two bits are taken together 28 14 . it will be from 0000 to 1111 • Example: let’s convert C2A7 to binary 1100 0010 1010 0111 27 Converting between Binary and Hexadecimal Converting from binary to hexadecimal: •Starting from the right side. then convert the decimal to hex • However. on four bits. take every four bits and convert them into a hex digit •A group of four bits will range from 0000 to 1111 •They’re assigned values from 0 to F •Example: let’s 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.3/2/2015 Converting between Binary and Hexadecimal • One way to convert from binary to hex: convert the binary to decimal.

B and Y are binary. 30 15 . 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 enter a number in hex by using ‘0x’ before the number • Example: int number = 0x3A.3/2/2015 ‘AND’ Function • Let’s start with the ‘AND’ function • This symbol is called the ‘AND’ gate • It takes two inputs (A. 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 Let’s 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. B) and gives one output (Y) – A.

.. for example: 1a • %X gives the number in upper case. num2. result. 3A & 5E = 1A This is the operation in decimal... result = num1 & num2.\n"). result = num1 & num2.. num2 = 0x5E. num2. result. This is the operation in hexadecimal. for example: 1A int num1. num1. 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.. result). printf("This is the operation in decimal. printf("This is the operation in hexadecimal.\n").. num2. num1. num1 = 0x3A. num1 = 0x3A. result). 58 & 94 = 26 32 16 . num2. 31 ‘AND’ Function • Let’s see the output of this code: int num1.. printf("%d & %d = %d\n". printf("%X & %X = %X\n\n".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. num2 = 0x5E.

we get the same thing 0 • If we AND something with ‘0’.3/2/2015 ‘AND’ Function • Let’s 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’. 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 get 0 1 1 B 0 1 0 1 AND 0 0 0 1 • Let’s 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. we can say that the output of ‘OR’ is ‘1’ if any of the inputs is ‘1’ 34 17 . the AND operation can be used to zero some bits in a word 33 ‘OR’ Function • Another function is the ‘OR’ function that’s implemented by the OR gate • This is the symbol of the OR gate • It takes two inputs (A.

num2. num1 = 0x3A. 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.3/2/2015 ‘OR’ Function • This is an example of the OR operation • Here. result = num1 | num2. num2 = 0x5E. result. the bar ‘|’ operator is used to do an OR operation int num1. 36 18 .

called ‘XOR’ in short • This is the symbol of the XOR gate • It takes two inputs (A. we get the same thing • If we OR something with ‘1’. 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 ‘OR’ Function • Let’s look at the truth table of the OR gate one more time • What does the OR gate do? • If we OR something with ‘0’. the OR operation can be used to make some bits equal to ‘1’ 37 ‘XOR’ Function • Let’s look now at the ‘exclusive OR’ operation. we get 1 A 0 0 1 1 B 0 1 0 1 OR 0 1 1 1 • Let’s 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 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. num2.3/2/2015 ‘XOR’ Function • This is an example of the XOR operation • Here. num2 = 0x5E. 40 20 . result. the hat ‘^’ operator is used to do an XOR operation int num1. num1 = 0x3A. result = num1 ^ num2.

we get the same thing • If we XOR something with ‘1’. the XOR operation can be used to invert some bits in a word 41 Blinking LED Project • Let’s get back to the blinking LED code… 42 21 . it’s inverted A 0 0 1 1 B 0 1 0 1 XOR 0 1 1 0 • Let’s 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.3/2/2015 ‘XOR’ Function • Let’s look at the truth table of the XOR gate one more time • If we XOR something with ‘0’.

i++) // create a delay between toggles . // initialize LED1 and LED2 to off for (. // empty statement.) { //empty for loop is an infinite loop P1OUT ^= 0x01.. do nothing } } 43 Blinking LED Project • The code starts by inserting the ‘msp430g2553’ header file • This file contains addresses on the MSP430 board that we’ll use in the code • In addition. a global variable ‘i’ that’s used in the code is declared #include <msp430g2553. // counter as a global variable void main(void) { WDTCTL = WDTPW + WDTHOLD. // set the direction register for LED1 and LED2 P1OUT &= 0xBE.3/2/2015 Blinking LED Project • We’ll go over the code to see how it’s making the LED blink… #include <msp430g2553. // invert the state of LED1 for(i=0.h> // header for MSP430 G2553 chip unsigned int i = 0. // stop the watchdog timer P1DIR |= 0x41.h> // header for MSP430 G2553 chip unsigned int i = 0. i< 20000. // counter as a global variable 44 22 .

we’re not using the watchdog timer mechanism. // set the direction register for LED1 and LED2 46 23 . etc. they can be used as input or output • A bidirectional port usually has a ‘direction register’ associated with it • The direction register tells if we’re using the port as input or output • The red LED (called LED1) is associated to Port 1. Pin0 (see next slide) • Therefore. 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. // 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. the microcontroller will reboot so the program has the chance of running correctly • In our code. that’s why we disable it void main(void) { WDTCTL = WDTPW + WDTHOLD.) • If the correct software is running normally. it will reset the ‘watchdog timer’ • If the watchdog timer was not reset.3/2/2015 Blinking LED Project • In the ‘main’ function. every duration. 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. call a function that’s not supposed to run.

all the pins in Port 1 are configured are output pins – This can be done by writing the number in hex: P1DIR = 0xFF.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. // set the direction register for LED1 and LED2 48 24 . if we write ‘11111111’ (binary) to P1DIR. we can write ‘00000000’ to P1DIR to set all the pins as input – This can be done with: P1DIR = 0x00. • Otherwise. P1DIR |= 0x41.

we’ll make Pin6 and Pin0 in ‘P1DIR’ equal to ‘1’. // set the direction register for LED1 and LED2 49 Blinking LED Project • Before we start the infinite loop. Pin 6) is off • Therefore. 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. we’ll configure Pin0 and Pin6 of Port 1 as outputs • To do this. this can be done with: P1DIR = P1DIR | 0x41. we want to blink LED1 (Port1 Pin0) and make sure LED2 (Port 1. // initialize LED1 and LED2 to off 50 25 . • This can be written in the short notation in the line below P1DIR |= 0x41.3/2/2015 Blinking LED Project • In this code. 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 is the code in short notation: P1OUT &= 0xBE. we’ll 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’.

• This line of code can be written in the short notation below • We’ll put this line of code in the loop so that the LED alternates repeatedly between on and off P1OUT ^= 0x01. incrementation/decrementation) • This is actually an infinite loop • As a result.3/2/2015 Blinking LED Project • To switch LED1 between on and off. do nothing } 52 26 .. // invert the state of LED1 51 Blinking LED Project • The for-loop below doesn’t have the usual elements (initializations. // empty statement. i++) // create a delay between toggles . i< 20000. this is the line of code: P1OUT = P1OUT ^ 0x01. the LED will blink infinitely for (. // invert the state of LED1 for(i=0.) { //empty for loop is an infinite loop P1OUT ^= 0x01. termination condition. 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.

the delay created is small • If we want a larger delay. 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. // empty statement. // invert the state of LED1 for(i=0.) { //empty for loop is an infinite loop P1OUT ^= 0x01. do nothing } 54 27 . we can replace 20. // empty statement. // invert the state of LED1 for(i=0.) { //empty for loop is an infinite loop P1OUT ^= 0x01. we need to introduce a small delay so the cycle will be: <turn on><small delay><turn off><small delay>… for (. i++) // create a delay between toggles . we want to hold it for a small delay • If the LED switches between on and off as fast as it could. i< 20000.000 very fast... do nothing } 53 Blinking LED Project • The inner for-loop creates a small delay • It makes the counter ‘i’ count up to 20.3/2/2015 Blinking LED Project • Every time we switch the state of the LED.000 by a larger number for (. i< 20000. i++) // create a delay between toggles .000 • The body of the loop contains a ‘semi-column’. it will appear to be continually on! • Therefore.

//empty for loop is an infinite loop // invert the state of LED1 for(i=0. i< 20000. the microcontroller provides a ‘timer’ function that can be more precise and predictable in creating a delay for (.. // empty statement. // counter as a global variable void main(void) { WDTCTL = WDTPW + WDTHOLD. i++) // create a delay between toggles .) { P1OUT ^= 0x01. i< 20000. do nothing } 55 Blinking LED Project • This is the full code #include <msp430g2553. do nothing } } 56 28 ..3/2/2015 Blinking LED Project • Using a count-up code like below to create a delay is a rough estimate • Usually. // initialize LED1 and LED2 to off for (.) { //empty for loop is an infinite loop P1OUT ^= 0x01. // empty statement. // stop the watchdog timer P1DIR |= 0x41.h> // header for MSP430 G2553 chip unsigned int i = 0. // invert the state of LED1 for(i=0. // set the direction register for LED1 and LED2 P1OUT &= 0xBE. i++) // create a delay between toggles .

page • MSP430 Launchpad http://processors.ti.com/index.php/BoosterPacks 58 29 .wiki.wiki.com/index.php/MSP430_LaunchPad_%28MSPEXP430G2%29?DCMP=launchpad&HQS=Other+OT+launchpadwiki • “MSP-EXP430G2 Out of the box” http://processors.php/CCS_for_LaunchPad • You can find more resources on TI’s wiki page of MSP430 57 Resources • Additional boards that can be connected to the MSP430 board that we’re using http://processors.ti.ti.ti.com/index.3/2/2015 Resources • “New to MSP430” webpage on TI’s website http://www.wiki.com/lsds/ti/microcontroller/16-bit_msp430/newtomsp430.php/MSP-EXP430G2_Out_of_the_box • Code Composer Studio for Launchpad (download info) http://processors.com/index.wiki.ti.