0% found this document useful (0 votes)
63 views

Chapter 4

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)
63 views

Chapter 4

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/ 28

I/O Ports in AVR

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
 The structure of I/O pins
 I/O programming
 Bit manipulation

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:
1. Power (XCK/T0) PB0 1
40 PIN DIP
40 PA0 (ADC0)
 VCC +5 V
(T1) PB1 2 39 PA1 (ADC1)
(INT2/AIN0) PB2 3 38 PA2 (ADC2)
 Ground (OC0/AIN1) PB3 4 MEGA32 37 PA3 (ADC3)

2. Crystal (SS) PB4


(MOSI) PB5
5
6
36
35
PA4 (ADC4)
PA5 (ADC5)
 XTAL1 +5 V
(MISO) PB6
(SCK) PB7
7
8
34
33
PA6 (ADC6)
PA7 (ADC7)
 XTAL2 RESET 9 32 AREF +5 V
VCC 10 31 AGND
3. Reset GND 11 30 AVCC
XTAL2 12 29 PC7 (TOSC2)
2. I/O pins XTAL1 13 28 PC6 (TOSC1)

PORTA, PORTB,
(RXD) PD0 14 27 PC5 (TDI)

(TXD) PD1 15 26 PC4 (TDO)

PORTC, and PORTD (INT0) PD2


(INT1) PD3
16
17
25
24
PC3 (TMS)
PC2 (TCK)
3. Internal ADC pins (OC1B) PD4
(OC1A) PD5
18
19
23
22
PC1 (SDA)
PC0 (SCL)
• AREF, AGND, AVCC (ICP) PD6 20 21 PD7 (OC2)

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)
(MOSI) PB5 5 55 55 5 PA5 (ADC5)
(MISO) PB6
DDRx:
6
7
66 6 5 4 3
6 26 61 0
PA6 (ADC6)
(SCK) PB7 7 77 77 7 PA7 (ADC7)
PORTx: 7 6 5 4 3
DDRA 2 1 0
DDRB
PINB
PORTB
RESETPINx: 7 6 5
PORTA
4 3 2 1 0 AREF
PINA
VCC AGND
GND Px7 Px6 Px5 Px4 Px3 Px2 Px1 Px0 AVCC
XTAL2 PC7 (TOSC2)
XTAL1 PC6 (TOSC1)
(RXD) PD0 PC5 (TDI)
(TXD) PD1 PC4 (TDO)

DDRx
PC3 (TMS) 00 11

DDRx
(INT0) PD2
PORTx
DDRx.n PORTx
(INT1) PD3 PC2 (TCK)
00 high
highimpedance
impedance Out
Out00
(OC1B) PD4 PORTx.n PC1 (SDA)
(OC1A) PD5 PC011(SCL) pull-up
pull-up Out
Out11
(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
There are three 8-bit registers associated with
each port

DDRx - data direction register


PORTx – output register
PINx – input register

x indicates port – A, B, C, or D
If DDR bit is zero, then pin is input pin
Else if DDR bit is one, then pin is output

Port Registers
Port registers allow for lower-level and faster manipulation of
the i/o pins of the microcontroller on an Arduino board. The
chips used on the Arduino board (the ATmega328) have
three ports:
When using ports, the first thing you do is
•Port B (digital pin 8 to 13) Configure the port to be input or ouput.
•Port C (analog input pins) By writing a value to the DDR register.
•Port D (digital pins 0 to 7)

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. (XCK/T0) PB0
(T1) PB1
Mega32/Mega16
PA0 (ADC0)
PA1 (ADC1)

Port A is used as an output port (INT2/AIN0) PB2


(OC0/AIN1) PB3
PA2 (ADC2)
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)
1. Configure port A as an output port XTAL1
(RXD) PD0
PC6 (TOSC1)
PC5 (TDI)
Send FF to DDRA (TXD) PD1
(INT0) PD2
PC4 (TDO)
PC3 (TMS)
2. Make all pins of port A high (INT1) PD3
(OC1B) PD4
PC2 (TCK)
PC1 (SDA)
Write FF to PORTA register (OC1A) PD5
(ICP) PD6
PC0 (SCL)
PD7 (OC2)

LDI R16,$FF

DDRx
OUT DDRA,R16 00 11

DDRx
PORTx
PORTx
OUT PORTA,R16
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 D
forever with some time delay between “on” and
“off” states:

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


OUT DDRD,R16 ;make Port B an output port (1111 1111)
L1: LDI R16,0x55 ;R16 = 0x55 = 0b01010101
OUT PORTD,R16 ;put 0x55 on port B pins
CALL DELAY
LDI R16,0xAA ;R16 = 0xAA = 0b10101010
OUT PORTD,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
Setup()
{pinMode(0,OUPUT);
pinMode(1,OUPUT);
pinMode(2,OUPUT);
pinMode(3,OUPUT);
Setup()
pinMode(0,OUPUT);
{
pinMode(0,OUPUT);
DDRD = 0XFF;
pinMode(0,OUPUT);
}
pinMode(0,OUPUT);}
Loop()
{
Loop()
PORTD = 0XFF;
{
Delay(500)
digitalWrite(0,HIGH);
PORTD = 0X00;
digitalWrite(1,HIGH);
Delay(500)
}
Delay(500)
digitalWrite(0,LOW);
digitalWrite(1,LOW);

Delay(500)
}

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 PORTB. Display 1
on the 7-segment.
DDRB: 1 1 1 1 1 1 1 1
PORTB: 0 0 0 0 0 1 1 0

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

DDRx
OUT PORTB,R20 ;PORTC = 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 PORTB. Display 3
on the 7-segment.
DDRB: 1 1 1 1 1 1 1 1
PORTB: 0 1 0 0 1 1 1 1

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

DDRx
OUT PORTB,R20 ;PORTC = 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:
READ FROM PORT C AND ADD 5 TO IT AND SEND TO PORT B

PORT C -> INPUT PORT (WRITE $00 TO DDRC)


PORT B -> OUTPUT PORT (WRITE $FF TO DDRB)

LDI R16,0
OUT DDRC,R16

LDI R16,$FF
OUT DDRB,R16

L1: IN R16,PINC
LDI R17,5
ADD R16,R17
OUT PORTB,R16
RJMP L1

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

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
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
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 MEGA32


(OC0/AIN1) PB3 4 37 PA3 (ADC3)
(SS) PB4 5 36 PA4 (ADC4)
(MOSI) PB5 6 35 PA5 (ADC5)

port B. (MISO) PB6


(SCK) PB7
7
8
34
33
PA6 (ADC6)
PA7 (ADC7)
RESET 9 32 AREF
VCC 10 31 AGND
.INCLUDE “M32DEF.INC” GND 11 30 AVCC
XTAL2 12 29 PC7 (TOSC2)
LDI R20,0x0 ;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)
LDI R20,0xFF ;R20 = 11111111 (binary) (INT1) PD3 17 24 PC2 (TCK)
(OC1B) PD4 18 23 PC1 (SDA)
OUT DDRB,R20 ;DDRB = R20 (OC1A) PD5 19 22 PC0 (SCL)
(ICP) PD6 20 21 PD7 (OC2)
L1: IN R20,PINA ;R20 = PINA
OUT PORTB,R20 ;PORTB = R20
RJMP L1

DDRx
00 11

DDRx
PORTx
PORTx
READ FROM PORT A AND WRITE TO PORT B
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
I/O bit manipulation programming

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
SBI and CBI instructions
 SBR (set bit in GPR)
 CBR (clear bit in GPR)

DDRx
 SBI (Set Bit in IO register) PORTx
 SBI ioReg, bit ;ioReg.bit = 1 PINx
 Examples:

 SBI PORTD,0 ;PORTD.0 = 1


 SBI DDRC,5 ;DDRC.5 = 1

 CBI (Clear Bit in IO register)


 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 PA.4 continuously.
 Toggle pin 4 of port A….

SBI DDRA,4

NXT: SBI PORTA,4


CBI PORTA,4
RJMP NXT

When using ports, the first thing you do is


Configure the port to be input or output.
By writing a value to the DDR register.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example
 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
SBIC and SBIS
 SBIC (Skip the next instruction if Bit in IO register
Cleared)
 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 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
 SBRC (Skip the next instruction if Bit in GPR
register Cleared)
 SBRC GPR, bit ; if (GPR.bit = 0) skip next instruction
 Example:
SBRC R20 ;skip next instruction if PORTD.0=0
INC R20
LDI R19,0x23

 SBRS (Skip if Bit in GPR register Set)


 SBRS GPR, bit ; if (GPR.bit = 1) skip next instruction
 Example:
SBRS R21,5 ;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
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.

LDI R16,$FF
OUT DDRC,R16
SBI DDRD,3
CBI DDRB,2

L1: SBIS PINB,2


RJMP L1
LDI R16,$45
OUT PORTC,R16
SBI PORTD,3
CBI PORTD,3
RJMP L1

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

status of SW and send it to 270

the LED. LED

READ FROM THE SWITCH AND SEND ITS VALUE TO PB7

SBI DDRB,7 SBI DDRB,7 CBI DDRB,0


CBI DDRB,0 CBI DDRB,0 SBI DDRB,7

AGN: SBIS PINB,0 AGN: SBIC PINB,0 BACK: SBIC PINB,0


RJMP NXT RJMP NXT SBI PORTB,7
SBI PORTB,7 CBI PORTB,7 SBIS PINB,0
RJMP AGN RJMP AGN CBI PORTB,7
NXT: CBI PORTB,7 NXT: SBI PORTB,7 RJMP BACK
RJMP AGN RJMP AGN

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

4.7k AVR

PB0
Switch
PB7
270

LED

CBI DDRB,0
SBI DDRB,7

BACK: SBIC PINB,0


SBI PORTB,7
SBIS PINB,0
CBI PORTB,7
RJMP BACK

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

SBI DDRC,0 SBI DDRC,3 SBI DDRC,3

L1: SBI PORTC,0 L1: SBI PORTC,3 L1: SBI PORTC,3


CALL DELAY CALL DELAY CALL DELAY
CBI PORTC,0 CALL DELAY CBI PORTC,3
CALL DELAY CBI PORTC,3 CALL DELAY
RJMP L1 CALL DELAY CALL DELAY
RJMP L1 RJMP L1

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
(a) 50% Duty Cycle

SBI DDRC,3

L1: SBI PORTC,3


CALL DELAY
CALL DELAY
CALL DELAY
CALL DELAY
CBI PORTC,3
CALL DELAY
RJMP L1

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
(b) 60% Duty Cycle

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
WRITE A PROGRAM TO GENERATE A SQUARE WAVE OF 80% DUTY CYCLE

WRITE A PROGRAM TO GENERATE A SQUARE WAVE OF 33% DUTY CYCLE

WRITE A PROGRAM TO GENERATE A SQUARE WAVE OF 75% DUTY CYCLE

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

You might also like