본문 바로가기

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

PIC 타이머0 백그라운드 프로세스 타이밍

#include <xc.h>

#include <stdint.h>


#define _XTAL_FREQ 4000000


#define sFLASH  sGPIO.GP1

#define sPRESS  sGPIO.GP0


#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() {

    uint8_t dc;


    TRISB = 0b00000100;

    PORTB = 0;


    sGPIO.port = 0;


    OPTION_REGbits.T0CS = 0;

    OPTION_REGbits.PSA = 0;

    OPTION_REGbits.PS = 4;


    while(1) {

        for(dc = 0; dc < 125; dc++) {

            TMR0 = 0;

            while(TMR0 < 125) {

                sPRESS = !BUTTON;

                PORTB = sGPIO.port;

            }

        } sFLASH = !sFLASH;

    }

}