Arduino - EECS @ University of Michigan

Download Report

Transcript Arduino - EECS @ University of Michigan

Arduino
John Marcoux
Christopher Lesch
Thomas Dodge
Unless otherwise noted, all information and
pictures came from:
http://www.arduino.cc/
Overview
•
•
•
What is the Arduino?
o Basics
o Product Information
Hardware Overview
o
Arduino Uno
Programming Overview
Simple Example
o Interrupts
o
http://www.robotgear.com.au/Cache/Files/ProductImageOriginals/1122_Arduin
o%20Uno%20-%20with%20quarter.jpg
What is the Arduino?
•
•
•
•
Arduino is a customizable electronic
prototyping platform (microcontroller)
Intended for artists and designers, or anyone
looking to use a microcontroller
Arduino Ethernet
Very easy to use
Many choices
Arduino Mega 2560
Open-Sourced
•
•
All of its software can be downloaded for free, as well
as all of its hardware design files
Website: Explains how to use all of its material,
including many step by step tutorials
Simple things like LEDs, switches
Using Pulse Width Modulation (built in functions)
Interfacing directly with...
 potientometers
 piezoresistors
 accelerometers
 ultrasonic range finders
Easy to Implement (imported)
•
•
•
•
Basics
•
•
•
•
Simple enough to be used by someone with
little to no knowledge of programming or
circuits
Comprehensive enough to incorporate
projects which interface with many different
types of peripherals
Runs cross platform (Windows, Linux, Mac)
Can be powered via USB, or external power
supply (AC to DC adapter or battery)
Example
Hardware
ATmega16U2 MU
ATmega328P MU
ATmega328P MU
ATmega328P MU
ATmega328P MU
ATmega328P MU
ATmega328P MU
ATmega328P MU
ATmega328P MU
Arduino Uno
SmartFusion
Digital I/O Pins
14 (6 PWM)
130
Analog Input Pins
6
24
Flash Memory
32 KB
256 KB
SRAM
2 KB
64 KB (+ more)
EEPROM
1 KB
None
Clock Speed
16 MHz
100 MHz
Cost
~$30.00
$99
Image and information taken from:
http://www.actel.com/documents/SmartFusion_DS.PDF
So Where is Everything?
So Where is Everything?
So Where is Everything?
So Where is Everything?
So Where is Everything?
16MHz Resonator
16 MHz Crystal
So Where is Everything?
Programming Basics
•
•
•
Supports C language in free IDE
Example commands:
o digitalWrite(pin#, HIGH or LOW);
o digitalRead(pin#);
o Serial.println("stuff");
o TIMSK2 |= 1; //enable OVF interrupt for timer 2
Code structure:
o setup{ //code that runs once }
o void loop{ //main code }
Simple Example
void setup() {
Serial.begin(9600);
pinMode(4, OUTPUT);
pinMode(2, INPUT);
}
void loop() {
int sensorValue = digitalRead(2);
Serial.println(sensorValue, DEC);
if(sensorValue)
{
digitalWrite(4, HIGH);
}
else
{
digitalWrite(4, LOW);
}
http://www.youtube.com/watch?v=1Hhr4xX6Ois
&feature=colike
Code from Arduino.cc
Video by TheEquationSlayer (me!)
}
Interrupts
•
•
•
Enabled and masked with sei() and cli()
By default, no nested interrupts
Hardware provides two "external" interrupts
o
o
Only on digital pins 2 and 3
attachInterrupt( 0 or 1 , handler name, trigger mode)
Lower addresses have
higher priority.
Not all 26 interrupts
shown here!
Table 11-1 from official documentation. Available at Arduino.cc
Simple Example with Interrupt
#include <avr/io.h>
#include <avr/interrupt.h>
volatile int sensorValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(4, OUTPUT);
pinMode(2, INPUT); //INT0
void loop()
{
//Wait for voltage change on pin2
}
void INT0_handler()
{
Serial.println("Interrupt INT0!");
sensorValue = digitalRead(2);
if(sensorValue)
digitalWrite(4, HIGH);
else
digitalWrite(4, LOW);
sei(); //Enable all INTs
//Set interrupt. 0 means pin2, 1 means pin3
attachInterrupt(0, INT0_handler, CHANGE);
}
}
Code by Tdodge
Just the Tip of the Iceburg!
•
•
•
•
•
Debouncing?
What if we want more than two interrupts?
How do we use timers?
Download more slides and visit Arduino.cc!
http://tinyurl.com/6qjllky (mediafire)
Questions?
Simple Debouncer
void interruptHandler()
{
unsigned long this_INT = millis();
if(this_INT - last_INT > 300)
{
Serial.println("An INT!");
value = ~value;
digitalWrite(13, value);
}
last_INT = this_INT;
}
By Thomas, although I probably saw this on
a forum at Arduino.cc
•
•
•
•
millis returns milliseconds since
program setup. Takes ~50 days to
overflow!
this_INT stores current time.
last_INT can be a global that
stores when the last interrupt
happened.
If last interrupt was more than
300ms ago, go ahead and perform
interrupt.
Pin Change Interrupts
•
•
Can be mapped to any pin
More complex:
Only 3 address vectors
o Triggered on rising and falling edges
o Handler must sort out pin/type
o
•
Limited libraries
Best to modify registers yourself
o Follow documentation
o
Table from official documentation.
Pin Change Interrupt Example
http://www.youtube.com/watch?v=XWGOVgqt0gw&
feature=colike
Pin Change Interrupt Example
#include <avr/io.h>
#include <avr/interrupt.h>
int ledPin = 8; //pin for LED
int pin4_PCINT2 = 4;
int pin5_PCINT2 = 5;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
pinMode(pin4_PCINT2, INPUT);
pinMode(pin5_PCINT2, INPUT);
sei();
PCICR |= (1 << 2); //enable PCINT2
//declare pins 4&5 as PCINT2
//on any change they will fire PCINT2
PCMSK2 |= (1 << 4);
PCMSK2 |= (1 << 5);
}
ISR(PCINT2_vect)
{
if(digitalRead(pin4_PCINT2))
digitalWrite(ledPin, HIGH);
else if(digitalRead(pin5_PCINT2))
digitalWrite(ledPin, LOW);
}
void loop()
{
//Wait for voltage change
delay(100); //100ms delay
}
Code by thd!
Timers
•
•
•
3 hardware timers with PWM support
Clocked internally or externally
Example: Timer 2
8-bit
o Two compare registers with interrupts
o non-PWM or fast PWM modes
o Read count from TCNT2
o Write compare value to OCR2A or OCR2B
o TIMSK2 devoted to enabling compare interrupts and
overflow interrupts.
o
Timer Example
http://www.youtube.com/watch?v=N_ug38DxTW0&f
eature=colike
Timer Interrupt Example
#include <avr/io.h>
#include <avr/interrupt.h>
int ledPin = 8;
volatile int compAval = 10;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
sei(); //Enable interrupts
TCCR2A |= 3; //Fast PWM mode 3.
TCCR2A |= (3 << 6); //fire INT on Compare match
TCNT2 = 0; //initialize Timer2 to 0
OCR2A = compAval; //Set compare A value
TIMSK2 |= (1 << 1); //Enable CompA interrupt
TIMSK2 |= 1; //Enable OVF interrupt
TCCR2B |= 7; //Clock select. Internal, prescale
1024~64us
}
ISR(TIMER2_COMPA_vect)
{
digitalWrite(ledPin, HIGH);
}
ISR(TIMER2_OVF_vect)
{
digitalWrite(ledPin, LOW);
compAval = compAval + 4;
if(compAval > 254)
compAval = 10;
OCR2A = compAval;
}
void loop()
{
//Serial.println(TCNT2, DEC);
}
Code by Tom D!