DOC PREVIEW
SJSU ME 30 - lecture_9_microcontrollers_pt1

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

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

Unformatted text preview:

Lecture 9 Microcontrollers part 1 BJ Furman 29OCT2012 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 ATmega328 the brain of the Arduino http www amazon com AVR Pin 20MHz32K ATMega328 dp B004G5AVS6 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 Pin 13 LED Digital Pins Power LED USB jack Reset Button FTDI USB chip Voltage regulator Microcontroller power jack 13 breadboardable http arduino cc 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 R G Cathode B http www sparkfun com commerce images products 00105 03 L i ma jpg Pot 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 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 13 const 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 Using setup const byte ledPin 13 LED on digital pin 13 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 the world outside the microcontroller determines the voltage applied to the pin Information is taken in pinMode 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 Which has the value 1 INPUT is a macro where can you find out about the commands etc http arduino cc en Reference Extended Blinking the LED in loop digitalWrite define LED PIN 13 LED on digital pin 13 define DELAY ON 500 in ms define DELAY OFF 100 Causes the voltage on the indicated pin to go HIGH 5V or LOW 0V Note must first configure the pin to be an output void setup initialize the digital pin as an output pinMode LED PIN OUTPUT To make pin go to 5V high void loop digitalWrite pin num HIGH Best to define pin num digitalWrite LED PIN HIGH turn LED on delay DELAY ON wait for DELAY ON ms To make pin go to 0V low digitalWrite LED PIN LOW turn LED off digitalWrite pin num LOW delay DELAY OFF wait for DELAY OFF ms delay Causes the program to wait for a specified time in milliseconds http arduino cc en Reference Extended Spartronics Experimenter Button Pinout To ATmega328 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 12 8 7 4 Pull up Resistor Concept Pull up resistor OFF Pull up resistor ON ATmega328 ATmega328 Pull up resistor VTG 5V 1 PD3 0 VTG 5V 1 PD3 0 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 pull up 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 …


View Full Document

SJSU ME 30 - lecture_9_microcontrollers_pt1

Download lecture_9_microcontrollers_pt1
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 lecture_9_microcontrollers_pt1 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 lecture_9_microcontrollers_pt1 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?