Download presentation
Presentation is loading. Please wait.
Published byCornelia Wilkins Modified over 8 years ago
1
Microcontroller basics Embedded systems for mortals
2
Lesson 2 Repetetion Analog input Interrupts Basic serial
3
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
4
LED with button control
5
Code 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 } Controlling external LED with push button
6
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
7
How A/D conversion works With 10 bits you can have 2 10 = 1024 different values Linearly scaled 0V = 0 | 2,5V = 512 | 5V = 1023
8
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
9
Example 4 - Schematic
10
Example 4 – Breadboard Setup
11
Example 4 – Code 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); } Basic A/D
12
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 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) analogWrite(var1, var2) 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 analogReference is used to change the source of reference voltage for ADC. Var1 defines the source: (DEFAULT, INTERNAL or EXTERNAL) analogReference(var1)
13
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
14
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
15
Example 5 - Schematic
16
Example 5 – Breadboard Setup
17
Example 5 - Code 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); } External interrupt
18
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) Opposite of attachInterrupt. Brakes the connection between external interrupt pin and a function. Var1 is the number of the external interrupt pin. detachInterrupt(var1) IMPORTANT TO NOTICE: These functions only work for external interrupts. Internal interrupts have to be programmed using the registers and basic C -code
19
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
20
Example 7 - Code 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); } Using Serial communication over USB
21
Arduino C – Serial functions Serial.begin(baud, config) Serial.println(”text”) Serial.println(var) Starts the serial communication over the USB. Baud is the desired speed of communication (baud rate) and config is the configuration mode (optional). 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
22
In next lesson Serial communication (deivces) I2C communication SPI communication
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.