Interfacing Avr
Interfacing Avr
[email protected]
• Computer Interfacing handles how to connect
peripherals to a computer system.
• Interfacing includes how to integrate and
communicate with other devices outside the
computer system.
• It also includes designing software handlers
that perform the communication with the
peripherals.
• ES Interfacing handles how to connect with
peripherals to another ES.
• Interfacing includes how to integrate and
communicate ES with each other.
• It also includes designing software handlers
that perform the communication through the
peripherals .
ECU
S/W
Interface Circuit
Interface Circuit
Temperature
H/W
MC Heater Control
Sensor
Heater
AVR was developed in the year 1996 by Atmel
Corporation.
The architecture of AVR was developed by Alf-Egil
Bogen and Vegard Wollan.
AVR derives its name from its developers and stands
for Alf-Egil Bogen Vegard Wollan RISC
microcontroller.
Also known as Advanced Virtual RISC.
The AT90S8515 was the first microcontroller which
was based on AVR architecture.
The first microcontroller to hit the commercial
market was AT90S1200 in the year 1997.
1.TinyAVR :
• Less memory
• small size
• suitable only for simpler applications.
2.MegaAVR :
• These are the most popular ones
• having good amount of memory (upto 256 KB)
higher number of inbuilt peripherals
• suitable for moderate to complex applications.
3. XmegaAVR :
Used commercially for complex applications, which
require large program memory and high speed.
AVR microcontrollers are fast:
• executes most of the instructions in single execution
cycle.
• AVRs are about 4 times faster than PICs.
• they consume less power and can be operated in
different power saving modes.
• Free IDE.
• Available in Egyptian market.
comparison between the three most commonly used
families of microcontrollers:
• 8-bit microcontroller
• AVR follows Harvard Architecture
the processor is equipped with separate memories
and buses for Program and the Data information.
Here while an instruction is being executed, the next
instruction is pre-fetched from the program memory.
• The AVR microcontrollers are based on the advanced RISC
architecture and consist of 32 x 8-bit general purpose
working registers.
• Within one single clock cycle, AVR can take inputs from
two general purpose registers and put them to ALU for
carrying out the requested operation.
• AVR does not have any register like accumulator as in
8051 family of microcontrollers.
• the operations can be performed between any of the
registers and can be stored in either of them
Naming Convention
There are two flavors for Atmega16 microcontroller:
1. Atmega16:- Operating frequency range is 0 – 16 MHz.
2. Atmega16L:- Operating frequency range is 0 – 8 MHz.
1 ON
2 ON ON
3 ON
4 ON ON
5 ON
6 ON ON
7 ON
8 ON ON
Half Stepping
Stepping Modes
1. Wave drive :
In wave drive stepping mode only one phase is energized at a time.
2. Full Drive :
In full drive, two phases are energized at a time.
Stepping Modes
3. Half Drive :
In half drive, alternately one and two phases are energized. This increases the resolution of the
motor.
Types of Winding and Lead-out
In unipolar step motor:
there are two winding per phase. The two winding to a pole may have
one lead common i.e. center tapped.
The unipolar motor so, have five, six or eight leads.
I. Motor with six leads:
where the common of two poles are separate but center
tapped
II. Motor with five leads:
the center taps of the two poles are internally short.
III. Motor with Eight lead:
unipolar facilitates both series and parallel connection whereas
five lead and six lead motors have series connection of stator
coils.
ULN2003A Powering a Stepper
the port of AVR can only sink or source 20mA current
approximately.
they cannot be used to drive the coils directly.
The part that fit perfectly in this scenario is ULN2003A.
It is a high voltage, high current Darlington array.
It can be driven directly with a TTL level input and the
output can source up to 500ma.
ULN2003A Powering a Stepper
connect PIN 1,2,3,4 of ULN with
Pin 0,1,2,3 of uc port.
The output is available on pin
16,15,14 and 13 of ULN IC.
These are connected to the four
coils A,B,C and D of stepper
Common
motor. (MOTOR)
The common of stepper motor is
connected to 12v supply from 12
the adaptor. V
The pin 8 of ULN2003 IC is
connected to GND (common of
system).
ULN2003A Powering a Stepper
BC547 Powering a Stepper
A B
STOP 0 0
CW. 0 1
UCW 1 0
STOP 1 1
L293D Dual DC Motor Controller
void main()
{ //Setup port D pin 4,5 as output.
DDRD=(1<<PD4)|(1<<PD5);
while(1)
{ //Clock wise
PORTD=0B00010000;
Wait();
//Anti clock wise
PORTD=0B00100000;
Wait();
//Stop PORTD=0;
Wait();
}
}
Keypads are collection of push switches arranged in the
form of a matrix.
So there are rows and columns of switches.
The two connections of a switch are also connected in the
matrix, so that the row has common connection and
column has a common connection.
Scanning a Matrix Keypad:
To detect which key is pressed from the matrix:
1) Make row lines low one by one and read the columns.
2) first make Row1 low, then read the columns.
3) If any of the key in row1 is pressed will make the
corresponding column as low .
i.e if second key is pressed in Row1, then column2 will
give low. It mean that key 2 of Row1 is pressed.
So to scan the keypad completely:
1) make rows low one by one and read the columns.
2) If any of the button is pressed in a row, it will take the
corresponding column to a low state which tells us that a
key is pressed in that row.
3) If button 1 of a row is pressed then Column 1 will become
low, if button 2 then column2 and so on...
Pin No. Function Name
1 Ground (0V) Ground
2 Supply voltage; 5V (4.7V – 5.3V) Vcc
3 Contrast adjustment; through a variable resistor VEE
4 Selects command register when low Register Select
and data register when high
5 Low to write to the register Read/write
High to read from the register
6 Sends data to data pins Enable
when a high to low pulse is given
7 DB0
8 DB1
9 DB2
10 DB3
8-bit data pins
11 DB4
12 DB5
13 DB6
14 DB7
15 Backlight VCC (5V) Led+
16 Backlight Ground (0V) Led-
16x2 LCD means it can display 16 characters per line and there are 2 such lines.
In this LCD each character is displayed in 5x7 pixel matrix.
LCD has two registers:
Command/Instruction Register (IR) :
stores the command instructions given to the LCD.
This instruction to LCD to do a predefined task like:
initializing, clearing the screen, setting the cursor position, controlling display etc.
Data Register (DR) :
stores the data to be displayed on the LCD.
The data is the ASCII value of the character to be displayed on the LCD.
Commonly used LCD Command codes:
EEPROM
RAM
FLASH
MMC
Example
Interrupt Types:
• Exception handlings:
– Reset.
– Software Interrupts.
• Non maskable Interrupts:
– doesn’t depend on global interrupt enable in processor
status word .
– Usually it’s external interrupt.
• Maskable Interrupts:
– Depends on global interrupt enable in processor status
word.
– May be :
• External interrupt from external pin .
• Internal interrupt from peripheral .
Interrupts Concept
What happens when an interrupt occurs?
– The current instruction finishes execution.
– The address of the next instruction is calculated and pushed
onto the stack.
– All the CPU registers are pushed onto the stack.
– The program counter is loaded with the address pointed to
by the interrupt vector and execution continues.
Global Interrupt :
• In AVR processors on the other hand, interrupts are
vectored, with a fixed priority scheme.
• Each interrupt has an enable bit to enable/disable
the interrupt.
Interrupt vector table
• Constant table in ROM
• Special addresses with respect to CPU
• Each interrupt has specific address in interrupt
vector table
• This specific address should be programmed to
have the address of ISR of this interrupt
• At interrupt processing PC will contain this address
or it will be an instruction to jump to this address
ATmega32 Interrupt Vector
Interrupt priorities
• ISR
#include<avr/interrupt.h>
ISR(ADC_vect)
{
sei();
cli();
Serial Communication:
is the process of sending data one bit at a time,
sequentially, over a communication channel or
computer bus.
Parallel Communication:
where several bits are sent as a whole, on a link with
several parallel channels.
serial connection requires fewer interconnecting
cables and hence occupies less space.
the cost of cable and synchronization difficulties
make parallel communication impractical.
• For serial communications to be accomplished
there must be a transmitter (TX), and a
receiver (RX).
• The TX is responsible for taking a byte (or
several bits of data) in parallel, and convert
them to a series of successive bits.
• The RX on the other side, collects the
successive bits to reform the data once again.
A Serial Line.(HIGH=RED & LOW=WHITE)
1-Simplex:
one way communication where all signals can flow in only
one direction.
Example Radio & TX.
2- half-duplex system
It provides for communication in both directions, but only one
direction at a time (not simultaneously).
Example walkie-talkie.
3-Full-duplex
allows communication in both directions.
Example Telephone.
synchronous communication
Is transmission of data with the use of an external
clock signal.
Example:
Serial Peripheral Interface (SPI).
Inter-Integrated Circuit (I2C).
Asynchronous communication
Is transmission of data without the use of an external
clock signal.
Example:
Universal asynchronous receiver/transmitter (UART).
Serial Communication In Embedded Systems:
We use serial communication (UART-SPI-I2C) to interface
microcontroller with different devices such as :
1-Computer.
2-LCD.
3-EEROM.
4-Ultrasonic.
Also, serial communication used in embedded systems
networks such as:
1- Controller Area Networks (CAN)
2- Local Interconnect Networks (LIN)
• Bit time:
– The time taken to transmit a bit/sec.
• Bit rate:
– The number of bits transmitted per second.
– Bit rate = 1 / Bit time.
• Baud Rate:
– The number of symbols signaled in a second.
– Baud rate = 1 symbol / sec.
• Symbol = bit or byte or number of bits or bytes
• When using a device has a set of 8 different
symbols.
• Each symbol represents 3 bits of useful data.
• If it transmits 300 bits per second, it gathers
each 3 bits in 1 symbol.
• So it transmits 100 symbol per second
– 300 bit per second (bit rate)
– 100 baud (baud rate)
It also called Serial Communication Interface(SCI)
full-duplex communication.
Asynchronous communication.
Compatible with PC .
The Standard bit rates are:
100, 200, 300, 1200, 2400, 4800, 9600, 19200,
38400, 57600, 115200 bps.
• Frame : A frame is the unit of transmission in serial
communications.
• A frame contains:
– Start bit: To declare the start of transmission.
– Data bits: 4,5,6,7, or 8 bits of useful data bits.
– Parity bit : To check for transmission errors.
– Stop bit: To declare end of frame.
• Parity bit is used to check the integrity of a frame
and signal if an error occurred during transmission.
• Parity :
– Parity bit is an extra bit added to the end of a
frame.
• Even parity :
– The number of ‘1’ symbols inside a frame must
always be even.
• Odd parity :
– The number of ‘1’ symbols inside a frame must
always be odd.
Frame structure:
RS232 Data Interface
PC serial interface.
SPACE (0) will be between +3 to +15 volts.
MARK (1) will be between -3 and -15 volts.
Microcontroller O/P voltages:
o0 v for logic 0.
o5v for logic 1.
MAX232
Is an IC that converts signals
from an RS-232 serial port to
signals suitable for use in TTL
compatible digital logic circuits
Hyper Terminal from Start > Programs > Accessories >
Communications
UCSRA: USART Control and Status Register A
Bit No 7 6 5 4 3 2 1 0
Name RXC TXC UDRE FE DOR PE U2X MPCM
RXC this bit is set when the USART has completed receiving a byte from the host
UDRE this bit set when UDR emty.
Bit No 7 6 5 4 3 2 1 0
Name RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8
RXCIE: Receive Complete Interrupt Enable
TXCIE: Transmit Complete Interrupt Enable
RXEN: Receiver Enable
TXEN: Transmitter Enable
UCSZ2: USART Character Size
UCSRC: USART Control And Status Register C
Bit No 7 6 5 4 3 2 1 0
Name URSEL UMSEL UPM1 UPM0 USBS UCSZ1 UCSZ0
URSEL: USART register select.
The UCSRC and the UBRRH register shares same address so to determine which register user
want to write is decided with the 7th(last) bit of data if its 1 then the data is written to UCSRC
else it goes to UBRRH.
7 Bit Address
Read or Write
ACk
Send or Receive
If Combined MSG
Stop
1) The master is initially in master transmit mode by sending a
start bit.
2) Master send the 7-bit address of the slave it wishes to
communicate with. (0 Dominant )
3) Master send single bit representing whether it wishes to
write(0) to or read(1) from the slave.
4)If the slave exists on the bus then it will respond
with an ACK bit (active low for acknowledged) for that
address.
5)The master then continues in either transmit or
receive mode and the slave continues in its
complementary mode .
• The address and the data bytes are sent most
significant bit first. The start bit is indicated by a
high-to-low transition of SDA with SCL high; the stop
bit is indicated by a low-to-high transition of SDA
with SCL high.
• If the master wishes to write to the slave then it
repeatedly sends a byte with the slave sending an
ACK bit. (In this situation, the master is in master
transmit mode and the slave is in slave receive
mode).
• If the master wishes to read from the slave then it
repeatedly receives a byte from the slave, the
master sending an ACK bit after every byte but the
last one. (In this situation, the master is in master
receive mode and the slave is in slave transmit
mode).
6) The master then ends transmission with a stop
bit, or it may send another START bit if it wishes to
retain control of the bus for another transfer (a
"combined message").
Every master monitors the bus for start and stop bits, and
does not start a message while another master is keeping
the bus busy.
However, two masters may start transmission at about the
same time; in this case, arbitration occurs.
Slave transmit mode can also be arbitrated, when a master
addresses multiple slaves, but this is less common.
• I2C are not supported by PC directly.
Flash,MMC,SD,EEPROM memories.
LCD Displays.
DSP’s,FPGA’s.
The timer of the AVR can be monitor three Events through State
register (TIMSK):
Timer Overflow.
Compare Match.
Input Capture.
// Timer/Counter 0 initialization
// Clock source: T0 pin Rising Edge
TCCR0=0x07;
TCCR0 (Timer/Counter 0 Control Register)
Note
To be able to generate an output from the PWM, the Output
Compare Pin of Timer (OCn) is set up as output.
Compare Output Mode, non-PWM Mode
note
that the Data Direction Register (DDR) bit
corresponding to the OC0 pin must be set in order to
enable the output driver.
The PWM is used to generate a pulse train with varying
duty-cycle.
An analog filter can be used to shape the digital PWM
output to obtain an analog signal such as a sine wave.
In the AVR, the timer/counters are used to generate
PWM signals.
To change the PWM base frequency, the timer clock
frequency and top counter value is changed. Faster clock
and/or lower top value will increase the PWM base
frequency, or timer overflow frequency.
With full resolution (top value 255) the maximum PWM
base frequency is 250 kHz.
Altering the value of the Output Compare Registers
(OCR) changes the duty cycle.
Increasing the OCR value increases the duty cycle.
The fast Pulse Width Modulation or fast PWM mode
(WGM01:0 = 3) provides a high frequency.
PWM waveform generation option.
The fast PWM differs from the other PWM option by its
single-slope operation.
The counter counts from BOTTOM to MAX then restarts
from BOTTOM.
In fast PWM mode, the compare unit allows generation of
PWM waveforms on the OC0 pin.
Setting the COM01:0 bits to 2 will produce a non-inverted
PWM .
Inverted PWM output can be generated by setting the
COM01:0 to 3.
Compare Output Mode, Fast PWM Mode:
The phase correct PWM mode (WGM01:0 =
1) provides a high resolution phase correct
PWM
waveform generation option.
The phase correct PWM mode is based on a
dual slope operation.
The counter counts repeatedly from
BOTTOM to MAX and then from MAX to
BOTTOM.
The dual-slope operation has lower
maximum operation frequency than single
slope operation.
However, due to the symmetric feature of
the dual-slope PWM modes, these modes
are preferred for motor control applications.
The PWM resolution for the phase correct PWM
mode is fixed to eight bits.
In phase correct PWM mode the counter is
incremented until the counter value matches MAX.
When the counter reaches MAX, it changes the
count direction.
The TCNT0 value will be equal to MAX for one
timer clock cycle
Note
To be able to generate an output from the PWM, the Output
Compare Pin of Timer (OCn) is set up as output.
Initializations example :
// Timer/Counter 0 initialization
// Clock source: System Clock / 1024
// Mode: Non-Inverted Fast PWM
TCCR0=0x6D;
TCNT0=0x00;
OCR0=0x00;
TIMSK=0x02;
OCR0 = x; // x is a variable which change the duty cycle.
The ADC is enabled by setting the ADC Enable bit,
ADEN in ADCSRA.
Voltage reference and input channel selections will
not go into effect until ADEN is set.
The ADC does not consume power when ADEN is
cleared,
so it is recommended to switch off the ADC before
entering power saving sleep modes.
The ADC generates a 10-bit result which is
presented in the ADC Data Registers, ADCH and
ADCL.
By default, the result is presented right adjusted,
But can optionally be presented left adjusted by
setting the ADLAR bit in ADMUX.
If the result is left adjusted and no more than 8-bit
precision is required, it is sufficient to read ADCH.
Otherwise, ADCL must be read first, then ADCH, to
ensure that the content of the Data Registers
belongs to the same conversion.
ADMUX (ADC Multiplexer Selection Register)
ADLAR: ADC Left Adjust Result
MUX4:0: Analog Channel Selection Bits:
ADCSRA (ADC Control and Status Register A)