#include<plib.h> // Configuration Bit settings // SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV) // PBCLK = 40 MHz // Primary Osc w/PLL (XT+,HS+,EC+PLL) // WDT OFF // Other options are don't care // #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_8 #define CONFIG (CN_ON | CN_IDLE_CON) #define PINS (CN15_ENABLE) #define PULLUPS (CN15_PULLUP_ENABLE) #define INTERRUPT (CHANGE_INT_ON | CHANGE_INT_PRI_2) #define SYS_FREQ (80000000L) char a = 0; //flag char done = 0; void init (void) { unsigned int temp; //Configure cache, wait states and peripheral bus clock SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); //configure the port registers PORTSetPinsDigitalOut(IOPORT_D, BIT_0 | BIT_1); PORTSetPinsDigitalIn(IOPORT_F, BIT_5); //initialize the port pin states = outputs low mPORTDClearBits(BIT_0 | BIT_1); //enable change notice, enable discrete pins and weak pullups mCNOpen(CONFIG, PINS, PULLUPS); //read port(s) to clear mismatch on change notice pins temp = mPORTFRead(); //clear change notice interrupt flag ConfigIntCN(INTERRUPT); //enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); } void Timer (void) { //configure Timer 2 OpenTimer2(T2_ON | T2_SOURCE_EXT | T2_PS_1_1, 1); //set up the timer interrupt with a priority of 2 ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2); } int main(void) { init(); while(1) { // .. add things to do }; } void __ISR(_CHANGE_NOTICE_VECTOR, ipl5) ChangeNotice_Handler(void) { unsigned int temp; // clear the mismatch condition temp = mPORTFRead(); // clear the interrupt flag mCNClearIntFlag(); if (PORTReadBits(IOPORT_F, BIT_5) !=0 ) { if (done == 0) { char count = 0; // .. things to do .. toggle the led mPORTDToggleBits(BIT_0); Timer(); a = 1; //flag count = ReadTimer2(); } else { CloseTimer2(); } } if (PORTReadBits(IOPORT_F, BIT_5) ==0 ) { CloseTimer2(); a = 0; done = 1; } } void __ISR(_TIMER_2_VECTOR, ipl2) Timer1Handler(void) { // clear the interrupt flag mT2ClearIntFlag(); // .. things to do // .. in this case, toggle the LED mPORTDToggleBits(BIT_1); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)