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 the Timer 2 with hardware limit timer 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 pre-determined 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.

Open MCC

Task 3

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 to short to light the LEDs long enough to be perceived by the human eye. Therefore, the TMR2 output signal will be used as 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.

MPLAB® Xpress IDE is an abbreviated version of the full MPLAB X IDE; yet it has all the necessary features to develop, simulate, and program a project into a development board. To verify that a program has no errors you can build the program by clicking on the hammer icon in the main tool bar.

The Output window will show any warnings or errors in the program along with their location so you can correct them. Once the program compiles successfully, you will see a Build Successful message in the Output window.

The MPLAB® Xpress Development Board should be connected to an available USB port on the host computer through a USB cable to the micro B connector on the board. Drivers should install successfully the first time the board is connected and may take a minute or so. The connection is shown in the picture. No other components are required.

Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.

Click here to edit contents of this page.

Click here to toggle editing of individual sections of the page (if possible). Watch headings for an "edit" link when available.

Append content without editing the whole page source.

Check out how this page has evolved in the past.

If you want to discuss contents of this page - this is the easiest way to do it.

View and manage file attachments for this page.

A few useful tools to manage this Site.

See pages that link to and include this page.

Change the name (also URL address, possibly the category) of the page.