Академический Документы
Профессиональный Документы
Культура Документы
{
Embedding - SCAC
megaAVR
Microcontrollers
The acronym AVR has been reported to stand
for: Advanced Virtual RISC and also for the
chip's designers: Alf-Egil Bogen and Vegard
Wollan who designed the basic architecture at the
Norwegian Institute of Technology.
RISC stands for reduced instruction set
computer.
https://www.arduino.cc/
An inexpensive and all-included solution
A cross-platform tool chain running on
Windows, OS X, and Linux
A very easy high-level C language and library
that can also tweak the low-level bits and
A totally extensible open source framework
Why C?
C++ is a superset of C.
C++ literally means “increment C”, or perhaps “give
me the next C”.
C++ does everything C does plus a whole lot more.
These extra features don’t come free and embedded
applications usually cannot afford the overhead.
C or C++
Assembly has traditionally been used when
code space and speed are of utmost
importance. Years ago, virtually all embedded
work was done in assembly.
Assembly is processor-specific, unstructured,
not standardized, nor particularly easy to read
or write.
C now offers similar performance
characteristics to assembly but with all the
advantages of a modern structured language
assignGrade(Grade)
getStdDetails ( )
https://arduino.googlecode.com/svn/trunk/libraries/
LiquidCrystal/LiquidCrystal.cpp
LiquidCrystal
Class for manipulating LCD displays
print()
Writes an int, float, byte, char, char[] or a number in
decimal, hexadecimal, octal or binary base to the
display.
clear()
Clears the display screen.
home()
Sets the display cursor at the beginning of the
screen.
setCursor()
Moves the cursor to a specific column, row position.
What is a variable ?
Type Definition Size in memory
void- This particular type is used only in
function declarations and while defining
pointers with unknown types.
Boolean - It stores false or true.
char - It stores single-quoted characters such as
'a' as numbers, following the ASCII chart It is a
signed type and stores numbers from -128 to
127; it can be unsigned and then stores
numbers from 0 to 255.
Data Types
byte - It stores numbers as 8-bit unsigned data that means from 0
to 255.
int -It stores numbers as 2-bytes signed data which means from -
32,768 to 32,767 it can also be unsigned and then store numbers
from 0 to 65,535.
Word- It stores numbers as 2-bytes unsigned data exactly as
unsigned int does.
long - It stores numbers as 4-bytes signed data, which means from
-2,147,483,648 to 2,147,483,647 and can be unsigned and then
stores numbers from 0 to 4,294,967,295.
float - It basically stores numbers with a decimal point from -
3.4028235E + 38 to 3.4028235E + 38 as 4-bytes signed data.
Double - It generally stores float values with a precision two
times greater than the float value.
string - It stores text strings in an array of char where the last
element is null that is a particular character (ASCII code 0).
Contd
Qualifiers are the keywords that are used to
change the processor's behavior considering
the qualified variable.
int myGlobalVariable;
void setup()
{}
void loop()
{
myFunction(digitalPinValue);
}
void myFunction(argument)
{
int aLocalVariable;
aLocalVariable = aLocalVariable + argument;
Static }
The volatile qualifier in a variable declaration
statement makes the variable to be loaded from
the RAM instead of the storage register
memory space of the board.
Loading the variable from the RAM prevents
some possible inconsistencies of variable value.
Volatile
The const qualifier means constant.
Qualifying a variable with const
makes it unvariable
const
Operators
Arduino Interfacing
void setup() {
// put your setup code here, to run once:
void loop() {
// put your main code here, to run repeatedly:
Bare Minimum
Life of Arduino
Blinking LED
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Sketch
Interfacing Switch
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}
Sketch
Usually for robotics application Permanent magnet DC motors (PMDC)
are generally used. PMDC motors have magnets as stator (stationary
part) and windings as the rotor (rotating part). We need to excite (give
supply) the rotor in order to make the motor to rotate.
DC Motors
int motor_forward = 7;
int motor_reverse = 6;
void setup() {
pinMode(motor_forward, OUTPUT);
pinMode(motor_reverse, OUTPUT);
}
void loop() {
digitalWrite(motor_forward,1); //terminal D1 will be HIGH
digitalWrite(motor_reverse,0); //terminal D2 will be LOW
delay(5000); //creates a 5 seconds delay
digitalWrite(motor_forward,0); //terminal D1 will be LOW
digitalWrite(motor_reverse,1); //terminal D2 will be HIGH
delay(5000); //creates a 5 seconds delay
digitalWrite(motor_forward,0); //terminal D1 will be LOW
digitalWrite(motor_reverse,0); //terminal D2 will be LOW
delay(5000); //creates a 5 seconds delay
}
Sketch
Interfacing IR Sensor
const int SPin = 2; // the number of the sensor pin
const int ledPin = 13; // the number of the LED pin
int SenosrState = 0;
if (SensorState == HIGH)
digitalWrite(ledPin, LOW);
else
digitalWrite(ledPin, HIGH);
}
Sketch
Analog Input
int sensorPin = A0; // select the input pin for the
potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming
from the sensor
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin); // read the value from
the sensor:
digitalWrite(ledPin, HIGH); // turn the
ledPin on
delay(sensorValue); // stop the program for <sensorValue>
milliseconds
digitalWrite(ledPin, LOW); // turn the ledPin off:
delay(sensorValue); // stop the program for for <sensorValue>
milliseconds
}
Sketch
Analog Out
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30);
}
Sketch
Libraries are a collection of code
that makes it easy for you to
connect to a sensor, display,
module, etc.
For example, the built-
in LiquidCrystal library makes it
easy to talk to character LCD
displays. There are hundreds of
additional libraries available on
the Internet for download
Libraries
Function
This library allows an Arduino LiquidCrystal()
begin()
board to control Liquid Crystal clear()
displays (LCDs) based on the home()
Hitachi HD44780 (or a compatible) setCursor()
write()
chipset, which is found on most print()
text-based LCDs. The library works cursor()
with in either 4- or 8-bit mode (i.e. noCursor()
blink()
using 4 or 8 data lines in addition to noBlink()
the rs, enable, and, optionally, the display()
rw control lines). noDisplay()
scrollDisplayLeft()
scrollDisplayRight()
autoscroll()
LiquidCrystal noAutoscroll()
leftToRight()
rightToLeft()
createChar()
LCD
#include <LiquidCrystal.h>
void setup() {
lcd.begin(16, 2);
lcd.print("hello, world!");
}
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
}
Sketch
Functions
if (Serial)
available()
availableForWrite()
All Arduino boards have atleast begin()
one serial port (UART/USART) end()
find()
known as Serial used for findUntil()
communication between flush()
arduino board and a computer parseFloat()
or other devices. parseInt()
peek()
print()
println()
read()
readBytes()
readBytesUntil()
Serial readString()
readStringUntil()
setTimeout()
Communication write()
serialEvent()
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the
interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and
rows:
lcd.begin(16, 2);
// initialize the serial communications:
Serial.begin(9600);
}
void loop() {
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
Sketch }
// display each character to the LCD
lcd.write(Serial.read());
}
}
Sensor - Arduino
VCC – 5V
Gnd – Gnd
Trig – 13
Echo - 11
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW); //Set trigger pin low
delayMicroseconds(2000); //Let signal settle
digitalWrite(trigPin, HIGH); //Set trigPin high
delayMicroseconds(15); //Delay in high state
digitalWrite(trigPin, LOW); //ping has now been sent
delayMicroseconds(10); //Delay in low state
Serial.println(targetDistance);
delay(100); //delay tenth of a second to slow things down a little.
}
Wireless Communication
IEEE 802 refers to a family of
IEEE standards dealing with
Local Area Networks and
Metropolitan Area Networks
Bluetooth
Versions
HC-05 module is an easy to use Bluetooth SPP (Serial Port Protocol)
module, designed for transparent wireless serial connection setup.
Serial port Bluetooth module is fully qualified Bluetooth V2.0+EDR
(Enhanced Data Rate) 3Mbps Modulation with complete 2.4GHz radio
transceiver and baseband. It uses CSR Bluecore 04-External single chip
Bluetooth system with CMOS technology and with AFH(Adaptive
Frequency Hopping Feature). It has the footprint as small as
12.7mmx27mm. Hope it will simplify your overall
design/development cycle.
Bluetooth
Bluetooth networks (commonly
referred to as piconets) use a
master/slave model to control
when and where devices can
send data. In this model, a single
master device can be connected
to up to seven different slave
devices. Any slave device in the
piconet can only be connected to
a single master.
Bluetooth Profiles
Bluetooth’s original purpose was to replace RS232 cables
SPP is great for sending bursts of data between two devices
Using SPP, each connected device can send and receive data just as
if there were RX and TX lines connected between them.
void setup(){
Serial.begin(9600);
Serial1.begin(9600);
pinMode(led, OUTPUT);
}
void loop() {
if(Serial1.available()>0)
{
delay(100);
while(Serial1.available()>0)
val = Serial1.read();
Serial.println(val);
if (val == 'A')
digitalWrite(led, HIGH);
if(val == 'B')
digitalWrite(led, LOW);
if(val =='C')
{
digitalWrite(led, HIGH);
Sketch delay(3000);
digitalWrite(led, LOW);
}
}
}
The ESP8266 is a low-cost Wi-Fi chip with full TCP/IP stack and
microcontroller capability produced by Shanghai-based Chinese
manufacturer, Espressif Systems
32-bit RISC CPU: Tensilica Xtensa LX106 running at 80 MHz
64 KiB of instruction RAM, 96 KiB of data RAM
External QSPI flash - 512 KiB to 4 MiB (up to 16MiB is supported)
IEEE 802.11 b/g/n Wi-Fi
Integrated TR switch, balun, LNA, power amplifier and matching
network
WEP or WPA/WPA2 authentication, or open networks
16 GPIO pins
SPI, I²C,
I²S interfaces with DMA (sharing pins with GPIO)
UART on dedicated pins, plus a transmit-only UART can be
enabled on GPIO2
1 10-bit ADC
WiFi
AT Command Processor (Default)
NodeMCU - Lua interpreter onboard
Custom Firmware (using the Arduino IDE)
Espruino
Micropython
Using ESP8266
http://arduino.esp8266.c
om/stable/package_esp8
266com_index.json