Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

Similar presentations


Presentation on theme: "Lecture 10: Peripherals Bryan Burlingame 04 November 2015."— Presentation transcript:

1 Lecture 10: Peripherals Bryan Burlingame 04 November 2015

2 The Plan for Today  Finish discussing variable scope  Discuss peripherals on the Experimenter & the Arduino

3 Announcements Homework due two weeks No office hours tonight No lecture on November 11 (Veteran’s Day)  Normal labs on 5, 6 & 10  Thursday Lab will meet November 12  Take home lab will be assigned on November 11  Friday Labs on November 13 & Tuesday Lab on November 17 are open to all for assistance Midterms back in 2 weeks

4 Passing Arguments into Functions How are the arguments passed into functions?  'Pass by value'  function arguments are expressions  In the function call: Expressions are evaluated and copies of their values are put into temporary memory locations The names of the corresponding parameters in the function definition are made to be the names of the copies  The values of the expressions in the function call are not changed #include double product(double x, double y); int main() { int a = 10; double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double A, double B) { double result; result = A * B; return result; }

5 Identifiers and Scope Identifier  The name of a variable, function, label, etc. int my_var1;/* a variable */ pow_table();/* a function */ start:/* a label */ Question:  Does it make a difference where in a program an identifier is declared? YES! --> concept of ‘scope’

6 Scope of Identifiers Scope of a declaration of an identifier  The region of the program that the declaration is active (i.e., can access the variable, function, label, etc.) Five types of scope:  Program (global scope)  File  Function prototype  Function  Block (“between the { } scope”)

7 Scope of Identifiers - Program (Global) Scope Program (global) scope  if declared outside of all functions  "Visible" to all functions from point of declaration  Visible to functions in other source files  Use only when necessary and then very carefully!!  ex. from Ch var_scope.c #include int a = 10; double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }

8 Function scope  Applies only to labels start: * goto start;  Active from the beginning to the end of a function  Ex. Statement labels in a switch selection structure Scope of Identifiers - Function Scope #include int main() { int user_sel; /* prompt user for entry */ /* get user entry */ switch( user_sel ) { case 1: printf("\n message..."); /* call game function1 here */ break; case 2: printf("\n message..."); /* call game function2 here */ break; default: printf("Error"); break; }

9 Scope of Identifiers - Block Scope Block (local) scope  A block is a series of statements enclosed in braces { }  The identifier scope is active from the point of declaration to the end of the block ( } )  Nested blocks can both declare the same variable name and not interfere  ex. from Ch var_scope_block.c scope_nested_blocks.c #include double product(double x, double y); int main() { int a = 10; double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }

10 Storage Duration How long the identifier exists in memory Static storage class  Identifier exists when program execution begins For variables:  Storage allocated and variable is initialized once  Retains their values throughout the execution of the program #include void just_count(void); /* proto */ int main() { int i; for(i=0;i<10;i++) { just_count(); } return 0; } void just_count(void) { static int count_a; int count_b; count_a = count_a + 1; count_b = count_b + 1; printf("count_a== %d\n", count_a); printf("count_b== %d\n", count_b); } just_count.c

11 For functions:  function name exists when execution begins For variables with global scope:  i.e., declared outside of all functions and uses static keyword  "Visible" to all functions from point of declaration in this source file only  Keeps data ‘private’ to this file only Storage Duration, cont. #include static int a = 10; double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }

12 Recall: Definition of a Microcontroller A microcontroller is an integrated circuit which combines:  Microprocessor  Memory  Input/Output Peripherals Ex: Serial console, digital to analog converters

13 Arduino Peripherals (Uno) Serial (TTL) 6 PWM modules (Digital to Analog (DAC)) 6 ADC (Analog to Digital) SPI bus TWI (I 2 C) bus

14 Experimenter Red/Green/Blue (RGB) Light Emitting Diode (LED) 4 x Red LEDs 4 x Active low switches Servoelectric motor header (servo) Piezo-electric speaker (piezo) Potentiometer (POT) Photocell resistor Temperature sensor

15 131211109876543210 SCKMISOMOSISSOC1ICPAIN1AIN0T1T0INT1INT0TXDRXD LED pwm LED0LED1LED2LED3 redgreenblue piezo servo SW0SW1SW2SW3 Spartronics Experimenter Digital Pin Assignments

16 76543210 photocellPOTtemp sensor Spartronics Experimenter Analog Pin Assignments

17 Digital Signals Most basic form of sending information to the world On or Off (1 or 0)  Active High Logic  Think of a light

18 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 …  ?

19 Blinking the LED in loop() digitalWrite()  Causes the voltage on the indicated pin to go HIGH (+5V) or LOW (0V)  Note: must first configure the pin to be an output To make pin go to 5V (high):  digitalWrite(pin_num,HIGH);  Best to #define pin num. To make pin go to 0V (low):  digitalWrite(pin_num,LOW); delay()  Causes the program to wait for a specified time in milliseconds #define LED_PIN 13 // LED on digital pin 13 #define DELAY_ON 500 // in ms #define DELAY_OFF 100 void setup() { // initialize the digital pin as an output: pinMode(LED_PIN, OUTPUT); } void loop() { digitalWrite(LED_PIN, HIGH); // turn LED on delay(DELAY_ON); // wait for DELAY_ON ms digitalWrite(LED_PIN, LOW); // turn LED off delay(DELAY_OFF); // wait for DELAY_OFF ms } http://arduino.cc/en/Reference/Extended

20 Spartronics Experimenter LED Pinout Pin and LED map  11 - LED0 (red) or RGB (red)  9 - LED1 (red) or RGB (green)  6 - LED2 (red) or RGB (blue)  3 - LED3 (red)  13 - LED on Arduino Jumper determines whether pins map to red LEDs or the RGB 11963

21 Pull-up Resistor Concept ATmega328 PD3 V TG = +5V 0 1 ATmega328 PD3 V TG = +5V 0 1 Pull-up resistor OFFPull-up resistor ON Pull-up resistor

22 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); // make SW0 an INPUT digitalWrite(SW0, HIGH); // turn on pullup resistor etc. } (See full_test.pde for a more elegant approach to setting up button pins)

23 Digital I/O Example - Problem Statement Write a program to turn on the blue of the RGB LED (connected to digital pin 6) when SW0 is pressed (off otherwise)  Pseudocode: define pin assignments configure pins (which are input, which are output) loop forever  if SW0 button is pressed  make pin 6 high  else  make pin 6 low

24 Digital I/O Example - Pin Assignment and Configuration Refine the pseudocode:  define pin assignments const byte RGB_blue_pin = 6; const byte SW0_pin = 12;  configure pins (in function setup()) RGB_blue_pin  make it an _______ SW0_pin  make it an ______ turn on pull-up resistor on SW0 pin  pin will read high (1) until pulled low (0)  see schematic void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT); digitalWrite(SW0_pin, HIGH); } OUTPUT INPUT

25 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)

26 Digital I/O Example - Arduino Program Arduino program Suppose a change to the specifications:  LED is on until button pressed, then off  Contrast mechatronic approach vs. non- mechatronic re-wire, or… re-program the mechatronics approach separates the sensing elements from the control elements /* 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); digitalWrite(SW0_pin, HIGH); } /* loop forever */ void loop() { if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, HIGH); else digitalWrite(RGB_blue_pin, LOW); }

27 /* 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); digitalWrite(SW0_pin, HIGH); } /* 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

28 Practice - 1 Pair up with someone next to you that you do not know Develop an algorithm for:  Finding and printing out the largest of two numbers (3 min) One person work on the pseudocode, the other on a flowchart (1 min) Compare pseudocode and flowchart (3 min) Write out the algorithm in C

29 Serial console Allows the microcontroller to provide text feedback to the programmer

30 Arduino Text Output No screen Serial output  RS232 over USB  Console built into the Arduino IDE

31 Example: Serial Console void setup() { // initialize serial comm at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { Serial.println("Wax on"); delay(100); Serial.println("Wax kinda"); delay(100); Serial.println("Wax off"); delay(100); // place at least a 1 millisecond delay for stability }

32 Analog Signals Real world systems tend to be analog Covers the range from 0V to 5V Recall: Microcontrollers work in binary, so how does this work We approximate!  Step-wise approximation of a continuous function

33 Resolution All analog to digital and digital to analog systems have a minimum detectable change. This is the resolution of the system On these Arduinos  8 BIT DAC (digital to analog conversion) 5V / 2 8  10 BIT ADC (analog to digital conversion) 5V / 2 10

34 Analog In with Serial Out Read the POT NNote: analog voltage! 0 V  0 5 V  1023 Blink an LED at a rate proportional to the pot voltage Output the pot voltage to the serial monitor Initialize with Serial.begin() Map voltage to delay Write a line with Serial.print or Serial.println #define MAX_DELAY_TIME 1000 // max delay in ms #define MIN_DELAY_TIME 10 // min delay in ms #define MAX_POT_VALUE 855 // max pot reading #define MIN_POT_VALUE 0 // min pot reading const byte potPin = 1; // pot output on pin 1 const byte ledPin = 6; // blue LED on pin 6 unsigned int potVoltage = 0; // value of pot voltage unsigned int delay_ms; void setup() { pinMode(ledPin, OUTPUT); pinMode(potPin, INPUT); Serial.begin(9600); // init serial comm at 9600 bps } void loop() { potVoltage = analogRead(potPin); // read pot delay_ms = map(potVoltage,MIN_POT_VALUE,MAX_POT_VALUE,MIN _DELAY_TIME,MAX_DELAY_TIME); Serial.print("sensor = " ); // print to monitor Serial.print(potVoltage); Serial.print(" delay, ms = " ); Serial.println(delay_ms); // print delay and linefeed digitalWrite(ledPin, HIGH); // turn the LED on delay(delay_ms); // wait for delay_ms digitalWrite(ledPin, LOW); // turn the LED off: delay(delay_ms); // wait for delay_ms } POT_input_Serial_Out.pde

35 Analog Out (PWM) Concept No facility exists on most microcontrollers to directly output an analog voltage (i.e., a voltage that varies continuously over the range of 0 to 5V)  Use Pulse Width Modulation (PWM) to approximate Digital outputs are capable of 0V or 5V Over a fraction (t on ) of a time period t cycle, keep pin at 5V, the rest of the time, at 0V  Average voltage is proportional to t on /t cycle, called the ‘Duty Cycle’ 5V time

36 Arduino analogWrite( ) analogWrite (pin, value);  0  value  255 0% duty cycle --> 0 V --> analogWrite (pin, 0); 100% duty cycle --> 5 V --> analogWrite (pin, 255);  Must be a PWM pin

37 131211109876543210 SCKMISOMOSISSOC1ICPAIN1AIN0T1T0INT1INT0TXDRXD LED pwm LED0LED1LED2LED3 redgreenblue piezo servo SW0SW1SW2SW3 Spartronics Experimenter Digital Pin Assignments

38 Analog Output Example Fade the red LED in, then out  duty cycle is incremented then decremented  256 steps 0% to 100% const byte ledPin = 3; // red RGB LED on Experimenter const byte FADE_MAX = 255; // max value for setting duty cycle const byte FADE_INC = 5; // increment for changing duty cycle void setup() { pinMode(ledPin, OUTPUT); } void loop() { int fadeValue; // PWM value // fade in from min to max in increments of 5 points: for(fadeValue = 0 ; fadeValue <= FADE_MAX; fadeValue +=FADE_INC) { analogWrite(ledPin, fadeValue); // sets the value (range from 0 to 255): } // fade out from max to min in increments of 5 points: for(fadeValue = FADE_MAX; fadeValue >= 0; fadeValue -=FADE_INC) { analogWrite(ledPin, fadeValue); // sets the value (range from 0 to 255): } fade_example.pde

39 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 Arduino Home Page. (2009, November 21). Retrieved November 21, 2009, from http://arduino.cc/ http://arduino.cc/


Download ppt "Lecture 10: Peripherals Bryan Burlingame 04 November 2015."

Similar presentations


Ads by Google