123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- // Declarations
- void ledBlinkFunction();
- // LED connected to digital pin 17
- const int LED_PIN = 17;
- unsigned int blinkLed;
- // Interface function to blink the yellow LED
- void blinkTheLED() {
- blinkLed++;
- }
- void setup_timer_interrupt()
- {
- // initialize digital led-pin as an output.
- pinMode(LED_PIN, OUTPUT);
- blinkLed = 0;
- //set timer1 interrupt at 10Hz
- TCCR1A = 0;// set entire TCCR1A register to 0
- TCCR1B = 0;// same for TCCR1B
- TCNT1 = 0;//initialize counter value to 0
- // set compare match register for 1hz increments
-
- // OCR1A = 40. This results is 16MHz / 8 / 40 = 20 uS between interrupts
- OCR1A = 40;
- // turn on CTC mode
- TCCR1B |= (1 << WGM12);
- // Set CS12:CS10 to 010 = /8 prescaler
- TCCR1B |= (1 << CS11);
-
- // enable timer compare interrupt
- TIMSK1 |= (1 << OCIE1A);
- }
- enum {
- MODE_INACTIVE,
- MODE_ON,
- MODE_OFF,
- MODE_SWITCH, // Just have been off
- };
- // 10uS timer interrupt function
- ISR(TIMER1_COMPA_vect,ISR_NOBLOCK) {
-
- static unsigned int ledCnt=10000;
-
- rxInterrupt();
-
- if( (--ledCnt) == 0 ) {
- ledCnt = 10000;
- ledBlinkFunction();
- }
- }
- // 10Hz (100mS) timer interrupt function
- void ledBlinkFunction() {
- static unsigned long timer = 0;
- static int mode = MODE_INACTIVE;
- switch( mode ) {
- case MODE_INACTIVE:
- if( blinkLed > 0 ) {
- blinkLed--;
- mode = MODE_ON;
- timer = 1;
- digitalWrite(LED_PIN, LOW); // turn the LED on (HIGH is the voltage level)
- }
- break;
- case MODE_ON:
- timer--;
- if( timer == 0 ) {
- mode = MODE_OFF;
- digitalWrite(LED_PIN, HIGH); // turn the LED off by making the voltage LOW
- timer = 1;
- }
- break;
- case MODE_OFF:
- timer--;
- if( timer == 0 ) {
- mode = MODE_INACTIVE;
- digitalWrite(LED_PIN, HIGH); // turn the LED off by making the voltage LOW
- break;
- }
- }
- }
|