Confused about timers

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

Wonder if someone can hep me out

 

I'm working on some existing code to generate an interupt every 4096 clock cycles

 

// CTC with OCR0A as TOP
	TCCR0A = (1 << WGM01);
	// clk_io/1024 (From prescaler)
	TCCR0B = ((1 << CS02) | (1 << CS00));
	// Generate an interrupt every 4096 clock cycles
	OCR0A = 3;
	// Enable Timer/Counter0 Compare Match A interrupt
	TIMSK0 |= (1 << OCIE0A);

The interrupt is firing, but I'm not sure of the interval and I think this is for an atmega328p running at 16Mhz

 

I'm using an Atmega32PB with an external clock source running at 20Mhz.(connected to just the one clock input pin)  I've read though the Atmel docs, but it's making my head spin.

 

Will the above apply for my setup or need I make some changes?

 

Would be very grateful if someone could enlighten me

 

regards

 

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Surely the number of cycles (4096) does not matter? It must surely be the period/frequency that matters?. 

 

If it was 4096 cycles (0..3 times 1024) at 16MHz then the period would have been 4096/16000000s or 256us so I guess that's what you want to achieve again. At 20MHz the cycle time is 50ns (0.05us) so you want 256/.05 cycles which is 5,120 cycles. So you'll have to look at smaller prescalers to see how close you can get.