0% found this document useful (0 votes)
19 views47 pages

Mps_ch04 - Io Ports in Avr

This document provides an overview of I/O ports in AVR microcontrollers, specifically focusing on the ATmega16 and ATmega32 models. It covers topics such as pinout, I/O port programming, and examples of digital input/output operations using assembly language. Additionally, it includes details on register addresses and programming examples for manipulating I/O pins.

Uploaded by

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

Mps_ch04 - Io Ports in Avr

This document provides an overview of I/O ports in AVR microcontrollers, specifically focusing on the ATmega16 and ATmega32 models. It covers topics such as pinout, I/O port programming, and examples of digital input/output operations using assembly language. Additionally, it includes details on register addresses and programming examples for manipulating I/O pins.

Uploaded by

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

I/O Ports in AVR

Chapter 4

The AVR microcontroller


and embedded
systems
using assembly and c

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Topics
 AVR pin out
 I/O Port Programming
 I/O Bit manipulation
programming
 Simple Digital Input/Output
devices

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
ATmega16/mega32 pinout

1. Vital Pins:
40 PIN DIP
1. Power (XCK/T0) PB0 1 40 PA0 (ADC0)

VCC +5 V
(T1) PB1
(INT2/AIN0) PB2
2
3
39
38
PA1 (ADC1)
PA2 (ADC2)

Ground (OC0/AIN1) PB3 4 MEGA32 37 PA3 (ADC3)
(SS) PB4 5 36 PA4 (ADC4)
2. Crystal (MOSI) PB5 6 35 PA5 (ADC5)
(MISO) PB6 7 34 PA6 (ADC6)

XTAL1 +5 V (SCK) PB7 8 33 PA7 (ADC7)
+5 V

XTAL2 RESET
VCC
9
10
32
31
AREF
AGND

3. Reset GND
XTAL2
11
12
30
29
AVCC
PC7 (TOSC2)

2. I/O pins XTAL1


(RXD) PD0
13
14
28
27
PC6 (TOSC1)
PC5 (TDI)
• PORTA, PORTB, (TXD) PD1
(INT0) PD2
15
16
26
25
PC4 (TDO)
PC3 (TMS)
PORTC, and PORTD (INT1) PD3 17 24 PC2 (TCK)
(OC1B) PD4 18 23 PC1 (SDA)

3. Internal ADC (OC1A) PD5


(ICP) PD6
19
20
22
21
PC0 (SCL)
PD7 (OC2)
pins
• AREF, AGND, AVCC
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
ATMega32 I/O Port Registers
There are three registers associated with
Input/Output Ports in AVR.

(Read/Write) (Read/Write) (Read)

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Register Addresses for ATmega32 Ports
IO SRAM
Port Usage
Address Address
PORTA $1B $3B Output
DDRA $1A $3A Direction
PINA $19 $39 Input
PORTB $18 $38 Output
DDRB $17 $37 Direction
PINB $16 $36 Input
PORTC $15 $35 Output
DDRC $14 $34 Direction
PINC $13 $33 Input
PORTD $12 $32 Output
DDRD $11 $31 Direction
PIND $10 $30 Input

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Topics
 AVR pin out
 I/O Port Programming
 I/O Bit manipulation
programming
 Simple Digital Input/Output
devices

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
The structure of IO pins
Mega32/Mega16
(XCK/T0) PB0 0 00 00 0 PA0 (ADC0)
(T1) PB1 1 11 11 1 PA1 (ADC1)
(INT2/AIN0) PB2 2 22 22 2 PA2 (ADC2)
(OC0/AIN1) PB3 3 33 33 3 PA3 (ADC3)
(SS) PB4 4 44 44 4 PA4 (ADC4)
DDRx:
(MOSI) PB5 5
7 565 5 4 3 5 25 51 0
PA5 (ADC5)
PORTx:
(MISO) PB6 6
7 666 5 4 3 6 26 61 0
PA6 (ADC6)
(SCK) PB7 PINx: 7
7 767 5 4 3 7 27 71 0
PA7 (ADC7)
DDRA
DDRB
PINB
PORTB
RESET PORTA AREF
PINA
Px7 Px6 Px5 Px4 Px3 Px2 Px1 Px0
VCC AGND
GND AVCC
XTAL2 PC7 (TOSC2)
XTAL1 PC6 (TOSC1)
(RXD) PD0 PC5 (TDI)

DDRx
(TXD) PD1 PC4 (TDO) 0 11

DDRx
PORTx 0
PORTx
(INT0) PD2 PC3 (TMS)
DDRx.n 0 high impedance
high impedance Out
Out00
(INT1) PD3 PC20(TCK)
(OC1B) PD4 PORTx.n 11(SDA)pull-up
PC1 pull-up Out
Out11
(OC1A) PD5 PC0 (SCL)
(ICP) PD6 PINx.n PD7 (OC2)
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 1
 Write a program that makes all the pins of
PORTA one.
Mega32/Mega16
(XCK/T0) PB0 PA0 (ADC0)
(T1) PB1 PA1 (ADC1)
(INT2/AIN0) PB2 PA2 (ADC2)
(OC0/AIN1) PB3 PA3 (ADC3)
(SS) PB4 PA4 (ADC4)
DDRA: 1 1 1 1 1 1 1 1 (MOSI) PB5 PA5 (ADC5)
(MISO) PB6 PA6 (ADC6)
PORTA: 1 1 1 1 1 1 1 1 (SCK) PB7
PINA
PA7 (ADC7)

PORTB
DDRB
PINB
RESET DDRA AREF
PORTA
VCC AGND
PORTC
GND DDRC AVCC
PINC
XTAL2 PC7 (TOSC2)
XTAL1 PC6 (TOSC1)
(RXD) PD0 PC5 (TDI)

.INCLUDE “M32DEF.INC” (TXD) PD1


(INT0) PD2
PC4 (TDO)
PC3 (TMS)
(INT1) PD3 PC2 (TCK)
LDI R20,0xFF ;R20 = 11111111 (binary) (OC1B) PD4 PC1 (SDA)
(OC1A) PD5 PC0 (SCL)
OUT PORTA,R20 ;PORTA = R20 (ICP) PD6 PD7 (OC2)

OUT DDRA,R20 ;DDRA = R20

DDRx
00 11

DDRx
PORTx
PORTx
00 high
highimpedance
impedance
Out
Out0 0

11 pull-up
pull-up Out
Out1 1

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 2
 The following code will toggle all 8 bits of
Port B forever with some time delay
between “on” and “off” states:

LDI R16,0xFF ;R16 = 0xFF = 0b11111111


OUT DDRB,R16 ;make Port B an output port (1111 1111)
L1: LDI R16,0x55 ;R16 = 0x55 = 0b01010101
OUT PORTB,R16 ;put 0x55 on port B pins
CALL DELAY
LDI R16,0xAA ;R16 = 0xAA = 0b10101010
OUT PORTB,R16 ;put 0xAA on port B pins
CALL DELAY
RJMP L1

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 3
 A 7-segment is connected to PORTA.
Display 1 on the 7-segment.
DDRC: 1 1 1 1 1 1 1 1
PORTC: 0 0 0 0 0 1 1 0

ATmega32 0
5 1
.INCLUDE “M32DEF.INC” 8 6
PORTC
LDI R20,0x06 ;R20 = 00000110 (binary) 4 2
OUT PORTC,R20 ;PORTC = R20
3
LDI R20,0xFF ;R20 = 11111111 (binary)

DDRx
OUT DDRC,R20 ;DDRC = R20 00 11

DDRx
PORTx
L1: RJMP L1 PORTx
00 high
highimpedance
impedance
Out
Out0 0

11 pull-up
pull-up Out
Out1 1

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 4
 A 7-segment is connected to PORTA.
Display 3 on the 7-segment.
DDR: 1 1 1 1 1 1 1 1
PORTC: 0 1 0 0 1 1 1 1

ATmega32 0
5 1
.INCLUDE “M32DEF.INC” 8 6
PORTC
LDI R20,0x4F ;R20 = 01001111 (binary) 4 2
OUT PORTC,R20 ;PORTC = R20
3
LDI R20,0xFF ;R20 = 11111111 (binary)

DDRx
OUT DDRC,R20 ;DDRC = R20 00 11

DDRx
PORTx
L1: RJMP L1 PORTx
00 high
highimpedance
impedance
Out
Out0 0

11 pull-up
pull-up Out
Out1 1

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 5: Input
 The following code gets the data present at the pins of port
C and sends it to port B indefinitely, after adding the value
5 to it:

.INCLUDE "M32DEF.INC"
LDI R16,0x00 ;R16 = 00000000 (binary)
OUT DDRC,R16 ;make Port C an input port
LDI R16,0xFF ;R16 = 11111111 (binary)
OUT DDRB,R16 ;make Port B an output port(1 for
Out)
L2: IN R16,PINC ;read data from Port C and put in
R16
LDI R17,5
ADD R16,R17 ;add 5 to it
OUT PORTB,R16 ;send it to Port B
RJMP L2 ;continue forever
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Pull-up resistor

vcc
1 = Close
PORTx.n 0 = Open

pin n of
port x PINx.n

Outside the Inside the


AVR chip AVR chip

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
The structure of I/O pins
RDx
PUD
P
DATA BUS
Q D
DDRxn
Q WR DDRxn
CLK

RESET
DDRx.n
RRx
OUTPUT
PORTx.n
Pxn Q D
PORTxn
Sleep WR PORTxn
Q CLK
PINx.n INPUT
RESET
SYNCHRONIZER

D Q D Q
PINxn
L Q Q RPx
N
RESET RESET
CLKI/O

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Out 0

RDx
PUD
P
1 1 DATA BUS
Q D 0
DDRxn
Q WR DDRxn
CLK

RESET

RRx

0 0 0 0
Pxn Q D
PORTxn
Sleep WR PORTxn
Q CLK

RESET
SYNCHRONIZER

D Q D Q
PINxn
L Q Q RPx
N
RESET RESET
CLKI/O

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Out 1

RDx
PUD
P
1 1 DATA BUS
Q D 0
DDRxn
Q WR DDRxn
CLK

RESET

RRx

1 1 1 1
Pxn Q D
PORTxn
Sleep WR PORTxn
Q CLK

RESET
SYNCHRONIZER

D Q D Q
PINxn
L Q Q RPx
N
RESET RESET
CLKI/O

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 6
 Write a program that continuously sends out to
Port C the alternating values of 0x55 and 0xAA.

.INCLUDE "M32DEF.INC"
LDI R16,0xFF ;R16 = 11111111
(binary)
OUT DDRC,R16 ;make Port C an output
port
L1: LDI R16,0x55 ;R16 = 0x55
OUT PORTC,R16 ;put 0x55 on Port C
pins
LDI R16,0xAA ;R16 = 0xAA
OUT PORTC,R16 ;put 0xAA on Port C
pins
RJMP L1
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
The structure of IO pins
DDRx

00 11
DDRx

PORTx
PORTx
00 high
highimpedance
impedance
Out
Out0 0
RDx
PUD
11 pull-up
pull-up Out
Out1 1 P
DATA BUS
Q D
DDRxn
Q WR DDRxn
CLK

RESET

RRx

Pxn Q D
PORTxn
Sleep WR PORTxn
Q CLK

RESET
SYNCHRONIZER

D Q D Q
PINxn
L Q Q RPx
N
RESET RESET
CLKI/O

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Input (Tri-state vs. pull up)

RDx
PUD
P
0 0 DATA BUS
Q D
DDRxn
Pull-up WR DDRxn
Q CLK
Resistor
RESET

0 RRx

0
Pxn Q D
PORTxn
WR PORTxn
Q CLK
Sleep RESET

SYNCHRONIZER

0 0 0 0 0
0 D Q D Q
PINxn RPx
N L Q Q
RESET RESET
CLKI/O

The represents how the content of PORTx register affects the pull-up resistor;
while the shows how a data can be read from a pin

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 7
40 PIN DIP

Write a program that reads


(XCK/T0) PB0 1 40 PA0 (ADC0)
 (T1) PB1 2 39 PA1 (ADC1)
(INT2/AIN0) PB2 3 38 PA2 (ADC2)

from port A and writes it to (OC0/AIN1)


(SS)
PB3
PB4
4
5
MEGA32 37
36
PA3
PA4
(ADC3)
(ADC4)

port B. (MOSI)
(MISO) PB6
PB5 6
7
35
34
PA5 (ADC5)
PA6 (ADC6)
(SCK) PB7 8 33 PA7 (ADC7)
RESET 9 32 AREF
VCC 10 31 AGND
.INCLUDE “M32DEF.INC” GND 11 30 AVCC
XTAL2 12 29 PC7 (TOSC2)
LDI R20,0 ;R20 = 00000000 (binary) XTAL1 13 28 PC6 (TOSC1)
(RXD) PD0 14 27 PC5 (TDI)

OUT DDRA,R20 ;DDRA = R20 (TXD) PD1 15 26 PC4 (TDO)


(INT0) PD2 16 25 PC3 (TMS)
(INT1) PD3 17 24 PC2 (TCK)
LDI R20,0xFF ;R20 = 11111111 (binary) (OC1B) PD4 18 23 PC1 (SDA)
(OC1A) PD5 19 22 PC0 (SCL)
OUT DDRB,R20 ;DDRB = R20 (ICP) PD6 20 21 PD7 (OC2)

L1: IN R20,PINA ;R20 = PINA


OUT PORTB,R20 ;PORTB = R20

DDRx
00 11

DDRx
RJMP L1 PORTx
PORTx
00 high
highimpedance
impedance
Out
Out0 0

11 pull-up
pull-up Out
Out1 1

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Synchronizer delay
 Example: Port A is configured first as an input port by writing all Os to
register DDRA, and then data is received from Port A and saved in a
RAM location:
.INCLUDE "M32DEF.INC"
.EQU MYTEMP 0x100 ;save it here
LDI R16, 0x00 ;R16 = 00000000 (binary)
OUT DDRA,R16 ;make Port A an input port (0 for In)
NOP ;synchronizer delay
IN R16,PINA ;move from pins of Port A to R16
STS MYTEMP,R16 ;save it in MYTEMP
 The input circuit of the AVR has a delay of 1 clock cycle. In
other words, the PIN register represents the data that was
present at the pins one clock ago. In the above code, when the
instruction "IN R16, PINA" is executed, the PINA register contains the
data, which was present at the pins one clock before. That is why the
NOP is put before the "IN R16, PINA" instruction. (If the NOP is
omitted, the read data is the data of the pins when the port was
output.)
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 8 (1/2)
 Write a test program for the AVR chip to toggle all the bits of
PORTB, PORTC, and PORTD every 1/4 of a second. Assume a
crystal frequency of 1 MHz.
 Solution:
LDI R16, 0xFF
.INCLUDE "M32DEF.INC" OUT DDRB, R16
LDI R16, HIGH(RAMEND) OUT DDRC, R16
OUT SPH, R16 OUT DDRD, R16
LDI R16, LOW(RAMEND) LDI R16, 0x55
L3:
OUT SPL, R16 ;initialize stack pointer
OUT PORTB, R16
OUT PORTC, Rl6
OUT PORTD, Rl6
CALL DELAY
COM R16
RJMP L3

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 8 (2/2)
;---------------1/4 SECOND DELAY
DELAY:
LDI R21, 200
D1: LDI R22, 250
D2: NOP
NOP
DEC R22
BRNE D2
DEC R21
BRNE D1
RET
 Calculations:
1/1MHz =1 s  1 MC = 1 s
Delay = 200 x 250 x 5 x 1 s = 250,000 s (If we include
the overhead, we will have 250,608 us.)
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Topics
 AVR pin out
 I/O port programming
 I/O Bit manipulation
programming
 Simple Digital Input/Output
devices

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
SBI and CBI instructions
 SBI (Set Bit in IO register)
SBI A,b ; I/O(A,b) ← 1 where 0 ≤ A ≤ 31, 0 ≤ b ≤ 7
 SBI ioReg, bit ;ioReg.bit  1
 Examples:

SBI PORTD,0 ;PORTD.0 = 1

SBI DDRC,5 ;DDRC.5 = 1
 CBI (Clear Bit in IO register)
CBI A,b ; I/O(A,b) ← 0 where 0 ≤ A ≤ 31, 0 ≤ b ≤ 7
 CBI ioReg, bit ;ioReg.bit  0
 Examples:

CBI PORTD,0 ;PORTD.0 = 0

CBI DDRC,5 ;DDRC.5 = 0

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example
 Write a program that toggles PORTA.4
continuously.

.INCLUDE “M32DEF.INC”
SBI DDRA,4
L1: SBI PORTA,4
CBI PORTA,4
RJMP L1

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 4-2
 An LED is connected to each pin of Port D. Write a
program to turn on each LED from pin D0 to pin D7.
Call a delay module before turning on the next LED.
.INCLUDE "M32DEF.INC"
LDI R20, 0xFF
OUT DDRD, R20 ;make PORTD an output port
SBI PORTD,0 ;set bit PD0
CALL DELAY ;delay before next one
SBI PORTD,1 ;turn on PD1
CALL DELAY ;delay before next one
SBI PORTD,2 ;turn on PD2
CALL DELAY
SBI PORTD,3
CALL DELAY
SBI PORTD,4
CALL DELAY
SBI PORTD,5
CALL DELAY
SBI PORTD,6
CALL DELAY
SBI PORTD,7
CALL DELAY
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 4-3 (1/3)
 Write the following programs:
(a) Create a square wave of 50% duty cycle on bit 0 of Port
C.
(b) Create a square wave of 66% duty cycle on bit 3 of Port
C.
duty cycle = 50% (TON = TOFF)

duty cycle = 66% (TON = 2TOFF)

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 4-3 (2/3)
 Solution: (a)
.INCLUDE "M32DEF. INC“
LDI R20, HIGH(RAMEND)
OUT SPH, R20
LDI R20, LOW (RAMEND)
OUT SPL, R20 ;initialize stack pointer
SBI DDRC, 0 ;set bit 0 of DDRC (PCO = Output)
HERE:
SBI PORTC, 0 ;set to HIGH PCO (PCO = 1)
CALL DELAY ;call the delay subroutine
CBI PORTC, 0 ; PCO = 0
CALL DELAY
RJMP HERE ; keep doing it
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 4-3 (3/3)
 Solution: (b)
.INCLUDE "M32DEF. INC“
LDI R20, HIGH(RAMEND)
OUT SPH, R20
LDI R20, LOW (RAMEND)
OUT SPL, R20 ;initialize stack pointer
SBI DDRC, 3 ;set bit 3 of DDRC (PC3 = out)
HERE:
SBI PORTC, 3 ;set to HIGH PC3 (PC3 = 1)
CALL DELAY ;call the delay subroutine
CALL DELAY ;call the delay subroutine
CBI PORTC, 3 ;PC3 = 0
CALL DELAY
RJMP HERE ;keep doing it
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
SBIC and SBIS
 SBIC (Skip if Bit in IO register Cleared)
SBIC A,b ; 0 ≤ A ≤ 31, 0 ≤ b ≤ 7
 SBIC ioReg, bit ; if (ioReg.bit = 0) skip next
instruction
 Example:
SBIC PORTD,0 ;skip next instruction if PORTD.0=0
INC R20
LDI R19,0x23

 SBIS (Skip if Bit in IO register Set)


SBIS A,b ; 0 ≤ A ≤ 31, 0 ≤ b ≤ 7
 SBIS ioReg, bit ; if (ioReg.bit = 1) skip next instruction
 Example:
SBIS PORTD,0 ;skip next instruction if PORTD.0=1
INC R20
LDI R19,0x23
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 4-4
 Write a program to perform the following:
(a) Keep monitoring the PB2 bit until it becomes HIGH;
(b) When PB2 becomes HIGH, write value $45 to Port C, and also
send a HIGH-to-LOW pulse to PD3.

.INCLUDE "M32DEF.INC"
CBI DDRB, 2;make PB2 an input
SBI PORTB,2
LDI R16, 0xFF
OUT DDRC, R16 ;make Port C an output port
SBI DDRD, 3;make PD3 an output
AGAIN: SBIS PINB, 2 ;Skip if Bit PB2 is HIGH
RJMP AGAIN ;keep checking if LOW
LDI R16, 0x45
OUT PORTC, R16 ;write 0x45 to port C
SBI PORTD, 3 ;set bit PD3 (H-to-L)
CBI PORTD, 3 ;clear bit PD3
HERE: RJMP HERE

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 4-5 (1/2)
 Assume that bit PB3 is an input and represents the
condition of a door alarm. If it goes LOW, it means that the
door is open. Monitor the bit continuously. Whenever it goes
LOW, send a HIGH-to-LOW pulse to port PCS to turn on a
buzzer.
 Solution:
.INCLUDE "M32DEF. INC"
CBI DDRB, 3 ;make PB3 an input
SBI DDRC, 5 ;make PC5 an output
HERE: SBIC PINB, 3 ;keep monitoring PB3 for
HIGH
RJMP HERE ;stay in the loop
SBI PORTC, 5 ; make PC5 HIGH
CBI PORTC, 5 ;make PC5 LOW for H-to-
L
AVR Microcontroller and EmbeddedRJMP HERE
System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 4-5 (2/2)

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 4-8
VCC
 A switch is connected to pin PB0 4.7k AVR
and an LED to pin PB7. Write a PB0
Switch
program to get the status of SW PB7
and send it to the LED. 270

LED

.INCLUDE "M32DEF.INC"
CBI DDRB,0 ;make PB0 an input
SBI DDRB,7 ;make PB7 an output
AGAIN: SBIC PINB,0 ;skip next if PB0 is clear
RJMP OVER ;(JMP is OK too)
CBI PORTB,7
RJMP AGAIN ;we can use JMP too
OVER: SBI PORTB,7
RJMP AGAIN ;we can use JMP too

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Topics
 AVR pin out
 I/O port programming
 I/O Bit manipulation
programming
 Simple Digital Input/Output
devices

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Input Mechanisms in Software
 Polling

Main loop checks each I/O device periodically.

If input is ready, processor initiates
communication.

 Interrupts

External hardware alerts the processor that input is
ready.

Processor suspends what it is doing.

Processor invokes an interrupt service routine
(ISR).

ISR interacts with the application concurrently.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Polling

Processor Setup Code

Processor checks I/O control register Ready


Processor services I/O 1
for status of peripheral 1
Not Ready

Processor checks I/O control register Ready Processor services I/O 2


for status of peripheral 2
Not Ready

Processor checks I/O control register


Ready Processor services I/O 3
for status of peripheral 3
Not Ready

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Interrupts
 Interrupt Service Routine (ISR)
Short subroutine that handles the interrupt

Processor Setup Code

Register the Interrupt Service Routine

Interrupt!
Context switch
Processor executes task code Run Interrupt Service Routine
Resume

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Input devices: switches and push buttons

Notes:
• SPST = Single Pole Single-Throw; DPST = Double Pole Single-Throw

• NO = Normally Open; NC = Normally Closed

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Switches and push buttons
Common types of switches

Common types of momentary push-buttons

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Output devices: LEDs

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 1: Whenever we press and hold the push
button “Test” then LED is ON; otherwise LED is OFF

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 2: It is similar to example 1 but with
different hardware

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 3: Whenever there is a falling edge on
PB0 then LED is toggled.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Seven-Segment LED Display

Note:
• dp or DP (decimal point); CC = Common Cathode; CA = Common Anode

• The required current per segment varies from about 2 mA (very small displays) to

about 60 mA (very large displays, 100mA or more).

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Interfacing LED array

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights

You might also like