AVR I/O port programming
I/O Port Pins and their functions, Role
of DDR/DDRx Registers in Input and
output operations
Introduction
• In the AVR microcontroller, ATmega32 ports are 8-bit
SFRs
• It has 4 ports, Port A, Port B, Port C and Port D
• Ports are gateways of the microcontroller
• They are used to bring in data from the outside world
into the MC in which case they are called input ports
• They are used to send out data into the outside
world from the MC in which case they are called
output ports
• Port are also used for alternative functions
Register addresses for ATMega32 ports
• Each port has three I/O registers
associated with it.
• They are designated as PORTx, DDRX,
and PINx.
• DDR stands for Data Direction
Register
• PIN stands for Port INput pins.
Relations between the registers and pins of
AVR
• Each of the I/O registers is 8 bits wide and each port has a
maximum of 8 pins
• Each bit of the I/O registers affects one of the pins (the content
of bit0 of DDRB represents the direction of the PB0 pin and so
on.
The following code will toggle all 8 bits of Port B forever
with some time delay between “on” and “off” states
Relations between the registers and pins of AVR
(contd…)
• The DDRX I/O register is used solely for the
purpose of making a given port an input or output
port.
• For example, to make a port an output, we write 1
s to the DDRx register.
LDI R16, 0xFF ;R16 = 0xFF = 0b11111111
OUT DDRB, R16 ; make port B an output port
• It must be noted that unless we set the DDRX bits
to one, the data will not go from the port register
to the pins of the AVR.
Upon reset, all ports have the value 0x00 in their
DDR registers. This means that all ports are
configured as input
Role of pull-up resistor
Let’s say you have one pin configured as an
input. If there is nothing connected to the pin
and your program reads the state of the pin, will
it be high (pulled to VCC) or low (pulled to
ground)? It is difficult to tell. This phenomena is
referred to as floating. To prevent this unknown
state, a pull-up or pull-down resistor will ensure
that the pin is in either a high or low state, while
also using a low amount of current.
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
If we want to make the pull-up registers of Port C
active, we must put 1s into the Port C register
Different states of a pin in AVR
microcontroller
The following code will continuously send out to Port A
the alternating values of 0x55 and 0xAA
In the following code, Port A is configured first as an input port by
writing all 0s to register DDRA,and then data is received from Port A
and saved in a RAM location
The following code will continuously send out to Port B
the alternating values of 0x55 and 0xAA
In the following code, Port B is configured first as an input port by
writing all 0s to register DDRA, and then data is received from Port
A and saved in a RAM location
Alternate functions of Port A and Port B
Alternate functions of Port C and Port D
Write a test program for the AVR chip to toggle all the
bits of PORT B, PORTC and PORTD approximately every
¼ of a second. Assume a crystal frequency of 1MHz
I/O Bit manipulation
Programming
I/O ports and bit-addressibility
• Sometimes we need to access only 1 or 2 bits of the
port instead of the entire 8 bits
• A powerful feature of AVR I/O ports is their
capability to access individual bits of the port
without altering the rest of the bits in that port
• Some single-bit instructions of the AVR
SBI (set bit in I/O register)
SBI ioReg, bit_number SBI PORTB, 5
CBI (clear bit in I/O register)
CBI ioReg, bit_num ber CBI PORTA, 2
I/O ports and bit-addressability
(contd..)
• Here ioreg refer to the SFRs that are
part of the I/O registers in the data
memory space with addresses
ranging from $00 to $3F
• bit_number is from 0 to 7 which
refer to individual bits of the i/o
register referenced
I/O ports and bit-addressability
(contd..)
• We have Instructions to make
decisions based on the status of a
given bit in the file register
• These single-bit instructions are
widely used for I/O operations
• They allow you to monitor a
single pin and make a decision
depending on whether it is 0 or 1
I/O ports and bit-addressability
(contd..)
• SBIS (Skip the next instruction if Bit in I/O
register is Set)
• SBIS ioreg, bit_number
SBIS PINB, 2, SBIS PORTA, 5
• SBIC (Skip the next instruction if Bit in I/O
register is Cleared)
• SBIC ioreg, bit_number
SBIC PINB, 2, SBIC PORTA, 5
Example:
An LED is connected to each pin of Port D. Write
an ALP to turn ON each LED from pin D0 to pin
D7. Call a delay subroutine before turning ON
the next LED
Example:
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
Example:
Write a program to perform the following:
(a) Keep monitoring PB2 bit until it becomes
HIGH
(b) When PB2 becomes HIGH, write the value
$45 to Port C, and also send a HIGH-to-LOW
pulse to PD3
Example:
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 pin PC5 to turn on a
buzzer.
Example:
A switch is connected to pin PB2. Write a
program to check the status of SW and
perform the following:
(a) If SW = 0, send letter ‘N’ to PORT D
(b) If SW = 1, send letter ‘Y’ to PORT D
.INCLUDE [Link]
CBI DDRB, 2 ; make PB2 an input
LDI R16, 0xFF
OUT DDRD, R16 ; make PORTD an input
AGAIN: SBIS PINB, 2 ;skip next if PB bit is HIGH
RJMP OVER ; SW is low
LDI R16, ‘Y’ ; R16 = ‘Y’ (ASCII Letter Y)
OUT PORTD, R16 ; PortD = ‘Y’
RJMP AGAIN
OVER: LDI R16, ‘N’ ; R16 = ‘N’ (ASCII Letter N)
OUT PORTD, R16 ; PortD = ‘N’
RJMP AGAIN