0

Does somebody know what can be wrong in this, where I try to set Timer1 interrupt in Arduino Mega in C code only. I'm trying CTC Mode and the purpose is LED blinking with 1[s] Period.

I work with documentation program doesn't work completely.

 #include <avr/io.h>
 #include <avr/interrupt.h>



 void setup() 
 {
   //LED -> PB7 (Digital pin 13 
     DDRB |= (1<<PB7); 

  //FCPU = 16MHz, Prescaler :256 | CTC
   TCCR1B |= (1<<CS12) | (1<<WGM12);
   TIMSK1 |= (1<<OCIE1A);  // Compare Match A interrupt allowing
     OCR1A = 62500;

    sei();
    }

  void loop() 
  {

   }

   ISR(TIMER1_COMPA_vect)
   {
     PORTB ^= (1<<PB7); 
    }
  • Where is your main procedure? For toggling you can use PINB = _BV(PB7); // or (1<<PB7), for setting up timer you can use assigments directly, as you don't care about previous values... – KIIV Jul 23 '17 at 9:26
  • Actually you should use assignments. The Arduino core library configures timer 1 for its own purposes (for phase-correct PWM at 490 Hz), so you should set both TCCR1A and TCCR1B to whatever suits your application, and not rely on their previous values. Alternatively, if you are not using the core, you could write your own main(), which would skip the Arduino hardware initialization. – Edgar Bonet Jul 23 '17 at 10:31
  • Btw the arduino IDE requires using setup() and loop() section as a main(). So you suggest using "=" instead of "|=" regarding to preliminary arduino settings? – M_K Jul 23 '17 at 10:39
  • So that "pure C" doesn't really mean you have simple *.c file with everything? You have *.ino and you're just using registers directly? Then yes, you have to make sure everything for Timer1 is in correct states. For example all counters are configured to the 8bit fast PWM mode and setting WGM12 doesn't change anything as it's already set. – KIIV Jul 23 '17 at 10:50
  • Ok, ok. I catch it. To be clear. I'm want to code in pure C but using Arduino IDE. So It requires specific arduino's program structure with setup() and loop(). And apart from it, everything is as a normal pure C language, working with uC registers not with arduino's libraries. That's my purpose. – M_K Jul 23 '17 at 11:11
1

I work with documentation program doesn't work completely.

simple:

1) check out the datasheet; 2) identify those registers needed to get tmr1 to work; 3) set or clear, as appropriate, ALL bits that are needed. Do NOT rely on default values. 4) done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.