0% found this document useful (0 votes)
3 views91 pages

a1_arduino_hw

The document provides an overview of microcontrollers, specifically focusing on Arduino's history, hardware families, and the architecture of the ATmega328P microcontroller. It explains the components and functionalities of microcontrollers, their applications, and the memory architecture used in the ATmega328P. Additionally, it discusses the use of interrupts, timers, and pulse width modulation (PWM) in embedded systems.

Uploaded by

mourad.aziri2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views91 pages

a1_arduino_hw

The document provides an overview of microcontrollers, specifically focusing on Arduino's history, hardware families, and the architecture of the ATmega328P microcontroller. It explains the components and functionalities of microcontrollers, their applications, and the memory architecture used in the ATmega328P. Additionally, it discusses the use of interrupts, timers, and pulse width modulation (PWM) in embedded systems.

Uploaded by

mourad.aziri2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 91

Microcontrollers & Arduino

Francesco de Palma
[email protected]
Arduino History
• The Arduino concept of open-source hardware was developed by the
visionary Arduino team of Massimo Banzi, David Cuartilles,Tom Igoe,
Gianluca Martino, and David Mellis in Ivrea, Italy.
• The team’s goal was to develop a line of easy–to–use microcontroller
hardware and software such that processing power would be readily
available to everyone.

• Main sources:
• Arduino.cc & “Arduino Microcontroller Processing for Everyone!” Steven F.
Barrett & Maurício Féo @ ISOTDAQ 2024 - International School of Trigger
and Data AcQuisition

18/10/2024 F. de Palma 2
Arduino HW families
• Arduino has over the years released over 100 hardware products:
boards, shields, carriers, kits and other accessories. In this page, you
will find an overview of all active Arduino hardware, including the
Nano, MKR and Classic families.
• https://www.arduino.cc/en/hardware
• At their core there is a microcontroller (as the Raspberry Pi Pico
family)

18/10/2024 F. de Palma 3
What is a microcontroller?

Tiny computer
integrated in the
same chip.

All the components of a


normal computer but
integrated on the same
chip:
• CPU
• Memories
• I/O Interfaces
• Etc.
From: Maurício Féo – ISOTDAQ 2024
18/10/2024 F. de Palma 4
What is a microcontroller?
• Tiny computers integrated into a single chip
• CPU, Memories, and Peripherals included.

• Main differences w.r.t. a computer:


• Suitable for embedded applications.
• Low cost (ATtiny9: ~$0.27)
• Low power consumption (PIC16LF: 50μA/MHz or 20nA sleep)
• Reduced clock frequency (~ dozens of MHz)
• Stand-alone devices (Some require only power to work)
• Low-level control of your application.

18/10/2024 F. de Palma
5
What are they used for?
• Monitoring
• (Slow) Data Acqusition
• Control

• Applications where:
• High performance is not required.
• Other devices are inadequate (overkill) due to:
• High power consumption;
• Need of external memories and peripherals;
• Cost;
• Etc.

18/10/2024 F. de Palma 6
Where are they used?
• Everywhere!
• Consumer electronics, home appliances, toys, vehicles, computers,
hobbyist projects, etc.

• According to ARM:Each vehicle on the road contains around a


hundred microcontrollers (MCUs) to operate lower-level functions,
such as electric seats, transmission changes, and range reporting.
• In cell phones there are various microcontrollers integrated with the
main CPU

18/10/2024 F. de Palma 7
Where are they used?

18/10/2024 F. de Palma 8
AVR Architecture Which of the following 8-bit processor
families would you consider for your next
(ATmega328P) embedded project?

Embedded Market Study - April 2023


https://www.embedded.com/embedded-survey/

The one used in arduino uno

18/10/2024 F. de Palma 9
AVR Architecture
(ATmega328P)

• The ATMega328P architecture is based on the Reduced


Instruction Set Computer (RISC, e.g. Advanced RISC Machine ,
ARM) concept designed to simplify the individual instructions
given to the computer to accomplish tasks.
The one used in arduino • The goal is to offset the need to process more instructions by
increasing the speed of each instruction wrt Complex
Instruction Set Computer (CISC e.g. x86)
• This allows the ATmega328p to complete 20 million
instructions per second (MIPS) when operating at 20 MHz!
18/10/2024 F. de Palma 10
AVR Architecture
(ATmega328P)

The one used in arduino uno

18/10/2024 F. de Palma 11
• 8 bits architecture
AVR Architecture • 32kB Flash program memory
(ATmega328P) • 2kB RAM
• 2kB EEPROM
• Max 20MHz
• 6 x PWM
• 6 x ADC channels (10bits)
• 23 I/O pins
• 3 timers (2x8 bits 1x16 bits)
• 1x USART
• 1x SPI
The one used in arduino uno
• 1x TWI (I2C)
• 0.6mA/MHz

18/10/2024 F. de Palma 12
The trend?
32 Bit microcontrollers

https://www.statista.com/statistics/553426/worldwide-
microcontroller-unit-market-size/

Arduino UNO R4
18/10/2024 F. de Palma 13
AVR Architecture
(ATmega328P)
• Memories

18/10/2024 F. de Palma 14
https://docs.arduino.cc/learn/programming/memory-guide/

ATmega328P memory architecture


• The Harvard architecture, named after the Harvard Mark
I relay-based computer, was first introduced in the mid '40s.
• This architecture's main characteristic is that it uses two
separate memory units, one for storing program instructions
and one for storing program data.
• Both memory units in the Harvard architecture are accessed
by the CPU using different communication buses.
• Microcontrollers have a small program and data memory
that needs to be accessed simultaneously-> they mainly use
the Harvard architecture model.
• However, Harvard architecture is not always used in
microcontrollers; some microcontroller families use hybrid
or Von Neumann architecture models.

18/10/2024 F. de Palma 15
Alternative architecture: Von Neumann
• The von Neumann architecture, named after the
mathematician, physicist, and computer scientist John
von Neumann, was first introduced in the mid '40s. It
is also known as the Princeton architecture.
• This architecture stores program data and
instructions in the same memory unit.
• Both are accessed by the CPU using the same
communications bus. Von Neumann's architecture is
fundamental since nearly all digital computers design
have been based on this architecture.
• Modern computing systems use hybrid architectures
models that maximize performance using the best of
both worlds, the von Neumann and the Harvard
models.

18/10/2024 F. de Palma 16
Atmega328 Memory
• All the different memory units inside a microcontroller can be divided
into two main types: RAM and ROM.
• RAM (from Random-Access Memory) in microcontroller-based systems is a
volatile memory used to store temporary data such as the system's firmware
variables.
• ROM (from Read-Only Memory) in microcontroller-based systems is non-
volatile memory used to store permanent data such as the system's firmware.
• The ATmega328 is equipped with three main memory sections the
first 2 are ROM, the last RAM:
1. flash electrically erasable programmable read only memory (EEPROM),
2. and byte-addressable EEPROM for data storage,
3. static random access memory (SRAM),

18/10/2024 F. de Palma 17
Atmega328 Memory
• Bulk programmable flash EEPROM is used to store programs that can be
executed. It can be erased and programmed as a single unit.
• Also, should a program require a large table of constants, it may be
included as a global variable within a program and programmed into flash
EEPROM with the rest of the program.
• Flash EEPROM is nonvolatile meaning memory contents are retained when
microcontroller power is lost.
• The ATmega328 is equipped with 32K bytes of onboard reprogrammable
flash memory.
• This memory component is organized into 16K locations with 16 bits at
each location.

18/10/2024 F. de Palma 18
Atmega328 Memory
• Byte–addressable EEPROM is used to permanently store and recall
variables during program execution.
• It too is nonvolatile.
• It is especially useful for logging system malfunctions and fault data during
program execution.
• It is also useful for storing data that must be retained during a power
failure but might need to be changed periodically.
• Examples where this type of memory is used are found in applications to
store system parameters, electronic lock combinations, and automatic
garage door electronic unlock sequences.
• The ATmega328 is equipped with 1024 bytes of EEPROM.

18/10/2024 F. de Palma 19
Atmega328 Memory A six-transistor CMOS
SRAM cell. , Wiki.
• Static RAM (Random access memory) memory is volatile.
• That is, if the microcontroller loses power, the contents of SRAM memory are lost.
• It can be written to and read from during program execution.
• The ATmega328 is equipped with 2K bytes of SRAM.
• A small portion of the SRAM is set aside for the general-purpose registers used by the processor
and also for the input/output and peripheral subsystems aboard the microcontroller
• During program execution, RAM is used to store global variables, support dynamic memory
allocation of variables, and to provide a location for the stack.
• SRAM is a type of RAM that uses a flip-flop to store one bit of data. Another type of RAM that can
be found in microcontrollers: DRAM (Dynamical RAM, less expensive, need to be periodically
refreshed, usually consisting of a tiny capacitor and a transistor, usually higher data capacitance).
• Commonly used memories in modern PCs are DDR (Double Data Rate, twice each clock) SDRAM
(Synchronous Dynamic RAM, the access is synchronized by the clock). Current best memories are
DDR5.

18/10/2024 F. de Palma 20
Interrupt
Main program
instruction 1
• Interrupts break the program flow to
handle some event. instruction 2
ISR()
instruction 3
• It may be triggered by: inst 1
instruction 4
• Pin change (rise/fall/toggle) inst 2
• Timers / Counters instruction 5
inst 3
• Analog Comparator instruction 6
inst 4
• ADC reading done instruction 7
• Serial interfaces (Rx/Tx done) instruction 8
• It allows the program to handle an event instruction 9
"right after" its occurrence, regardless of • The ATmega328 has 26 interrupt sources.
where the program is and without the • Two of the interrupts are provided for external
need of polling constantly. interrupt sources while the remaining interrupts
support the efficient operation of peripheral
18/10/2024 F. de Palma subsystems aboard the microcontroller. 21
AVR Architecture
(ATmega328P)
• Ports

18/10/2024 F. de Palma 22
Port System ATmega328 28 pins

• The Atmel ATmega328 is equipped with three, 8–bit general purpose,


digital input/output (I/O) ports designated PORTB (8 bits,
PORTB[7:0]), PORTC (8 bits, PORTC[6:0]), and PORTD (8 bits,
PORTD[7:0]).
• Each port has three registers associated with it:
• Data Register PORTx –used to write output data to the port.
• Data Direction Register DDRx – used to set a specific port pin to either output
(1) or input (0).
• Input Pin Address PINx – used to read input data from the port.

18/10/2024 F. de Palma 23
General purpose Input/output (GPIO)
• Pins programmable as Input and
Output
• Read / Write digital signals DDR reg
D
• ‘0’ = 0V (Gnd), ‘1’ = 5V (Vcc) A
T
A PORT reg
• Controlled by 3 registers: Pin
B
• DDR (Data Direction Register) U
• PORT (Where you write when it’s PIN reg
S
output)
• PIN (Where you read when it’s
input)
18/10/2024 F. de Palma 24
Port System

• If selected for input, the pin may be selected for either an input pin or to operate in the
high impedance (Hi–Z) mode.
• With PORTxn in logic zero it is Hi–Z mode, the input appears as high impedance to a particular pin.
This setting is used to is to effectively remove the device's influence from the rest of the circuit.
• If PORTxn is written logic one, the pull-up resistor is activated (used to ensure a known state for a
signal).
• If selected for output, the pin may be further configured for either logic low or logic high
(depending to the rest of the circuit needs).
• Port pins are usually configured at the beginning of a program for either input or output
and their initial values are then set.
• Usually, all eight pins for a given port are configured simultaneously.

18/10/2024 F. de Palma 25
ATmega328 pin out.

Port System

• VCC: Digital supply voltage.


• GND: Ground.
• AVCC is the supply voltage pin for the A/D converter
• AREF is the analog reference pin for the A/D converter.

• From: ATmega328P [DATASHEET]

18/10/2024 F. de Palma 26
AVR Architecture
(ATmega328P)
• Timers / Counters

18/10/2024 F. de Palma 27
Timers / Counters
• Internal registers that increment triggered by:
• A clock source: Timer
• An external event: Counter
• May be used to:
• Measure time
• Raise interruption on:
• Overflow Ultrasonic distance sensor
• Reach a certain value (OCR) Measures distance based on the
• Create waveform time to echo of an ultrasonic
pulse.
• PWM

18/10/2024 F. de Palma 28
Pulse Width Modulation (PWM)
• You can create an output signal which value depends on the status of
the timer.
• Outputs a train of periodic digital pulses with controlled width.
• (Can be used to "mimic" an analog signal)

191
(75%)

75%
25%

18/10/2024 F. de Palma 29
Pulse Width Modulation (PWM)
• You can create an output signal which value depends on the status of
the timer.
• Outputs a train of periodic digital pulses with controlled width.
• (Can be used to "mimic" an analog signal)

18/10/2024 F. de Palma 30
Pulse Width Modulation Channels
• A PWM signal is characterized by a fixed
frequency and a varying duty cycle.
• Duty cycle is the percentage of time a repetitive
signal is logic high during the signal period.
• It may be formally expressed as:

18/10/2024 F. de Palma 31
Pulse Width Modulation Channels
• The ATmega328 is equipped with four pulse width modulation (PWM)
channels.
• The PWM channels coupled with the flexibility of dividing the time
base down to different PWM subsystem clock source frequencies
allows the user to generate a wide variety of PWM signals: from
relatively high frequency low duty cycle signals to relatively low
frequency high duty cycle signals.
• PWM signals are used in a wide variety of applications including
controlling the position of a servo motor and controlling the speed of
a DC motor.

18/10/2024 F. de Palma 32
Pulse Width Modulation Channels
• If you repeat this on-off pattern fast enough with an LED
for example, the result is as if the signal is a steady
voltage between 0 and Vcc controlling the brightness of
the LED.
• The green lines represent a regular time period.
• This duration or period is the inverse of the PWM
frequency.
• In other words, with Arduino's PWM frequency at about
500Hz, the green lines would have a temporal distance
of 2 milliseconds between each other.
• A call to analogWrite() is on a scale of 0 - 255, such that
analogWrite(255) requests a 100% duty cycle (always
on), and analogWrite(127) is a 50% duty cycle (on half
the time) for example.

https://www.arduino.cc/en/Tutorial/Foundations/PWM
18/10/2024 F. de Palma 33
PWM
• The PWM frequency for arduino uno is approximativelly 490 Hz
• Pin 5 and 6 have a frequency of around 980 Hz

• https://www.arduino.cc/reference/it/language/functions/analog-
io/analogwrite/
• https://docs.arduino.cc/tutorials/generic/secrets-of-arduino-pwm

18/10/2024 F. de Palma 35
AVR Architecture
(ATmega328P)
• Comparator & converter

18/10/2024 F. de Palma 36
Analog Comparator
• Tells whether positive pin AIN0 voltage is
higher than negative pin AIN1.
• Output is the internal bit ACO* of reg V 1 > V2 Vout = 1
ACSR*. V1 < V2 Vout = 0
• Can be used to:
• Compare two analog signals
• Trigger a Timer/Counter * ACO = Analog Comparator Output
ACSR = Analog Comp. Control and Status Register
• Trigger an interrupt (rise, fall or toggle)

18/10/2024 F. de Palma 37
Analog to Digital Converter (ADC)
• 10-bit resolution
• 0𝑉𝑉 − 𝑉𝑉𝑟𝑟𝑟𝑟𝑟𝑟 → 0-1023
• 𝑉𝑉𝑟𝑟𝑟𝑟𝑟𝑟 can be:
• Vcc (Power source)
• 1.1V internal ref. (from bandgap)
• External ref. on pin ‘AREF’
• Successive approximation (SAR)
• 13-260 us Conversion time

• An analog voltage between 0 and 5 V will be


encoded into one of 1024 binary representations between 000 16 and 3𝐹𝐹𝐹𝐹 16
with a voltage resolution of approximately 4.88 mV.
• Interrupt on complete
• 6 multiplexed channels on Dual in-line package (DIP) package
• (internal Temp sensor on ch8)
18/10/2024 F. de Palma
38
AVR Architecture
(ATmega328P)
• Serial Interfaces

18/10/2024 F. de Palma 40
ATmega328 Serial Communications
• The ATmega328 is equipped with a host of different serial
communication subsystems including:
• the Universal Synchronous and Asynchronous Serial Receiver and Transmitter
(USART),
• the serial peripheral interface (SPI),
• and the Two–wire Serial Interface.
• What all these systems have in common is the serial transmission of
data.
• In a serial communications transmission, serial data is sent a single bit
at a time from transmitter to receiver.

18/10/2024 F. de Palma 41
Serial Interfaces: USART
Universal Synchronous-Asynchronous Receiver
Transmitter
• A simple protocol
• Widely used to communicate with
PCs due to compability with RS232
protocol. (RS232 is not used anymore in
most PCs but it’s still very easy to find
USB, Universal Serial Bus,-Serial
converters)
• Up to 250kbps
• May trigger interrupts:
• Tx complete
• Rx complete
• Data reg empty
18/10/2024 F. de Palma 42
ATmega328 Serial USART
• The serial USART may be used for full duplex (two way) communication between a receiver and
transmitter.
• This is accomplished by equipping the ATmega328 with independent hardware for the transmitter
and receiver.
• The USART is typically used for asynchronous communication (there is not a common clock
between the transmitter and receiver to keep them synchronized with one another)
• To maintain synchronization between the transmitter and receiver, framing start and stop bits are
used at the beginning and end of each data byte in a transmission sequence.
• The ATmega328 USART has the capability to be set to a variety of data transmission rates known
as the Baud (bits per second) rate.
• The USART may also be set for data bit widths of 5 to 9 bits with one or two stop bits.
• The ATmega328 is equipped with a hardware generated parity bit (even or odd) and parity check
hardware at the receiver. A single parity bit allows for the detection of a single bit error within a
byte of data.
• The USART may also be configured to operate in a synchronous mode.

18/10/2024 F. de Palma 43
Serial Interfaces: SPI
Serial Peripheral Interface
• Differently from the USART, SPI can talk to multiple devices on the
same bus, but needs a Slave Select signal per Slave Device
• Up to 10Mbps! (clk/2)
• Slaves do not "talk" autonomously.
• Must be querried (and clocked) by master.

SD cards
use the SPI serial interface
and can be easily accessed from
a uC.

18/10/2024 F. de Palma 44
ATmega328 Serial Peripheral Interface–SPI
• The ATmega328 Serial Peripheral Interface (SPI) can also be used for two–
way serial communication between a transmitter and a receiver.
• In the SPI system, the transmitter and receiver share a common clock
source.
• This requires an additional clock line between the transmitter and receiver
but allows for higher data transmission rates as compared to the USART.
• The SPI may be viewed as a synchronous 16–bit shift register with an 8–bit
half residing in the transmitter and the other 8–bit half residing in the
receiver.
• The transmitter is designated the master since it is providing the
synchronizing clock source between the transmitter and the receiver.
• The receiver is designated as the slave.

18/10/2024 F. de Palma 45
2
Serial Interfaces: TWI (I C)
Two wire interface (Inter-integrated Circuit)
• I2C allows multiple Masters and Slaves on the same bus. (up to 128)
• Up to 400kbps (on the Atmega328)
• Used in a variety of digital sensors.

18/10/2024 F. de Palma 46
ATmega328 Two–wire Serial Interface–TWI
• The TWI subsystem allows the system designer to network a number
of related devices (microcontrollers, transducers, displays, memory
storage, etc.) together into a system using a two wire interconnecting
scheme.
• Each device has its own unique address and may both transmit and
receive over the two wire bus at frequencies up to 400 kHz.
• This allows the device to freely exchange information with other
devices in the network within a small area.

18/10/2024 F. de Palma 47
AVR Architecture
(ATmega328P)
• Timers

18/10/2024 F. de Palma 48
ATmega328Time Base
• The microcontroller is a complex synchronous state machine.
• It responds to program steps in a sequential manner as dictated by a user–
written program.
• The microcontroller sequences through a predictable fetch–decode–
execute sequence.
• Each unique assembly language program instruction issues a series of
signals to control the microcontroller hardware to accomplish instruction
related operations.
• The speed at which a microcontroller sequences through these actions is
controlled by a precise time base called the clock.
• The clock source is routed throughout the microcontroller to provide a
time base for all peripheral subsystems.

18/10/2024 F. de Palma 49
Watchdog Timer (WDT)
• A Watchdog Timer is a timer clocked by an on-chip oscillator
• Once the counter reaches a certain value, the WDT may:
• Trigger an interrupt
• Reset the microcontroller
• Used to prevent your program from getting stuck in any part of the
code.
• You use it by enabling the WDT and spreading WDT reset instructions
on particular places of your code.
• If it gets stuck in an infinite loop, for ex., the counter won’t be reset and the
microcontroller will be reset.
18/10/2024 F. de Palma 50
Clock circuit
• Up to 20MHz from:
• External clock from a pin
• an external RC network, a ceramic
resonator, or a crystal oscillator
• Low frequency crystal oscillator
(32khz)
• Full Swing Crystal Oscillator (4-
20MHz)
• Internal RC oscillator (7.3-8.1 MHz)

• System Clock Prescaler


• Divides the clock if needed
• Keep in mind:
• Power consumption is proportional
to clock frequency.

18/10/2024 F. de Palma 51
AVR Architecture
(ATmega328P)
• Power and Debug

18/10/2024 F. de Palma 52
Sleep Modes
• There are six Sleep Modes available.
• Each turns off certain parts of the microcontroller to save power and can only be waken up by
certain sources.
• Idle: light sleep. You may need to switch off other components directly via the Power Reduction
Register (PRR) so that the ATmega328P does not wake up unintentionally.
• ADC Noise Reduction: Even in this mode, many functions remain active. The ADC Noise Reduction
Mode, as the name suggests, is also used to reduce noise in analog digital conversions. This allows
a higher resolution to be achieved.
• Power-down: the most energy-saving deep sleep. Only external interrupts, TWI (Two Wire
Interface – > I2C) or the watchdog interrupt can wake up the ATmega328P.
• Power-save: is like the power-down mode, but the Timer2 is still awake and could be operated via
an external clock.
• Stand-by: the system oscillator is still running here. This mode is typically chosen when quick
waking is necessary. Only six cycles are needed. It is important to know that an external quartz
oscillator needs time in the millisecond range to provide a stable system clock.
• Extended Standby: Compared to the stand-by mode, the Timer2 is active.
18/10/2024 F. de Palma 53
Sleep Modes

18/10/2024 F. de Palma 54
Power and Debug
• Brown-Out Detector (BOD)
• Resets the device whenever Vcc is below a certain threshold.
• Power-on Reset (POR)
• Ensures the device is reset from Power On.
• DebugWIRE
• On-chip debug tool from AVR.

18/10/2024 F. de Palma 55
Review

18/10/2024 F. de Palma 56
Atmega328 minimum required circuit
Needed Circuitry: using an external crystal

18/10/2024 F. de Palma 57
Atmega328 minimum required circuit
Needed Circuitry: Using the internal oscillator
• Atmega328 comes with an internal 8MHz oscillator that can minimize
the required circuit to a single battery.

18/10/2024 F. de Palma 58
Usage of Microcontrollers
Development Cycle
• Write your code. From Assembly to C. Or even higher level:
• Compile it. (debug)
• Upload to the uC memory (On Chip debug)
• Parallel programming
• Serial downloading (SPI)
• Self-programming (With bootloader)

• (Burn the fuses)

18/10/2024 F. de Palma 59
Self-Programming: Bootloader
• The AVR core can write to it’s own program memory.
• The Bootloader Section can be locked from rewritting.
• This way developers can allow users to write their
own programs without compromising the bootloader
section.
• This can also be used to ease the programming of the
memory, eliminating the need for an external
programmer.

USB-Serial

18/10/2024 F. de Palma 60
Arduino
• Open-source platform created for
"makers" who have no knowledge in
electronics but still want their creations
to interact with the environment.
• Custom IDE + libraries
• Inexpensive and really easy to use
• (Almost plug and play)
• Huge community all over the world
creating libraries, compatible hardware
and sharing projects
• Stackable addons called shields

18/10/2024 F. de Palma 61
75% start their projects with dev. Boards

Which development boards are you


currently using?

18/10/2024 Embedded Market Study


F. de- April
Palma 2023 62
https://www.embedded.com/embedded-survey/
Setting PWM to 25% Duty Cycle
without the arduino software
25% 75%
Registers:
DDR = Data Direction Register
TTCR0A = Timer/Counter Control Register A
TTCR0B = Timer/Counter Control Register B
OCR0A = Output Compare Register 0 A
Set direction of pin 6 from PORTD to output.
Set WGM to Fast PWM.
Set COM to «Clear On Compare».
Set OCR to 25% (0x3F of 0xFF)
Set the clock source to timer.
Bits:
WGM0[1..0] = Waveform Generator Mode 0
COM0A[1..0] = Compare Output Mode 0 A
CS0[2..0] = Clock Select 0

18/10/2024 F. de Palma 63
Setting PWM to 25% Duty Cycle
With Arduino software
25% 75%

Look at the board which pin you want to use.

25 𝑥𝑥
= Find x.
100 255

18/10/2024 F. de Palma 64
Advantages of Prototyping Platforms
PRO CON

Community / Support / StackOverflow Performance is generally not good

Much easier to learn No full control over the code

Fast development and Prototyping Cost is higher

Portable code between supported devices Not all devices are suported

18/10/2024 F. de Palma 65
Advantages of Prototyping Platforms

http://xkcd.com/979/
18/10/2024 F. de Palma 66
Usage of Microcontrollers
Choosing a microcontroller
• What kind of problem do I have?
• Processing intensive? Power limitation? Embedded?
• Which kind of sensors/actuators will be used?
• Digital/Analog? Voltage levels?
• What are the required peripherals?
• USB? I2C? ADCs? Timers?
• What is the enviroment?
• Space?
• Right next to the LHC beam?
• (Temperature, radiation, etc.)

18/10/2024 F. de Palma
67
18/10/2024 F. de Palma 68
Arduino UNO R3 layout

18/10/2024 F. de Palma 69
Arduino UNO R3 layout

• Working clockwise from the left, the board is equipped with a USB
connector to allow programming the processor from a host PC.
• The board may also be programmed using In System Programming (ISP)
techniques. A 6–pin ISP programming connector is on the opposite side of
the board from the USB connector.
• The board is equipped with a USB–to–serial converter to allow
compatibility between the host PC and the serial communications systems
aboard the ATmega328 processor.
• The UNO R3 is also equipped with several small surface mount LEDs to
indicate serial transmission (TX) and reception (RX) and an extra LED for
project use.
18/10/2024 F. de Palma 70
Arduino UNO R3 layout

• The header strip at the top of the board provides access for an analog
reference signal, pulse width modulation (PWM) signals, digital
input/output (I/O), and serial communications.
• The header strip at the bottom of the board provides analog inputs
for the analog–to–digital (ADC) system and power supply terminals.
• Finally, the external power supply connector is provided at the
bottom left corner of the board.
• The top and bottom header strips conveniently mate with an Arduino
shield to extend the features of the host processor.
18/10/2024 F. de Palma 71
Power supply
• The Arduino processing boards may be powered from the USB port
during project development.
• However, it is highly recommended that an external power supply be
employed.
• This will allow developing projects beyond the limited current
capability of the USB port. Arduino www.arduino.cc recommends a
power supply from 7–12 V DC with a 2.1 mm center positive plug.

18/10/2024 F. de Palma 72
Arduino UNO R3 Open source Schematic
• The entire line of Arduino products is based on the visionary concept
of open source hardware and software.
• That is, hardware and software developments are openly shared
among users to stimulate new ideas and advance the Arduino
concept.
• In keeping with the Arduino concept, the Arduino team openly shares
the schematic of the Arduino UNO R3 processing board.

18/10/2024 F. de Palma 73
Arduino UNO R3 open source schematic

18/10/2024 F. de Palma 74
https://www.arduino.cc/en/uploads/Main/arduino-uno-schematic.pdf
Software
• Download the Arduino software called the Arduino Development
Environment.
• https://www.arduino.cc/en/software

18/10/2024 F. de Palma 75
Embedded Systems, are there alternatives?
• Microcontroller
• FPGA – Field Programmable Gate Array
• DSP – Digital Signal Processor
• SoC – System On a Chip
• Single Board Computer (e.g. Rasberry Pi)
• ASIC – Application Specific Integrated Circuit

18/10/2024 F. de Palma 76
What is a Field Programmable Gate Array ?
• An FPGA is an integrated circuit
• Mostly digital electronics
• An FPGA is programmable in the in the field (=outside the factory), hence
the name “field programmable”
• Circuit design is specified with a hardware description language or schematics
• Tools compute a programming file for the FPGA (bitstream)
• The FPGA is configured with the design (gateware / firmware)
• Your electronic circuit is ready to use
• With an FPGA you can build electronic circuits :
• without using a bread board or soldering iron
• without plugging together NIM modules
• without having a chip produced at a factory
From Hannes Sakulin CERN / EP-CMD slides @ ISOTDAQ 2024
18/10/2024 F. de Palma 77
Basic elements of an FPGA
• extremely flexible: can
connect any block output
to any input
• Fine-grained: 10.000’s up
to millions of logic blocks

18/10/2024 F. de Palma Programmable Input11


/ Output pins
78
Lookup table (LUT)-based Fabrics

AND gate

q = a && b;

OR gate

q = a || b;

18/10/2024 F. de Palma 12 79
Typical LUT-based Logic Cell Xilinx: logic cell,
Altera: logic element

• LUT may implement any function


of the inputs
• Flip-Flop registers the LUT
output
• May use only the LUT or only the
Flip-flop
• LUT may alternatively be
configured a shift register
• Additional elements (not
shown): fast carry logic

18/10/2024 F. de Palma 13 80
General-Purpose Input/Output (GPIO)
• Today: Up to >1000 user
I/O pins Input and / or
output
• Voltages from (1.0), 1.2 ..
3.3 V Many IO standards
• Single-ended: LVTTL,
LVCMOS, …
•…

18/10/2024 F. de Palma 81
Additional elements in an FPGA
• Besides logic cells and interconnect (distributed logic) we have
additional elements in an FPGA:
• Either to provide functions that cannot be implemented with distributed logic
(because the logic would be too slow)
• Clock resources, clock Managers
• Gigabit transceivers
• …
• Or to provide functionality that could also be implemented with distributed
logic, but is more efficiently(*) implemented as a hard macro (in silicon)
• Multipliers, DSPs
• RAM
• Processors (*) smaller chip area, smaller power consumption, faster

18/10/2024 F. de Palma 33 82
Embedded RAM blocks
• Can be used in many ways:
• Look-up of mathematical function
Buffer memory
•…
• Today: Up to ~500 Mbit of RAM

18/10/2024 F. de Palma 83
Digital Signal Processor (DSP)

DSP block (Xilinx 7-series)


Up to several 1000 per chip
8
18/10/2024 F. de Palma
4
Soft and Hard Processor Cores
• Soft core
• Design implemented with the
programmable resources (logic
cells) in the chip
• Could be added after its design
• Hard core
• Processor core that is available in
addition to the programmable
resources
• E.g.: Power PC, ARM

18/10/2024 F. de Palma
FPGA «programming» steps:
• Hardware architecture design. In the case of a System on a Chip
(Soc) FPGA, the hardware-software SoC architecture with memory,
hardware logic, peripherals, and other components.
• Design. This is the process of creating the hardware logic itself,
typically by writing register-transfer logic (RTL) using a hardware
description language (HDL) such as VHDL® or Verilog®. The goal is
to match the functionality of the algorithm while operating on a
continuous stream of data, using fixed-point operations for
efficiency.
• Verification. This step ensures that the design works as intended
before FPGA programming. This can be as simple as a VHDL
testbench or Verilog testbench; commercial projects typically use a
methodology such as the Universal Verification Methodology
(UVM).
• Synthesis. This technology transforms the RTL to digital logic gates
and attempts to meet your register-to-register clock frequency
goals while minimizing use of the resources on the FPGA.

https://it.mathworks.com/discovery/fpga-programming.html
18/10/2024 F. de Palma 86
FPGA «programming» steps:
• Integration. An FPGA contains a lot of dedicated resources
already—the pins, the clock signal, input/output processing
such as analog-to-digital converters (ADC), and interfaces to
off-chip memory and other devices on the board. An SoC
FPGA also has dedicated registers that both the hardware
and software can use to communicate with each other. Your
design will need to plug into this “reference design.”
• Implementation. This is the process of determining which
physical resources on the FPGA to program with which logic,
and how to connect (route) them. This produces the
bitstream that is loaded onto the device for FPGA
programming.
• Lab testing and debug. After FPGA programming, you can
run using real input or test input. The first few tries often
involve figuring out why it does not work and how to fix it.
Most of the time this is due to problems in the design step
that were not identified in the verification step.

https://it.mathworks.com/discovery/fpga-programming.html
18/10/2024 F. de Palma 87
Current FPGA Technologies https://doi.org/10.1007/978-3-031-01771-1_2

TYPE PRO CONS


SRAM-based FPGAs • field programmable and • They often require external boot devices;
reprogrammable. • volatile and may not hold configuration if a power
• It is mostly used and improved. glitch occurs.
• more substantial routing delays, require more power,
and have a higher susceptibility to bit errors.
Antifuse FPGAs • non-volatile • a relatively more complicated fabrication process
• minimal delay due to routing, • only one-time programmable.
resulting in a faster speed and
lower power consumption.
• Rad-tolerant secure
Flash-based FPGAs • non-volatile • that runtime reconfiguration is not recommended
• reprogrammable (but not all can due to the potentially destructive results if radiation
be in-system reprogrammed), effects occur during the reconfiguration process.
• low power consumption and route • The stability of stored charge on the floating gate can
delay. be influenced by temperature and E.M. fields
18/10/2024 F. de Palma 88
How are FPGAs used in physics experiments?
• First level trigger:
• They are fast:
• Much faster than discrete electronics (shorter connections)
• Many inputs:
• Data from many parts of the detector has to be combined
• All operations are performed in parallel:
• Can build pipelined logic
• They can be re-programmed:
• Trigger algorithms can be optimized
• In data acquisition:
• Frontend electronics:
• Pedestal subtraction, timing, Zero suppression ,…
• Communication
• Interface from custom hardware to commercial electronics
18/10/2024 F. de Palma 89
Microcontroller vs FPGA

µC / CPU FPGA
Principle of operation Source code is translated to Hardware Description Language is
machine instructions and translated to configuration of FPGA,
executed by CPU defining an electronic circuit
Processing time Microseconds 10’s of nanoseconds

9
18/10/2024 F. de Palma
0
18/10/2024 F. de Palma 91
https://qbaylogic.com/cpu-vs-fpga/
What is an ASIC? Example ASIC with a DPS. Image used courtesy of Wiki
Commons and Pauli Rautakorpi

• Really generic definition:


• [A]n integrated circuit designed for
a specific customer, application, or
market using cell-based techniques,
where the necessary functional
blocks are taken from a cell library,
interconnected, and simulated to
provide the desired system
functions and level of performance.
This definition excludes ICs
designed with conventional
“custom” design techniques.
[ Analog Dialogue (p. 12)]
18/10/2024 F. de Palma 92
FPGA – ASIC comparison
ASIC
FPGA (Application Specific Integrated Circuit)
• A chip (the FPGA) is configured to represent a • A chip is produced in a foundry for a specific
digital circuit purpose
• May be reprogrammed in the field (gateware • Design cannot be changed once it is produced
upgrade)
• New features
• Long development cycle (weeks / months)
• Bug fixes • Analog designs possible
• Rapid development cycle (minutes / hours) • Higher performance
• Only digital designs are possible • Speed, Area, Power

• Low development cost • Better radiation hardness


• You can get started with a development board (< $100) • Extremely high development cost
and free software • ASICs are produced at a semiconductor fabrication
• High-end FPGAs rather expensive facility (“fab”) according to your design
• Lower cost per device compared to FPGA, when
large quantities are needed

18/10/2024 F. de Palma 93

You might also like