This document discusses I/O ports, how to use them, and handling the bouncing problem with switches. It explains that I/O ports allow communication between a microcontroller and the outside world by reading and writing voltage levels on pins. The direction of pins is set by a TRIS register. Switches connected to pins can bounce, so software reads the pin multiple times with a delay to filter out false readings. LEDs are used as simple outputs, requiring current limiting resistors. Sample code is provided to output patterns on one port based on inputs to another, including a function to handle switch bouncing.
2. AGENDA
I/O ports and how to use them
Some exceptions with some ports.
Handling the bouncing problem.
3. AGENDA
I/O ports and how to use them
Some exceptions with some ports.
Handling the bouncing problem.
4.
5. I/O PORTS
Every microcontroller must have means of communication to the
outside world.
The simplest way for communication is using general purpose I/O
ports.
6. I/O PORTS
Reading a port:
- Means reading the status (voltage level) present on the
-
pin.
Writing to a port:
Means writing to the port latches.
- You have to determine the direction of the I/O pin before
using it, this is done by changing the value of the TRIS
register.
7. I/O PORTS
Example on reading a port
If the voltages: 0.5v, 4v, 2v were present on three different pins.
If you read these pins, you’ll get the digital logic value
corresponds to each voltage level 0 1 0.
Considering the range of ‘0’ : 0v 2.5v
the range of ‘1’ : 2.5v 5v
8. I/O PORTS
The I/O pin direction is controlled by a register called TRIS.
PORT<x> is controlled by the register TRIS<x>.
If you write ‘1’ in a bit in TRIS<x> register, this means that the
corresponding bit in PORT<x> is input.
A ‘0’ means output.
9. I/O PORTS
HOW?
Setting a bit in the TRIS<x> register puts the
corresponding output driver in a high-impedance input
mode (O/P mode is off).
Clearing a bit in the TRISB register puts the contents of
the output latch on the selected pin(s). (O/P mode is on)
I/O ports are multiplexed with other peripherals, if these
peripherals were used you won’t use the pin as I/O pin.
10.
11. I/O PORTS
Notes:
1)
If port pins are multiplexed with analog peripherals, you must select
the function of them to be analog input or digital I/O, you do so by
changing the value of ADCON1 register.
2)
For analog inputs, make the corresponding TRIS 1.
12. I/O PORTS
3)
-
-
Each of the PORTB pins has a weak internal pull-up resistors.
A single control bit can turn on all the pull-ups. This is performed by
clearing bit RBPU (OPTION_REG<7>).
The weak pull-up is automatically turned off when the port pin is
configured as an output.
13. I/O PORTS
4)
RA4 pin is "Open-Drain" type
output pin. This means it cannot
source current and it will be high
impedance when assigned logic '1'
to it.
- You cannot set this pin as an output
pin by only using 'TRISA=0x00'.
The solution is attaching a pull up
resistor (10K) to the RA4 pin in
order to use this pin as an output
pin.
14. SWITCHES
We use switches to give an order to the MCU to do
something by changing the voltage level applied on
an I/O pin (input).
16. SWITCHES
-
-
-
When the switch is closed, the two contacts actually separate
and reconnect, typically 10 to 100 times in about 100ms.
This bouncing would send multiple key press signals to the
MCU which is not what you want.
While 100 milliseconds is quite short for us as humans, it is
actually quite long for a digital circuit and the period between
spikes is often enough for the application to respond.
17. SWITCHES
Solution:
1-Using RC circuit:
- We use it as a LPF, as the ripples
happen very fast (high frequency).
2-By software:
- By reading the value of the pin more than one time in small
time intervals to make sure of the real value on the pin.
-
This done by testing the value, wait for some time (110 ms)
and test again if the value is the same, so it’s the true value.
18. LEDS
It’s the simplest way to indicate an
output.
We have to use a current limiting
resistor with it (typically 330Ω).
20. ASSIGNMENT
-
Write a code for a MCU that outputs these results on PORTC<5:7>
for the inputs entered on PORTB<3:5>.
Write a function that handles the bouncing problem.
PORTB (input)
PORTC (output)
000
010
001
110
010
011
001
001