2 - Ch-2 GPIO
2 - Ch-2 GPIO
Nurye Hassen
INPUT..?
• What is Input?
• Any signal that the microcontroller receive from the external world
• What is Output?
• Any signal that a microcontroller send to external world
MSB LSB
7 6 5 4 3 2 1 0
REGISTER FOR GPIO
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
• 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 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
0 — Input
1 — Output
DDRB 0 0 0 0 0 0 0 1
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 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
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
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
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!