#include __CONFIG(WDTDIS & XT & UNPROTECT & LVPDIS); #define XTAL 4000000 // crystal frequency - 4MHz long tics = 0l; int flag = 1; void interrupt prv_int(void) { if (!TMR2IF) return; //**************************************************************** // TMR2IF is 1 when the counter TMR2 is arrived to the // PR2 register value (default: 0xFF = 255) // so to calculate when will enter here: // // Freq = (clock cycle / 4 / 16 / 255 / 16 = 15Hz // Period = 1 / 15Hz = 65msec //**************************************************************** TMR2IF = 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; //****************************************************** // The following lines set the T2CON control register TOUTPS3 = 1; // these are the 6,5,4,3 bits of the TOUTPS2 = 1; // control register T2CON and set TOUTPS1 = 1; // the postscale value TOUTPS0 = 1; // TMR2ON = 1; // this enable the Timer2 T2CKPS1 = 1; // these are the 1 and 0 bits of the T2CKPS0 = 1; // control register T2CON and set the // prescale value // Is also possible set the bits of T2CON control register // with a unique comand like the following: // // T2CON = 0x7E; // // here the hexadecimal value 7E means // 76543210 // 1111110 // 1111 -> postscale value 1:16 // 1 -> enable timer2 // 1x -> prescaler value 1:16 //****************************************************** TMR2IE = 1; // TMR2 to PR2 enable match interrupt // PR2 = 0xFF; default value INTCON = 0; // reset all the flag bits for all the interrupts PEIE = 1; // peripheral interrupt enable bit GIE = 1; // global interrupt enable bit (1: enable all) while(1) continue; // while forewer }