Dean M Aslam
www.egr.msu.edu/~aslam
aslam@msu.edu
Updated: 3-15-15
ECE101
Introduction to Electrical and Computer Engineering
WEEK ?
Experiment # ?: Build Your Own Robot; Microcontroller
Programming & Line Tracker Robot
Contents:
Activities 1-5
Explore More
Link to ECE Research Areas
Link to ECE Courses
Appendices A, B and C
New developments in robotic technologies, dancer & actor robots, nanorobots and mindcontrol robots, make robotics exciting vehicles for learning not only robotic technologies but
also a whole range of related areas including materials, computer and mechanical engineering,
computer science & sensors and actuators. This experiment explores the design, building and
programming of robot using LEGO components.
Acknowledgement: The work reported in this manual was partly supported by the Engineering
Research Centers Program (Wireless Integrated Micro Systems) of the National Science Foundation under
Award Number EEC-9986866 during 2000 - 2010.
www.egr.msu.edu/~aslam
aslam@msu.edu
Experiment: Build Your Own Robot; Microcontroller Programming & Line Tracker
Robot
Learning Objectives:
What is needed?
1. MSP430 LaunchPad (see Fig.1) and
CCS software (comes free with
LaunchPad)
2. PC with CCS installed
3. A breadboard, wires, battery, LEDs
and a UBS cable
4. LEGO motors, cables and other
LEGO pieces
Getting Started
How can I build my own robot? A
very inexpensive (starting under $ 5)
microcontroller programming kit, used in
this experiment, is based on MSP430
family of microcontrollers manufactured
by Texas Instruments. This manual,
prepared for beginners, requires no prior
Fig. 1 MSP430 LaunchPad kit:
http://processors.wiki.ti.com/index.php/MSP430_LaunchPad_(MSPknowledge of programming:
EXP430G2)?DCMP=launchpad&HQS=Other+OT+launchpadwiki.
1. A typical procedure for
microcontroller
Microcontroller: The use of zeros and ones, the binary numbers, to control a
programming is to:
computer is the most fundamental computer language called binary. Assembly language
www.egr.msu.edu/~aslam
aslam@msu.edu
(a)
(b)
www.egr.msu.edu/~aslam
aslam@msu.edu
www.egr.msu.edu/~aslam
aslam@msu.edu
Step 2
C Code: The structure of C code is shown in Fig. 5. The port 1 in MSP430G2211
Now we want to transfer the has 8 programmable I/O ports (pins 2 9). The logic state (0 or 1) of a BIT, assigned to
MSP430G2211 to a breadboard each I/O port, determines whether the output of an I/O port is high or low (see table
and complete the circuit with an below). For example, if the BIT0, called the least significant BIT, is set to 1 (see the
position of 1 in column 3 and row 2), the output of the I/O port 1.0 will be high. The
LED, a resistor and a battery:
C-code statement, in this case will look like this: P 1.0 00000001 or P 1.0 0x01
Be careful when (a) you
remove the microcontroller
from the socket on the
LauchPad and (b) mount it on
the breadboard. Use a special
tool, provided by the
instructor, to remove the
microcontroller
On a breadboard, complete
the circuit, containing MSP430G2211, as shown in
Fig. 7. The connection details inside the breadboard
are shown in Fig. 8(a).
You should carefully note the following: (a) the
location of pin 1 which is connected to the positive
terminal of the battery, (b) resistor (220 K)
connected in series with the green LED (connect the
negative of LED to the ground), and (c) the
connection of a 1 M resistor, called pull-up resistor, Fig. 7 MSP430G2211 pin layout and LED
between the pin 10 and the positive of battery.
circuit
Important Note: Because you will use the
breadboard [Fig. 8(a)] with the attached three-cell battery holder, you will need to remove
one of the AA batteries and replace that with a
dummy metal-wrapped LEGO beam (consult the instructor). This is needed because the
MSP430G2211 needs only 3 Volts that can be supplied by two AA batteries.
If the LED in your circuit blinks, congratulations, you have learned how to program a
microcontroller.
Quiz Question 1: 20 points
Change the blinking frequency of the LED by entering different values for counter (the value
of variable i in Fig. 5) and show it to the lab instructor.
Remove the LED and the resistor. Connect a LEGO RCX motor between pin 2 and ground.
Does the motor run and stop?
www.egr.msu.edu/~aslam
aslam@msu.edu
Fig. 8 MSP430G2211 used to control a motor; (a) breadboard description, (b) circuit on breadboard and (c) circuit
schematic.
If you need to restart the IAR software, make sure that you complete the procedure described
in Figs. 4 and 6.
Use the code provided in Fig. 9.
Download the compiled C-code to MSP430G2211 using the LaunchPad programmer.
Be careful when you transfer the microcontroller from LauchPad to the breadboard.
Connect an RCX motor and
#include <msp430g2211.h>
/// this file identifies the family of
/// microcontroller to be used
connector cables in the circuit
shown in Figure 8 (c).
void main(void )
// Start the Program,
If the RCX motor runs for 5
{
seconds, then you
WDTCTL = WDTPW + WDTHOLD;
// Stop watchdog timer
accomplished activity #2.
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P1OUT = 0x00;
P1DIR |= BIT3;
P1OUT |= BIT3;
__delay_cycles(5000000);
P1OUT &= ~BIT3;
// Calibrates internal
// clock frequency to 1MHz
// Sets Sub-port 1.3 (which is pin #5)
// of the port P1.3 to output direction
// All sub-ports of port 1 are set low.
// Turn Motor On
// Delay 5 second
// Turn Motor Off
www.egr.msu.edu/~aslam
aslam@msu.edu
Disconnect motor connections from the breadboard and get a light sensor module from your
instructor.
Use the information provided in Fig. 10 to connect the sensor module to the breadboard.
As shown in Fig. 10 (c), connect red and black wires to positive and ground, respectively.
Connect the sensor output wire (green) to Pin 9 of the microcontroller that is already on the
breadboard (you will change the microcontroller to MSP430G2231 later).
If you need to restart the IAR software, make sure that you complete the procedure described
in Figs. 4 and 6.
Use the code provided in Fig.12 (C-Code for the Light Sensor) If errors are accruing try to
clear the temp files.
Download the compiled C-code to MSP430G2231 using the LaunchPad programmer.
Be careful when you transfer the microcontroller from LauchPad to the breadboard.
Move the sensor module over black and white areas on a paper and observe what happens.
You should see that, as you move the sensor module over black and white areas, the red LED
on the breadboard, as shown in Fig. 10 (c), will switch on and off. If that happens you are
ready to move on to the next activity.
Fig. 10 Light sensor module interfaced to microcontroller; (a) sensor circuit, (b) system circuit and (c)
complete system using breadboard.
www.egr.msu.edu/~aslam
aslam@msu.edu
but without the MSP430G2231 inserted into the LaunchPad socket as shown in Fig. 11. This
means that you will do in-circuit programming using G2231.
www.egr.msu.edu/~aslam
#include <msp430g2231.h>
void main( )
{
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
unsigned int ADCread =0;
unsigned int cal_data = 0;
aslam@msu.edu
// ADC10ON, interrupt
// enabled, referenced to Vcc
Int I;
// port P1.6 to output direction
for (int i=0; i <= 100; i++)
// Make Counter Loop up to 100 turns
{
ADC10CTL0 |= ENC + ADC10SC;
// Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0 - Low Power Mode 0
sensor_data+=ADC10MEM;
// Add Conversion Results
}
cal_data = sensor_data/100;
// Return calibrated value into the variable.
for(;;)
// Infinite Loop
{
ADC10CTL0 |= ENC + ADC10SC;
__bis_SR_register(CPUOFF + GIE);
ADCread = ADC10MEM;
www.egr.msu.edu/~aslam
aslam@msu.edu
When the front of the sensor module is facing a white surface (approximately 1-2 cm away
from the surface), switch on the battery power. Use a white paper placed near the edge of the
table for better results.
The green LED should turn on (when the sensor faces a white surface) indicating a first
success.
Move the whole system containing the light
sensor away from the edge of the table. The
green LED will switch off but the red LED
will switch on.
Instead of the edge of the table, you can also
use white and black surfaces to see the
switching of red and green LEDs.
If
10
www.egr.msu.edu/~aslam
aslam@msu.edu
Using the robot from activity 5a, complete the circuit shown in Figure 14. In other words,
you are connecting one motor in parallel with the green LED and the other in parallel with
the red LED.
Place the line tracker on the provided black line and switch on the battery power.
The robot will follow the line either to the left or to the right side of the black line. If you
face a problem, get help from the instructor.
Explore More:
C-Programming.Com : http://www.cprogramming.com
MSP430 Official Web Site: http://ti.com/msp430
Control And Embedded Systems: http://www.learn-c.com
11
www.egr.msu.edu/~aslam
Servomechanism
Building and commercialization of inexpensive robots
12
aslam@msu.edu
www.egr.msu.edu/~aslam
aslam@msu.edu
Appendix A:
Control of I/O Ports: In typical microcontroller
architecture, digital input/output (I/O) ports are
controlled by 8-bit data, presented by binary numbers; 0
and 1. For example, the figure shows a byte of data,
where only the bit2 is set to 1 (meaning that bit2 is set to
high).
Pins and Ports of Microcontrollers: Microcontroller
manufactures use numbers or letters to denote the ports.
For example, Texas Instruments uses numbers to label
MSP430F2013 Pin Diagram
the digital ports using the following notation (figure): The ports are
labeled as PORT1.x (x refers to a bit.). In other words the port 1.0, assigned to
microcontroller pin 2,
PORT1.7 PORT1.6 PORT1.5 PORT1.4 PORT1.3 PORT1.2 PORT1.1 PORT1.0
represents port
BIT 7
BIT 6
BIT 5
BIT 4
BIT 3
BIT 2
BIT 1
BIT 0
number 1 and bit
number 0 (see table).
Port Assigned 8-BIT HEX
Setting I/O Ports: Before a port
Comment
Label
BIT
Data Value
is used, it has to be set as an
P1.0 is set to high
P 1.0
BIT0 00000001 0x01
input or output. For any
P1.1 is set to high
P 1.1
BIT1 00000010 0x02
MSP430 family, this is done
P1.2 is set to high
P 1.2
BIT2 00000100 0x04
by PXDIR statement in C
P1.3 is set to high
00001000
0x08
P
1.3
BIT3
programming: PDIR |=
P1.4 is set to high
P 1.4
BIT4 00010000 0x10
00000001 or 0x01.
P1.5 is set to high
00100000
0x20
P
1.5
BIT5
Programming in C: While
P1.6 is set to high
P 1.6
BIT6 01000000 0x40
programming in C, we use hex
P1.7 is set to high
P 1.7
BIT7 10000000 0x80
numbers to reduce possibility of
position error. In the above example, 000000012 refers to 0116. In this case, programmers are
supposed to have basic knowledge of base-n numbers. In C programming, 0x is used to
describe following numbers as hex numbers
To switch a port to on or off to output direction, the same method is followed but PXOUT is
used for this purpose. More information can be found in MSP430-family users manual Digital
I/O Section . Indeed, using binary or hex numbers is not the only way to set the specific bit as
high or low. In the msp430 library file, these hex numbers are assigned to a word `BITX` (X
refers to bit number). This time, lets consider pin12 as an input. This pin corresponds to
PORT2.7. To do this,
P2DIR &= ~BIT7;
C statement is typed (&= AND Operand, ~ Inverter Operand). In this example, an
inverter operand is used to convert BIT7 to low.
13
www.egr.msu.edu/~aslam
Appendix B:
aslam@msu.edu
.equation B1.
10
0.0V
0x000
0.1V
0x022
0.2V
0x044
0.3V
0x066
0.4V
0x088
0.5V
0x0AA
0.6V
0x0CD
0.7V
0x0EF
0.8V
0x111
0.9V
0x133
1.0V
0x155
1.1V
0x177
1.2V
0x199
1.3V
0x1BB
1.4V
0x1DD
1.5V
0x200
2.0V
0x2AA
2.5V
0x355
3.0V
0X1FF
= 1023 x
.equation B2.
For our light sensor, the output voltage range is between 3 volts and 0 volts. So in the ccode, we configured ADC10 register for 3V Hi reference voltage and 0V low voltage
reference. Therefore, the equation becomes
ADCcode =1023 x
.equation B3.
Eventually, we reach the converted value but the result is a decimal number. So the
corresponding binary number can be found after the binary conversion.
Prepared by Prof. Dean Aslam and his GSIs
14
www.egr.msu.edu/~aslam
aslam@msu.edu
Appendix C:
How do we calculate the duration of delay?
Basically, the c-code in activity#2 runs
the motor for 5 seconds and turns off
the motor. For the 5 seconds of delay,
we calibrated the internal clock
Fig.7 Clock Signal of MSP430
frequency to 1MHz and determined the
number of cycles passed without any operation. To do this, we need to calculate how many
cycles will be passed to create 5 seconds delay. As we know from the code, the internal clock
frequency is calibrated to 1MHz. Therefore, we can calculate the number of clock cycles to
plug into delay cycles function. The period of a cycle gives us the time passed when a cycle is
executed. The period can be calculated by,
1 1
Then, we can calculate the number of cycles needed to pass 5 seconds by dividing the total
delay time with the period.
# of Cycles =
15