led_blink.ino_not_used 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Declarations
  2. void ledBlinkFunction();
  3. // LED connected to digital pin 17
  4. const int LED_PIN = 17;
  5. unsigned int blinkLed;
  6. // Interface function to blink the yellow LED
  7. void blinkTheLED() {
  8. blinkLed++;
  9. }
  10. void setup_timer_interrupt()
  11. {
  12. // initialize digital led-pin as an output.
  13. pinMode(LED_PIN, OUTPUT);
  14. blinkLed = 0;
  15. //set timer1 interrupt at 10Hz
  16. TCCR1A = 0;// set entire TCCR1A register to 0
  17. TCCR1B = 0;// same for TCCR1B
  18. TCNT1 = 0;//initialize counter value to 0
  19. // set compare match register for 1hz increments
  20. // OCR1A = 40. This results is 16MHz / 8 / 40 = 20 uS between interrupts
  21. OCR1A = 40;
  22. // turn on CTC mode
  23. TCCR1B |= (1 << WGM12);
  24. // Set CS12:CS10 to 010 = /8 prescaler
  25. TCCR1B |= (1 << CS11);
  26. // enable timer compare interrupt
  27. TIMSK1 |= (1 << OCIE1A);
  28. }
  29. enum {
  30. MODE_INACTIVE,
  31. MODE_ON,
  32. MODE_OFF,
  33. MODE_SWITCH, // Just have been off
  34. };
  35. // 10uS timer interrupt function
  36. ISR(TIMER1_COMPA_vect,ISR_NOBLOCK) {
  37. static unsigned int ledCnt=10000;
  38. rxInterrupt();
  39. if( (--ledCnt) == 0 ) {
  40. ledCnt = 10000;
  41. ledBlinkFunction();
  42. }
  43. }
  44. // 10Hz (100mS) timer interrupt function
  45. void ledBlinkFunction() {
  46. static unsigned long timer = 0;
  47. static int mode = MODE_INACTIVE;
  48. switch( mode ) {
  49. case MODE_INACTIVE:
  50. if( blinkLed > 0 ) {
  51. blinkLed--;
  52. mode = MODE_ON;
  53. timer = 1;
  54. digitalWrite(LED_PIN, LOW); // turn the LED on (HIGH is the voltage level)
  55. }
  56. break;
  57. case MODE_ON:
  58. timer--;
  59. if( timer == 0 ) {
  60. mode = MODE_OFF;
  61. digitalWrite(LED_PIN, HIGH); // turn the LED off by making the voltage LOW
  62. timer = 1;
  63. }
  64. break;
  65. case MODE_OFF:
  66. timer--;
  67. if( timer == 0 ) {
  68. mode = MODE_INACTIVE;
  69. digitalWrite(LED_PIN, HIGH); // turn the LED off by making the voltage LOW
  70. break;
  71. }
  72. }
  73. }