Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

39
Lecture 10: Peripherals Bryan Burlingame 04 November 2015

Transcript of Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

Page 1: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

Lecture 10: Peripherals

Bryan Burlingame

04 November 2015

Page 2: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

the Arduino

Page 3: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

Page 4: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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 <stdio.h>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;

}

Page 5: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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’

Page 6: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

Page 7: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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 <stdio.h>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; }

Page 8: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

Function scope Applies only to labelsstart:

***

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 <stdio.h>

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;}

Page 9: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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 canboth declare the same variable name and not interfere

ex. from Ch var_scope_block.c scope_nested_blocks.c

#include <stdio.h>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; }

Page 10: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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 <stdio.h>

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

Page 11: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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 <stdio.h>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; }

Page 12: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

Page 13: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

Arduino Peripherals (Uno)

Serial (TTL) 6 PWM modules (Digital to Analog (DAC)) 6 ADC (Analog to Digital) SPI bus TWI (I2C) bus

Page 14: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

Page 15: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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 pwm pwm pwm pwm

LED0 LED1 LED2 LED3

red green blue

piezo

servo

SW0 SW1 SW2 SW3

Spartronics Experimenter Digital Pin Assignments

Page 16: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

7 6 5 4 3 2 1 0

photocell POT temp sensor

Spartronics Experimenter Analog Pin Assignments

Page 17: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

Digital Signals

Most basic form of sending information to the world

On or Off (1 or 0) Active High Logic Think of a light

Page 18: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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 aboutthe 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 … ?

Page 19: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

Page 20: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

11 9 6 3

Page 21: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

Pull-up Resistor Concept

ATmega328

PD3

VTG= +5V

0

1

ATmega328

PD3

VTG= +5V

0

1

Pull-up resistor OFF Pull-up resistor ON

Pull-up resistor

Page 22: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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 SW0const byte SW1 = 8; // button SW1const byte SW2 = 7; // button SW2const 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)

Page 23: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

Page 24: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

Page 25: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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)

Page 26: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

Page 27: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

/* 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

Page 28: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

Page 29: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

Serial console

Allows the microcontroller to provide text feedback to the programmer

Page 30: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

Arduino Text Output

No screen Serial output

RS232 over USB Console built into

the Arduino IDE

Page 31: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

Example: Serial Consolevoid 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}

Page 32: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

Page 33: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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 / 28

10 BIT ADC (analog to digital conversion) 5V / 210

Page 34: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

Analog In with Serial Out

Read the POT 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 1const byte ledPin = 6; // blue LED on pin 6unsigned int potVoltage = 0; // value of pot voltageunsigned 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

Page 35: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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 (ton) of a time period tcycle, keep pin

at 5V, the rest of the time, at 0V Average voltage is proportional to ton/tcycle, called the

‘Duty Cycle’

5V

time

Page 36: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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

Page 37: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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 pwm pwm pwm pwm

LED0 LED1 LED2 LED3

red green blue

piezo

servo

SW0 SW1 SW2 SW3

Spartronics Experimenter Digital Pin Assignments

Page 38: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

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 Experimenterconst byte FADE_MAX = 255; // max value for setting duty cycleconst 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

Page 39: Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

References

Modular Programming in C http://www.icosaedro.it/c-modules.html

math.h http://www.opengroup.org/onlinepubs/007908799/xsh/math.h.html

Arduino Home Page. (2009, November 21). Retrieved November 21, 2009, from http://arduino.cc/