-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplePWMread.cpp
146 lines (129 loc) · 4.17 KB
/
simplePWMread.cpp
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
#include "simplePWMread.hpp"
// test case - fixed pin number
const byte PWM_pin = 9;
const byte MAX_ISR_COUNT = 1;
byte isr_count = 0;
byte isr_pin[MAX_ISR_COUNT];
volatile unsigned int isr_value[MAX_ISR_COUNT];
volatile bool isr_last_state[MAX_ISR_COUNT];
volatile bool isr_trigger_state[MAX_ISR_COUNT];
volatile unsigned long isr_timer[MAX_ISR_COUNT];
volatile unsigned long isr_age[MAX_ISR_COUNT];
volatile unsigned long isr_counter;
void ISR_generic(byte isr) {
unsigned long now = micros();
bool state_now = digitalRead(isr_pin[isr]);
if (state_now != isr_last_state[isr]) {
if (state_now == isr_trigger_state[isr]) {
isr_timer[isr] = now;
} else {
isr_value[isr] = (unsigned int)(now - isr_timer[isr]);
isr_age[isr] = now;
}
isr_last_state[isr] = state_now;
}
}
ISR (PCINT0_vect)
{
isr_counter++;
ISR_generic(0);
} // end of PCINT1_vect
/*void ISR_0() {
ISR_generic(0);
}
*/
PWM::PWM(byte pin) {
pin = PWM_pin;
my_isr = isr_count;
isr_count++;
isr_pin[my_isr] = pin;
pinMode(isr_pin[my_isr], INPUT);
}
int PWM::begin(bool measure_pulse_high) {
pinMode(isr_pin[my_isr], INPUT);
isr_last_state[my_isr] = digitalRead(isr_pin[my_isr]);
isr_trigger_state[my_isr] = measure_pulse_high;
switch (my_isr) {
case 0:
// attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_0, CHANGE);
// attachInterrupt(5, ISR_0, CHANGE);
PCIFR |= bit (PCIF0); // clear any outstanding interrupts
PCICR |= 0x01;
PCMSK0 |= PCINT1;
break;
/*
case 1:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_1, CHANGE);
break;
case 2:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_2, CHANGE);
break;
case 3:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_3, CHANGE);
break;
case 4:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_4, CHANGE);
break;
case 5:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_5, CHANGE);
break;
case 6:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_6, CHANGE);
break;
case 7:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_7, CHANGE);
break;
case 8:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_8, CHANGE);
break;
case 9:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_9, CHANGE);
break;
case 10:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_10, CHANGE);
break;
case 11:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_11, CHANGE);
break;
case 12:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_12, CHANGE);
break;
case 13:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_13, CHANGE);
break;
case 14:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_14, CHANGE);
break;
case 15:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_15, CHANGE);
break;
case 16:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_16, CHANGE);
break;
case 17:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_17, CHANGE);
break;
case 18:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_18, CHANGE);
break;
case 19:
attachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]), ISR_19, CHANGE);
break;
*/
default:
return -1; // Error.
}
return 0; // Success.
}
unsigned int PWM::getValue() {
return isr_value[my_isr];
}
void PWM::end() {
detachInterrupt(digitalPinToInterrupt(isr_pin[my_isr]));
}
unsigned long PWM::getAge() {
return (micros() - isr_age[my_isr]);
}
unsigned long PWM::getCounter() {
return isr_counter;
}