0

I am trying to generate LED blink after 4 second using Interrupt but Interrupt is not triggering not even once . Request: if you have arduino uno r3 please check at your end and let me know result. I have read Timer counter section multiple times and applied everything i can for example :-

  • Enabled Global Interrupt
  • Select CTC Mode
  • Enable Interrupt for OCIE0A
  • Prescaling to clk/1024
  • Initializing OCR0A
  • When OCR0A equal TCNT0 ,interrupt will generate
int var;
void setup() {
 // put your setup code here, to run once:
 SREG = SREG | (1<<7); // Gloval interrupt enable
 TCCR0A = TCCR0A | (1<<WGM01); // Clear counter on compare match mode
 OCR0A = 255; // when TCNT equal OCR0 interrupt will generate
 TIMSK0 = TIMSK0 | (1<<OCIE0A); // Output Compare Match A Interrupt Enable
 TCCR0B = TCCR0B | (1<<CS01) | (1<<CS02); // Prescaling by 1024
 DDRB = DDRB | (1<<5); 
 Serial.begin(9600); // Testing 
 Serial.println("Start"); // Testing
}
void loop() {
 // put your main code here, to run repeatedly:
}
ISR(TIMER0_COMPA_vect){
 var++;
 Serial.print(var); // Testing
 volatile long unsigned i;
 if(var == 246){
 PORTB = PORTB | (1<<5);
 for(i=0; i<30000; i++);
 PORTB = PORTB & ~(1<<5);
 for(i=0; i<30000; i++);
 var=0; 
 }
}
  • clock is 16 00 00 00 Hz
  • Prescaling by 1024 = 15625
  • 1 Tick = 1/15625 = 0.00 00 64 sec
  • OCR0 is initialized by 255
  • TCNT0 Ticks to 255
  • Total time taken to generate one interrupt = 255 * 0.00 00 64 = 0.01632 sec that means after every 0.01632 sec Interrupt supposed to generate
  • How many interrupt is required to generate LED after 4 sec => 4/0.01632 = 246

Update working code

int var;
void setup() {
 // put your setup code here, to run once:
 SREG = SREG | (1<<7); // Gloval interrupt enable
 TCCR0A =(1<<WGM01); // Clear counter on compare match mode
 OCR0A = 255; // when TCNT equal OCR0 interrupt will generate
 TIMSK0 = (1<<OCIE0A); // Output Compare Match A Interrupt Enable
 TCCR0B = (1<<CS00) | (1<<CS02); // Prescaling by 1024
 DDRB = DDRB | (1<<5); 
 Serial.begin(9600); // Initialize the USART 
 Serial.println("Start"); // Write the welcome message
}
void loop() {
 // put your main code here, to run repeatedly:
}
ISR(TIMER0_COMPA_vect){
 var++;
 // Serial.print(var);
 volatile long unsigned i;
 if(var == 246){
 PORTB = PORTB | (1<<5);
 for(i=0; i<300000; i++);
 PORTB = PORTB & ~(1<<5);
 var=0; 
 }
}
asked May 29, 2020 at 17:02

1 Answer 1

0

There are a few issues with your code:

First, you should completely overwrite the timer control registers rather than changing single bits. For example, when you write

TCCR0A = TCCR0A | (1<<WGM01); // Clear counter on compare match mode

You end up with a timer mode that results from the combination of the bits that were already set there and the ones you are setting. Judging from the comment, you assume that this will be CTC mode, but this is not granted, because the Arduino initialization routine has already set some bits in this register. Since you are taking the timer for your own usage, just overwrite all the control registers:

TCCR0A = (1<<WGM01); // Clear counter on compare match mode

and same for TCCR0B and TIMSK0.

The second problem is that you misread the "Clock Select Bit Description" table from the datasheet. Your timer is being clocked from an external clock source on T0 pin. Presumably you have nothing connected there, so the timer does not increment. You probably meant

TCCR0B = (1<<CS00) | (1<<CS02); // Prescaling by 1024

A smaller issue is your delay loop. The program would be easier to understand if you instead use a proper delay function. The Arduino delay() will not work, as you have reconfigured Timer 0, but you can use _delay_ms() from the avr-libc if you #include <util/delay.h>.

answered May 29, 2020 at 19:58
1
  • Thanks a lot , you are right i miss read Prescaling bits . my code is working now Commented May 30, 2020 at 5:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.