lecture 9 microcontrollers pt1

33
Lecture 9: Microcontrollers – part 1 BJ Furman 29OCT2012

Upload: hoang-manh

Post on 15-Dec-2015

18 views

Category:

Documents


4 download

DESCRIPTION

lecture

TRANSCRIPT

Page 1: Lecture 9 Microcontrollers Pt1

Lecture 9: Microcontrollers – part 1

BJ Furman

29OCT2012

Page 2: Lecture 9 Microcontrollers Pt1

The Plan for Today

Microcontrollers for engineering applications What is a microcontroller? How are microcontrollers used? The Arduino hardware platform The Spartronics Experimenter board Programming the Arduino

Basic steps Digital I/O Analog I/O

Page 3: Lecture 9 Microcontrollers Pt1

Learning Objectives Explain what a microcontroller is Explain where microcontrollers are used Describe the Arduino prototyping platform Describe the Spartronics Experimenter board Explain what is meant by a pin being an input

or an output Write programs for the Arduino that can do:

Digital I/O Analog I/O

Page 4: Lecture 9 Microcontrollers Pt1

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

ATmega328the ‘brain’ of the Arduino

Page 5: Lecture 9 Microcontrollers Pt1

Where are Microcontrollers Used?

Everywhere! Car Phone Toothbrush Microwave oven Copier Television PC keyboard Appliances

http://ecomodder.com/wiki/index.php/MPGuino

Page 6: Lecture 9 Microcontrollers Pt1

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’

FTDIUSB chip

Digital Pins

Analog Pins

USBjack

Microcontrollerpowerjack

Voltageregulator

Pwr/GND Pins

ICSPHeader

ResetButton

PowerLED

Pin 13 LEDRx + TxLEDs

http://arduino.cc/

Page 7: Lecture 9 Microcontrollers Pt1

The Spartronics Experimenter Board Momentary SPST

push-button switches Red LEDs Piezo speaker Potentiometer (pot) Temperature sensor Light sensor Dual 7-segment display RGB LED

http://www.sparkfun.com/commerce/images/products/00105-03-L_i_ma.jpg

Cathode

R

GB

speaker

Pot

RGB LED

Light sensor

Dual 7-segment display

Page 8: Lecture 9 Microcontrollers Pt1

Handling the Arduino - How NOT to Do It!

Improper Handling - NEVER!!!

Page 9: Lecture 9 Microcontrollers Pt1

Handling the Arduino - The Proper Way

Proper Handling - by the edges!!!

Page 10: Lecture 9 Microcontrollers Pt1

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

/* Blink - turns on an LED for DELAY_ON msec, then off for DELAY_OFF msec, and repeats*/const byte ledPin = 13; // LED on digital pin 13const int DELAY_ON = 1000;const int DELAY_OFF = 1000;

// setup() method runs once, when the sketch starts

void setup(){ // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); }

// loop() method runs forever,// as long as the Arduino has power

void loop() { digitalWrite(ledPin, HIGH); // set the LED on delay(DELAY_ON); // wait for DELAY_ON msec digitalWrite(ledPin, LOW); // set the LED off delay(DELAY_OFF); // wait for DELAY_OFF msec}

Page 11: Lecture 9 Microcontrollers Pt1

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 12: Lecture 9 Microcontrollers Pt1

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 13: Lecture 9 Microcontrollers Pt1

12 8 7 4

Spartronics Experimenter Button Pinout

Pin and Button map 12 - SW0 8 - SW1 7 - SW2 4 - SW3

How should the associated pins be configured: as INPUTS or as OUTPUTS?

‘Active LOW’ Voltage on pin changes

from 5V to 0V when switch is pressed

Need to turn on internal ‘pull-up’ resistor, so that 5V is supplied to pin

To ATmega328

Page 14: Lecture 9 Microcontrollers Pt1

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 15: Lecture 9 Microcontrollers Pt1

Spartronics Experimenter LED Pinout

Pin and LED map 11 - LED0 (red) 9 - LED1 (red) or RGB (green) 6 - LED2 (red) or RGB (blue) 3 - LED3 (red) or RGB (red) 13 - LED on Arduino

Jumper determines whether pins map to red LEDs or the RGB

11 9 6 3

Page 16: Lecture 9 Microcontrollers Pt1

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

green blue red

piezo

servo

SW0 SW1 SW2 SW3

Spartronics Experimenter Digital Pin Assignments

Page 17: Lecture 9 Microcontrollers Pt1

7 6 5 4 3 2 1 0

photocell POT temp sensor

Spartronics Experimenter Analog Pin Assignments

Page 18: Lecture 9 Microcontrollers Pt1

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 19: Lecture 9 Microcontrollers Pt1

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 20: Lecture 9 Microcontrollers Pt1

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 21: Lecture 9 Microcontrollers Pt1

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 22: Lecture 9 Microcontrollers Pt1

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 23: Lecture 9 Microcontrollers Pt1

/* 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 24: Lecture 9 Microcontrollers Pt1

Comparison of Digital I/O Programs/* 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);}

/* Blue_LED_button_cntrl2 - turns off blue LED when SW0 on Experimenter board is pressed, on 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); digitalWrite(RGB_blue_pin, HIGH);}

/* loop forever */void loop(){ if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, LOW); else digitalWrite(RGB_blue_pin, HIGH);}

Page 25: Lecture 9 Microcontrollers Pt1

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 26: Lecture 9 Microcontrollers Pt1

Effect of Using delay()

Leads to poor (slow) performance as delay time increases

Try to avoid long delays Use millis() instead Check for time exceeding millis() + delay_time Ex. POT_in_Serial_Out.pde

Note also the use of #ifdef for ‘conditional compilation’ Note how roll-over of millis() is handled

Page 27: Lecture 9 Microcontrollers Pt1

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 an analog voltage 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 The average voltage is proportional to ton/tcycle, which is

called the ‘Duty Cycle’ See Lab View PWM_demo.vi

5V

time

Page 28: Lecture 9 Microcontrollers Pt1

30% dutycycle

Front Panel

Block Diagram

Page 29: Lecture 9 Microcontrollers Pt1

Arduino analogWrite( )

analogWrite(pin, value); 0 value 255

0% duty cycle --> 0 V --> analogWrite(pin, 0); 100% duty cycle --> 5 V --> analogWrite(pin, 255);

fade_example.pde (see next page)

Page 30: Lecture 9 Microcontrollers Pt1

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 31: Lecture 9 Microcontrollers Pt1

Review

Page 32: Lecture 9 Microcontrollers Pt1

References

Microcontroller. (2009, November 20). In Wikipedia, the free encyclopedia. Retrieved November 21, 2009, from http://en.wikipedia.org/wiki/Microcontroller

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

Page 33: Lecture 9 Microcontrollers Pt1

Spartronics Experimenter Board