Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016.

Similar presentations


Presentation on theme: "Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016."— Presentation transcript:

1 Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016

2 Announcements & The Plan for Today™  Homework #2 due  Homework #3 due in two weeks  Discuss the Arduino and embedded systems  Discuss the flow control statements, if, for, & while

3 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-to- digital converters, serial communication hardware, etc. http://www.amazon.com/AVR-Pin-20MHz- 32K-ATMega328/dp/B004G5AVS6 ATmega328 the ‘brain’ of the Arduino

4 Where are Microcontrollers Used? Everywhere!  Car  Phone  Toothbrush  Microwave oven  Copier  Television  PC keyboard  Appliances http://ecomodder.com/wiki/index.php/MPGuino

5 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  $13 ‘breadboardable’ FTDI USB chip Digital Pins Analog Pins USB jack Microcontroller power jack Voltage regulator Pwr/GND Pins ICSP Header Reset Button Power LED Pin 13 LED Rx + Tx LEDs http://arduino.cc/

6 Handling the Arduino - How NOT to Do It! Improper Handling - NEVER!!!

7 Handling the Arduino - The Proper Way Proper Handling - by the edges!!!

8 Fundamental Flow of an Arduino Program Start Setup Loop End

9 Programming the Arduino An arduino program == ‘sketch’  Must have: setup() loop()  setup() 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); }

10 Using setup() 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 the world outside the microcontroller determines the voltage applied to the pin  Information is taken in const byte ledPin = 13; // LED on digital pin 13 void setup() { // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); } where can you find out about the commands, etc? http://arduino.cc/en/Reference/Extended pinMode()  sets whether a pin is an input 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 …  ?

11 Digital I/O Example - loop() Algorithm Refine the pseudocode, cont. :  loop forever (use function loop()) If button is not pressed:  voltage on button pin 12 will be _______  make pin 6 voltage low (LED will go off or stay off) If button is pressed:  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); } high (5V) low (0V)

12 /* 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); } 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

13 131211109876543210 SCKMISOMOSISSOC1ICPAIN1AIN0T1T0INT1INT0TXDRXD LED pwm LED0LED1LED2LED3 greenbluered piezo servo SW0SW1SW2SW3 Spartronics Experimenter Digital Pin Assignments

14 76543210 photocellPOTtemp sensor Spartronics Experimenter Analog Pin Assignments

15 Code to Set Up Button Pins Two steps: 1. Make the pin an INPUT pinMode() 2. Turn the pull-up resistor on digitalWrite() a 1 to the pin const byte SW0 = 12; // button SW0 const byte SW1 = 8; // button SW1 const byte SW2 = 7; // button SW2 const byte SW3 = 4; // button SW3 void setup() { pinMode(SW0, INPUT_PULLUP); // make SW0 an // INPUT with a pullup resistor etc. } (See full_test.pde for a more elegant approach to setting up button pins)

16 Boolean Logic And && FalseTrue False TrueFalseTrue Or || FalseTrue False True Exclusive Or (Xor) ^^ FalseTrue False True False

17 Numeric Comparison Operators  ! (not) Changes true to false and false to true , >= Less than, and less than or equal to are different operations Note: !( =  ==, != (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

18 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 e); //false (b e); // true (b e) || (c < e); //true (b e) || (b + c); //true, why? printf(“%f”, b) && (b + c); //true, why?

19 Flow control These Boolean operations are used along with flow control (or branching) statements to control the flow of a program Decisions TrueFalse

20 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.

21 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

22 Single Selection IF - Flowchart TRUE FALSE Speed > 65 connector flow line decision symbol action symbol Print “You’re speeding” The symbol > is a Relational Operator. The Expression “speed > 65” evaluates to 1 if true, 0 if false

23 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

24 Double-Selection IF - Flowchart TRUE Speed > 65 FALSE Print “Over speed limit” Print “Within speed limit”

25 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” ); }

26 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” ); }

27 for Loop Structure – Flow Chart initialization T F Terminal decision statement Iteration operation Initializes the loop control variable: ex. a = 0; Tests the loop control variable to see if it is time to quit looping: ex. a < 5; Increments the loop control variable: ex. ++a

28 for Loop Structure – Flow Chart initialization T F Terminal decision statement Iteration operation for( a = 0; a < 5; ++a ) { //for( initialization, termination, iteration) printf( “%d\n”, a ); }

29 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 statement TRUE FALSE condition

30 while Loop - Flowchart View statement TRUE FALSE condition while( a < 5 ) { printf( “%d\n”, a ); ++a; }

31 References Modular Programming in C http://www.icosaedro.it/c-modules.html http://www.icosaedro.it/c-modules.html math.h http://www.opengroup.org/onlinepubs/007 908799/xsh/math.h.html http://www.opengroup.org/onlinepubs/007 908799/xsh/math.h.html


Download ppt "Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016."

Similar presentations


Ads by Google