aboutsummaryrefslogtreecommitdiffstats
path: root/Lab4.X/lab4_button.c
diff options
context:
space:
mode:
Diffstat (limited to 'Lab4.X/lab4_button.c')
-rw-r--r--Lab4.X/lab4_button.c68
1 files changed, 48 insertions, 20 deletions
diff --git a/Lab4.X/lab4_button.c b/Lab4.X/lab4_button.c
index 611cf03..f889502 100644
--- a/Lab4.X/lab4_button.c
+++ b/Lab4.X/lab4_button.c
@@ -1,5 +1,6 @@
#include "xc.h"
#include "lab4_button.h"
+#define circBuffSize 2
void initPushButton(){
//Configure Timer 2 (500ns / count, 25ms max).
@@ -9,22 +10,21 @@ void initPushButton(){
T2CONbits.TCS = 0b0;
T2CONbits.TGATE = 0b0;
TMR2 = 0;
- PR2 = 0xf424; // Set period to be larger than max external sig duration
+ PR2 = 62500; // Set period to be larger than max external sig duration
T2CONbits.TON = 1; // Start 16-bit Timer2
// Initialize the Input Capture Module
- IC1CONbits.ICTMR = 1; // Select Timer2 as the IC1 Time base
- IC1CONbits.ICI = 0b00; // Interrupt on every capture event
- IC1CONbits.ICM = 0b011; // Generate capture event on every Rising edge
- // Enable Capture Interrupt And Timer2
-
- IPC0bits.IC1IP = 1; // Setup IC1 interrupt priority level
- IFS0bits.IC1IF = 0; // Clear IC1 Interrupt Status Flag
- IEC0bits.IC1IE = 1; // Enable IC1 interrupt
+ IC1CON = 0; // Turn off and reset internal state of IC1
+ IC1CONbits.ICTMR = 1; // Use Timer 2 for capture source
+ IC1CONbits.ICM = 0b011; // Turn on and capture every rising edge
+ IC1CON = 1;
+
+ _IC1IF = 0; // Clear IC1 Interrupt Status Flag
+ _IC1IE = 1; // Enable IC1 interrupt
- IFS0bits.T2IF = 1;
- IEC0bits.T2IE = 1;
-}
+ _T2IF = 1;
+ _T2IE = 1;
+}
volatile unsigned long overflow = 0;
//Timer 2 overflow interrupt
@@ -34,14 +34,42 @@ void __attribute__((__interrupt__,__auto_psv__)) _T2Interrupt(void) {
overflow++;
}
-volatile long int curPeriod=0;
+//Buffer functions
+volatile unsigned long buffer[circBuffSize];
+int buffSize = 0, write = 0, read = 0;
+void addBuffer(unsigned long x) {
+ if (buffSize < circBuffSize) {
+ buffer[write++] = x;
+ write %= circBuffSize;
+ ++buffSize;
+ }
+ //Otherwise get rid of the data.
+}
+unsigned long getBuffer() {
+ unsigned long returnValue = buffer[read++];
+ read %= circBuffSize;
+ --buffSize;
+ return returnValue;
+}
+int emptyBuffer(void) {
+ if (buffSize <= 0) {
+ buffSize=0;
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+volatile unsigned long curPeriod = 0, prevPress = 0, currPress = 0;
//IC1 interrupt
void __attribute__((__interrupt__,__auto_psv__)) _IC1Interrupt(void) {
- static unsigned int prevEdge=0;
- int curEdge;
-
_IC1IF = 0;
- curEdge = IC1BUF + 62500L * overflow;
- curPeriod = curEdge - prevEdge;
- prevEdge = curEdge;
-}
+ unsigned int time;
+ currPress = (IC1BUF + 62500 * (long) overflow);
+ curPeriod = currPress - prevPress;
+ time = curPeriod / 62.5;
+ if (time > 10) {
+ addBuffer(time);
+ }
+ prevPress = currPress;
+} \ No newline at end of file