0% found this document useful (0 votes)
15 views28 pages

2 - Ch-2 GPIO

Gpio

Uploaded by

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

2 - Ch-2 GPIO

Gpio

Uploaded by

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

Real-time and Embedded Systems

CHAPTER 2: HARDWARE ARCHITECTURE FOR EMBEDDED SYSTEM


Ports, Registers, GPIO (General Purpose Input Output), Analog I/O
(ATmega328 microcontroller Architecture )

Nurye Hassen
INPUT..?
• What is Input?
• Any signal that the microcontroller receive from the external world

• Name few examples for input devices


• Switches, pushbuttons, sensors…
OUTPUT..?

• What is Output?
• Any signal that a microcontroller send to external world

• Name few examples for Output devices


• LED, LCD, 7-SEG…
REGISTER STRUCTURE

MSB LSB

7 6 5 4 3 2 1 0
REGISTER FOR GPIO

• DDRx - DATA DIRECTION REGISTER


• PORTx - DATA REGISTER
• PINx - INPUT PIN ADDRESS REGISTER
LOGICAL OPERATIONS

AND OR
0 0 0 0 0 0
0 1 0 0 1 1
1 0 0 1 0 1
1 1 1 1 1 1

Any value & (AND) zero will give zero


Any value | (OR) one will give one
CONTROL REGISTERS FOR GPIO

1. Data Direction Register


DDRx  8bit register used to configuring the direction of the pin
(Input/Output).
CONTROL REGISTERS FOR GPIO

• Data Direction Register


DDRB DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0

DDRC DDC7 DDC6 DDC5 DDC4 DDC3 DDC2 DDC1 DDC0

DDRD DDC7 DDD6 DDD5 DDD4 DDD3 DDD2 DDD1 DDD0


CONTROL REGISTERS FOR GPIO

DDRx  Values indicate whether the pin is input or output


• 0 — Input
• 1 — Output
METHODS FOR SETTING OR CLEARING
REGISTER BITS

• INDIVIDUAL BITS,
• 8 BIT DATA

• INDIVIDUAL BITS
We will set or clear a particular bit of the register without disturbing the state of other bits in
the register
• 8 BIT DATA
We can use this method to set or clear the bits in a register if we want to provide values for all
the bits in the register
INDIVIDUAL BITS SETTING IN REGISTER
DDRx  8bit register used to indicate the direction of the pin (Input/output ).

0 — Input
1 — Output

DDRB DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0

How we can make PORTB zero's bit as output


DDRB = DDRB | (1 << DDB0);
INDIVIDUAL BITS SETTING IN REGISTER
DDRx  8bit register used to indicate the direction of the pin (Input/output ).
0 — Input
1 — Output

DDRB DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0

Let us check by assuming DDB0 initially zero


DDRB = DDRB | (1 << DDB0);

DDRB 0 0 0 0 0 0 0 0

(1 << DDB0) 0 0 0 0 0 0 0 1

DDRB 0 0 0 0 0 0 0 1
INDIVIDUAL BITS SETTING IN REGISTER
DDRx  8bit register used to indicate the direction of the pin (Input/output ).
0 — Input
1 — Output

DDRB DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0


How we can make PORTB zero's bit as output
DDRB = DDRB | (1 << DDB0);
DDRB |= (1 << DDB0);
Similarly, to assign DDB0, DDB1 as an output
DDRB |= (1 << DDB0) | (1 << DDB1); DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0
To assign DDB0, DDB1, DDB2, DDB3 an output
DDRB |= (1 << DDB0) | (1 << DDB1) | (1 << DDB2) | (1 << DDB3);

DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0


INDIVIDUAL BITS SETTING IN REGISTER
DDRx  8bit register used to indicate the direction of the pin (Input/output ).

0 — Input
1 — Output

DDRB DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0

Similarly, to configure DDB0 as input


DDRB = DDRB & (~(1 << DDB0));
INDIVIDUAL BITS SETTING IN REGISTER
DDRx  8bit register used to indicate the direction of the pin (Input/output ).
0 — Input
1 — Output
DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0
DDRB
Let us check by assuming DDB0 initially one
DDRB = DDRB & (~(1 << DDB0));

DDRB 0 0 0 0 0 0 0 1

(1 << DDB0) 0 0 0 0 0 0 0 1 &


(~(1 << DDB0)) 1 1 1 1 1 1 1 0

DDRB 0 0 0 0 0 0 0 0
INDIVIDUAL BITS SETTING IN REGISTER
DDRx  8bit register used to indicate the direction of the pin (Input/output ).
0 — Input
1 — Output

DDRB DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0

How we can make PORTB zero's bit as input


DDRB = DDRB & (~(1 << DDB0));  DDRB &= (~(1 << DDB0));
Similarly, to assign DDB0, DDB1 as an input
DDRB &= (~(1 << DDB0)) & (~(1 << DDB1)) ; DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0
To assign DDB0, DDB1, DDB2, DDB3 an input
DDRB &= (~(1 << DDB0)) & (~(1 << DDB1)) &= (~(1 << DDB2)) & (~(1 << DDB3))
DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0
8 BIT DATA SETTING IN REGISTER
DDRx  8bit register used to indicate the direction of the pin (Input/output ).
0 — Input
1 — Output

DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0


DDRB
To assign DDB0, DDB1, DDB2, DDB3 an input and DDB4, DDB5, DDB6, DDB7 as output
DDRB = 0b11110000;  DDRB = 0xF0;

DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0

DDRB 1 1 1 1 1 1 0 0
HEXADECIMAL BINARY
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
8 BIT DATA SETTING IN REGISTER
DDRx  8bit register used to indicate the direction of the pin (Input/output ).
0 — Input
1 — Output

DDRB DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0

To assign DDB2, DDB4, DDB5 as an input and DDB0, DDB1, DDB3, DDB6, DDB7 as output

1 1 0 0 1 0 1 1
DDRB = 0b11001011;
 DDRB = 0xCB;
CONTROL REGISTERS FOR GPIO
2. Data Register - PORTx
(Where x can be B, C, or D depending on which port registers are being addressed).
If pin is configured as output:
 These are 8bit register used to set the logic on the pins HIGH or LOW.
 Writing a one to the bits in this register puts a HIGH logic (5V) on those pins.
 Whereas writing a zero to the bits in this register puts a LOW logic (0V) on those pins.
All bits in these registers can be read as well as written to.
PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0
PORTB
Example: For using the PORTx register of Port B to write a value 0x55 to Port B.
PORTB = 0b01010101;  PORTB = 0x55;
CONTROL REGISTERS FOR GPIO
2. Data Register - PORTx - x can be B, C, or D

0 — Input
1 — Output
PORTB PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0

Example: To turn ON PB0 If pin is configured as input PORTx is used enable


PORTB =| (1<< PB0) or disable the pull up of the pin
To turn OFF PB0
PORTB &= (~(1<<PB0))
CONTROL REGISTERS FOR GPIO
3. INPUT PIN ADDRESS REGISTER - PINx
(Where x can be A, B, C, or D depending on which port registers are being addressed).

These are 8bit register used to read the status of a pins when configured as input.
 These bits are read-only bits and cannot be written to.
PINB PINB7 PINB6 PINB5 PINB4 PINB3 PINB2 PINB1 PINB0
Example: To read the status of PB0
if (PINB & (1<<PINBO))
{

}
else
{

}
CONTROL REGISTERS FOR GPIO
3. INPUT PIN ADDRESS REGISTER - PINx
(Where x can be A, B, C, or D depending on which port registers are being addressed).

These are 8bit register used to read the status of a pins when configured as input.
 These bits are read-only bits and cannot be written to.
PINB PINB7 PINB6 PINB5 PINB4 PINB3 PINB2 PINB1 PINB0
Example: To read the status of PB0
0 0 0 0 0 0 0 0
if (PINB & (1<<PINBO)) PINB
{ &
0 0 0 0 0 0 0 1
(1<<PINBO)
}
else
{ (PINB & (1<<PINBO)) 0 0 0 0 0 0 0 0

}
ARDUINO UNO GPIO PIN DIAGRAM
DIGITAL INPUT WITH PULL-UP RESISTOR

• Sometimes switching between one state to another or pins configured as input with
nothing connected to them may arise situation of High-impedance state i.e. floating
state.
• This state may report random changes in pin state.
• To avoid this state, there is option of adding pull-up (to +5V) or pull-down (to Gnd)
resistor which helps to set the input to a known state.
DIGITAL INPUT WITH PULL-UP RESISTOR

• Arduino (ATmega) has in-built configurable pull-up resistor.


• When connecting a device or sensor to a pin configured as input with pull-up, the other end
should be connected to ground.
EXERCISE!

1. Write a c-program that controls 8 LEDs. Out of the 8 LEDs, only the four
are ON at a time. When the first four are ON, the others are OFF and vice
versa. You can use any port available to you. The LEDs are controlled by
an external switch
2. Write a c-program that controls 8-leds separately. Each led is controlled by
its own switch. This means there are 8-leds and 8- switches controlling the
LEDs.
3. Repeat Exercise-2 using only three switches. Only one of the 8-leds
should be at a time
Thank You!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy