Timer logic on AVR -


i trying understand piece of code, not able understand how interrupt routine works ocr1a getting updated. using avr series of controller run code.

 void timerinit(void)     {         disable_timer_int;  // disable timer interrupt          m_nglobaltime = 0;  // reset system time          ocr1a  += ticks_per_msecond;    // set first clock period          tccr1a  = 0;// set timermode normal         tccr1b |= (1 << cs10);// clckio, no pre-scaler; set timermode          enable_interrupts;         enable_timer_int;// enable send timer interrupt (1 ms)     }        isr( timer1_compa_vect) {         uint16_t ntemp;           ntemp = tcnt1;  // current time         ntemp -= ocr1a; // subtract interrupt time          if (ntemp < (ticks_per_msecond / 2))// if more half period left             {             ocr1a += (ticks_per_msecond);// add offset ocr1a relative         }          else         {             ocr1a = tcnt1 + (ticks_per_msecond);// set ocr1a 1 ms absolute         }          m_nglobaltime++;                                                                 } 

the usual way output compare interrupt fire @ regular interval add constant amount ocr1a. happening at

ocr1a += (ticks_per_msecond); 

for reason, writer added logic handle bad luck. perhaps, period short, or maybe oc interrupt may delayed due other interrupt running.

if these cases occur, next oc interrupt not occur ticks_per_msecond later last, rather ticks_per_msecond plus entire cycle of counter. is, correct time missed, oc register set number after number has been passed.

this code attempt correct situation. being said, i'm not sure works correctly. potential problem ntemp unsigned, < comparison might not writer expects.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -