5 - PIC Digital Input
5 - PIC Digital Input
2
Pull-Up and Pull-Down Resistor Networks
4
Pull-Up and Pull-Down Resistor Networks
• 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
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
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
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
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
11
Button Press Detection
3.5
2.5
Button Voltage
1.5
0.5
0
0 5 10 15 20 25 30 35
Cycles
3.5
2.5
Button Voltage
1.5
0.5
0
0 5 10 15 20 25 30 35
Cycles
3.5
2.5
Button Voltage
1.5
0.5
0
0 5 10 15 20 25 30 35
Cycles
3.5
2.5
Button Voltage
1.5
0.5
0
0 5 10 15 20 25 30 35
Cycles
3.5
2.5
Button Voltage
1.5
0.5
0
0 5 10 15 20 25 30 35
Cycles
3.5
2.5
Button Voltage
1.5
0.5
0
0 5 10 15 20 25 30 35
Cycles
3.5
2.5
Button Voltage
1.5
0.5
0
0 5 10 15 20 25 30 35
Cycles
3.5
2.5
Button Voltage
1.5
0.5
0
0 5 10 15 20 25 30 35
Cycles
3.5
2.5
Button Voltage
1.5
0.5
0
0 5 10 15 20 25 30 35
Cycles
• When the button is pressed (falling edge) we will toggle pin RD0
• When the button is released (rising edge) we will toggle pin RD7
21
Debouncing
22
Debouncing
23
Debouncing
• 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
while(PORTBbits.RB0 != 0) {
}
26
Triggering a Sequence with a Blocking
While loop
Initialize all
Start registers and
variables
Yes
Execute our
sequence of
sending a single
LED riding up
the bits
27