I/O Ports in AVR Microcontrollers: Sepehr Naimi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

College of Engineering

IENF641: Microcontrollers

I/O Ports in AVR Microcontrollers


Chapter 2

Sepehr Naimi
Edited by:
Dr. Essam Alnatsheh
[email protected]
www.NicerLand.com
Topics

 AVR pin out


 The structure of I/O pins
 I/O programming
 Bit manipulating
I/O unit in AVR

RAM EEPROM Timers

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

Outside the Inside the


AVR chip AVR chip
ATMega16 Pin out & Descriptions
Case 2 : To activate / Deactivate pull up resistors-Data Register PORTx

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

to send value 0xAA to


XTAL1 555 PB5 (SCK)
XTAL2 444 PB4 (MISO)

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

0 high impedance Out 0


while (1);
return 0; 1 pull-up Out 1

}
Example 2
 Write a program that 777
666

makes all the pins of


XTAL1 555 PB5 (SCK)
XTAL2 444 PB4 (MISO)

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

0 high impedance Out 0


while (1);
1 pull-up Out 1
return 0;
}
Time Delays in Microcontroller
 You can use predefined functions of compilers to make time
delays

In Atmel Studio:

First you should include:


#define F_CPU 4000000UL
#include <util/delay.h>

and then you can use


_delay_us(200); //200 microseconds
_delay_ms(100); //100 milliseconds

 It is compiler dependant
I/O programming in Microcontroller

Byte size I/O programming in Microcontroller

DDRB = 0xFF;
while (1) {
PORTB = 0xFF;
_delay_ms(500);
PORTB = 0x55;
_delay_ms(500);
}
Digital Output

Controlling Lights with Microcontrollers


Indicator Lights
Indicator lights are so common to almost be not
noticed.

Using lights for indication is simply a matter of


connecting and disconnecting them from a
power source.
The LED as a Light
An LED, Light Emitting Diode, is a very popular choice as an
indicator light because of its low power use and
extremely long life.

It is quite simple to control with the low voltage of the


microcontroller but requires:
 A resistor to limit the current.

 Being connected in the correct orientation.


The LED as a Light
Example:
From Ohm's Law, if V is 5 volt source and
R is 1 , how much current will try to
flow?
Note: An LED drops approximately 1.4V.

Solution:
An LED drops approximately 1.4V, leaving 3.6V.
I = (5V-1.4V)/1 = 3.6 A

• 3.6 A can destroy the LED.


• The maximum current a typical LED can handle is around
30mA, or .030 A.
The LED as a Light

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.

What happens if the LED is reversed?


What happens if a 1K ohm resistor is used?
Example 3
 Write an AVR C program that
make the LED connected to
pin PB0 will light.
DDRB: 1 1 1 1 1 1 1 1
PORTB: 0 0 0 0 0 0 0 1
#include <avr/io.h>

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.

 It is desired to make the LED blink 10 times


through looping and then stop.

 A FOR loop is a simple structure to count


between 2 values.
Example 7
 Write an AVR program to make the LED (pin PB0) blink 10
times 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;
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.

 The bi-color LED is simply 2


LED elements in a single
package connected in opposite
ways.
Using a Bi-Color LED
Controlling a bi-color LED with the microcontroller
requires two I/O pins.

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

1110 1111 1110 1111


& 0000 0001 | 0000 0001 ~ 1110 1011
-------------- -------------- --------------
0000 0001 1110 1111 0001 0100
Shift operations in C
 data >> number of bits to be shifted right
 data << number of bits to be shifted left

1110 0000 >> 3 0000 0001 <<2


-------------- --------------
0001 1100 0000 0100
Setting a bit in a Byte to 1
 We can use | operator to set a bit of a byte to 1
xxxx xxxx xxxx xxxx
| 0010 0000 OR | 1 << 5
------------- -------------
xx1x xxxx xx1x xxxx
We'd like to send 5 V to PC5, so we set PORTC bit 5 to 1:
PORTC = PORTC | (00100000); // apply the bit-wise OR
PORTC = PORTC | (00000001 << 5); // apply the bit shift

PORTC = PORTC | (1 << 5); // re-write the "1" as an


// 8 bit binary value
PORTC |= (1<<5); //set bit 5 (6th bit) of PORTC
Clearing a bit in a Byte to 0
 We can use & operator to set a bit of a byte to 0
xxxx xxxx xxxx xxxx
& 1110 1111 OR & ~(1 << 4)
------------- -------------
xxx0 xxxx xxx0 xxxx
We'd like to send 0 V to PD2, so we clear PORTD bit 2:
PORTD = PORTD & (11111011); // apply the bit-wise AND
PORTD = PORTD & ~(00000100); // apply the bit-wise NOT

PORTD = PORTD & ~(00000001 << 2);// apply the bit shift
PORTD = PORTD & ~(1 << 2); // write out the "1"

PORTD &= ~(1<<2); //clear bit 2 (3rd bit) of PORTD


Example 9
 Using the bit-wise logical operators, write an AVR program to
make the LED (pin PB0) blink forever with 500ms delay time.
#define F_CPU 4000000UL
#include <avr/io.h>
#include <util/delay.h>

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

If we XOR anything with If we XOR anything with


a 0, it remains what is a 1, it toggles what the
was before: value was before:
0^0=0 0^1=1
1^0=1 1^1=0
X^0=X X^1=Y
(Y means the toggled value of X)
Toggling Bits
 We can use ^ operator to invert the state of a pin
xxxx xxxx xxxx xxxx
^ 0010 0000 OR ^ 1 << 5
------------- -------------
xxYx xxxx xxYx xxxx
We'd like to toggle the state of PC5 (PORTC bit 5 to 1):
PORTC = PORTC ^ (00100000); // apply the XOR
PORTC = PORTC ^ (00000001 << 5); // apply the bit shift

PORTC = PORTC ^ (1 << 5); // re-write the "1" as an


// 8 bit binary value
PORTC ^= (1<<5); // toggle bit 5 (6th bit) of PORTC
Example 10
 Using the toggle operators, write an AVR program to make
the LED (pin PB0) blink forever with 500ms delay time.
#define F_CPU 4000000UL
#include <avr/io.h>
#include <util/delay.h>

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 

You might also like