XC8 Code for PIC10F320 – PIC10F322 – Input or Button Debounce

I put together some code to debounce a signal on the inputs of the PIC10F32X device.

Switch debouncing is one of those things you generally have to live with when playing with switches and digital circuits. If you want to input a manual switch signal into a digital circuit you’ll need to debounce the signal so a single press doesn’t appear like multiple presses.

The PIC10F322 debounce function (debounce.c and debounce.h) can be found here  GitHub – https://github.com/JamieStarling/MCU_FUN/tree/master/Microchip/PIC10F322/includes

The function needs to include the gpio to work.

The function debounce(pin, trigger) takes two inputs – the pin you want to debounce, and the value.

The function, compares the input pin to the desired trigger value – if the values are the same, it will wait a given amount of time – which you can set in the debunce.h header file – defined by delay_time. Default is 25ms.

After 25ms it tests the pin again comparing the sampled value to the current value.  If the current value is the same as the sampled value,  the function returns a 1 (TRUE), if not returns a 0(FALSE).

Here is an example using the LED controlled by a button code. led_button_control_debounce.c

 

#include "includes/10F322_deviceconfig.h"
#include "includes/gpio.h"
#include "includes/wpua.h"
#include "includes/debounce.h"


void main(void) 
{
    //Setup
    pinMode(0,OUTPUT);
    pinMode(2,INPUT);
    enableWPUA(2);
    
    
    while(1)
    {
        if(inputDebounce(2,HIGH)) //Check the input if high turn off the LED
        {
            digitalWrite(0,LOW);
        }
        else
        {
            digitalWrite(0,HIGH);
        }
        
        
    }
}