This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

APNT_105, understanding "A routine to get the current tick"

Ap note 105 says "You must encapsulate acecess to the timer tick variable to guaranteee that your program does not access this variable at the same time the interrupt is updating it"

How can these happen at the same time? Is the main program still running when the interrupt is being serviced? This is not how I thought it worked.

Thanks,
Bob

  • How can these happen at the same time?

    That's easy.

    1. The timer tick counter must be a multi-byte variable.
    2. The main program starts to read the counter variable. The first byte of the variable is read.
    3. The timer tick interrupt occurs. The timer tick counter variable increments.
    4. The interrupt exits.
    5. The main program continues reading the timer tick counter variable unaware that the interrupt may have overflowed the byte that was already read.

    That's why the example described in the application note probably disables interrupts when it reads the counter variable.

    Jon