I/O Ports in AVR Microcontrollers: Sepehr Naimi
I/O Ports in AVR Microcontrollers: Sepehr Naimi
I/O Ports in AVR Microcontrollers: Sepehr Naimi
IENF641: Microcontrollers
Sepehr Naimi
Edited by:
Dr. Essam Alnatsheh
[email protected]
www.NicerLand.com
Topics
PROGRAM
ROM
Program
Bus Bus
CPU
Interrupt Other
OSC I/O Port
Unit Peripherals
I/O
PINS
ATMega16 Pinout & Descriptions
Port
Clears B
all the Port A
registers supply
Provides and
restart the
voltage to the Reference
These
chip. pins
execution are
of
It should voltage
Supplyfor ADC
voltage
used to connectto
be program
connected for ADC and
external+5 crystal
portA. Connect
or RC oscillator
it to VCC
Port C
Port D
ATMega16 Pinout
ATMega16 Pin out & Descriptions
ATMega16 Pin out & Descriptions
ATMega16 Pin out & Descriptions
Digital IO is the most fundamental mode of connecting a
microcontroller to external world. The interface is done using what
is called a PORT. A port is the point where internal data from
microcontroller chip comes out or external data goes in.
They are present is form of PINs of the IC. ATMega16 ports are
named PORTA, PORTB, PORTC, and PORTD.
ATMega16 Pin out & Descriptions
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)
(MOSI) PB5 PA5 (ADC5)
(MISO) PB6 PA6 (ADC6)
(SCK) PB7 PA7 (ADC7)
PINA
PORTB
DDRB
PINB
RESET DDRA AREF
PORTA
VCC AGND
PORTC
GND DDRC AVCC
PINC
XTAL2 PC7 (TOSC2)
XTAL1 PC6 (TOSC1)
(RXD) PD0 PC5 (TDI)
(TXD) PD1 PC4 (TDO)
(INT0) PD2 PC3 (TMS)
(INT1) PD3 PC2 (TCK)
(OC1B) PD4 PC1 (SDA)
(OC1A) PD5 PC0 (SCL)
(ICP) PD6 PD7 (OC2)
The structure of I/O pins
777
666
XTAL1 555 PB5 (SCK)
XTAL2 444 PB4 (MISO)
RESET 333 PB3 (MOSI/OC2A)
VCC
DDRx: 7 6 5 4 3 22 2 21 0 PB2 (SS/OC1B)
GND
PORTx: 7 6 5 4 3 12 1 11 0 PB1 (OC1A)
AVCC
PINx: 7 6 5 4 3 02 0 01 0 PB0 (ICP1/CLK0)
PINB
AREF DDRB
Px7 Px6 Px5 Px4 PORTB
Px3 Px2 Px1 Px0
PORTC PORTD
DDRC DDRD
PINC PIND
(ADC0) PC0 PD7 (AIN1)
000 777
(ADC1) PC1 666 PD6 (AIN0)
111
(ADC2) PC2 222 555 PD5 (T1)
(ADC3) PC3 333 444 PD4 (T0/XCK)
(ADC4/SDA) PC4 444 333 PD3 (INT1)
(ADC5/SCL) PC5 555 222 PD2 (INT0)
666 111 PD1 (TXD)
777 000 PD0 (RXD)
ATMega16 Pin out & Descriptions
ATMega16 Pin out & Descriptions
Defining a pin as either Input or Output – The DDRx Registers
DDRB = 0b01110101;
/* Configuring I/O pins of port B */
ATMega16 Pin out & Descriptions
Case 1 : To make a pin go high or low ( if it is an output pin)- Data Register PORTx
Pull-up resistor
Pull-up resistors are used in electronic logic circuits to ensure that
inputs to logic systems settle at expected logic levels if external devices
are disconnected or high-impedance
vcc
1 = Close
PORTx.n 0 = Open
pin n of
port x PINx.n
DDRx
DDRx.n 0 1
PORTx
PORTx.n high impedance Out 0
0
1 pull-up Out 1
PINx.n
ATMega16 Pin out & Descriptions
The PINx register gets the reading from the input pins of the microcontroller
Example 1
Write an AVR C program 777
666
PORTD.
RESET 333 PB3 (MOSI/OC2A)
VCC 222 PB2 (SS/OC1B)
GND 111 PB1 (OC1A)
AVCC 000 PB0 (ICP1/CLK0)
DDRD: 1 1 1 1 1 1 1 1 AREF
PINB
DDRB
PORTB
PORTD: 1 0 1 0 1 0 1 0 PORTC PORTD
DDRC DDRD
PINC PIND
(ADC0) PC0 PD7 (AIN1)
000 777
(ADC1) PC1 666 PD6 (AIN0)
111
#include <avr/io.h> (ADC2) PC2 222 555 PD5 (T1)
(ADC3) PC3 333 444 PD4 (T0/XCK)
(ADC4/SDA) PC4 444 333 PD3 (INT1)
555 222 PD2 (INT0)
int main () (ADC5/SCL) PC5
666 111 PD1 (TXD)
777 000
{ PD0 (RXD)
DDRD = 0xFF;
PORTD = 0xAA;
DDRx
0 1
PORTx
}
Example 2
Write a program that 777
666
PORTB one.
RESET 333 PB3 (MOSI/OC2A)
VCC 222 PB2 (SS/OC1B)
GND 111 PB1 (OC1A)
AVCC 000 PB0 (ICP1/CLK0)
DDRB: 1 1 1 1 1 1 1 1 AREF
PINB
DDRB
PORTB
PORTB: 1 1 1 1 1 1 1 1 PORTC PORTD
DDRC DDRD
PINC PIND
(ADC0) PC0 PD7 (AIN1)
000 777
(ADC1) PC1 666 PD6 (AIN0)
111
(ADC2) PC2 222 555 PD5 (T1)
#include <avr/io.h> (ADC3) PC3 333 444 PD4 (T0/XCK)
(ADC4/SDA) PC4 444 333 PD3 (INT1)
(ADC5/SCL) PC5 555 222 PD2 (INT0)
666 111
int main () 777 000
PD1 (TXD)
PD0 (RXD)
{
DDRB = 0xFF;
DDRx
PORTB = 0xFF; 0 1
PORTx
In Atmel Studio:
It is compiler dependant
I/O programming in Microcontroller
DDRB = 0xFF;
while (1) {
PORTB = 0xFF;
_delay_ms(500);
PORTB = 0x55;
_delay_ms(500);
}
Digital Output
Solution:
An LED drops approximately 1.4V, leaving 3.6V.
I = (5V-1.4V)/1 = 3.6 A
Example:
Using the 470 resistor in series with the LED, how much
current will be able to flow with a 5V source?
Solution:
(5V-1.4V)/470 = 0.0077 Amps or 7.7mA
Building and Testing the Light Circuit
As the current path from Vdd(+) to Vss(-) is completed,
the LED will light.
int main ()
{
DDRB = 0xFF;
while(1)
{ PB0
PORTB = 0b00000001;
}
return 0;
}
Example 4
Write an AVR program to make the LED (pin PB0) blink
forever with 500ms time delay between “on” and “off” states.
#define F_CPU 4000000UL
#include <avr/io.h>
#include <util/delay.h>
int main ()
{
DDRB = 0xFF;
while(1)
{
PORTB = 0b00000001;
_delay_ms(500);
What happens as the arguments for
PORTB = 0b00000000;
_delay_ms(500); the delay instructions are changed?
} What happens if the while(1)
return 0; instructions are commented out ?
}
Example 5
Write a program to make two LEDs (pin PB0 and pin PB1)
blink in sequence with 800ms total delay time for every blink.
PB0
PB1
Write a program to make two LEDs (pin PB0 and pin PB1)
blink at the same time with 800ms total delay time for every
blink.
Example 6
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 time before turning on the next LED.
Counting and Repeating
Counting and making decisions based on a
value are key components to many programs.
int main ()
{
DDRB = 0xFF;
for(int i=1; i<=10; i++)
{
PORTB = 0b00000001;
_delay_ms(500);
PORTB = 0b00000000;
_delay_ms(500); What is the value of variable i
} when the loop is complete?
return 0;
}
Using a Bi-Color LED
A Bi-Color LED is one which will
light 2 different colors, such as
red or green, depending on
direction of current flow through
it. An example is a security light.
PB0
PB1
Using a Bi-Color LED
By using 2 I/O Pins PB0
and defining which is
Vss (LOW) and which
is Vdd (HIGH) the PB1
direction of current
flow can be
controlled.
PB0
PB1
Example 8
Write a program to shows how you can use PB0 and PB1 to
control the current flow in the bi-color LED circuit using the
following sequence:
make the bi-color LED red using HIGH PB0 and LOW PB1.
use 500ms as delay time
make the bicolor LED glow green by using LOW PB0 and
HIGH PB1.
use 500ms as delay time.
turn off the LED by sending low signals to both PB0 and
PB1. In other words, use LOW on both pins.
use 500ms as delay time and repeat again.
Bit Manipulations
Setting Bits
Clearing Bits
Toggling Bits
Bit-wise logical operators
PORTD = PORTD & ~(00000001 << 2);// apply the bit shift
PORTD = PORTD & ~(1 << 2); // write out the "1"
int main ()
{
DDRB = 0xFF;
while(1)
{
PORTB |=(0x01<<0); //Turn ON the LED
_delay_ms(500);
PORTB &=~(0x01<<0); //Turn OFF the LED
_delay_ms(500);
}
return 0;
}
Toggling Bits
A B XOR
0 0 0
XOR truth table:
0 1 1
1 0 1
1 1 0
int main ()
{
DDRB = 0xFF;
while(1)
{
PORTB ^=(0x01<<0); // toggle the state of the LED
_delay_ms(500);
}
return 0;
}
Example 11
Using the following code, describe how this circuit will work:
#define F_CPU 4000000UL
#include <avr/io.h>
#include <util/delay.h>
int main ()
{
DDRB = 0xFF;
while(1)
{
for(int i=0; i<=3; i++)
{
PORTB ^=(0x01<<i);
_delay_ms(200);
}
}
return 0;
}
ASSESSMENT
Make a 10-second countdown using one yellow LED
and one bicolor LED.
Make the bicolor LED start out red for 3 seconds
(connected at I/O pin PB0 ). After 3 seconds, change the
bicolor LED to green (connected at I/O pin PB1). When
the bicolor LED changes to green, flash the yellow LED
(connected at I/O pin PB2) on and off once every
second for ten seconds. When the yellow LED is done
flashing, the bicolor LED should switch back to red and
stay that way.
Draw the schematic diagram for the circuit.
References