// 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/timer1.htm // Reference: PIC18F14K50 data sheet #include "configurationsbits.h" #include #define _XTAL_FREQ 1000000 // Default PIC18F14K50 clock #define TIMER1_VALUE 1000 // T1 count from 0 to 1000 #define TIMER0_VALUE 64000 // T0 count from 64000 to 65536 #define RED LATCbits.LC0 // Output #1 - main code #define BLUE LATCbits.LC1 // Output #2 - my_isr_low #define GREEN LATCbits.LC2 // Output #3 - my_isr_high void interrupt __low_priority my_isr_low(void) { if (INTCONbits.TMR0IF == 1) // Interrupt from Timer0? { WriteTimer0(TIMER0_VALUE); // Set T0 starting value for (unsigned char i = 0; i < 50; i++) BLUE = ~BLUE; // Change RC1 pin status INTCONbits.TMR0IF = 0; // Clear interrupt status } } void interrupt __high_priority my_isr_high(void) { if (PIR1bits.CCP1IF == 1) // Interrupt from Timer1? { for (unsigned char i = 0; i < 10; i++) GREEN = ~GREEN; // Change RC2 pin status PIR1bits.CCP1IF = 0; // Clear interrupt status } } void main(void) { TRISC = 0x00; // Set PORTC as output RCONbits.IPEN = 1; // Enable priority levels on interrupts OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_EDGE_FALL & T0_PS_1_1); // Enable T0: interrupt, 16 bit, internal clock INTCON2bits.TMR0IP = 0; // Set low priority interrupt WriteTimer0(TIMER0_VALUE); // Set T0 starting value OpenTimer1(TIMER_INT_OFF & T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_1); // Enable T1: internal clock, no interrupt IPR1bits.TMR1IP = 1; // TMR1 Overflow Interrupt Priority set to high OpenCompare1(COM_INT_ON & COM_TRIG_SEVNT, TIMER1_VALUE); // Enable CCP as compare: at trigger -> interrupt + Timer1 clear T3CONbits.T3CCP1 = 0; // Timer1 is the clock source for compare/capture of ECCP1 IPR1bits.CCP1IP = 1; // CCP1 Interrupt Priority set to high INTCONbits.GIEL = 1; // Enables all low priority interrupts INTCONbits.GIEH = 1; // Enables all interrupts while (1) { RED = ~RED; // Change RC0 pin status } }