Microcontroller Basic, Lesson2 File

Download Report

Transcript Microcontroller Basic, Lesson2 File

Microcontroller basics
Embedded systems for
mortals
Lesson 2
•
•
•
•
Repetetion
Analog input
Interrupts
Basic serial
Repetition
• Make a connection for a LED that can be
controlled with a button
• Code it by yourself
• Don’t use the code you used last time! Try to
do it by yourself
LED with button control
Code
Controlling external LED with push button
byte ledPin = 24; //new ledPin value
byte buttonPin = 12;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
}
void loop()
{
if(digitalRead(buttonPin) == LOW)
{
digitalWrite(ledPin, HIGH); //LED ON
}
else
{
digitalWrite(ledPin, LOW); //LED OFF
}
}
Analog input in Teensy
• Multiple voltages can be read between 0-5V
• Input voltage is compared to a reference
voltage
• By default the reference voltage has to be connected to
AREF-pin
• 10 bit conversion
How A/D conversion works
• With 10 bits you can have 210 = 1024 different
values
• Linearly scaled
• 0V = 0 | 2,5V = 512 | 5V = 1023
Potentiometer
• The resistance value on potentiometer is the
resistance between pins A and B
• Moving the wiper creates a variable voltage
divider between pins A-W-B
• The voltage on any point between pins A and
B can be read from pin W
Example 4 - Schematic
Example 4 – Breadboard Setup
Example 4 – Code
Basic A/D
byte analogPin = 38; //Analog pin F0
byte ledPin = 6;
int value = 0; //can store value from -32,768 to 32,767
void setup()
{
pinMode(analogPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop()
{
value = analogRead(analogPin); //Read analog value
digitalWrite(ledPin, HIGH);
delay(value);
digitalWrite(ledPin, LOW);
delay(value);
}
Arduino C – Analog functions
analogRead(var1)
analogRead function performs an analog reading
operation on a selected pin. Var1 is the number of the
analog pin.
analogWrite(var1, var2)
analogWrite can be used on PWM pins to generated a
square wave. Var1 is the number of the pin and var2 is the
value (0-255)
analogReference(var1)
analogReference is used to change the source of reference
voltage for ADC. Var1 defines the source: (DEFAULT,
INTERNAL or EXTERNAL)
IMPORTANT TO NOTICE:
• These analog functions are generic functions that are supposed to work on all
Arduino boards. The actual effect might not be what you initially expect!
• Using analogRead will force the reference voltage to be whatever has been set with
analogReference -function
Interrupts
• There are both internal and external
interrupts
• Internal interrupts
• Generated by timers, communication protocols, ADC
and many other internal components
• External interrupts
• Generated by changes in certain pins
Interrupts
• When an interrupt is detected the processor
moves directly to execute the code related to
given interrupt
• After the interrupt routine is completed the
processor returns to it’s previous position
before the interrupt occured
Example 5 - Schematic
Example 5 – Breadboard Setup
Example 5 - Code
External interrupt
byte analogPin = 38;
byte ledPin = 6;
byte interruptPin = 0; //External interrupt pin D0
int value = 0; //can store value from -32,768 to 32,767
void setup()
{
pinMode(analogPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(interruptPin, interrupt1, FALLING);
}
void loop()
{
value = analogRead(analogPin); //Read analog value
if(digitalRead(ledPin))
{
delay(value);
digitalWrite(ledPin, LOW);
}
}
void interrupt1()
{
digitalWrite(ledPin, HIGH);
}
Arduino C – Interrupt functions
attachInterrupt(var1,
var2, var3)
attachInterrupt creates a connection with external
interrupt source and a function. Var 1 is the number of
the external interrupt pin, var 2 is the name of the
function to call when interrupt occurs and var3 is the
mode of operation: (LOW, CHANGE, FALLING or RISING)
detachInterrupt(var1)
Opposite of attachInterrupt. Brakes the connection
between external interrupt pin and a function. Var1 is the
number of the external interrupt pin.
IMPORTANT TO NOTICE:
• These functions only work for external interrupts. Internal interrupts have to be
programmed using the registers and basic C -code
Serial communication over USB
• Can be used for multiple things
• Communicating with computer programs
• Debugging
• Requires the use of Serial library
• Serial library is automatically included when
Serial.begin() -function is called
Example 7 - Code
Using Serial communication over USB
byte ledPin = 6;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600); //Begin serial communication with baud rate 9600
}
void loop()
{
digitalWrite(ledPin, HIGH);
Serial.println(“LED ON”); //Send a line of text to serial port
delay(1000);
digitalWrite(ledPin, LOW);
Serial.println(“LED OFF”);
delay(1000);
}
Arduino C – Serial functions
Serial.begin(baud,
config)
Starts the serial communication over the USB. Baud is the
desired speed of communication (baud rate) and config is
the configuration mode (optional).
Serial.println(”text”)
Serial.println(var)
Print a line to the serial port. After printing send a new
line character (“\n”). With quotes it sends text and
without, it prints the contents of the variable.
Serial.print(”text”)
Serial.print(var)
Same as the Serial.println() except it doesn’t send the new
line character.
MORE INFO:
• http://arduino.cc/en/reference/serial
In next lesson
• Serial communication
(deivces)
• I2C communication
• SPI communication