/******* IntProg2.c ************ * * Use Fosc = 4 MHz for Fcpu = Fosc/4 = 1 MHz. * Timer1 and Timer3 are both clocked by the internal clock. * ******* Program hierarchy ***** * * main * Initial * * LoPriISR * * HiPriISR * ******************************* */ #include // Define PIC18LF4321 registers and bits /******************************* * Configuration selections ******************************* */ #pragma config OSC = INTIO1 // Use internal osc, RA6=Fosc/4, RA7=I/O #pragma config PWRT = ON // Enable power-up delay #pragma config LVP = OFF // Disable low-voltage programming #pragma config WDT = OFF // Disable watchdog timer initially #pragma config MCLRE = ON // Enable master clear pin #pragma config PBADEN = DIG // PORTB<4:0> = digital #pragma config CCP2MX = RB3 // Connect CCP2 internally to RB3 pin #pragma config BOR = SOFT // Brown-out reset controlled by software #pragma config BORV = 3 // Brown-out voltage set for 2.0V, nominal #pragma config LPT1OSC = OFF // Deselect low-power Timer1 oscillator /******************************* * Global variables ******************************* */ unsigned char i; // Index into strings unsigned int DELAY; // Sixteen-bit counter for obtaining a delay /******************************* * Function prototypes ******************************* */ void Initial(void); void HiPriISR(void); void HPISRhandler(void); void LoPriISR(void); void LPISRhandler(void); /******************************* * Macros ******************************* */ #define Delay(x) DELAY = x; while(--DELAY){ Nop(); Nop(); } /******************************* * Interrupt vectors ******************************* */ // For high priority interrupts: #pragma code high_vector=0x08 void interrupt_at_high_vector(void) { _asm GOTO HiPriISR _endasm } #pragma code #pragma interrupt HiPriISR // For low priority interrupts: #pragma code low_vector=0x18 void interrupt_at_low_vector(void) { _asm GOTO LoPriISR _endasm } #pragma code #pragma interruptlow LoPriISR /******************************* * HiPriISR * * Clear flag, toggle RB1 and return. ******************************* */ void HiPriISR() { HPISRhandler(); } /******************************* * LPISRhandler ******************************* */ void HPISRhandler() { TMR3H = 0xFF; PIR2bits.TMR3IF = 0; // Clear interrupt flag PORTBbits.RB1 ^= 1; // Toggle pin } /******************************* * LoPriISR * * Clear flag, toggle RB0 and return. ******************************* */ void LoPriISR() { LPISRhandler(); } /******************************* * LPISRhandler ******************************* */ void LPISRhandler() { TMR1H = 0xFF; PIR1bits.TMR1IF = 0; // Clear interrupt flag PORTBbits.RB0 ^= 1; // Toggle pin } /////// Main program ////////////////////////////////////////////////////////// /******************************* * main ******************************* */ void main() { Initial(); // Initialize everything while (1) { PORTCbits.RC2 = 1; // Set pin high Delay(14); PORTCbits.RC2 = 0; Delay(20); } } /******************************* * Initial * * This function performs all initializations of variables and registers. ******************************* */ void Initial() { OSCCON = 0b01100010; // Use Fosc = 4 MHz (Fcpu = 1 MHz) ADCON1 = 0b00001011; // RA0,RA1,RA2,RA3 pins analog; others digital TRISA = 0b00001111; // Set I/O for PORTA TRISB = 0b01000100; // Set I/O for PORTB TRISC = 0b10000000; // Set I/O for PORTC TRISD = 0b10000000; // Set I/O for PORTD TRISE = 0b00000010; // Set I/O for PORTE PORTA = 0; // Set initial state for all outputs low PORTB = 0; PORTC = 0; PORTD = 0b00100000; // except RD5 that drives LCD interrupt PORTE = 0; Delay(50000); // Pause for 0.5 second to make use of brownout // reset on any power switch contact bounce // and to let LCD controller initialize before // any display strings are sent to it. RCONbits.SBOREN = 0; // After this delay, disable brown-out reset T1CON = 0b01001101; // Timer1 - loop time via low-pri interrupts Use internal clock T3CON = 0b00000101; // Timer3 - step motor via hi-pri interrupts TMR1H = 0xFE; // Set Timer1 to be 10 ms away from TMR1L = 0xB9; // next roll over (65536 + 1 - 328 = 0xFEB9) TMR3H = 0xFF; // Set Timer3 to be 4 ms away from TMR3L = 0xAE; // next roll over (65536 + 1 - 131 = 0xFF7E) // PIE1bits.TMR1IE = 1; // Enable local interrupt source PIE2bits.TMR3IE = 1; // Enable local interrupt source IPR1bits.TMR1IP = 0; // Use Timer1 for low-priority interrupts IPR2bits.TMR3IP = 1; // Use Timer3 for hi-priority interrupts RCONbits.IPEN = 1; // Enable high/low priority interrupt feature INTCONbits.GIEL = 1; // Global low-priority interrupt enable INTCONbits.GIEH = 1; // Enable both high and low interrupts }