3D printed Arduino Airsoft Chronograph
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timing.cpp 871B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * timing.cpp
  3. *
  4. * OpenChrono BB speed measurement device.
  5. *
  6. * Copyright (c) 2022 Thomas Buck <thomas@xythobuz.de>
  7. *
  8. * Two phototransistors connected to external interrupts 0 and 1.
  9. */
  10. #include <Arduino.h>
  11. #include "timing.h"
  12. #include "config.h"
  13. volatile uint8_t trigger_a = 0, trigger_b = 0;
  14. volatile uint16_t time_a = 0, time_b = 0;
  15. void interrupt_init() {
  16. // trigger both on rising edge
  17. EICRA = (1 << ISC00) | (1 << ISC01);
  18. EICRA |= (1 << ISC10) | (1 << ISC11);
  19. // enable interrupts
  20. EIMSK = (1 << INT0) | (1 << INT1);
  21. }
  22. ISR(INT0_vect) {
  23. time_a = timer_get();
  24. trigger_a = 1;
  25. }
  26. ISR(INT1_vect) {
  27. time_b = timer_get();
  28. trigger_b = 1;
  29. }
  30. // --------------------------------------
  31. void timer_init() {
  32. // normal mode, prescaler 1
  33. TCCR1A = 0;
  34. TCCR1B = (1 << CS10);
  35. }
  36. uint16_t timer_get() {
  37. return TCNT1;
  38. }