Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Duty cycle calculation using input capture unit
- // Author: Nick Gammon
- // Date: 5 November 2013
- // Input: Pin D8
- volatile boolean first;
- volatile boolean triggered;
- volatile unsigned long startTime, finishTime;
- ISR (TIMER1_CAPT_vect) {
- // wait until we noticed last one
- if (triggered)
- return;
- if (first) {
- startTime = ICR1 ;
- TCCR1B = bit (CS10); // No prescaling, Input Capture Edge Select (falling on D8)
- first = false;
- return;
- }
- finishTime = ICR1 ;
- triggered = true;
- }
- void prepareForInterrupts (){
- first = true;
- triggered = false; // re-arm for next time
- TCCR1B = bit (CS10) | bit (ICES1); // plus Input Capture Edge Select (rising on D8)
- }
- void setup () {
- Serial.begin(115200);
- Serial.println("Pulse width measure");
- TCCR1A = 0;
- TCCR1B = 0;
- // Timer 1 - interrupt on input capture, no prescaler
- TIMSK1 = bit (ICIE1);
- TCCR1B = bit (CS10) | bit (ICES1); // plus Input Capture Edge Select (rising on D8)
- }
- void loop ()
- {
- // wait till we have a reading
- if (!triggered)
- return;
- Serial.print ("Pulse took: ");
- Serial.print (float (finishTime-startTime) * 62.5e-9 * 1e6); // convert to microseconds
- Serial.println (" uS. ");
- // Just for printing the value to serial monitor
- delay (500);
- prepareForInterrupts ();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement