Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Debouncing function for switch in MPLAB XC8

Status
Not open for further replies.

engr_joni_ee

Advanced Member level 3
Joined
Nov 3, 2018
Messages
750
Helped
2
Reputation
4
Reaction score
4
Trophy points
18
Activity points
6,233
Hi,
I am looking for the debouncing function for switch in MPLAB XC8 that I can use with PIC 18F. Do they provide any library in MPLAB for debouncing ?
 

I don’t know if theres a library function, but it’s pretty easy to write your own routine.

1) Read the switch state
2) wait 10 ms
3) Read switch state again
4) if it’s the same, it’s a valid state.

There are more sophisticated methods, but it depends on what you need.
 

A retriggerable monostable multivibrator with
a timeout that exceeds the range of bounce-
interval, would be a piece-part solution - and
a model for code implementations.
 

There are literally thousands of code examples for this.
My favourite approach is the 10mS timer interrupt that polls the input pins (yes - this can be used for multiple input pins that need to be debounced) and keep record as a bit in a variable of the last value. If the current value is the same as the last one then set that in a variable that can be accessed by the main code as that will be the last debounced state of each pin.
(Pretty much the same as @barry's method but makes clear that you need a timer ISR, a couple of variables and can be used for multiple pis simultaneously.)
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top