Select Page

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.

How it works


The function input_debounce_A2(void) looks at pin A2.

The function, compares the input of the pin to the desired trigger value – if the values are the same, (in this case we are looking for logic 0) it will wait a given amount of time – which you can set in the __delay_ms(). Here I used 50ms.

After 50ms 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).

The Circuit

The Code

/*
 * File:  debounce_example.c
 * Author: Jamie Starling - GizoFoundry.com 
 *
 * Created on:  September 7, 2021, 7:45 PM
 * 
 * Code provided as-is.
 */

#include <xc.h>
#include <stdint.h>

//Device Configuration
#pragma config FOSC = INTOSC  // Oscillator Selection 
#pragma config BOREN = ON    // Brown-out Reset
#pragma config WDTE = OFF    // Watchdog Timer
#pragma config PWRTE = ON    // Power-up Timer
#pragma config MCLRE = OFF   // MCLR Pin Function Select bit->MCLR pin function is digital input, MCLR internally tied to VDD
#pragma config CP = OFF      // Code Protection 
#pragma config LVP = OFF     // Low-Voltage Programming 
#pragma config LPBOR = ON    // Brown-out Reset Selection bits
#pragma config BORV = LO    // Brown-out Reset Voltage Selection
#pragma config WRT = OFF    // Flash Memory Self-Write Protection

//Used to calculate the delay time - Change depending on processor Speed
#define _XTAL_FREQ 16000000  //16Mhz

//Prototypes
void setup(void);
uint8_t input_debounce_A2(void);


void main(void)
{
    setup();
    
    while(1)
    {
        uint8_t switch_status = 0;
        switch_status = input_debounce_A2();
        
        //The above is more long hand, it can be simplified down to.. 
        //if (input_debounce_A2()) - instead of creating a variable, reading the results into it - and compairing that variable, 
        
        if (switch_status == 1)
        {
           LATAbits.LATA0 = 1; 
        }
        
        else
        {
            LATAbits.LATA0 = 0; 
        }
    
    }
}

void setup(void)
{
   
    //Disable analog for A.0, set as Output, Set to Low
    ANSELAbits.ANSA0 = 0;
    TRISAbits.TRISA0 = 0; //A0 Output
    LATAbits.LATA0 = 0;  //Put A0 low
    
    //Setup A.2 as input with WPU
    ANSELAbits.ANSA2 = 0;
    TRISAbits.TRISA2 = 1;
    WPUAbits.WPUA2 = 1; //Enable Weakpull up on A2   
    OPTION_REGbits.nWPUEN = 0; //Requires being enabled in option reg as well
    
    //Set the System Clock - You can change this to match the setting you are looking for
    OSCCONbits.IRCF = 0b111;  //Set System Clock to 16Mhz FOSC/4 = 4Mhz
    
   
}

/*reads the input on A2 to see if it is low - we have a pullup here
 *so a low input would indicate that the switch that is tied to ground is being pressed
 *If low - we wait 50ms and read the status again
 *If low after 50ms, we return 1 to be true the switch is being pressed, otherwise 0 is returned. 
 *You also can adjust the value of the delay and do something like 1 second, which would require
 *the switch to be held for 1 second before something happens. */
uint8_t input_debounce_A2(void)
{
    if (PORTAbits.RA2 == 0)
    {
        __delay_ms(50);
        
        if (PORTAbits.RA2 == 0)
        {
            return 1;
        }
        
    return 0;  
    }
    
    return 0;    
}

Let’s talk!

I am always looking for oppunities to put my skills to work.
I am always looking to connect with like minded people.

Want to connect – drop me an email

jamie@jamiestarling.com