#include __CONFIG(WDTDIS & XT & UNPROTECT & LVPDIS); #define XTAL 4000000 // crystal frequency - 4MHz long tics = 0l; int flag = 1; void interrupt prv_int(void) { //**************************************************************** // To calculate when will enter here: // // Freq = clock cycle / 4 / 256 = 4000000Hz / 4 / 256 = 3906Hz // Period = 1 / 3906Hz = 0.256msec //**************************************************************** if (!T0IF) return; //**************************************************************** // T0IF is 1 when the counter TMR0 is arrived to the // overflow = 0xFF = 256 so to calculate when will enter here: // // Freq = (clock cycle / 4 / 256) / 256 = 3906Hz / 256 = 15Hz // Period = 1 / 15Hz = 65msec //**************************************************************** T0IF = 0; tics++; if (tics == 15l) { tics = 0l; //*********************************************************** // To calculate when will enter here: // Freq = 15Hz / 15 = 1Hz and periord is 1 second //******************************+**************************** if (flag == 1) { RB5 = 1; flag = 0; } else { RB5 = 0; flag = 1; } } } main() { TRISB = 0b11000000; CMCON = 0b00000111; // disable comparator on ra0,1,2 TRISA = 0b11111111; TMR0 = 0; // Reset the TMR0 counter T0CS = 0; // Enable timer to get the input from internal counter XTAL/4 PSA = 0; // Enable prescaler to divide the frequency PS2 = 1; // PS1 = 1; // 111 mean prescaler set to 1:256 PS0 = 1; // INTCON = 0; // reset all the flag bits for all the interrupts GIE = 1; // global interrupt enable bit (1: enable all) T0IE = 1; // TMR0 overflow interrupt enable bit (1: enable TMR0) while(1) continue; // while forewer }