Switch debounce is typically a software routine but with the Core Independent Peripherals (CIPs), switch debounce can be performed with hardware and no code requirements other than setting up the CIPs using the MPLAB® Code Configurator (MCC) within MPLAB X IDE.
Objective
When you activate a switch there is some period of time where the electrical contacts within the switch may bounce before settling.
A microcontroller configured to respond when an activated switch is detected could actually perceive this switch bouncing as multiple activations. Traditionally, in applications that use a microcontroller, switch bounce or noise is eliminated using a debounce software routine that will check for an activated switch, wait a short period of time to give the switch time to settle and then recheck to see if the switch is indeed still activated. If so, the microcontroller reacts accordingly. Otherwise, the first switch activation detection is ignored.
The problem with this method, other than forcing the developer to write and debug code, is that this algorithm ties up the CPU on the microcontroller preventing it from doing anything else while this debouncing routine is being executed.
However, there is a hardware-based core independent alternative available using 8-bit PIC® microcontrollers featuring Timer 2 with the Hardware Limit Timer (HLT) peripheral.
Timer 2 will automate the debouncing process using its monostable mode of operation. In this mode, the very first switch activation is used to start the timer counting, ignoring any subsequent bouncing. Once the timer count reaches a predetermined value, the timer peripheral will produce a signaling event that can be used to indicate that a valid switch activation has been detected.
In this example, the pushbutton (S2), connected to pin RA5 of the PIC16F18855 populating the MPLAB Xpress Evaluation Board, will be debounced. While the button is pressed all of the LEDs on the board will light.
MPLAB® Xpress IDE Edition - Episode 11 - Code Free Switch Debouncing Using the TMR2 with HLT
Materials
Hardware Tools (Optional)
Tool | About | Purchase |
---|---|---|
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® Xpress
Cloud Integrated Development Environment |
| | |||
MPLAB® Code Configurator
Dynamic Code Generation |
| |
Exercise Files
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project and Source Files
|
| | | |
Board Schematic
|
| | | |
Procedure
To follow along with these steps, the MPLAB Xpress Evaluation Board should be open and you should be logged in so that the MPLAB Code Configurator plug-in can be used.
1
Create Project
Create a new project in MPLAB Xpress for a PIC16F1855 called CIP_Button_Debouncing.
If this is your first project please visit the "MPLAB® Xpress: Create a Project" page.
2
Open MCC
The default system settings can be used.
If this is your first project using MCC please visit the "Opening MCC in MPLAB® Xpress" page.
3
Task 3
a
Task 3a
Add the TMR2 peripheral to the Project Resources by double-clicking on the peripheral instance under the Peripheral > Timer category in Device Resources.
b
Task 3b
Select Project Resources > Peripherals > TMR2 to open the Hardware Settings for TMR2 and configure as follows:
- Clock Source: FOSC/4
- Prescaler: 1:16
- Timer Period: 10 ms is used. This is the time that the TMR2 peripheral will count to before generating an output pulse. This value should provide sufficient time for the S2 switch to stop bouncing while being too fast to allow a user to physically press/release the switch more than once.
- Control Mode: Monostable. This mode will generate an output pulse after the timer reaches the defined timer period of 100 ms when the first instance of the 'Ext. Reset Source' selected edge is detected.
- Ext. Reset Source: T2CKIPPS pin. This will use the T2IN input signal that will be tied to the RA5 pin later in this tutorial.
- Start/Reset Option: Starts on rising/falling edge on TMR2_ers (TMR2 external reset source). This will determine the edge of the T2IN input signal that will start the timer (begin the debounce algorithm). The switch on the MPLAB Xpress board tied to RA5 is a momentary SPST type. Debouncing will be required when the switch is activated (pressed) and when the switch is released as bounce is likely to occur in both instances. The S2 switch is pulled HIGH meaning that when the switch is pressed the RA5 input will transition from HIGH-to-LOW and then from LOW-to-HIGH when released.
The TMR2 Hardware Settings should resemble the image below.
c
Task 3c
Navigate to the Pin Manager and tie the RA5 pin connected to switch S2 to the T2IN TMR2 signal.4
Task 4
Now the TMR2 output signal will be used to light the LEDs on the MPLAB Xpress Board while the S2 switch is pressed. The output signal indicating that the TMR2 has reached the user-defined time of 10 ms is very short. Much too short to light the LEDs long enough to be perceived by the human eye. Therefore, the TMR2 output signal will be used as a clock source to a 'Toggle Flip-Flop' that will be implemented using the Configurable Logic Cell JK flip-flop with R mode of operation. In this way, every output pulse from TMR2 will toggle the output of the JK Flip-Flop from LOW-to-HIGH when the switch is pressed, and then from HIGH-to-LOW when the switch is released as per the Start/Reset Option configured earlier.
a
Task 4a
Add the CLC1 peripheral to the Project Resources by double-clicking on the peripheral instance under the Peripheral>Timer category in Device Resources.
b
Task 4b
Select Project Resources > Peripherals > CLC1 to open the Hardware Settings for TMR2 and configure as follows:
- Mode: JK flip-flop with R
- Select the TMR2=PR2 signal from the top input signal drop-down and connect the JK Flip-Flop clock input through OR Gate 1
- Tie both J and K inputs of the JK Flip-Flop HIGH by inverting the outputs of the associated OR Gates by clicking inside of the dashed box on the output.
The CLC1 Hardware Settings should resemble the image below.
c
Task 4c
Navigate to the Pin Manager and tie the RA0, RA1, RA2 and RA3 pins connected to the four LEDs on the MPLAB Xpress Board to the CLC1OUT output signal.
5
Program Xpress Board
Compile the project by clicking on the Make and Program Device icon and then drag the .hex file to the Xpress board shown in the file manager.