I am trying to use two timer interrupts, i am using timer1(1hz) and timer2(120hz) interrupt,but my program is not executing the timer1 for 1second, actually i know that timer1 has highest priority than timer2.But here timer1 is not executing because of frequency.
There is no priority, they are executed one after another.
If they happen at the same time (almost impossible) then there is some kind of priority.
It is even possible to enable the interrupts in a ISR to allow another interrupt to be executed, but that can be dangerous, so that is not adviced.
int cnt=0,cnt1=0,cnt2=0;
void setup(){
Serial.begin(9600);
cli();//stop interrupts
//set timer1 interrupt at 1Hz
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 = 65;// = (16*10^6) / (1*1024) - 1 (must be <65536)//3905//15624
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
TCCR2A = 0;// set entire TCCR2A register to 0
TCCR2B = 0;// same for TCCR2B
TCNT2 = 0;//initialize counter value to 0
// set compare match register for 8khz increments
OCR2A = 130;// = (16*10^6) / (120*1024) - 1 (must be <256)
// turn on CTC mode
TCCR2A |= (1 << WGM21);
// Set CS21 bit for 8 prescaler
TCCR2B |= (1 << CS21);
// enable timer compare interrupt
TIMSK2 |= (1 << OCIE2A);
sei();//allow interrupts
//cli();
}//end setup
//ISR(TIMER0_COMPA_vect){//timer0 interrupt 2kHz toggles pin 8
////generates pulse wave of frequency 2kHz/2 = 1kHz (takes two cycles for full wave- toggle high then toggle low)
// cnt2=cnt2+1;
//Serial.print("cnt2:");
//Serial.println(cnt2);
//Serial.println("c");
//}
ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz toggles pin 13 (LED)
//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low)
cnt=cnt+1;
Serial.print("cnt:");
Serial.println(cnt);
Serial.println("a");
}
ISR(TIMER2_COMPA_vect){//timer1 interrupt 8kHz toggles pin 9
//generates pulse wave of frequency 8kHz/2 = 4kHz (takes two cycles for full wave- toggle high then toggle low)
cnt1=cnt1+1;
Serial.print("cnt1:");
Serial.println(cnt1);
Serial.println("b");
}
void loop(){
//do other things here
}