EMBEDDED SYSTEMS
GPIO PORTS
Dr. Khalil Ismail Khalil Yousef
Assoc. Prof., Electrical Eng. Dept.
Faculty of Eng., Assiut University
kyousef@aun.edu.eg
EMBEDDED SYSTEMS
GPIO
The first input/output interface will use the parallel ports or
General Purpose Input Output (GPIO) allowing us to
exchange digital information with the external world.
GPIO have their own general purpose input/output registers
used to input data to the MCU and to output data from it (in
digital form).
GPIO must be configured as input or output so they can be used.
Controlling the data direction occurs through writing values of 1
and 0 in a specific register (special function register in RAM).
Register names vary from microcontroller to another one.
GPIO
We will learn how to use AVR ports and program them to read or
write from port pins.
Now the port has multiple pins associated with it.
For example ATmega16 has 8-bit port, i.e. it has 8 pins in a single
port. Each bit represents a pin.
As you can see in the diagram, ATmega16 has 4 ports named
as A, B, C & D. Each of these ports has 8-pins (MCU pins).
Note that: These port pins might have multiple functionality i.e.
you can use them as GPIO or as UART or ADC etc. You can use a
port pin for a single functionality at a time.
ATMEGA16 PIN DIAGRAM
Port A (pin 33 ~ pin 40)
Port B (pin 1 ~ pin 8)
Port C (pin 22 ~ pin 29)
Port D (pin 14 ~ pin 21)
ATMEGA16 PIN DIAGRAM
GPIO REGISTERS
Every GPIO has three registers associated with it in order to
control a particular pin. For AVR microcontrollers these registers
are:
1. DDRn – Data Direction Register
2. PORTn – Port Output data Register
3. PINn – Port Input Register
where n- Indicates the port name i.e. A, B, C & D.
REGISTER DESCRIPTION FOR I/O PORTS
• PORTn set a port pin’s value either LOW or as HIGH.
• DDRn configures a port pin either an INPUT or OUTPUT.
• PINn reads a port pin’s current status.
DDRN REGISTER
Data Direction Register (DDR) configures data direction of a
port or a port pin. A port will be used for input or output.
DDRn Register 0 1 1 1 0 0 1 1
Writing a value 0 configure that port I/O
pin as INPUT and writing a value 1 STATE
configures a port pin as OUTPUT.
IN OUT OUT OUT IN IN OUT OUT
DDRN REGISTER
Let’s assume Port D should be configured as OUTPUT:
• DDRD = 0xFF; //HEX NOTATION
• DDRD = 0b11111111; //BINARY NOTATION
Now, if we want to configure port D as INPUT:
• DDRD =0x00; 0b00; // Binary and HEX Notation
Note that: it’s not necessary to configure a whole port as input or
output. You can configure a single pin as input or output as well.
Suppose you want to configure only 2nd bit (pin) as OUTPUT:
• DDRD |= 0x02;
PORTN REGISTER (1)
PORTn register is used to output data when port is configured as
output.
PORTn Register 0 1 1 1 0 0 1 1
When configured as output, PORTn
register controls the HIGH/LOW state of WRITE
a port pin. Writing value 1 places that port
pin in HIGH state and writing a value 0
places that port in LOW state.
OUT OUT OUT OUT OUT OUT OUT OUT
If all the pins in PORTn are
configured as OUTPUT.
LOW HIGH HIGH HIGH LOW LOW HIGH HIGH
PORTN REGISTER (2)
PORTn register is used to activate/deactivate internal pull-up registers when
port is configures as input.
VCC
PORTn Register
0 1 1 1 0 0 1 1
When you set bits in DDRn to 0, i.e.
make port pins as inputs, then
corresponding bits in PORTn register
are used to activate/deactivate pull-up
registers associated with that pin. In
order to activate pull-up resister, set bit IN IN IN IN IN IN IN IN
in PORTn to 1, and to deactivate (i.e to
make port pin tri stated) set it to 0.
PORTN REGISTER
Let’s assume that we want to set port D’s pins in high state, then
first we have to configure port D as output using DDRD register
and then we’ll place it in high state using PORTD register.
• DDRD =0xFF; //Configure Port D as OUPUT
• PORTD=0xFF;//Place Port D in High state
Let’s you want to activate internal pull-up registers on port D then
first you have to configure port D as input using DDRD register
and then activate pull-up registers using PORTD register:
• DDRD =0x00;// Port D configured as INPUT
• PORTD=0xFF;//Pull-up registers activated on Port D
PINN REGISTER
The PINn register keeps the status of all the pins in that port.
PINn Register 0 1 1 1 0 0 1 1
By reading this register we
can get the current logic
level (0 or 1) on the pin.
READ
If the pins in PORTn are
configured as INPUT:
PINn register reads IN IN IN IN IN IN IN IN
incoming state (value) at
the pin.
LOW HIGH HIGH HIGH LOW LOW HIGH HIGH
PINN REGISTER
intinial status;
• DDRD =0x00; //Configure Port D as input
• status =PIND; //Read status of all the 8 pins in status
SWITCH DE-BOUNCING RESOLVE
SWITCH DE-BOUNCING
It can be implemented in 4 ways,
1. Hardware Debouncing
2. RC Debouncing
3. Software Debouncing
4. Using Debouncing IC’s
1. HARDWARE DEBOUNCING
Hardware debouncing technique uses an S-R latch to avoid
bounces in the circuit along with the pull up resistors. S-R circuit
is most effective of all debouncing approaches
2. RC DEBOUNCING
The S-R circuit is common but the bulkiness of the circuit causes
it to be used rarely also SPDT switches are costlier than SPST
(Single Pole Single Throw) switch. Another method of
debouncing is to use a R-C circuit. The basic idea behind such
circuit is to use a capacitor to filter out quick changes in the
switch signal.
3. SOFTWARE DEBOUNCING IC
Software debouncing can be used while suggests delaying the
reception of the intended signal till get bounced (settled).
delay function can be used,
Looping may be also suggested.
4. SWITCH DEBOUNCING IC
Switch debouncing can also be done by some specialized IC.
They are only a few of them and rarely used.
MAX6816, MAX 6817, MAX 6818
MC 14490
Switch Debouncing using IC MAX6816
DEBOUNCING AND EFFECT ON PERFORMANCE
GUI PROGRAMMING
GPIO PROJECTS
7 Segment Display
The seven segment is a very common
display method that can display
numbers from 0 to 9 and in some
cases the decimal point. It consists of
7 LED connected together by their
anodes (common anodes) or by their
cathodes (common cathodes ) the
different numbers are displayed by
selecting some LEDs to be ON and
others to be OFF. Example to show
number 1 LEDs b and c are turned
ON while the rest are set to OFF.
GPIO PROJECTS
LCD
A 2x16 Liquid Crystal Display (LCD)
can be used to display numbers
(0~9), letters (a-z, A~Z), special
letters (*&#$@()[]{}), and words
(names, statements, requests, etc
…….).
It can also be used to show the
pressed keys by the user.
Each LCD has its own decoder.
GPIO PROJECTS
Matrix Keypad (4x3)
The rows A0 to A3 are connected to Input lines of
Microcontroller. The i/o pins where they are connected
are made Input. This is done by setting the proper DDR
Register in AVR. The column C0 to C3 are also connected
to MCUs i/o line. These are kept at High Impedance
State, in high z state (z= impedance) state these pins are
neither HIGH or LOW they are in TRISTATE (floating).
And in their PORT value we set them all as low, so as
soon as we change their DDR bit to 1 they become output
with value LOW.
One by One we make each Column LOW (from high Z
state) and read state of A0 to A3.
GPIO PROJECTS
Matrix Keypad (4x3)
Project
Connect a (4x3) keypad and a LCD to ATmega16.
Display the pressed numbers sequentially on the LCD until
the LCD (2 lines) are full, then clear the LCD and start over.