#include #include #include #include #include /* must include for atoi */ #define DATA_EEMEM_PROTECT_DISABLE 0x0100 #define XT_OSC 0x0001 #define DISABLE_DEBUG 0x0800 #define CP0 0x1010 #define CP1 0x2020 /* CP0,CP1 code protect off*/ /* other bits zero which means WDT disabled Low Voltage prm disabled, brownout disabled power up timer enabled */ __CONFIG((CP1 | CP0) | DATA_EEMEM_PROTECT_DISABLE | XT_OSC | DISABLE_DEBUG); #define bitset(var,bitno) ((var) |= (1 << (bitno))) #define bitclr(var,bitno) ((var) &= ~(1 << (bitno))) #define bittst(var,bitno) (var & (1 << (bitno))) /* use Capture mode to measure the pulse width of a switch */ /* return 8 bit char from Recieve port */ char getch () { unsigned char c; /* while (!RCIF) */ while (!bittst(PIR1,5)); /* check RCIF bit */ c = RCREG; return(c); } /* send 8 bit char to Recieve port */ void putch (c) char c; { /* wait until transmit reg empty */ /* while (!TXIF) */ while (!bittst(PIR1,4)); /* check RCIF bit */ TXREG = c; } void pcrlf () { putch(0x0a); putch(0x0d); } unsigned int last_capture; unsigned int this_capture; unsigned int delta; unsigned char capture_flag; unsigned char edge_type; void interrupt timer_isr(void) { bitclr(PIR1,2); // CCP1IF = 0; clear capture interrupt flag this_capture = CCPR1H << 8 ; this_capture = this_capture | CCPR1L ; if (edge_type == 0) { last_capture = this_capture; } else { /* compute delta */ if (this_capture > last_capture) { delta = this_capture - last_capture; } else { /* this is timer overflow case */ delta = this_capture + ~last_capture + 1; } } capture_flag = 1; } main(void) { unsigned char i; /* setup Async communication */ bitclr(TXSTA, 6); /* 8-bit transmit */ bitset(TXSTA, 5); /* transmit enable*/ bitclr(TXSTA, 4); /* async mode */ bitset(TXSTA, 2); /* high speed mode */ bitset(TRISC, 7); /* serial port enable */ bitset(TRISC, 6); /* serial port enable */ bitset(RCSTA, 7); /* serial port enable */ bitclr(RCSTA, 6); /* 8-bit reception */ /* set baud rate */ SPBRG = 95; /* 9600 in high baud rate mode, crystal = 14.7456 Mhz */ bitset(RCSTA, 4); /* enable recieve */ // initialize timer 1 // prescale by 8 bitset(T1CON,5); // T1CKPS1 = 1; bitset(T1CON,4); // T1CKPS0 = 1; bitclr(T1CON,3); // enable the oscillator T1OSCEN = 0; bitclr(T1CON,1); //choose internal FOSC/4 TMR1CS = 0; bitset(T1CON,0); // enable oscillator TMR1ON = 1; /* set up capture /* set CCP1 input */ bitset(TRISC,2); /* capture every falling edge 0100 */ bitclr(CCP1CON, 3); bitset(CCP1CON, 2); bitclr(CCP1CON, 1); bitclr(CCP1CON, 0); // enable interrupts bitclr(PIR1,2); // CCP1IF = 0; clear capture interrupt flag bitset(PIE1, 2) ; // enable capture interrupt CCP1IE = 1; bitset(INTCON,6); /* enable peripheral interrupts, PEIE */ bitset(INTCON,7); /* enable global interrupts, GIE */ printf("Ready for button mashing!"); pcrlf(); for(;;) { CCP1CON = 0; /* turn off when changing modes */ CCP1CON = 0x4; /* capture every falling edge */ edge_type = 0; /* looking for falling edge */ capture_flag = 0; while(!capture_flag); capture_flag = 0; CCP1CON = 0; /* turn off when changing modes */ CCP1CON = 0x5; /* capture every rising edge */ edge_type = 1; /* looking for rising edge */ while(!capture_flag); printf ("Switch pressed, timer ticks: %u", delta); pcrlf(); } }