Thank youFor each button is better to use its own counter variable.
Button 1 toggle LED 1
Button 2 toggle LED 2
In this way we can read all four button's
C:
#define _XTAL_FREQ 8000000
#include <xc.h>
// PIC18F45K80 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config RETEN = OFF // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF // Extended Instruction Set (Enabled)
// CONFIG1H
#pragma config FOSC = INTIO2 // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = OFF // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3 // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
// CONFIG2H
#pragma config WDTEN = OFF // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576 // Watchdog Postscaler (1:1048576)
// CONFIG3H
#pragma config CANMX = PORTB // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7 // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K // Boot Block Size (2K word Boot Block size)
// CONFIG5L
#pragma config CP0 = OFF // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF // Code Protect 06000-07FFF (Disabled)
// CONFIG5H
#pragma config CPB = OFF // Code Protect Boot (Disabled)
#pragma config CPD = OFF // Data EE Read Protect (Disabled)
// CONFIG6L
#pragma config WRT0 = OFF // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF // Table Write Protect 06000-07FFF (Disabled)
// CONFIG6H
#pragma config WRTC = OFF // Config. Write Protect (Disabled)
#pragma config WRTB = OFF // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF // Data EE Write Protect (Disabled)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF // Table Read Protect 06000-07FFF (Disabled)
// CONFIG7H
#pragma config EBTRB = OFF // Table Read Protect Boot (Disabled)
unsigned char counter1;
unsigned char counter2;
unsigned char counter3;
unsigned char counter4;
__bit BUTTON_x1;
__bit BUTTON_x2;
__bit BUTTON_x3;
__bit BUTTON_x4;
__bit PREVIOUS_BUTTON_x1 = 0;
__bit PREVIOUS_BUTTON_x2 = 0;
__bit PREVIOUS_BUTTON_x3 = 0;
__bit PREVIOUS_BUTTON_x4 = 0;
__bit Interrupt_Flag_1ms;
#define Button_1 PORTBbits.RB0
#define Button_2 PORTBbits.RB1
#define Button_3 PORTBbits.RB2
#define Button_4 PORTBbits.RB3
#define LED_1 LATDbits.LATD0
#define LED_2 LATDbits.LATD1
#define LED_3 LATDbits.LATD2
#define LED_4 LATDbits.LATD3
#define DEBOUNCE_TIKS 10
void Port_Initialized (void);
void Timer0_Initialized (void);
void __interrupt(high_priority) tcInt(void);
void Port_Initialized (void)
{
// LATx registers
LATA = 0x00;
LATB = 0x00;
LATC = 0x00;
LATD = 0x00;
LATE = 0x00;
// TRISx registers
TRISA = 0x00; // All are output, Unused
TRISB = 0xFF; // Button connected to RB0
TRISC = 0x00; // all are output, Unused
TRISD = 0x00; // LED connected to RD2 pin
TRISE = 0x00; // All are output, Unused
ANCON0 = 0x00; // set to digital port
ANCON1 = 0x00; // Set to digital port
CM1CON = 0x00; // Comparator off
CM2CON = 0x00; // Comparator off
ADCON0 = 0x00; // A/D conversion Disabled
ADCON1 = 0x00; // A/D conversion Disabled
ADCON2 = 0x00; // A/D conversion Disabled
}
void Timer0_Initialized (void)
{
TMR0L = 250; //Timer0 Register Low Byte
//T0CON: TIMER0 CONTROL REGISTER
TMR0ON = 1; //Timer0 On
T08BIT = 1; // Timer0 is configured as an 8-bit timer/counter0
T0CS = 0; // Internal instruction cycle clock (CLKO)
T0SE = 1; //Increments on high-to-low transition on T0CKI pin0
PSA = 0; //Timer0 prescaler is assigned; Timer0 clock input comes from prescaler output
//1:8 Prescale value
T0PS2 = 1;
T0PS1 = 1;
T0PS0 = 1;
//INTCON: INTERRUPT CONTROL
PEIE = 0; //Disables all peripheral interrupts
TMR0IE = 1; //Enables the TMR0 overflow interrupt
INT0IE = 0; //Disables the INT0 external interrupt
RBIE = 0; //Disables the RB port change interrupt
TMR0IF = 0;// cleared timer overflow flag
INT0IF = 0; // disabled external interrupt
RBIF = 0; //disabled Port Change Interrupt Flag bit
GIE = 1; // Enable Global Interrupt Enable bit
}
void Sw1 ()
{
if (Button_1 == 0)
{
counter1 = 0; // button is open, reset everything
BUTTON_x1 = 0;
}
else
{
if(BUTTON_x1 == 0)
{
counter1++;
if(counter1 == DEBOUNCE_TIKS)
BUTTON_x1 = 1; // fully debounced
} // else, button TRUE and is already debounced, nothing to do
}
}
void Sw2 ()
{
if (Button_2 == 0)
{
counter2 = 0; // button is open, reset everything
BUTTON_x2 = 0;
}
else
{
if(BUTTON_x2 == 0)
{
counter2++;
if(counter2 == DEBOUNCE_TIKS)
BUTTON_x2 = 1; // fully debounced
} // else, button TRUE and is already debounced, nothing to do
}
}
void Run_Debounce(void)
{
Sw1 ();
Sw2 ();
}
void main(void)
{
Port_Initialized ();
Timer0_Initialized ();
LED_1 = 0;
LED_2 = 0;
while (1)
{
if (Interrupt_Flag_1ms == 1)
{
Interrupt_Flag_1ms = 0; // clear the flag for next time
Run_Debounce(); // process switch
}
if((PREVIOUS_BUTTON_x1 == 0) && (BUTTON_x1==1))
{
LED_1 =~ LED_1;
}
PREVIOUS_BUTTON_x1 = BUTTON_x1; // update ?previous? condition every time through
if((PREVIOUS_BUTTON_x2 == 0) && (BUTTON_x2==1))
{
LED_2 =~ LED_2;
}
PREVIOUS_BUTTON_x2 = BUTTON_x2; // update ?previous? condition every time through
}
}
void __interrupt(high_priority) tcInt(void)
{
if (TMR0IF == 1) // Timer0 overflow interrupt flag bit
{
TMR0IF = 0;
TMR0L = 250; //Timer0 Register Low Byte
Interrupt_Flag_1ms = 1;
}
}



