0% found this document useful (0 votes)
5 views

5 - PIC Digital Input

The document provides detailed information on using digital inputs in microcontrollers, specifically focusing on configuring input pins, reading their states, and managing pull-up and pull-down resistor networks. It discusses the importance of avoiding floating inputs, detecting button presses, and handling debouncing issues to ensure accurate readings. Additionally, it includes code examples for implementing these concepts in practical applications, such as counting button presses and triggering sequences based on button states.

Uploaded by

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

5 - PIC Digital Input

The document provides detailed information on using digital inputs in microcontrollers, specifically focusing on configuring input pins, reading their states, and managing pull-up and pull-down resistor networks. It discusses the importance of avoiding floating inputs, detecting button presses, and handling debouncing issues to ensure accurate readings. Additionally, it includes code examples for implementing these concepts in practical applications, such as counting button presses and triggering sequences based on button states.

Uploaded by

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

PJEELA2

PIC Digital Input

Shastri Jayram (FROM: Deon Sabatta)


Digital Input

• TRISx Register bits must be set to 1 for a pin to be used as an


input
• PORTx Register is used to read the current state of the pin
• Vdd is the supply voltage whose acceptable limits are specified in
the datasheet. Vss is ground.
• Reading input
– Any voltage value above 0.5 x Vdd will be read as a 1
– Any voltage value less than 0.5 x Vdd will be read as a 0
– Voltage values in the region of 0.5 x Vdd can produce undefined results and
should be avoided.
– Input pin has a very high impedance (+- 600 MΩ) and thus will not draw a
large current. (typically nA)
– Do not exceed Vdd!

2
Pull-Up and Pull-Down Resistor Networks

• In a digital input pin is left


unconnected to ground or a
voltage source it is “Floating”.
• An input pin that is floating is
sensitive to having charges
induced on it or removed by
electrical fields and changes in
capacitance in its environment. Demo-board switch network:
• This can result in fluctuating
readings.
• To avoid this a pull-up or pull-down
resistor network is usually used at
a digital input.
3
Digital Input Waveform

4
Pull-Up and Pull-Down Resistor Networks

• A pull-up resistor network is used Pull-Up Resistor network:


to ensure the voltage at the input
pin is pulled to a high value when
the switch is open.
• A pull-down resistor network is
used to ensure that the voltage at
the input pin in pulled to ground
when the switch is open.
Pull-Down Resistor network:
• This arrangement also ensures
there is a low current flow from Vdd
to ground when the switch is
closed.
• Rin is much larger than the
external resistor.
5
Pull-Up and Pull-Down Resistor Networks

• The PIC18F45K20 has a set of programmable internal weak pull-


ups on PORTB as described in Section 10.0

• They are controlled by setting the bits in the WPUB register.

• These pull-up resistors are useful for small MCU applications


where and external resistor is difficult to place BUT it is always
preferable to use an external discrete pull-up or pull-down resistor
if possible.

• If this resistor fails then the MCU will still be operational and a
discrete resistor is more robust.

6
Basic example of digital input using demo
board switch

• The most basic way we can visualize the current state of the input
pin B0 will be to do the following:
– Using TRISB we set pin B0 to be an input, we leave the rest of PORT B set as
outputs
– We set all Port D pins as output.
– Inside an infinite loop we read the current state of Port B and set the value of
the LATD register to match.
• See example project DigitalInputBasic
void main(void) {
TRISB = 0b00000001; //Set Port B Pin 0 to be an input
TRISD = 0; //Set all Port D pins to be outputs

while(1) {
LATD = PORTB; //Read the current status of the input pins
// on Port B and place the value into LATD

return; 7
}
Basic example of digital input using demo
board switch

• It does not work!


• So we go and examine the datasheet section about Port B and
we see that by default the Port B pins are set as Analog Inputs on
Power-On Restart (POR)!
– We can either set the Configuration Bits PBADEN to be 1.
– OR we can just set the ANSELH Register to zero to disable the analog inputs.
• As the switch circuit is a pull-up circuit the result will be that LATD
pin 0 will be High until the button is pressed at which time it will
become Low.
void main(void) {
TRISB = 0b00000001; //Set Port B Pin 0 to be an input
TRISD = 0; //Set all Port D pins to be outputs

ANSELH = 0; //Turns off the Analog input on Port B which is enabled by default
//This is detailed in the datasheet Section 10.2
while(1) {
LATD = PORTB; //Read the current status of the input pins on Port B
//and place the value into 8LATD
}
}
Detecting a button press
Example project DigitalButtonPress_1

To react to a button press we cannot just use an If statement to


check if the button is current being pressed as this statement will
be true for every cycle that the button is down.
void main(void) {
TRISB = 0b00000001; //Set Port B Pin 0 to be an input
TRISD = 0; //Set all Port D pins to be outputs
ANSELH = 0; //Turns off the Analog input on Port B which is enabled by default

int buttonPressCount = 0; //Will count how many times the button has been pressed.
while(1) {
if(PORTBbits.RB0 == 0) { 3.5
buttonPressCount++;
} 3
} 2.5
} Button Voltage
2

Each cycle shown in plot is very 1.5

1
fast (around 25 µS) so even a 0.5
quick press will produce a few 0
0 5 10 15 20 25 30 35
counts. Cycles
9
7 down counts
Detecting a button press

• We want to detect a button press event where the state of the


button changes.
– From released to pressed which we will call a button press or falling edge
– From pressed to released which we will a button release or rising edge
• To detect these events we have to compare 2 subsequent button
states to see if they have CHANGED.
Button Press Button Release
3.5

2.5
Button Voltage

1.5

0.5

0 10
0 5 10 15 20 25 30 35
Cycles
Binary Counter Increment Example
Example Project DigitalButtonPress_2

Initialize registers and


Start
buttonState =1

• This project increments the value of


LATD when a Button Press event is
detected.
• This event is the case when the
button was Up and is now Down. Is Pin B0 == 0 AND was True Press Event
Code:
• The simplest way to do this is to use previous buttonState == 1
LATD = LATD + 1

a variable to remember what the state


of the button was during the last cycle
False
and check if it has changed during
this cycle.
Record current button
state:
buttonState = Pin B0

11
Button Press Detection

3.5

2.5
Button Voltage

1.5

0.5

0
0 5 10 15 20 25 30 35
Cycles

Initialize prevButtonState to first measurement


12
Button Press Detection

3.5

2.5
Button Voltage

1.5

0.5

0
0 5 10 15 20 25 30 35
Cycles

Compare prevButtonState with nextButtonState


Then nextButtonState becomes the prevButtonState 13
Button Press Detection

3.5

2.5
Button Voltage

1.5

0.5

0
0 5 10 15 20 25 30 35
Cycles

Compare prevButtonState with next button state


Then nextButtonState becomes the prevButtonState 14
Button Press Detection

3.5

2.5
Button Voltage

1.5

0.5

0
0 5 10 15 20 25 30 35
Cycles

Compare prevButtonState with next button state


Then nextButtonState becomes the prevButtonState 15
Button Press Detection

3.5

2.5
Button Voltage

1.5

0.5

0
0 5 10 15 20 25 30 35
Cycles

Compare prevButtonState with next button state


Then nextButtonState becomes the prevButtonState 16
Button Press Detection

3.5

2.5
Button Voltage

1.5

0.5

0
0 5 10 15 20 25 30 35
Cycles

Compare prevButtonState with next button state


Then nextButtonState becomes the prevButtonState 17
Button Press Detection

3.5

2.5
Button Voltage

1.5

0.5

0
0 5 10 15 20 25 30 35
Cycles

Compare prevButtonState with next button state


Then nextButtonState becomes the prevButtonState 18
Button Press Detection

3.5

2.5
Button Voltage

1.5

0.5

0
0 5 10 15 20 25 30 35
Cycles

Compare prevButtonState with next button state


Then nextButtonState becomes the prevButtonState 19
Button Press Detection

3.5

2.5
Button Voltage

1.5

0.5

0
0 5 10 15 20 25 30 35
Cycles

BUTTON PRESS DETECTED


prevButtonState = 1 and nextButtonState = 0 20
Detect Rising and Falling Edge
Example Project DigitalButtonPress_3

• In this project we are going to do something on both the falling


edge AND something on the rising edge.

• When the button is pressed (falling edge) we will toggle pin RD0

• When the button is released (rising edge) we will toggle pin RD7

• Lets go see the code!

21
Debouncing

• Mechanical switches never make perfectly clean contact during a


state change. This is due to the mechanical way in which the
contacts come together.
• Below is a trace of the bouncing that can occur with a pull-up
switch circuit:

22
Debouncing

• Depending on the severity of the bouncing these pulses can be


detected as multiple button presses as there are multiple falling
edges when a button is pressed.
• Not all buttons exhibit bad bouncing but some of you might notice
that with the previous project when you push the button the LATD
register increments more than once for a single button press.

23
Debouncing

• It is possible to deal with this problem using an external circuit


such as an RC filter as shown below. This circuit filters out the
high frequency bounce pulses as it needs to charge the capacitor
to a high enough voltage to trigger the digital gate which takes
some time.
• Other circuits exist for debouncing which
use shift registers to provide the delay.

• The basis of these circuits is to introduce a


time delay before the circuit indicates the
switch has been closed so that all the
“bouncing” effects have subsided before
the output state of the circuit changes.
24
Blocking While Loop

• There are times when you want to be able to wait at a point in


your code until a condition has been met.
• This is called Blocking and the easiest way to do it is to use a
blocking while loop.

while(Statement is true) { //Wait here until the statement become false


}

• This loop has no code in the body of the loop but will just loop
from the opening { to the closing } and then check the control
statement.
• This is termed spinning and it will spin until the statement become
false and the code will continue from after the closing }

25
Triggering a Sequence with a Blocking
While loop

• What if we want to write a program that waits for the button to


pressed and once its pressed it executes a sequence of
statements?

• Once the sequence is complete the program must once again


stop and wait for the button to be pressed.

• We will make use of a Blocking While Loop that checks if the


button is pressed and will spin while it is NOT pressed.

while(PORTBbits.RB0 != 0) {
}
26
Triggering a Sequence with a Blocking
While loop

Initialize all
Start registers and
variables

Has the button been


No pressed?

Yes

Execute our
sequence of
sending a single
LED riding up
the bits

27

You might also like