DOC PREVIEW
SJSU ME 30 - Microcontrollers

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Week 11 Microcontrollers BJ Furman 03APR2010 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 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 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 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 Digital Pins Power LED USB jack Voltage regulator 13 breadboardable Pin 13 LED Reset Button FTDI USB chip Microcontroller power jack Pwr GND Pins Analog Pins ICSP Header 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 Dual 7 segment display RGB LED speaker Light sensor Pot R G Cathode B http www sparkfun com commerce images products 00105 03 L i ma jpg Handling the Arduino How NOT to Do It Improper Handling NEVER Handling the Arduino The Proper Way Proper Handling by the edges 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 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 like while 1 Where is main 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 13 const int DELAY ON 1000 const int DELAY OFF 1000 Arduino simplifies things Does things for you 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 Using setup const byte ledPin 13 LED on digital pin 13 Digital pins can either be outputs or inputs 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 taken in pinMode the world outside the microcontroller determines the voltage applied to the pin Information is sent out Input void setup initialize the digital pin as an output pinMode ledPin OUTPUT sets whether a pin is an input or an output ledPin byte constant assigned the value of 13 OUTPUT is a macro defined constant for the value 1 INPUT where can you find out about the commands etc http arduino cc en Reference Extended 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 1 To make pin go to 0V low void loop digitalWrite ledPin HIGH turn LED on delay DELAY ON wait for DELAY ON ms digitalWrite ledPin LOW turn LED off delay DELAY OFF wait for DELAY OFF ms digitalWrite pin 0 delay Causes the program to wait for a specified time in milliseconds http arduino cc en Reference Extended 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 pullup resistor so that 5V is supplied to pin 12 8 7 4 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 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 Code to Set Up Button Pins Two steps 1 Make the pin an INPUT 2 pinMode Turn the pullup resistor on const byte SW0 const byte SW1 const byte SW2 const byte SW3 12 8 7 4 button SW0 button SW1 button SW2 button SW3 void setup pinMode SW0 INPUT make SW0 an INPUT digitalWrite SW0 HIGH turn on pullup resistor etc digitalWrite a 1 to the pin See full test pde for a more elegant approach to setting up button pins 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 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 SW0 pin make it an OUTPUT INPUT 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 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 Arduino Program Arduino program Suppose a change to the specifications LED is on until button pressed then off Contrast mechatronic approach vs nonmechatronic re wire or re program separation of sensing elements from 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


View Full Document
Download Microcontrollers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Microcontrollers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Microcontrollers and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?