Week 11 - Microcontrollers

Week 4:
Microcontrollers & Flow
Control
Bryan Burlingame
22 Feb 2017
Announcements & The Plan for Today™

Homework #2 due in two weeks



Extra week given
Discuss the Arduino and embedded systems
Discuss the flow control statements, if, for, &
while
What is a Microcontroller?

A small computer usually implemented on a
single IC that contains a central processing
unit (CPU), some memory, and peripheral
devices such as counter/timers, analog-todigital converters, serial communication
hardware, etc.
ATmega328
the ‘brain’ of the Arduino
http://www.amazon.com/AVR-Pin-20MHz32K-ATMega328/dp/B004G5AVS6
Where are Microcontrollers Used?

Everywhere!

Car
 Phone
 Toothbrush
 Microwave oven
 Copier
 Television
 PC keyboard
 Appliances
http://ecomodder.com/wiki/index.php/MPGuino

The Arduino Platform


Atmel ATmega328
microcontroller
14 digital I/O pins







6 with PWM
6 analog I/O pins
32 kB (-2 kB)
Flash memory
2 kB RAM
1 kB EEPROM
16 MHz clock
$22 - $30 built

Rx + Tx
LEDs
Pin 13 LED
Digital Pins
Power
LED
USB
jack
Reset
Button
FTDI
USB chip
Voltage
regulator
Microcontroller
power
jack
$13 ‘breadboardable’
http://arduino.cc/
Pwr/GND Pins
Analog Pins
ICSP
Header
Fundamental Flow of an Arduino Program
Start
Setup
Loop
End
Programming the Arduino

An arduino program == ‘sketch’
 Must have:



setup()


setup()
loop()
configures pin modes and
registers
loop()

runs the main body of the
program forever


like while(1) {…}
Where is main() ?


Arduino simplifies things
Does things for you
/* Blue_LED_button_cntrl1 - turns on blue LED
when SW0 on Experimenter board is pressed, off
otherwise
*/
/* pin assignments */
const byte RGB_blue_pin = 6;
const byte SW0_pin = 12;
/* configure pins */
void setup()
{
pinMode(RGB_blue_pin, OUTPUT);
pinMode(SW0_pin, INPUT_PULLUP);
}
/* loop forever */
void loop()
{
if(digitalRead(SW0_pin) == LOW)
digitalWrite(RGB_blue_pin, HIGH);
else
digitalWrite(RGB_blue_pin, LOW);
}
Using setup()

const byte RGB_blue_pin = 6;
const byte SW0_pin = 12;
A digital pin can either be
an output or an input

Output

your program determines
what the voltage on a pin is
(either 0V (LOW or logic 0)
or 5V (HIGH or logic 1)


Information is sent out
Input


Information is taken in
pinMode()



the world outside the
microcontroller determines
the voltage applied to the
pin

/* configure pins */
void setup()
{
pinMode(RGB_blue_pin, OUTPUT);
pinMode(SW0_pin, INPUT_PULLUP);
}

sets whether a pin is an
input, input_pullup or
an output
ledPin  byte constant
assigned the value of 13
OUTPUT is a macro
defined constant
 Which has the value 1
INPUT is a macro …  ?
where can you find out about
the commands, etc?
http://arduino.cc/en/Reference/Extended
Digital I/O Example - loop() Algorithm

Refine the pseudocode, cont.:

loop forever (use function loop())

If button is not pressed:



high (5V)
voltage on button pin 12 will be _______
make pin 6 voltage low (LED will go off or stay off)
If button is pressed:


low (0V)
voltage on button pin 12 will be _______
make pin 6 voltage high (LED will go on or stay on)
void loop()
{
if(digitalRead(SW0_pin) == LOW)
{
digitalWrite(RGB_blue_pin, HIGH);
}
else
{
digitalWrite(RGB_blue_pin, LOW);
}
}
Digital I/O Example - Modification

Modify Arduino
program, so that
LED is on until
button is pressed,
then turns off

How?

Pin assignments?


setup()?
 Need to turn
on the LED!
loop()?
 Swap values
of second
argument in
digitalWrite
calls
/* Blue_LED_button_cntrl1 - turns on blue LED when
SW0 on Experimenter board is pressed, off otherwise
*/
/* pin assignments */
const byte RGB_blue_pin = 6;
const byte SW0_pin = 12;
/* configure pins */
void setup()
{
pinMode(RGB_blue_pin, OUTPUT);
pinMode(SW0_pin, INPUT_PULLUP);
}
/* loop forever */
void loop()
{
if(digitalRead(SW0_pin) == LOW)
digitalWrite(RGB_blue_pin, HIGH);
else
digitalWrite(RGB_blue_pin, LOW);
}
Spartronics Experimenter Digital Pin
Assignments
13
12
11
10
9
8
7
6
5
4
3
2
1
0
SCK
MISO
MOSI
SS
OC1
ICP
AIN1
AIN0
T1
T0
INT1
INT0
TXD
RXD
LED
LED
LED
pwm
pwm
LED0
pwm
pwm
pwm
pwm
LED1
LED2
LED3
green
blue
red
piezo
servo
SW0
SW1
SW2
SW3
Spartronics Experimenter Analog Pin
Assignments
7
6
5
4
3
2
1
0
photocell
POT
temp sensor
Recall: Boolean Logic
And &&
False
True
False
False
False
True
False
True
False
True
False
False
True
True
True
False
False
True
False
False
True
True
True
True
Exclusive Or (Xor) ^^
Or ||
Numeric Comparison

Operators

! (not)


<, <=, >, >=



Changes true to false and false to true
Less than, and less than or equal to are different operations
Note: !(<) is the same as >=
==, != (not equal)


Note: equivalence uses a double ‘=‘, assignment uses a
single ‘=‘, be wary
= returns the value being assigned
Technically, a = b = c = d = 5; is legal. Why?
= is performed right to left, so the d is assigned 5, which returns 5. That 5 is
assigned to c

Examples
float b = 17.0; float d = 3.14;
float c = 20.0; float e = 33.0;
(b < c); //true
(b + c); //true (not zero)
((int)(b/c)); //false (is zero, why?)
(b < c) && (d > e); //false
(b < c) || (d > e); // true
(b < c) && (d > e) || (c < e); //true
(b < c) && (d > e) || (b + c); //true, why?
printf(“%f”, b) && (b + c); //true, why?
Flow control

These Boolean operations are used along
with flow control (or branching)
statements to control the flow of a program
True
Decisions
False
Flow control



if/if else/else – do this, do that, or do the other
switch – choose between a bunch of items
for – Do something for some number of times



also commonly referred to as iteration i.e. iterating
over a range or iterating over a data set
while – For as long as some decision is true,
keep doing some stuff
do .. while – Do something. At the end, if some
thing is true, do it again.
Selection Structure Overview

Three kinds of selections structures

if (also called, ‘single-selection’)

if condition is true
Perform action



if condition is false, action is skipped, program continues
if/else (also called, ‘double-selection’)

if condition is true

Perform action
else (if condition is false)
Perform a different action (this will be skipped if condition is true)
switch (also called ‘multiple-selection’)

Allows selection among many actions depending on the
integral value of a variable or expression
Single Selection IF - Flowchart
connector
flow line
action symbol
decision symbol
Speed > 65
TRUE
Print “You’re
speeding”
FALSE
The symbol > is a Relational Operator. The Expression “speed > 65”
evaluates to 1 if true, 0 if false
if - example
int speed = 5; int b = 4;
if( speed > 65 )
{ // do everything until the closing }
printf( “You are speeding!\n” );
} // technically, when one statement is between
// the curly braces, the braces are optional.
// Even so, don’t omit them
Double-Selection IF - Flowchart
Print “Within
speed limit”
Speed > 65
FALSE
TRUE
Print “Over
speed limit”
if - example
int speed = 5; int b = 4;
if( speed > 65 )
{ // do everything until the closing }
printf( “You are speeding!\n” );
} // technically, when one statement is between
// the curly braces, the braces are optional.
// Even so, don’t omit them
else
{ // note the indentation.
printf( “Speed is within legal limits\n” );
}
if - example
int speed = 5; int b = 4;
if( speed > 65 )
{ // do everything until the closing }
printf( “You are speeding!\n” );
} // technically, when one statement is between
// the curly braces, the braces are optional.
// Even so, don’t omit them
else if( speed < 65 )
{ // note the indentation.
printf( “Speed is within legal limits\n” );
}
else
{
printf( “Speed is precisely 65\n” );
}
for Loop Structure – Flow Chart
initialization
Tests the loop control
variable to see if it is
time to quit looping:
ex. a < 5;
Terminal
decision
Initializes the loop
control variable:
ex. a = 0;
T
statement
Iteration
operation
F
Increments the
loop control
variable:
ex. ++a
for Loop Structure – Flow Chart
initialization
Terminal
decision
T
statement
Iteration
operation
F
for( a = 0; a < 5; ++a )
{ //for( initialization, termination, iteration)
printf( “%d\n”, a );
}
while Loop - Flowchart View

Statement is
executed while
condition is true

Note that the
condition must
first be true in
order for the
statement to
be executed
even once
condition
FALSE
TRUE
statement
while Loop - Flowchart View
while( a < 5 )
{
printf( “%d\n”, a );
++a;
}
condition
FALSE
TRUE
statement
References
Modular Programming in C
http://www.icosaedro.it/c-modules.html
 math.h
http://www.opengroup.org/onlinepubs/007
908799/xsh/math.h.html
