// PIC18 in C - Versione 0.1 - Luglio 2014 // Copyright (c) 2014, Vincenzo Villa // Creative Commons | Attribuzione-Condividi allo stesso modo 3.0 Unported. // Creative Commons | Attribution-Share Alike 3.0 Unported // https://www.vincenzov.net/tutorial/PIC18/pwm.htm // Reference: PIC18F14K50 data sheet #define _XTAL_FREQ 1000000 // Default PIC18F14K50 clock #define TIMER2_VALUE 195u // See text - 2.5 Hz blink #include "configurationsbits.h" void interrupt my_isr_l(void) { if (PIR1bits.TMR2IF) // Interrupt from TMR2 to PR2 Match ? { LATCbits.LC0 = ~LATCbits.LC0; // Change LEDs status PIR1bits.TMR2IF = 0; // Clear interrupt status } } void main(void) { TRISC = 0x00; // Set PORTC as output RCONbits.IPEN = 1; // Enable priority levels on interrupts T2CONbits.T2OUTPS = 15; // 1:16 Postscale T2CONbits.T2CKPS = 2; // 1:16 Prescaler PR2 = TIMER2_VALUE; // Timer2 Period Register (end count) PIE1bits.TMR2IE = 1; // Enables the TMR2 to PR2 match interrupt IPR1bits.TMR2IP = 1; // TMR2 to PR2 Match Interrupt Priority set to high T2CONbits.TMR2ON = 1; // Timer2 is on INTCONbits.GIEH = 1; // Enables all interrupts while (1); }