-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflashTester.ino
169 lines (133 loc) · 4.39 KB
/
flashTester.ino
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
/**
* @name Vela flash tester
* @brief Test flash speed using Arduino
* @copyright (C) 2015 Vela Labs Ltd
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* /
/**
* @file flashTester.ino
* @author Matt Kane <[email protected]>
*/
static uint16_t triggerTime;
static uint16_t flashStart;
static uint8_t flash;
static uint8_t ignore;
static uint8_t triggering;
#define BUTTON_PIN 3
#define FLASH_PIN 4
void setup() {
//Disable interrupts while we're fiddling with settings.
cli();
//Timer4 is our flash trigger pulse and housekeeping timer.
//Enable overflow and output compare interrupts. Start turned off.
TIMSK4 = _BV(TOIE4);
TCCR4A = 0;
TCCR4B = 0;
OCR4A = 1;
//Timer1 has already been configured in wiring.c, but we want a different speed.
//Mode 0: normal mode, no port outputs.
TCCR1A = 0;
//This sets the clock to no prescale (16MHz).
//Trigger on rising edge.
TCCR1B = _BV(CS10) | _BV(ICES1);
//Enable analog comparator input capture.
ACSR = _BV(ACIC);
//Enable input capture interrupts
TIMSK1 |= _BV(ICIE1);
//Use multiplexer for comparator negative input.
//This is our reference voltage.
//Defaults to ADC0, i.e. PF0/Analog 5
ADCSRB |= _BV(ACME);
//Turn off the ADC as we'll be using its multiplexer.
ADCSRA &= ~_BV(ADEN);
//Turn off digital input on the comparator negative input (PE6)
DIDR1 |= _BV(AIN0D);
//Enable input for start button (INT0/ digital 3)
pinMode(BUTTON_PIN, INPUT_PULLUP);
//Turn on external interrupts
EIMSK = _BV(INT0);
//Interrupt on falling edge
EICRA = _BV(ISC01);
//Flash is output
pinMode(FLASH_PIN, OUTPUT);
digitalWrite(FLASH_PIN, LOW);
//Enable interrupts
sei();
Serial.begin(9600);
}
void loop() {
if(flash == 1) {
//We were triggered by the button
if(triggerTime > 0) {
Serial.print("Delay ");
Serial.print(clockCyclesToMicroseconds(flashStart - triggerTime), DEC);
Serial.println("us");
}
Serial.print("Pulse ");
Serial.print(clockCyclesToMicroseconds(ICR1), DEC);
Serial.println("us");
Serial.println("********");
ignore = 0;
flash = 0;
}
}
// Input capture interrupt
ISR(TIMER1_CAPT_vect) {
if(bit_is_set(TCCR1B, ICES1)) {
//We're currently triggering on rise. Change to fall
TCCR1B &= ~_BV(ICES1);
//Reset counter
TCNT1 -= ICR1;
flashStart = ICR1;
} else {
//We're triggering on fall. Change to rise.
TCCR1B |= _BV(ICES1);
flash = 1;
}
}
// Start button pressed
ISR(INT0_vect) {
if(ignore == 1) {
return;
}
ignore = 1;
digitalWrite(FLASH_PIN, HIGH);
triggerTime = TCNT1;
//Reset and start timer4 with no prescaler
TCNT4 = 0;
TCCR4B = _BV(CS40);
triggering = 1;
}
// Timer overflow
ISR(TIMER4_OVF_vect) {
if(triggering == 1) {
//Turn off the trigger pulse after 1 overflow (~40us)
digitalWrite(FLASH_PIN, LOW);
} else if(triggering == 100) {
//Reset everything after 100 overflows (~4ms)
//Turn off timer4
TCCR4B = 0;
ignore = 0;
triggerTime = 0;
}
triggering++;
}