0% found this document useful (0 votes)
52 views32 pages

Unit-Iv Microcontroller (EC405) AVR Counters, Interrupt, Serial Input/output Data

The document discusses AVR counters, interrupts, and serial input/output on microcontrollers. It covers how counters are used to count events, the purpose of interrupts in accepting external inputs and executing interrupt service routines, and basics of serial communication including synchronous and asynchronous transmission and different transmission modes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views32 pages

Unit-Iv Microcontroller (EC405) AVR Counters, Interrupt, Serial Input/output Data

The document discusses AVR counters, interrupts, and serial input/output on microcontrollers. It covers how counters are used to count events, the purpose of interrupts in accepting external inputs and executing interrupt service routines, and basics of serial communication including synchronous and asynchronous transmission and different transmission modes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

UNIT-IV

Microcontroller (EC405)
AVR Counters, interrupt,
serial input/output data

By Suraj Kumar Saw


AVR Counters
 Many application need to count an event or generate
time delays. So there are counter registers in
microcontroller.

 For the purpose of counting an event, we connect


external source to the clock pin of thre counter register

 when an event occurs externally, the content of the


counter is incremented; in this way, the content of the
counter represents how many times an event has
occurred.
A general view of AVR Counters/Timers
Introduction
Introduction to
to AVR
AVR Timers
interrupts
 Microcontrollers can accept inputs from I/O ports,
interrupts are used for accepting inputs generated
by external events.

 Interrupt event directs the flow of program


execution with a totally independent piece of code,
known as "Interrupt Sub-Routine".

 There are many sources of interrupts that are


available for a microcontroller. Most of them are
generated by internal modules and are called as
internal interrupts.
AVR interrupt
 Event that disrupts the normal execution of a
program and causes the execution of special
instructions

 A single microcontroller can serve several devices.

 That are two ways to do that: interrupts or polling.

n The program which is associated with the interrupt


is called the interrupt service routine (ISR) or
interrupt handler
AVR interrupt
External hardware devices. Common example is
pressing on the key on the keyboard, which
causes to the keyboard to send interrupt to the
microcontroller to read the information of the
pressed key.
• The processor can send interrupts to itself as
a result of executing the program, to report an
error in the code. For example, division by 0 will
causes an interrupt.
• In the multi-processor system, the processors
can send to each other interrupts as a way to
Polling Methods
 In the Polling method, the microcontroller must
"access by himself" the device and “ask” for the
information it needs for processing. In fact we see
that in the Polling method the external devices are
not independent systems; they depend on the
microcontroller, and only the micro is entitled to
obtain access to the information it needs.

 The main drawback of this method when writing


program is waste of time of microcontroller, which
needs to wait and check whether the new information
has arrived.
Interrupt method
 Interrupt is the signal sent to the micro to mark the
event that requires immediate attention. Interrupt is
“requesting" the processor to stop to perform the
current program and to “make time” to execute a
special code.
 Whenever any device needs its service, the device
notifies the microcontroller by sending it an
interrupt signal. Upon receiving an interrupt signal,
the microcontroller interrupts whatever it is doing
and serves the device. The program which is
associated with the interrupt is called the interrupt
service routine (ISR) or interrupt handler.
n The “request” for the microcontroller to “free itself”
Interrupt Service Routine (ISR)

 For every interrupt, there is a fixed location in


memory that holds the address of its ISR.
The group of memory locations set aside to
hold the addresses of ISRs is called the
interrupt vector table.

 It is also known as interrupt handler.when an


interrupt is invoked, the microcontroller runs
the interrupt service routine
Steps in executing interrupt
1. Microcontroller normally completes the
instruction which is being executed.
2. The program control transfers to Interrupt
Service Routine (ISR). Each interrupt have an
associated ISR which is a piece of code which tells
the microcontroller what to do when an interrupt
has occurred.
3. Execution of ISR is performed by loading the
beginning address of the corresponding ISR into
program counter.
4. Execution of ISR continues until the return
from the interrupt instruction (RETI) is
encountered.
Sources of interrpt in AVR
1. There are atleast two interrupts set aside for each
of the timers, one for overflow and another for
compare match.

2. Three interrupts are set aside for external


hardware interrupt. Pin PD2 (port D,2), PD3 and
pB2 are for external hardware interrupts INT0,
INT1,and INT2 respectively.

3. serial communication USART has three


interrupts, one for receive and two interrupts for
transmitt.
Enabling and disabling interrupt
Upon reset, all interrupt are disabled (masked),
meaning none will be responded to by the
microcontroller if they are activated

The interrupt must be enabled (unmasked) by


software inorder for the microcontroller to
respond to them.

The D7 bit of SREG (status register) register is


responsible for enabling and disabling the
interrupt globally.

The I bits make the job disabling all the


interrupt easy. With a single instruction 'CLI'
Status Register (SREG)

Bits of status Register


steps in enabling an interrupt

 To enable any one of the interrupt, we take the following


steps:

 1. Bit D7 (I) of the SREG register must be set to HIGH to


allow the interrupts to happen. This is done with the "
SEI" (set interrupt) instruction.

 2. If I=1, each interrupt is enabled by setting to HIGH the


interrupt enable (IE) flag bit for that interrupt. There are
some I/O registers holding the interrupt enable bits.
Interrupt Priority in AVR
If two interrupt are activated at the same time,
the interrupt with the higher priority is served
first the priority of each interrupt is related to
the address of that interrupt in the interrpt
vector.

The interrupt that has a lower address, has a


higher priority.

for example the address of external interrupt 0


is 2, while the address of external interrupt 2
is 6; thus, external interrupt 0 has a higher
Context Saving
In big programs we might not have enough
registers to use separate registers for different
tasks.

In this cases, we can save the contents of


registers on the stack before execution of each
task, and reload the registers at the end of the
task.

This savings of the CPU contents before


switching to a new task is called context
Interrupt latency
The time from the moment and interrupt is
activated to the moment the CPU starts to execute
the task is called interrupt latency.

This latency is 4 machine cycle times. During this


time the PC register is pushed on the stack and the
I bit of the SREG register clears, causing all the
interrupts to be disabled.

The duration of an interrupt latency can be affected


by the type of instruction that the CPU is executing
when the interrupt comes in, since the CPU
finishes the execution of the current instructions
Basics of serial
communication
When a microprocessor communicates with the
outside world, it provides the data in byte size
chunks. For some devices, such as printers, the
information is simply grabbed from the 8-bitt
data bus and presented to the 8-bit data bus of
the device. This can work only if the cable is not
too long, because long cables diminish and
even distort signals.Furthermore, an 8-bit data
path is expensive.
For these reasons, serial communication is
used for transferring data between two systems
located at distance of 100 of feet to millions of
Serial Vs parallel data
transfer
Serial Vs parallel data
transfer
In parallel data transmission the complete byte of data is sent at a time, with
each bit having a separate dedicated line or it can be serial where only one
communication line is available which is shared by all the bits sequentially.

Setup for parallel data transfer is not cost effective but is a very fast method of
communication. Serial communication is cost effective because it requires only
a single line of connection but on the other hand is a slow process in
comparison to parallel communication.
Serial data communication
There are two methods for serial data communication (i) Synchronous and (ii)
Asynchronous communication. In Synchronous communication method
complete block (characters) is sent at a time. It doesn’t require any additional
bits (start, stop or parity) to be added for the synchronization of frame. The
devices are synchronized by clock. And in asynchronous communication data
transmission is done byte by byte i.e., one byte at a time. The additional bits
are added to complete a frame. In synchronous communication the frame
consists of data bits while in asynchronous communication the total number
of bits in a frame may be more than the data bits
Types of synchronous
transmission
There are three modes of transmission
Modes
simplex, halfof transmission
duplex, and full duplex.
Transmission mode describes the direction,
of flow of signal between two connected
devices. The main difference between
simplex, half duplex, and full duplex is that in
a simplex mode of transmission the
communication is unidirectional whereas, in
the half-duplex mode of transmission the
communication is two directional but the
channel is alternately used by the both the
connected device.
Modes of transmission
On the other hand, in the full duplex mode
of transmission, the communication is bi-
directional, and the channel is used by both
the connected device simultaneously.

The full duplex transmission mode is like a


two way road in which traffic can flow in both
the direction at the same time. The entire
capacity of the channel is shared by both the
transmitted signal traveling in opposite
direction.
AVR UART
The AVR UART is a very powerful and useful
peripheral and used in most projects. It can be
used for debugging code, user interaction, or just
sending data for logging it on a PC.
The AVR UART can be set up to transmit 8 or 9
bits, no parity, one Stop bit. It filters the data
received and also detects overrun conditions and
framing errors.
the transmitter and receiver share the UDR (UART
Data Register). Actually they only share the UDR
address: The "real" register is divided into the
transmitter and receiver register so that received
AVR UART block diagram
Baud rate in AVR
The baud rate of USART/UART is set by the
UBRR registrar. This register is used to
generate the data transmission at the specific
speed. The UBRR is a 16-bit register. Since the
AVR is a 8-bit microcontroller and its any
register size is 8-bit. Hence, here the 16-bit
UBRR register is composed of two 8-bit
registers such as UBRR (H), UBRR(L).
The formula of the baud rate is
BAUD= Fosc/(16*(UBBR+1))
The formula of the UBRR register is
RS 232 standard basic
RS-232 (Recommended Standard – 232) is a
standard interface approved by the Electronic
Industries Association (EIA) for connecting
serial devices.

In other words, RS-232 is a long established


standard that describes the physical interface
and protocol for relatively low-speed serial data
communication between computers and related
devices.
RS232 standard basic
RS-232 is the interface that your computer
uses to “talk” to and exchange data with your
modem and other serial devices. The serial
ports on most computers use a subset of the
RS-232C standard.

RS-232 protocol is mostly used over the


DB9 port (commonly known as serial port),
however earlier it was used over the DB25
port (also known as parallel port). We will
have a look at both of them here.
RS-232 over DB-9
The pin configuration of DB-9 port is as
follows. Yes, it looks exactly like (in fact it is)
the serial port you would find in older
computers.
Nine most important signal of
RS 232
1. DTR (data terminal ready): When terminal is
turned on, it sends out signal DTR to indicate
that it is ready for communication.
2. DSR (data set ready): When DCE is turned on
and has gone through the self-test, it assert DSR
to indicate that it is ready to communicate.
3. RTS (request to send): When the DTE device
has byte to transmit, it assert RTS to signal the
modem that it has a byte of data to transmit.
4. CTS (clear to send): When the modem has
room for storing the data it is to receive, it sends
out signal CTS to DTE to indicate that it can
Nine most important signal of
RS 232
5. DCD (data carrier detect): The modem asserts
signal DCD to inform the DTE that a valid carrier
has been detected and that contact between it
and the other modem is established.
6.RI (ring indicator): An output from the modem
and an input to a PC indicates that the telephone
is ringing. It goes on and off in synchronous with
the ringing sound.
7. RxD (Received data): The RxD pin is the Data
Receive pin. This is the pin where the receiver
receives data.
8. TxD (Transmitted data): The TxD pin is the
Data Transmit pin. This is the pin through which

You might also like