This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
InputCapture.c
298 lines (265 loc) · 9.59 KB
/
InputCapture.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* File: InputCapture.c
*
* Created on February 9, 2010, 10:53 AM
*/
// include files
#include <stdio.h>
#include <stdlib.h>
#include <p33Fxxxx.h>
#include "InputCapture.h"
#include "OutputCompare.h"
//Global variables: each variable is stored in an array to hold all the input
// channel values (from IC1 - IC8)
int t1[8]; // Time 1 and Time 2 capture for
int t2[8]; // each input channel
short icInterruptFlag[8]; // Flag bit
unsigned int icTime[8]; // Time between interrupts (time between each pulse)
// Time calculation stored in an array
void initTimer2(void) // Initialize and enable Timer2
{
T2CONbits.TON = 0; // Disable Timer
T2CONbits.TCS = 0; // Select internal instruction cycle clock
T2CONbits.TGATE = 0; // Disable Gated Timer mode
T2CONbits.TCKPS = 0b01; // Select 1:8 Prescaler
TMR2 = 0x00; // Clear timer register
setPeriod(20);
//PR2 = MSEC * 20; // Load the period value = 20ms //
IPC1bits.T2IP = 0x01; // Set Timer 2 Interrupt Priority Level - Lowest
IFS0bits.T2IF = 0; // Clear Timer 2 Interrupt Flag
IEC0bits.T2IE = 0; // Enable Timer 2 interrupt
T2CONbits.TON = 1; // Start Timer
}
//Input Capture #1 Interrupt Function
void __attribute__((__interrupt__,no_auto_psv)) _IC1Interrupt(void)
{
if (PORTDbits.RD8 == 1) // if IC signal is goes 0 --> 1, set t1 equal to
t1[0]=IC1BUF; // buffer value or else if signal goes 1 --> 0
else // set t2 to buffer
{
t2[0]=IC1BUF;
icInterruptFlag[0] = 1;
}
IFS0bits.IC1IF=0;
}
//Input Capture #2 Interrupt Function
void __attribute__((__interrupt__,no_auto_psv)) _IC2Interrupt(void)
{
if (PORTDbits.RD9 == 1)
t1[1]=IC2BUF;
else
{
t2[1]=IC2BUF;
icInterruptFlag[1] = 1;
}
IFS0bits.IC2IF=0;
}
//Input Capture #3 Interrupt Function
void __attribute__((__interrupt__,no_auto_psv)) _IC3Interrupt(void)
{
if (PORTDbits.RD10 == 1)
t1[2]=IC3BUF;
else
{
t2[2]=IC3BUF;
icInterruptFlag[2] = 1;
}
IFS2bits.IC3IF=0;
}
//Input Capture #4 Interrupt Function
void __attribute__((__interrupt__,no_auto_psv)) _IC4Interrupt(void)
{
if (PORTDbits.RD11 == 1)
t1[3]=IC4BUF;
else
{
t2[3]=IC4BUF;
icInterruptFlag[3] = 1;
}
IFS2bits.IC4IF=0;
}
//Input Capture #5 Interrupt Function
void __attribute__((__interrupt__,no_auto_psv)) _IC5Interrupt(void)
{
if (PORTDbits.RD12 == 1)
t1[4]=IC5BUF;
else
{
t2[4]=IC5BUF;
icInterruptFlag[4] = 1;
}
IFS2bits.IC5IF=0;
}
//Input Capture #6 Interrupt Function
void __attribute__((__interrupt__,no_auto_psv)) _IC6Interrupt(void)
{
if (PORTDbits.RD13 == 1)
t1[5]=IC6BUF;
else
{
t2[5]=IC6BUF;
icInterruptFlag[5] = 1;
}
IFS2bits.IC6IF=0;
}
//Input Capture #7 Interrupt Function
void __attribute__((__interrupt__,no_auto_psv)) _IC7Interrupt(void)
{
if (PORTDbits.RD14 == 1)
t1[6]=IC7BUF;
else
{
t2[6]=IC7BUF;
icInterruptFlag[6] = 1;
}
IFS1bits.IC7IF=0;
}
//Input Capture #8 Interrupt Function
void __attribute__((__interrupt__,no_auto_psv)) _IC8Interrupt(void)
{
if (PORTDbits.RD15 == 1)
t1[7]=IC8BUF;
else
{
t2[7]=IC8BUF;
icInterruptFlag[7] = 1;
}
IFS1bits.IC8IF=0;
}
void initInputCapture(char initIC) // Capture Interrupt Service Routine
{ // unsigned int timePeriod= 0;
if (initIC & 0b01){
IC1CONbits.ICM = 0b00; // Disable Input Capture 1 module
IC1CONbits.ICTMR = 1; // Select Timer2 as the IC1 Time base
IC1CONbits.ICI = 0b11; // Interrupt on every capture event
IC1CONbits.ICM = 0b001; // Generate capture event on every Rising and Falling edge
// Enable Capture Interrupt And Timer2
IPC0bits.IC1IP = 7; // Setup IC1 interrupt priority level - Highest
IFS0bits.IC1IF = 0; // Clear IC1 Interrupt Status Flag
IEC0bits.IC1IE = 1; // Enable IC1 interrupt
}
if (initIC & 0b10){
IC2CONbits.ICM = 0b00; // Disable Input Capture 2 module
IC2CONbits.ICTMR = 1; // Select Timer2 as the IC1 Time base
IC2CONbits.ICI = 0b11; // Interrupt on every capture event
IC2CONbits.ICM = 0b001; // Generate capture event on every Rising edge
// Enable Capture Interrupt And Timer2
IPC1bits.IC2IP = 7; // Setup IC2 interrupt priority level - Highest
IFS0bits.IC2IF = 0; // Clear IC2 Interrupt Status Flag
IEC0bits.IC2IE = 1; // Enable IC2 interrupt
}
if (initIC & 0b100){
IC3CONbits.ICM = 0b00; // Disable Input Capture 3 module
IC3CONbits.ICTMR = 1; // Select Timer2 as the IC1 Time base
IC3CONbits.ICI = 0b11; // Interrupt on every capture event
IC3CONbits.ICM = 0b001; // Generate capture event on every Rising edge
// Enable Capture Interrupt And Timer2
IPC9bits.IC3IP = 7; // Setup IC3 interrupt priority level - Highest
IFS2bits.IC3IF = 0; // Clear IC3 Interrupt Status Flag
IEC2bits.IC3IE = 1; // Enable IC3 interrupt
}
if (initIC & 0b1000){
IC4CONbits.ICM = 0b00; // Disable Input Capture 4 module
IC4CONbits.ICTMR = 1; // Select Timer2 as the IC1 Time base
IC4CONbits.ICI = 0b11; // Interrupt on every capture event
IC4CONbits.ICM = 0b001; // Generate capture event on every Rising edge
// Enable Capture Interrupt And Timer2
IPC9bits.IC4IP = 7; // Setup IC4 interrupt priority level - Highest
IFS2bits.IC4IF = 0; // Clear IC4 Interrupt Status Flag
IEC2bits.IC4IE = 1; // Enable IC4 interrupt
}
if (initIC & 0b10000){
IC5CONbits.ICM = 0b00; // Disable Input Capture 5 module
IC5CONbits.ICTMR = 1; // Select Timer2 as the IC1 Time base
IC5CONbits.ICI = 0b11; // Interrupt on every capture event
IC5CONbits.ICM = 0b001; // Generate capture event on every Rising edge
// Enable Capture Interrupt And Timer2
IPC9bits.IC5IP = 7; // Setup IC5 interrupt priority level - Highest
IFS2bits.IC5IF = 0; // Clear IC5 Interrupt Status Flag
IEC2bits.IC5IE = 1; // Enable IC5 interrupt
}
if (initIC & 0b100000){
IC6CONbits.ICM = 0b00; // Disable Input Capture 6 module
IC6CONbits.ICTMR = 1; // Select Timer2 as the IC1 Time base
IC6CONbits.ICI = 0b11; // Interrupt on every capture event
IC6CONbits.ICM = 0b001; // Generate capture event on every Rising edge
// Enable Capture Interrupt And Timer2
IPC10bits.IC6IP = 7; // Setup IC6 interrupt priority level - Highest
IFS2bits.IC6IF = 0; // Clear IC6 Interrupt Status Flag
IEC2bits.IC6IE = 1; // Enable IC6 interrupt
}
if (initIC & 0b1000000){
IC7CONbits.ICM = 0b00; // Disable Input Capture 7 module
IC7CONbits.ICTMR = 1; // Select Timer2 as the IC1 Time base
IC7CONbits.ICI = 0b11; // Interrupt on every capture event
IC7CONbits.ICM = 0b001; // Generate capture event on every Rising edge
// Enable Capture Interrupt And Timer2
IPC5bits.IC7IP = 7; // Setup IC7 interrupt priority level - Highest
IFS1bits.IC7IF = 0; // Clear IC7 Interrupt Status Flag
IEC1bits.IC7IE = 1; // Enable IC7 interrupt
}
if (initIC & 0b10000000){
IC8CONbits.ICM = 0b00; // Disable Input Capture 8 module
IC8CONbits.ICTMR = 1; // Select Timer2 as the IC1 Time base
IC8CONbits.ICI = 0b11; // Interrupt on every capture event
IC8CONbits.ICM = 0b001; // Generate capture event on every Rising edge
// Enable Capture Interrupt And Timer2
IPC5bits.IC8IP = 7; // Setup IC8 interrupt priority level - Highest
IFS1bits.IC8IF = 0; // Clear IC8 Interrupt Status Flag
IEC1bits.IC8IE = 1; // Enable IC8 interrupt
}
}
unsigned int* getICValues(){
//Calculate and Update the Input Values
short ic;
for (ic = 0; ic < 8; ic++)
{
//If new data has been received on the given channel
if (icInterruptFlag[ic] == 1)
{
//If the second time is greater than the first time then we have not met roll over
if(t2[ic] > t1[ic])
{
icTime[ic] = (t2[ic] - t1[ic]);
icInterruptFlag[ic] = 0;
}
//Roll over has been made, therefore do math to find the time difference
//(Max time - t1) is the upper difference
//Adding the second time (time from 0) gives you the total time difference
else
{
icTime[ic] = ((PR2 - t1[ic]) + t2[ic]);
icInterruptFlag[ic] = 0;
}
}
}
return icTime;
}
unsigned int getICValue(unsigned char channel){
unsigned int ic = (unsigned int)channel;
//If new data has been received on the given channel
if (icInterruptFlag[ic] == 1)
{
//If the second time is greater than the first time then we have not met roll over
if(t2[ic] > t1[ic])
{
icTime[ic] = (t2[ic] - t1[ic]);
icInterruptFlag[ic] = 0;
}
//Roll over has been made, therefore do math to find the time difference
//(Max time - t1) is the upper difference
//Adding the second time (time from 0) gives you the total time difference
else
{
icTime[ic] = ((PR2 - t1[ic]) + t2[ic]);
icInterruptFlag[ic] = 0;
}
}
return icTime[ic];
}
// initialize function
void initIC(char initIC)
{
initInputCapture(initIC);
initTimer2();
}