avr - 1MHz timer on 16MHz atmega8535 -


can't seem head around this, although looks quite easy me. i've made many timers in atmel atmega8535 1 hits me. also, i've seen many calulations , such not quite looking for.

it's quite simple:

my atmega8535 runs @ 16mhz, want timer of 1mhz can output data on pins @ 1mhz basis.

so how proceed? sounded ok (found here )

target timer count = (1 / target frequency) / (1 / timer clock frequency) - 1                    = (1 / 1000000) / (1 / 16000000) - 1                    = 0.000001 / 0.0000000625 - 1                    = 16 - 1                    = 15  

then result in

void inittimer() {         //8 bit timer 2 setup         tccr2 = (1<<cs20); // timer clock = system clock/1          tifr = 1<<tov2; // clear tov2/ clear pending interrupts          timsk = 1<<toie2; // enable timer2 overflow interrupt         sei(); }  isr(timer2_ovf_vect) // 16 bit timer 2 overflow interrupt vector {         tcnt2= 256-15;    // make sure every overflow reset tcnt2 1mhz setup         addupsomething++; // (...not relevant sample) } 

could please advise on calculations? result in 1mhz timer?

it operation doing inside interrupt function takes longer interval between interrupts. when have 1 mhz timer on 16 mhz device have 16 clock ticks between timer interrupts not whole lot meaningful. besides, have overhead of calling interrupt function (this alone more 16 ticks, don't know) gives less 16 ticks something. when code running inside interrupt function takes more interval between interrupts reduce timer frequency unknown amount , leave little cpu time main code.

i suggest reducing timer frequency timer interrupt has enough time run , enough cpu time left main code (if need that) or choosing device runs @ higher cpu frequency.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -