Unit-1 Embedded
Unit-1 Embedded
Introduction to arduino platform, Block diagram, AT mega 328p architecture, Pin function,
Overview of main features-I/O ports, Features-timers, interrupts, Features-PWM, SERIAL
PORT Features-ADC, Introduction to Arduino IDE, Writing, saving, compiling with IDE
1
Specification:
The input voltage (7-12 V) to the Arduino board when it’s using an external power source (as
opposed to 5 volts from the USB connection or other regulated power source). You can supply
voltage through the Vin pin, or, if supplying voltage via the power jack, access it directly
through Vin pin.
2
Lock program functionality for programming code security
Contains total three timers two 8-bit and one 16 bit
Total number of I/O pins are 23
Total number of PWM channels are 6
Minimum and maximum operating voltage from 1.8V DC to 5.5V DC
Serial Port
The function of the serial port remains unchanged from the earliest days of Arduino. The
connectors have changed, but everyone pretends that everything is the same. From a functional
perspective, this is certainly true. The serial port is used to communicate. In the development
stage of your Arduino project, the communication is between the Arduino and your PC, where
you’re writing, compiling, and uploading your sketch to the I/O Board. In the application (or
deployment) phase of your project, when your Arduino is performing its intended purpose, the
serial port may continue to communicate with your PC, if that is part of the plan, or it may
communicate with another serial device. The use of the serial port is optional at the application
stage, so it may be communicating with nothing at all. If this is the case, the receive (RX) and
transmit (TX) pins can be used as general-purpose input/output (I/O) lines. There are several
types of serial communication protocols. The Arduino’s serial port (internally referred to as the
USART peripheral or Universal Asynchronous/Synchronous Transmitter/Receiver) is used in an
asynchronous mode, meaning it doesn’t provide or require an independent clock signal. This
mode of operation is identical to the serial ports of most PCs, also known as RS-232 ports. The
built-in serial port hardware on the ATmega328 chip is capable of other modes of operation,
including synchronous mode, where a separate, dedicated signal carries the clock information.
The asynchronous method uses one signal to transmit data and another to receive data.
Depending on your application requirements, you may need to transmit, receive, do both, or do
neither.
3
Power Supply
The power supply circuit doesn’t actually supply any power to the Arduino. It only routes,
regulates, and filters power supplied from an external source. The present circuit has evolved
over the years to make it a convenient and almost foolproof process. The circuit selects the
highest available voltage and uses that source to supply the remainder of the circuit. There is
even a resettable fuse installed on the board to help prevent damage in the event of a short, thus
lessening the likelihood of an unauthorized thermal event. This is a great example of how the
Arduino Team has listened to the user community and added incremental improvements to the
product over the years. There are several ways to get power to your Arduino. The simplest, at
least initially, is to use the power supplied with the USB cable, which comes from your PC. The
USB standard allows for the supply of up to 100mA (milliamps, or 0.1 amps) of current at 5.0V
for an unenumerated USB device (that is, a device plugged into the USB bus but not properly
identifying itself to the host, such as a USB power tap) and as much as 500mA (0.5 amps) for a
properly enumerated USB device. This is more than enough electrical power to light up several
LEDs and a few low-power sensors. It isn’t sufficient for larger electrical loads, such as relays,
heaters, fans, motors, or solenoids. When the Arduino isn’t connected to a PC via the USB cable,
regulated 5V power can be supplied to it through the power expansion connector pins labeled 5V
and GND.
Due to a temporary worldwide shortage of the beloved 28-pin dual inline package (DIP) version
of the ATmega328 processor (whose Atmel part number, to help differentiate it from the other
packaging options, is ATMEGA328P-PU, the first P being for picoPower technology and the
second P meaning plastic DIP), a surface-mount version of the Arduino Uno was released,
dubbed the Arduino Uno SMD (surface-mount device). It’s functionally identical in operation to
the Uno. The only drawback is that the surface-mount processor can’t easily be removed from
the PCB, as was the case with the socketed DIP versions. See Figure 1-3.
4
Figure 1-3. The Arduino Uno (left) and the Arduino Uno SMD (right)
The main brain of the Arduino Uno is the Atmel AVR ATmega328, the black, rectangular plastic
block with two rows of pins protruding from its sides. On the SMD version, the processor is one
of the two miniscule black squares soldered directly to the PCB. This device is essentially a
computer on a chip, containing a central processing unit (CPU), memory arrays, clocks, and
peripherals in a single package. See Figure 1-4.
Arduino Types
Mega: In almost every respect, the Arduino Mega 2560 is identical to its smaller sibling. It runs
at the same speed of 16MHz, requires approximately the same amount of power, executes the
same software, and uses the same development tools. The primary difference between the Uno
and the Mega is the processor, the ATmega2560, which has more memory and more peripherals
than the ATmega328. The PCB is also larger
Nano: Gravitech manufactures the Arduino Nano, which can easily be installed in either a DIP
socket or solderless breadboard.
5
LilyPad: SparkFun also manufactures the LilyPad Arduino, which is the basis of an entire
ecosystem of wearable electronics, or e-textiles. The LilyPad Arduino was developed by Leah
Buechley and designed by Leah and SparkFun.
6
The ATmega328 chip is derived from the original Arduino processor, the ATmega8. It contains
more memory and more peripheral capability than its predecessor while using less power. The
ATmega328 processor can operate from a wide range of power-supply voltages, from 1.8V to
5.5V. This makes it well suited for battery-powered applications. At the lowest voltages, the
processor has a maximum clock rate of 4MHz (millions of cycles per second). Increase the
supply voltage to at least 2.7V, and you can increase the clock rate to 10MHz. To run at the rated
maximum clock rate of 20MHz, the chip needs at least 4.5V. The Arduino I/O Board provides
5.0V for the ATmega328 chip, so it can run at any speed, up to the maximum of 20MHz.
The current crop of ATmega328 chips from Atmel feature the company’s picoPower technology,
which dramatically reduces power consumption in the device. These parts are designated with a
P suffix: for example, ATmega328P. The previous versions available were able to run either at
lower voltages (such as the ATmega328V) but not full speed, or at full speed but not at voltages
below 2.7V. The picoPower technology eliminates this limitation, allowing both full speed (at
appropriate supply voltages) and low power operation at reduced speeds. Although the new
ATmega328 chip can run up to 20MHz, the original ATmega8 topped out at 16MHz. The
16MHz clock rate has been maintained in all subsequent Arduino models to preserve
compatibility.
7
The Architecture of Arduino or precisely the IC of Arduino is ATmega328p. The ATmega328/P
is a low-power CMOS 8-bit microcontroller based on the AVR® enhanced RISC (reduced
instruction set computer) architecture.
In Order to maximize performance and parallelism, the AVR uses Harvard architecture – with
separate memories and buses for program and data. Instruction in the program memory is
executed with a single level of pipelining.
The clock is controlled by an external 16MHz Crystal Oscillator.
Figure 3-3. The AVR Core contains the essential components of the AVR central processing unit (CPU). It’s
tightly integrated into the rest of the microcontroller.
8
The basic working of CPU of ATmega328:-
1. The data is uploaded in serial via the port (being uploaded from the computer’s Arduino IDE).
The data is decoded and then the instructions are sent to instruction register and it decodes the
instructions on the same clock pulse.
2. On the next clock pulse the next set of instructions are loaded in instruction register.
3. In general purpose registers the registers are of 8-bit but there are 3 16-bit registers also.
a. 8-bit registers are used to store data for normal calculations and results.
b. 16-bit registers are used to store data of timer counter in 2 different register. Eg. X-low & X-
high. They are fast, and are used to store specific hardware functions.
4. EEPROM stores data permanently even if the power is cut out. Programming inside an
EEPROM is slow.
9
5. Interrupt Unit checks whether there is an interrupt for the execution of instruction to be
executed in ISR (Interrupt Service Routine).
6. Serial Peripheral Interface (SPI) is an interface bus commonly used to send data between
microcontrollers and small peripherals such as Camera, Display, SD cards, etc. It uses separate
clock and data lines, along with a select line to choose the device you wish to talk to.
8. Analog comparator compares the input values on the positive and negative pin, when the
value of positive pin is higher the output is set.
9. Status and control is used to control the flow of execution of commands by checking other
blocks inside the CPU at regular intervals.
10. ALU (Arithmetic and Logical unit)-The high performance AVR ALU operates in direct
connection with all the 32 general purpose working registers. Within a single clock cycle,
arithmetic operations b/w general purpose registers are executed. The ALU operations are
divided into 3 main categories – arithmetic, logical and bit-function.
11. I/O pins-The digital inputs and outputs (digital I/O) on the Arduino are what allow you to
connect the Arduino sensors, actuators, and other ICs. Learning how to use them will allow you
to use the Arduino to do some really useful things, such as reading switch inputs, lighting
indicators, and controlling relay outputs.
Memories:
The AVR Core has access to several arrays of memory and I/O devices. The AVR architecture
itself is based on a Harvard architecture, where the program and data memories are separated, as
opposed to a Von Neumann architecture, where the two memories are intermingled, overlapping,
and interchangeable.
Program Memory
The actual machine-language instructions for the processor to execute are stored in the program
memory. On AVR devices, this memory is implemented as a 16-bit wide array of in-system
reprogrammable flash memory. Because the program memory is also self-reprogrammable, a
certain amount of data can be stored in it, but the programming time and limited number of
writes limit its usefulness for transient data storage.
This program memory section is sometimes referred to as ROM, or read-only memory. This is
yet another anachronism that no longer accurately describes the present function or capabilities.
The term ROM was often used to describe the permanent or non-volatile bit codings that
persisted in a system’s memory even during power outages. This term was used to differentiate
this type of memory from RAM, or random-access memory, yet another misnomer used to
10
identify rewritable or volatile memory devices. The flash memory technology used by Atmel in
its AVR products as well as other semiconductor devices has all the advantages of non-volatility
while being easily reprogrammable without complex or expensive device programming
hardware.
Data Memory
The microcontroller usually needs to store variables and changing data during the course of the
program’s execution. This memory is implemented in the AVR as static, random-access memory
(SRAM). Technically, all the memory arrays within the microcontroller are random access, in
that any individual memory location can be directly accessed. The memory array is based on
static bit cells that require no active clock signal to retain their contents, as opposed to dynamic
RAM cells that require periodic refresh. The SRAM designation is yet another hold-over from
previous memory technologies. The SRAM memory array maintains its data integrity as long as
power is applied to the chip. The ATmega328 chip has 2KB of SRAM, and the ATmega2560 has
8KB of SRAM.
Registers
Every AVR chip from the smallest to the largest has a generous general-purpose register file.
There are 32 registers in the file, numbered R0 to R31. Most of the arithmetic and logical
instructions of the CPU can read and write to the individual registers within the register file
directly, often in only one clock cycle.
Six of the registers have a special use. They can be combined to form three 16-bit index pointers
to the data space. These index registers are called X, Y, and Z. The X index register is composed
of registers R26 (low 8 bits) and R27 (high 8 bits). The Y index register is composed of R28 and
R29, and the Z index register is composed of R30 and R31.
EEPROM
11
PIN FUNCTION
Pin Pin
Pin# Pin Function Description
Description Function
12
Pin Pin
Pin# Pin Function Description
Description Function
Voltage
8 GND Ground Ground of the system
9 XTAL Crystal This pin should be connected to one pin of
Oscillator the crystal oscillator to provide external
clock pulse to the chip
10 XTAL Crystal This pin should also be connected to
Oscillator another pin of the crystal oscillator to
provide external clock pulse to the chip
11 PD5 Digital Pin Pin 11 is used for external counter source
(PWM) Timer1
12 PD6 Digital Pin Positive Analog Comparator i/ps
(PWM)
13 PD7 Digital Pin Negative Analog Comparator i/ps
14 PB0 Digital Pin Counter or Timer input source
15 PB1 Digital Pin counter or timer compare match A.
(PWM)
16 PB2 Digital Pin This pin is act as a slave choice i/p.
(PWM)
17 PB3 Digital Pin This pin is used as a master data output and
(PWM) slave data input for SPI.
18 PB4 Digital Pin This pin is act as a master clock input and
slave clock output.
19 PB5 Digital Pin This pin is act as a master clock output and
slave clock input for SPI.
20 AVcc Positive Positive voltage for ADC (power)
Voltage
21 AREF Analog Analog Reference voltage for ADC
Reference (Analog to Digital Converter)
22 GND Ground Ground of the system
23 PC0 Analog Analog input digital value channel 0
Input
24 PC1 Analog Analog input digital value channel 1
Input
13
Pin Pin
Pin# Pin Function Description
Description Function
Status Register
The Status register contains information about the result of the most recently executed arithmetic
instruction. This information can be used for altering program flow in order to perform
conditional operations. The Status register is updated after all the Arithmetic Logical Unit (ALU)
operations. This will, in many cases, remove the need for using the dedicated compare
instructions, resulting in faster and more compact code.
When addressing I/O Registers as data space using LD and ST instructions, the provided offset
must be used. When using the I/O specific commands IN and OUT, the offset is reduced by
0x20, resulting in an I/O address offset within 0x00 - 0x3F.
Name: SREG
Offset: 0x5F
Reset: 0x00
The Global Interrupt Enable bit must be set for the interrupts to be enabled. The individual
interrupt enable control is then performed in separate control registers. If the Global Interrupt
Enable Register is cleared, none of the interrupts are enabled independent of the individual
14
interrupt enable settings. The I bit is cleared by hardware after an interrupt has occurred, and is
set by the RETI instruction to enable subsequent interrupts. The I-bit can also be set and cleared
by the application with the SEI and CLI instructions.
The Bit Copy instructions BLD (Bit LoaD) and BST (Bit STore) use the T-bit as source or
destination for the operated bit. A bit from a register in the Register File can be copied into T by
the BST instruction, and a bit in T can be copied into a bit in a register in the Register File by
the BLD instruction.
The Half Carry Flag H indicates a Half Carry in some arithmetic operations. Half Carry Flag is
useful in BCD arithmetic.
The S-bit is always an exclusive or between the Negative Flag N and the Two’s Complement
Overflow Flag V.
15
Even the smallest tinyAVR devices with no built-in SRAM have these registers at their disposal.
This large register array permits fast and complex algorithms to execute quickly, without having
to copy data back and forth from the SRAM memory.
Six of the registers have a special use. They can be combined to form three 16-bit index pointers
to the data space. These index registers are called X, Y, and Z. The X index register is composed
of registers R26 (low 8 bits) and R27 (high 8 bits). The Y index register is composed of R28 and
R29, and the Z index register is composed of R30 and R31.
The Register File is optimized for the AVR Enhanced RISC instruction set. In order to achieve
the required performance and flexibility, the following input/output schemes are supported by
the Register File:
Most of the instructions operating on the Register File have direct access to all registers, and
most of them are single cycle instructions. As shown in the figure, each register is also assigned
a data memory address, mapping them directly into the first 32 locations of the user Data Space.
16
Although not being physically implemented as SRAM locations, this memory organization
provides great flexibility in access of the registers, as the X-, Y-, and Z-pointer registers can be
set to index any register in the file.
The X-register, Y-register, and Z-register
The registers R26...R31 have some added functions to their general purpose usage. These
registers are 16-bit address pointers for indirect addressing of the data space. The three indirect
address registers X, Y, and Z are defined as described in the figure.
I/O ports
All the specific-purpose pins on both the ATmega328 and the ATmega2560 (power and ground
connections and AREF, RESET, and XTAL pins). Each and every one of the remaining pins can
be used as a general-purpose input/output pin, among (many) other things. The I/O ports on the
AVR are quite versatile. Each port can have up to eight pins associated with it. Each pin within
the port can be configured as an input or an output.
ATmega-328 pins are divided into different ports which are given in detail below.
Port B consists of the pins from PB0 to PB7. This port is an 8 bit bidirectional port having an
internal pull-up resistor.
Port C consists of the pins from PC0 to PC6. The output buffers of port C has symmetrical
drive characteristics with source capability.
Port D consists of the pins from PD0 to PD7. It is also an 8 bit input/output port having an
internal pull-up resistor.
17
TIMERS
The ATmega328 has three timer/counter peripherals available. Each one is a little different. The
ATmega2560, as you might expect, has even more. Let’s start with the ATmega328 and its three
unique timer/counters. They’re referred to as Timer/Counter 0, Timer/Counter 1, and
Timer/Counter 2.
Timer/Counter 0
At the heart of Timer/Counter 0 is an 8-bit counter. You can directly read from and write to this
counter using the 8-bit TCNT0 register. The behavior and operation of the counter are controlled
by the bit settings in two configuration registers, TCCR0A and TCCR0B. Many possible
combinations allow a wide variety of uses for this versatile peripheral. As its name suggests, this
peripheral can be used as either a timer or a counter. When clocked (triggered) by an external
signal, it performs as a counter. When clocked from the system clock (or a signal derived from
the system clock), it can be used as a timer or frequency generator. The timer/counter can be set
up to generate interrupts. This is a handy thing to have when you need to schedule something to
happen on a regular basis. Another popular function is the generation of pulse-width-modulated
(PWM) signals. Two different PWM outputs can be controlled, although they share the same
period and resolution. These PWM outputs, OC0A and OC0B, are connected to PD6 and PD5,
respectively. On the Arduino Uno, these signals are called D6 and D5.
Timer/Counter 1
Timer/Counter 1 shares many characteristics with the other timer/counters. Its principle
distinction is that the internal counter is 16 bits long instead of only 8. Two PWM channels are
also available for this timer/counter, just like Timer/Counter 0. These signals can be routed to
output pins OC1A and OC1B, also known as PB1 and PB2, or D9 and D10 on the Arduino Uno.
Timer/Counter 1 also has an input capture unit. This hardware allows very accurate timing
measurement of incoming signals.
Timer/Counter 2
Another 8-bit counter is found at the heart of Timer/Counter 2. It’s almost identical to
Timer/Counter 0, with two PWM channels (OC2A and OC2B, routed to pins PB3 and PD3,
a.k.a. D11 and D3, respectively), interrupts, the works. What makes Timer/Counter 2 special is
its ability to dance to the beat of its own drummer. It has an asynchronous mode of operation that
allows it to be clocked from a source other than the system clock or external signals. Assuming
the system is being clocked by the calibrated RC oscillator, the otherwise unused crystal
oscillator driver can be used to drive a low-frequency watch crystal. This configuration is ideal
for low-power, real-time clock applications. Unfortunately, there’s no easy way to do this using
an Arduino without extensive hardware and software hacks.
18
INTERRUPTS
After the chip is reset, the PC is set to zero. The first instruction is fetched from a location in the
program memory (which can change based on settings in the MCU Control Register), which is
called the reset vector. It generally contains an instruction to jump (transfer execution) to the
proper location in the code to begin any initialization processes needed, and then onward through
to the application program. These details are usually handled by the compiler software.
19
**The term vector is used throughout the Atmel AVR documentation. Vectors are generally
pointers to specific locations (addresses). The AVR vector table contains a list of executable
instructions, but these instructions can be (and usually are) unconditional branch instructions that
encode a particular destination address.
20
PWM
PWM stands for Pulse Width Modulation and it is a technique used in controlling the brightness
of LED, speed control of DC motor, controlling a servo motor or where you have to get analog
output with digital means.
The Arduino digital pins either gives us 5V (when turned HIGH) or 0V (when turned LOW) and
the output is a square wave signal. So if we want to dim a LED, we cannot get the voltage
between 0 and 5V from the digital pin but we can change the ON and OFF time of the signal. If
we will change the ON and OFF time fast enough then the brightness of the led will be changed.
21
Terminology:
Duty Cycle: It is the percentage of time when the signal was high during the time of period.
So at 50% duty cycle and 1Hz frequency, the led will be high for half a second and will be low
for the other half second. If we increase the frequency to 50Hz (50 times ON and OFF per
second), then the led will be seen glowing at half brightness by the human eye.
So if we are on for 1ms and then off for 1ms we will end up with a 50% duty cycle; If we are on
for 1ms and off for 3ms we end up with a 25% duty cycle.
22
On Arduino Uno, the PWM pins are 3, 5, 6, 9, 10 and 11. The frequency of PWM signal on
pins 5 and 6 will be about 980Hz and on other pins will be 490Hz. The PWM pins are labeled
with ~ sign.
Applications of Pulse Width Modulation (PWM)
Pulse Width Modulating signal can be generated using a Comparator. Modulating signal forms
one of the input to the Comparator and the other input is fed with a non-sinusoidal wave or saw
tooth wave. The Comparator compares the two signals and generates a PWM signal as its output
waveform.
If the value of the Saw tooth triangle signal is more than the modulation signal then the PWM
output signal is at “High” else it’s in “Low” state. Thus, the value of the input signal magnitude
determines the comparator output which defines the width of the pulse generated at the output.
23
THEORY OF OPERATION:
Fast PWM works the same way as the normal counter. The Control Logic receives the signal
and increments the TCNTn register. When a match is detected the OCFnx flag is set and signal
is send to the Waveform Generator. The Waveform Generator then changes the state of the
OCnx pin (the state is determined by the selected mode). When the TCNTn register passes the
TOP value (0xFF or OCRnA) it simply overflows (or overruns) back to 0, at the same time the
OCFnx flag is set. The OCFnx flag can be configured to trigger an interrupt. The OCFnx flag
can be cleared by software, but as always is cleared automatically when an interrupt request is
triggered.
Due to the high frequency of this mode is best used for DAC, fading LEDs, rectification and
Power regulation.
The Frequency of the fast PWM can be calculated by the following equation.
The phase corrected mode is a bit strange, it counts up until it hits the TOP value (FIXED,
OCRnA or ICRn) then starts to count down until it hits the BOTTOM (0). The Control
Logic receives the signal and increments the TCNTn register. When a match is detected the
OCFnx flag is set and signal is send to the Waveform Generator. The Waveform Generator then
changes the state of the OCnx pin (the state is determined by the selected mode). When the
TCNTn register hits the TOP value (FIXED, OCRnA or ICRn) the OCFnx flag is set. The
OCFnx flag can be configured to trigger an interrupt. The OCF1x flag can be cleared by
software, but as always is cleared automatically when an interrupt request is triggered.
This mode can be inverted or none-inverted. In none-inverting mode, the OCn pin is
LOW(GND) on the Compare Match between TCNTn and OCRnx while up-counting, and
HIGH(VCC) on the Compare Match while down-counting. In inverting mode, the OCn pin is
HIGH(VCC) on the Compare Match between TCNTn and OCRnx while up-counting, and
LOW(GND) on the Compare Match while down-counting.
This mode is recommended for motor control.
The frequency of the Phase Corrected PWM can be calculated by the following equation.
PWM_frequency = clock_speed / (2 * Prescaller_value * TOP_value )
24
Phase and Frequency Corrected PWM mode:
Phase Corrected and Phase and Frequency Corrected PWM modes function the same way if
we are not planning on changing our TOP value once the PWM mode is started. The only
difference is that the Phase and Frequency Corrected mode updates its TOP value when it hits
Bottom while the Phase Corrected mode updates its TOP value when it hits the TOP.
The frequency of the Phase and Frequency Corrected PWM can be calculated by the following
equation.
SERIAL PORT
ADC
Microcontrollers are meant to deal with digital information. They only understand ‘0’ and ‘1’
values. So what if we need to get some non-digital data into the microcontroller. The only way is
to digitize or, to speak to convert analog into digital. This is why almost all microcontrollers are
featured with the ADC module. Atmega328 microcontroller has 6 ADC input
channels/lines/pins/wires (A0-A5).
25
The Atmega328 chip has an analog-to-digital converter (ADC) inside of it. This must be or else
the Atmega328 wouldn't be capable of interpreting analog signals. Because there is an ADC, the
chip can interpret analog input, which is why the chip has 6 pins for analog input.
The ADC has 3 pins set aside for it to function- AVCC, AREF, and GND.
AVCC is the power supply, positive voltage, that for the ADC.
The ADC needs its own power supply in order to work.
GND is the ADC power supply ground.
AREF is the reference voltage that the ADC uses to convert an analog signal to its corresponding
digital value.
Analog voltages higher than the reference voltage will be assigned to a digital value of 1, while
analog voltages below the reference voltage will be assigned the digital value of 0.
Since the ADC for the Atmega328 is a 10-bit ADC, meaning it produces a 10-bit digital value, it
converts an analog signal to its digital value, with the AREF value being a reference for which
digital values are high or low.
Applications:
There are thousands of applications for Atmega328P also more to come in near future depends
on how creative one can think. Every day we see a new application built using the this chip by
electronic students, engineers, hobbyists, tinkerers. Some of the applications for the chip are as
follow.
26