Post on 26-Mar-2022
IntroductionATmega328p µC
Language Reference
Micro-controller programming: a case of study
Part I
Daniele Carnevale
June 23, 2010
1 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
List of Contents - Part I
Contents
The Arduino microcontroller board
ATmel AVR-8bit Microcontroller (µC)
Memory, ADC, DAC, Timers...
Programming the µC via Arduino IDE
Language programming
2 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
FTDI FT232RL USB-to-TTLa (5V) Serialchip (FTDI drivers provide a virtual com port)
aTransistor-Transistor Logic.bUniversal asynchronous receiver/transmitter.cIn-Circuit Serial Programming
3 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
4 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
FTDI FT232RL USB-to-TTLa (5V) Serialchip (FTDI drivers provide a virtual com port)
Capacitors, Jack, Voltage regulators,6-20V (recommended 7-12V) → 5V...
aTransistor-Transistor Logic.bUniversal asynchronous receiver/transmitter.cIn-Circuit Serial Programming
5 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
6 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
FTDI FT232RL USB-to-TTLa (5V) Serialchip (FTDI drivers provide a virtual com port)
Capacitors, Jack, Voltage regulators,6-20V (recommended 7-12V) → 5V...
Digital pins, Gnd, Aref, UARTb TTL (5V
serial communication on pins 0-RX and 1-TX)
aTransistor-Transistor Logic.bUniversal asynchronous receiver/transmitter.cIn-Circuit Serial Programming
7 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
8 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
FTDI FT232RL USB-to-TTLa (5V) Serialchip (FTDI drivers provide a virtual com port)
Capacitors, Jack, Voltage regulators,6-20V (recommended 7-12V) → 5V...
Digital pins, Gnd, Aref, UARTb TTL (5V
serial communication on pins 0-RX and 1-TX)
Analog pins and power
aTransistor-Transistor Logic.bUniversal asynchronous receiver/transmitter.cIn-Circuit Serial Programming
9 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
10 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
FTDI FT232RL USB-to-TTLa (5V) Serialchip (FTDI drivers provide a virtual com port)
Capacitors, Jack, Voltage regulators,6-20V (recommended 7-12V) → 5V...
Digital pins, Gnd, Aref, UARTb TTL (5V
serial communication on pins 0-RX and 1-TX)
Analog pins and power
External clock (16MHz), reset, ICSPc
aTransistor-Transistor Logic.bUniversal asynchronous receiver/transmitter.cIn-Circuit Serial Programming
11 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
12 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
FTDI FT232RL USB-to-TTLa (5V) Serialchip (FTDI drivers provide a virtual com port)
Capacitors, Jack, Voltage regulators,6-20V (recommended 7-12V) → 5V...
Digital pins, Gnd, Aref, UARTb TTL (5V
serial communication on pins 0-RX and 1-TX)
Analog pins and power
External clock (16MHz), reset, ICSPc
ATmel328p microcontroller
aTransistor-Transistor Logic.bUniversal asynchronous receiver/transmitter.cIn-Circuit Serial Programming
13 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
14 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board
FTDI FT232RL USB-to-TTLa (5V) Serialchip (FTDI drivers provide a virtual com port)
Capacitors, Jack, Voltage regulators,6-20V (recommended 7-12V) → 5V...
Digital pins, Gnd, Aref, UARTb TTL (5V
serial communication on pins 0-RX and 1-TX)
Analog pins and power
External clock (16MHz), reset, ICSPc
ATmel328p microcontroller
aTransistor-Transistor Logic.bUniversal asynchronous receiver/transmitter.cIn-Circuit Serial Programming
15 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board (cont’d)
From the www.arduino.cc home page:
“Arduino is an open-source electronics prototyping platformbased on flexible, easy-to-use hardware and software. It’sintended for artists, designers, hobbyists, and anyoneinterested in creating interactive objects or environments.”
born to make a device for controlling student-built interactiondesign projects (Bar of Design Institute of Ivrea by MassimoBanzi and collaborators - 2005 )
16 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino Microcontroller board (cont’d)
World
Sensors, actuators,electronic hardware
Continuous anddigital signals
⇐⇒
Arduino Board
Signal wiring andconditioning
power
m
ATmel328p µC
Signal processing andcomputation
17 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino programming
Arduino programming language
Based on open-source Wiring language: a simplified C++“customized” to easily interface with electronics (I/O).
AVR Libc package that provides a subset of the standard Clibrary for Atmel AVR 8-bit RISC microcontrollers
Avr-GCC compiler
18 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino programming
Arduino programming language
Based on open-source Wiring language: a simplified C++“customized” to easily interface with electronics (I/O).
AVR Libc package that provides a subset of the standard Clibrary for Atmel AVR 8-bit RISC microcontrollers
Avr-GCC compiler
19 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino programming
int ledPin = 0; // LED connected to digital pin 0
void setup()
{
pinMode(ledPin, OUTPUT); // set ledPin pin as output
}
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}20 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino programming (cont’d)
Arduino programming IDE
Created with Processing contains a code editor with messagearea, text console and a toolbar to interface with Arduino hardwareand to upload code
21 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino programming (cont’d)
Arduino programming IDE
Created with Processing contains a code editor with messagearea, text console and a toolbar to interface with Arduino hardwareand to upload code
Processing
Processing is an open-source programming language oriented forvisual applications, animation and interaction:
The Processing Development Environment (PDE)
A collection of functions (commands or methods)
A Java-like language syntax
22 / 70
IntroductionATmega328p µC
Language Reference
List of ContentsArduino Microcontroller boardArduino programming overview
Arduino programming (cont’d)
The Arduino IDE allows to:
Create anew/Open/close sketch(source file)
Verify and compile thecode
Upload the code to theonboard-µC’s Flashmemory
Open/Stop the serialmonitor tocommunicate with theµC
23 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: overview
ATmel picoPower Microcontroller ATmega328p :
AVR1-8 bit RISC2 technology, 0-20MHz@1.8V - 5.5V
1The AVR is a modified Harvard architecture 8-bit RISC single chipmicrocontroller developed by Atmel in 1996
2Reduced Instruction Set Computer3Electrically Erasable Programmable Read-Only Memory, it is non-volatile.4Million Instructions Per Second.
24 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: overview
ATmel picoPower Microcontroller ATmega328p :
AVR1-8 bit RISC2 technology, 0-20MHz@1.8V - 5.5V
32KB self-programming Flash Program Memory, 2KB SRAM,1KB EEPROM3
1The AVR is a modified Harvard architecture 8-bit RISC single chipmicrocontroller developed by Atmel in 1996
2Reduced Instruction Set Computer3Electrically Erasable Programmable Read-Only Memory, it is non-volatile.4Million Instructions Per Second.
25 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: overview
ATmel picoPower Microcontroller ATmega328p :
AVR1-8 bit RISC2 technology, 0-20MHz@1.8V - 5.5V
32KB self-programming Flash Program Memory, 2KB SRAM,1KB EEPROM3
8 Channel 10-bit A/D-converter, 14 Digital I/O Pins (ofwhich 6 provide PWM output)
1The AVR is a modified Harvard architecture 8-bit RISC single chipmicrocontroller developed by Atmel in 1996
2Reduced Instruction Set Computer3Electrically Erasable Programmable Read-Only Memory, it is non-volatile.4Million Instructions Per Second.
26 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: overview
ATmel picoPower Microcontroller ATmega328p :
AVR1-8 bit RISC2 technology, 0-20MHz@1.8V - 5.5V
32KB self-programming Flash Program Memory, 2KB SRAM,1KB EEPROM3
8 Channel 10-bit A/D-converter, 14 Digital I/O Pins (ofwhich 6 provide PWM output)
debugWIRE On-chip Debug System
1The AVR is a modified Harvard architecture 8-bit RISC single chipmicrocontroller developed by Atmel in 1996
2Reduced Instruction Set Computer3Electrically Erasable Programmable Read-Only Memory, it is non-volatile.4Million Instructions Per Second.
27 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: overview
ATmel picoPower Microcontroller ATmega328p :
AVR1-8 bit RISC2 technology, 0-20MHz@1.8V - 5.5V
32KB self-programming Flash Program Memory, 2KB SRAM,1KB EEPROM3
8 Channel 10-bit A/D-converter, 14 Digital I/O Pins (ofwhich 6 provide PWM output)
debugWIRE On-chip Debug System
Up to 20 MIPS4 throughput at 20 MHz
1The AVR is a modified Harvard architecture 8-bit RISC single chipmicrocontroller developed by Atmel in 1996
2Reduced Instruction Set Computer3Electrically Erasable Programmable Read-Only Memory, it is non-volatile.4Million Instructions Per Second.
28 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: overview
ATmel picoPower Microcontroller ATmega328p :
AVR1-8 bit RISC2 technology, 0-20MHz@1.8V - 5.5V
32KB self-programming Flash Program Memory, 2KB SRAM,1KB EEPROM3
8 Channel 10-bit A/D-converter, 14 Digital I/O Pins (ofwhich 6 provide PWM output)
debugWIRE On-chip Debug System
Up to 20 MIPS4 throughput at 20 MHz
(on Arduino board) 1/16MHz = 62.5ns per CPU (clock) cycle1The AVR is a modified Harvard architecture 8-bit RISC single chip
microcontroller developed by Atmel in 19962Reduced Instruction Set Computer3Electrically Erasable Programmable Read-Only Memory, it is non-volatile.4Million Instructions Per Second.
29 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
Schematic of ATmega328p
30 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: some features...
Two 8-bit Timer/Counters with Separate Prescaler and CompareMode
31 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: some features...
Two 8-bit Timer/Counters with Separate Prescaler and CompareMode
One 16-bit Timer/Counter with Separate Prescaler, Compare Mode,and Capture Mode
32 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: some features...
Two 8-bit Timer/Counters with Separate Prescaler and CompareMode
One 16-bit Timer/Counter with Separate Prescaler, Compare Mode,and Capture Mode
Real Time Counter with Separate Oscillator
33 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: some features...
6 PWM Channels
34 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: some features...
6 PWM Channels, usually 500 Hz but it can be changed:
//The code to set the PWM frequency
// Set pin 9’s PWM frequency to 3906 Hz (31250/8 = 3906)
// Note that the base frequency for pins 3, 9, 10, and 11 is
// 31250 Hz
setPwmFrequency(9, 8);
// Set pin 6’s PWM frequency to 62500 Hz (62500/1 = 62500)
// Note that the base frequency for pins 5 and 6 is 62500 Hz
setPwmFrequency(6, 1);
// Set pin 10’s PWM frequency to 31 Hz (31250/1024 = 31)
setPwmFrequency(10, 1024);
35 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: some features...
Two 8-bit Timer/Counters with Separate Prescaler and CompareMode
One 16-bit Timer/Counter with Separate Prescaler, Compare Mode,and Capture Mode
Real Time Counter with Separate Oscillator
6 PWM Channels (8 bit-DAC)
6-channel 10-bit ADC (successive approximation). The samplingrequires 12− 260µs (83-3.8KHz).Quantization: [0, 5V ] → [0, 1023] =⇒ 4.9mV per level. In somecase the quantization can be improved attaching the voltage Vref atthe AREF pin and calling analogReference(EXTERNAL), which setsthe voltage range for sampling as [0, Vref ] → [0, 1023].
36 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: some features...
Two 8-bit Timer/Counters with Separate Prescaler and CompareMode
One 16-bit Timer/Counter with Separate Prescaler, Compare Mode,and Capture Mode
Real Time Counter with Separate Oscillator
6 PWM Channels (8 bit-DAC)
6-channel 10-bit ADC
Programmable Serial USART - 2Mb (Universal Syncronous -Asynchronous Receiver - Transmitter) on digital pins 0(RX) and1(TX)
37 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: some features...
Two 8-bit Timer/Counters with Separate Prescaler and CompareMode
One 16-bit Timer/Counter with Separate Prescaler, Compare Mode,and Capture Mode
Real Time Counter with Separate Oscillator
6 PWM Channels (8 bit-DAC)
6-channel 10-bit ADC
Programmable Serial USART - 2Mb
Master/Slave SPI - 8Mb/4Mb (Serial Peripheral Interface) on pins10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK).
38 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: some features...
Two 8-bit Timer/Counters with Separate Prescaler and CompareMode
One 16-bit Timer/Counter with Separate Prescaler, Compare Mode,and Capture Mode
Real Time Counter with Separate Oscillator
6 PWM Channels (8 bit-DAC)
6-channel 10-bit ADC
Programmable Serial USART - 2Mb
Master/Slave SPI - 8Mb/4Mb
Byte-oriented 2-wire Serial Interface ( < 1Mb) , Philips I2C(Integrate to Circuit) compatible on digital pins 4 (SDA) and 5(SCL) using the Wire library.
39 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: some features...
Two 8-bit Timer/Counters with Separate Prescaler and CompareMode
One 16-bit Timer/Counter with Separate Prescaler, Compare Mode,and Capture Mode
Real Time Counter with Separate Oscillator
6 PWM Channels (8 bit-DAC)
6-channel 10-bit ADC
Programmable Serial USART - 2Mb
Master/Slave SPI - 8Mb/4Mb
Byte-oriented 2-wire Serial Interface ( < 1Mb)
Programmable Watchdog Timer with Separate On-chip Oscillatorat 128KHz (see pag. 50 and avr/wdt.h). Used as an internalinterrupt.
40 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
ATmega328p: some features...
Two 8-bit Timer/Counters with Separate Prescaler and CompareMode
One 16-bit Timer/Counter with Separate Prescaler, Compare Mode,and Capture Mode
Real Time Counter with Separate Oscillator
6 PWM Channels (8 bit-DAC)
6-channel 10-bit ADC
Programmable Serial USART - 2Mb
Master/Slave SPI - 8Mb/4Mb
Byte-oriented 2-wire Serial Interface ( < 1Mb)
Programmable Watchdog Timer
External interrupts and Wake-up on digital pin 2 and 3 change.
41 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
The Arduino bootloaderWhen powered-on, the Bootloader runs first determining whetherto re-program or jump to the main application (the newestuploaded sketch). When uploading the new code via Arduinosoftware:
The µC is resetThe pre-loaded Arduino bootloader (2KB of the Flashmemory) runs (Arduino’s buit-in LED blinks)The bootloader starts the initial configuration. If new code isarriving from the serial port it is saved into the Flash memoryThe bootloader runs the newest uploaded sketch
42 / 70
IntroductionATmega328p µC
Language Reference
Overviewbootloader
The Arduino bootloaderWhen powered-on, the Bootloader runs first determining whetherto re-program or jump to the main application (the newestuploaded sketch). When uploading the new code via Arduinosoftware:
The µC is resetThe pre-loaded Arduino bootloader (2KB of the Flashmemory) runs (Arduino’s buit-in LED blinks)The bootloader starts the initial configuration. If new code isarriving from the serial port it is saved into the Flash memoryThe bootloader runs the newest uploaded sketch
Burning the bootloader
Via an AVR-ISP (in-system programmer), USBtinyISP or aParallelProgrammer connected the ICSP pins and the softwareprovided by Arduino IDE.Arduino itself can be programmed as an ISP. 43 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
The main structures
When the bootloader runs thesketch:
setup() function is executedonce
the loop() function isexecuted repeatedly until theµC is powered-off, a reset istriggered, or an interrupt(internal or external) forcesthe ISRa execution: the µCjumps back to execute theloop() code once the ISR isperformed.
aInterrupt service routine.
Bootloader Power on
Initialization: pin settings,
serial communication,
interrupt service routine...
Setup()
Loop()
’Endless’ executed code...ISR
Interrupt
Reset
44 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Example: setup() and loop() functions
int buttonPin = 3;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
// loop checks the button pin each time,
// and will send serial if it is pressed
void loop()
{
if (digitalRead(buttonPin) == HIGH)
serialWrite(’H’);
else
serialWrite(’L’);
delay(1000);
}45 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Control Structures
almost like C...
if...else
for
switch case
while
do... while
break
continue
return
goto
46 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Operators
almost like C...
Aritmetic =,∗, %...
Comparison ==,! = ...
Boolean &&, ||, !
Pointer ⋆, &
Bitwise &, |, <<...
Compound ++, / =, & =, | =...
47 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Variables
almost like C...
Constants HIGH — LOW, INPUT — OUTPUT, true — false,integer and floating point constants
Data types void, boolean, char, unsigned char, byte, int, word,long, double, array, string . . .
Conversion char(), byte(), int(), float() . . .
Qualifiers variable scope, static, volatile, const
Utilities sizeof()
48 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Functions (AVR Libc)
Digital I/O
pinMode(pin,mode)void setup(){ pinMode(ledPin, OUTPUT); // sets the digital pin as}
49 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Functions (AVR Libc)
Digital I/O
pinMode(pin,mode)
digitalWrite(pin, value) [< 4µs → 64 CPU Clocka]digitalWrite(ledPin, HIGH); // sets the LED on
aCPU clock is 1/16MHz= 62.5ns.
50 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Functions (AVR Libc)
Digital I/O
pinMode(pin,mode)
digitalWrite(pin, value) [< 4µs → 64 CPU Clock]
int digitalRead(pin) [< 4µs → 64 CPU Clock]
#define inPin 7 // pushbutton connected to digital pin 7int val = 0; // variable to store the read value
void setup() {pinMode(ledPin, OUTPUT); // sets the digital pin 13 as outputpinMode(inPin, INPUT); // sets the digital pin 7 as input }
void loop(){val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); //sets the LED to the button’s value }
51 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Functions (cont’d)
Analog I/O
analogReference(type) , where type is
DEFAULT : 5VINTERNAL: 1.1V (≥ATmega328p), 2.5V (ATmega8)EXTERNAL: external voltage applied at pin AREF (Vref )
52 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Functions (cont’d)
Analog I/O
analogReference(type) , where type is
int analogRead(pin) [< 260µs → 4160 CPU Clock]
#define inPin 7 // pushbutton connected to digital pin 7int val = 0; // variable to store the read value
void setup(){Serial.begin(9600); // setup serial}
void loop(){val = analogRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button’s value}
53 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Functions (cont’d)
Analog I/O
analogReference(type) , where type is
int analogRead(pin) [< 260µs → 4160 CPU Clock]
analogWrite(pin, value) (PWM - value
255% Duty Cycle) )
[< 12µs → 192 CPU Clock]
int ledPin = 9; // LED connected to digital pin 9int analogPin = 3; // potentiometer at analog pin 3
void setup() {// sets the ledPin as a PWM output (not necessary)pinMode(ledPin, OUTPUT); }
void loop(){analogWrite(ledPin, analogRead(analogPin)/4);
// analogRead values ∈ [0,1023], analogWrite values ∈ [0, 255]}
54 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Functions (cont’d)
Time
unsigned long millis() [< 4µs → 64 CPU Clock]
unsigned long micros() [< 4µs → 64 CPU Clock]
delay(ms)
delayMicroseconds(us)
55 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Functions (cont’d)
Math, Bits and Bytes
min(x,y), max(x,y), abs(x), constraint(x,a,b)...
sin(rad), cos(rad), tan(rad)
randomSeed(seed), random(max), random(min,max)
lowByte(x), highByte(x),bitRead(x,n). . .
56 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Memory manipulation
Flash memory [32KB, 10K read/write]
Specific type of EEPROM storing the bootloader(2KB) and sketch. Use
the PROGMEM instruction to manipulate it.
57 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Memory manipulation
Flash memory [32KB, 10K read/write]
Specific type of EEPROM storing the bootloader(2KB) and sketch. Use
the PROGMEM instruction to manipulate it.
SRAM [2KB]
Is where all the sketch variables are placed.
58 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Memory manipulation
Flash memory [32KB, 10K read/write]
Specific type of EEPROM storing the bootloader(2KB) and sketch. Use
the PROGMEM instruction to manipulate it.
SRAM [2KB]
Is where all the sketch variables are placed.
EEPROM [1KB, 100K read/writes]
Can be used like a µHard Disk using the EEPROM library. Note: each
read/write operation requires about 3.5ms.
59 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Serial communication via RS232
Serial functions
Serial.
begin(bauda), baud= [300, 1200, 2400, 4800, 9600, . . . , 115200]
end()
available()
read()
flush()
print(val, format)
println(val, format)
write()
aSymbols per second or pulses per second.60 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Serial communication: example
int inByte = 0; // for incoming serial data
void setup()
{
Serial.begin(9600);
}
void loop()
{
// read from port 0
if (Serial.available())
{
inByte = Serial.read();
Serial.print(inByte, BYTE);
}
}61 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Serial communication via RS232
Serial communication
Depending on the serial port settings (start bit, parity bit...),usually 10 bits are sent over the port to transmit a byte→ 9600/10 = 960 bytes per second.
62 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Serial communication via RS232
Serial communication
Depending on the serial port settings (start bit, parity bit...),usually 10 bits are sent over the port to transmit a byte→ 9600/10 = 960 bytes per second.
to send 2 bytes of data (e.g. a value measured from ADC)⇒ 960/2 = 480 samples per second (sps)
63 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Serial communication via RS232
Serial communication
Depending on the serial port settings (start bit, parity bit...),usually 10 bits are sent over the port to transmit a byte→ 9600/10 = 960 bytes per second.
to send 2 bytes of data (e.g. a value measured from ADC)⇒ 960/2 = 480 samples per second (sps)
The function Serial.print(val, format) takes its own time...at19200 baud to send 2 bytes data it should require 1/960s,measured 2.5− 3ms. . .
64 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
Interrupts
Re-enabling and disabling interrupts
noInterrupts() disables interrupts
interrupts() re-enable interrupts (normally enabled atpowered-on)
Example:
void setup() {}
void loop(){ noInterrupts();// critical, time-sensitive code hereinterrupts();
// other code here
}
65 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
External Interrupts
attachInterrupt(interrupt, function, mode)
interrupt: 0 (on digital pin 2) and 1 (on digital pin 3)
66 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
External Interrupts
attachInterrupt(interrupt, function, mode)
interrupt: 0 (on digital pin 2) and 1 (on digital pin 3)
function: interrupt service routine (ISR) that must take noparameters and return nothing. Usually it modifies volatilevariables.
67 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
External Interrupts
attachInterrupt(interrupt, function, mode)
interrupt: 0 (on digital pin 2) and 1 (on digital pin 3)
function: interrupt service routine (ISR) that must take noparameters and return nothing. Usually it modifies volatilevariables.
mode:
LOW to trigger the interrupt whenever the pin is low,CHANGE whenever the pin changes valueRISING when the pin goes from low to high,FALLING when the pin goes from high to low.
68 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
External Interrupts
attachInterrupt(interrupt, function, mode)
interrupt: 0 (on digital pin 2) and 1 (on digital pin 3)
function: interrupt service routine (ISR) that must take noparameters and return nothing. Usually it modifies volatilevariables.
mode:
LOW to trigger the interrupt whenever the pin is low,CHANGE whenever the pin changes valueRISING when the pin goes from low to high,FALLING when the pin goes from high to low.
detachInterrupt(interrupt)....
69 / 70
IntroductionATmega328p µC
Language Reference
The main structuresFunctionsMemorySerial communicationInterrupts
attachInterrupt(interrupt, function, mode): example
//A digital signal is attached to the pin 2 (interrupt 0),
//but you do not measure it directly...
int pin = 13;
volatile int state = LOW;
void setup()
{
pinMode(pin, OUTPUT);
attachInterrupt(0, blink, CHANGE);
}
void loop()
{
digitalWrite(pin, state);
}
void blink() //ISR associated to the interrupt 0 (digital pin 2)
{
state = !state;
}70 / 70