PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

55 lines
1.4KB

  1. #include <TimerThree.h>
  2. // This example uses the timer interrupt to blink an LED
  3. // and also demonstrates how to share a variable between
  4. // the interrupt and the main program.
  5. const int led = LED_BUILTIN; // the pin with a LED
  6. void setup(void)
  7. {
  8. pinMode(led, OUTPUT);
  9. Timer3.initialize(150000);
  10. Timer3.attachInterrupt(blinkLED); // blinkLED to run every 0.15 seconds
  11. Serial.begin(9600);
  12. }
  13. // The interrupt will blink the LED, and keep
  14. // track of how many times it has blinked.
  15. int ledState = LOW;
  16. volatile unsigned long blinkCount = 0; // use volatile for shared variables
  17. void blinkLED(void)
  18. {
  19. if (ledState == LOW) {
  20. ledState = HIGH;
  21. blinkCount = blinkCount + 1; // increase when LED turns on
  22. } else {
  23. ledState = LOW;
  24. }
  25. digitalWrite(led, ledState);
  26. }
  27. // The main program will print the blink count
  28. // to the Arduino Serial Monitor
  29. void loop(void)
  30. {
  31. unsigned long blinkCopy; // holds a copy of the blinkCount
  32. // to read a variable which the interrupt code writes, we
  33. // must temporarily disable interrupts, to be sure it will
  34. // not change while we are reading. To minimize the time
  35. // with interrupts off, just quickly make a copy, and then
  36. // use the copy while allowing the interrupt to keep working.
  37. noInterrupts();
  38. blinkCopy = blinkCount;
  39. interrupts();
  40. Serial.print("blinkCount = ");
  41. Serial.println(blinkCopy);
  42. delay(100);
  43. }