sss
sss

Reputation: 45

Why does not my TIMER0 use the oscillator between XTAL1 and XTAL2 on my Atmega644 controller?

So I have tried to put a 16 MHz crystal oscillator between XTAL1 and XTAL2 pins on my Atmega644 MCU. But I don't get it to work.

I first define

volatile unsigned int input_timer = 150;

ISR (TIMER0_COMPA_vect) {
   if (input_timer>0){
      input_timer--;
   }
 }

I have initiated the timer as following

 TIMSK0 = (1 << OCIE0A); //Compare match enabled
 TCCR0A = (1 << WGM01); //CTC mode
 TCCR0B = ~(1 << CS02) | (1 << CS01) |( 1 << CS00); //DIVIDE 64
 OCR0A = 125; 

For testing if it works I am doing the following

 int p = 0;
 while(1) 
{  
 if (input_timer == 0) {
    lcd_clrscr();
    char c[7]
    itoa(p,c,10);
    lcd_puts(c);
    p++;
 }

So basically I am updating the lcd screen once everytime the counter has counted down from 150 to 0. If it is with the 16 MHz osc, this should happen every (16e6 / (150*125*64) = 13.3 updates/sec.

If it is done with F_CPU it should happen every (1e6/(125*150*64))= 0.83 updates/sec which is what is happening now.

So how do I actually correctly implement the oscillator on the XTAL1 and XTAL2 pins. It should be noted that I am not using the correct decoupling capacitor right now, not sure if it could matter.

Upvotes: 0

Views: 95

Answers (2)

AShelly
AShelly

Reputation: 35550

Your problem is here TCCR0B = ~(1 << CS02) | (1 << CS01) |( 1 << CS00); //DIVIDE 64

~(1<<CS02) sets every bit EXCEPT CS02. So instead of putting 00000011 into TCCR0B, you are putting 11111011. This means you are setting WGM to a reserved value, instead of leaving it in CTC mode. (Because bit 3 is part of the WGM setting).

The preferred way to indicate you want to put a zero in that bit position is (0<<CS02).

Upvotes: 0

0___________
0___________

Reputation: 67574

You need to set the fuses as well to let know your micro that you are using the external clock.

http://www.ladyada.net/learn/avr/fuses.html

Upvotes: 1

Related Questions