I have found the example for blinking led using timer interrupt.
In the code we can see that TCNT1 is set to 34286, but I wanna know if there is any way to change that withing program. For example that after LED blinks 5 times, I wanna set TCNT1 to 0 so I have longer "blinking" period.
Is this possible using hardware interrupt or somehow withing software ?
Thank you
Code:
/*
* Arduino 101: timer and interrupts
* 2: Timer1 overflow interrupt example
* more infos: http://www.letmakerobots.com/node/28278
* created by RobotFreak
*/
#define ledPin 13
void setup()
{
pinMode(ledPin, OUTPUT);
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 34286; // preload timer 65536-16MHz/256/2Hz
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enable all interrupts
}
ISR(TIMER1_OVF_vect) // interrupt service routine that wraps a user defined function supplied by attachInterrupt
{
TCNT1 = 34286; // preload timer
digitalWrite(ledPin, digitalRead(ledPin) ^ 1);
}
void loop()
{
// your program here...
}
In the code we can see that TCNT1 is set to 34286, but I wanna know if there is any way to change that withing program. For example that after LED blinks 5 times, I wanna set TCNT1 to 0 so I have longer "blinking" period.
Is this possible using hardware interrupt or somehow withing software ?
Thank you