본문 바로가기

정리중인 카테고리/전공 기술

PIC 타이머0 스위치 디바운싱

#include <xc.h>

#include <stdint.h>


#define _XTAL_FREQ 4000000


#define sFLASH  sGPIO.GP1


#define BUTTON  PORTBbits.RB2


#pragma config WDTE = OFF, PWRTE = OFF, CP = OFF, BOREN = OFF


union {

    uint8_t port;

    struct {

        unsigned GP0 : 1;

        unsigned GP1 : 1;

        unsigned GP2 : 1;

        unsigned GP3 : 1;

        unsigned GP4 : 1;

        unsigned GP5 : 1;

    };

} sGPIO;


void main() {

    TRISB = 0b00000100;

    PORTB = 0;


    sGPIO.port = 0;


    OPTION_REGbits.T0CS = 0;

    OPTION_REGbits.PSA = 0;

    OPTION_REGbits.PS = 5;


    while(1) {

        TMR0 = 0;

        while(TMR0 < 157)

            if(BUTTON == 1) TMR0 = 0;


        sFLASH = !sFLASH;

        PORTB = sGPIO.port;


        TMR0 = 0;

        while(TMR0 < 157)

            if(BUTTON == 0) TMR0 = 0;

    }

}