Page 209 - Programming Microcontrollers in C
P. 209

194    Chapter 4  Small 8-Bit Systems

                          been changed by the interrupt service routine, so the value that was
                          loaded by the lda instruction prior to the interrupt is no longer valid.
                          The earlier loaded value of PORTA will be restored by the above
                          sequence in the application code, and the change made in the inter­
                          rupt service routine will be undone.
                              This bit of interplay between the applications program and the
                          interrupt service routine is a type of software race. It can be easily
                          avoided. Whenever a variable is changed in both the applications
                          code and in an ISR, the erroneous set/reset can be avoided if the
                          program disables all interrupts before the offending instruction in
                          the applications code. The interrupts must then be re-enabled after
                          the code execution. In this case, the code will be
                   ·
                   ·
                   SEI ();
                   PORTA.BITABB=!PORTA.BITAPP;
                   CLI();
                   ·
                   ·

                          in the applications code. In this case, there can be no interrupt to
                          interfere with the proper handling of PORTA in the application.
                              In the timer routine above, there is what some might call a glaring
                          oversight. The timer was not initialized in the initializing routine of
                          the program. The first lines of code merely enabled the timer interrupt
                          and then proceeded into the FOREVER loop, which is the applications
                          portion of the program. The initial state of the TCR and the OCR regis­
                          ters is not established at reset. There is no way of knowing when the
                          first output compare will take place. If the initial value of the OCR
                          happens to be one less than that of the TCR, there will be an output
                          compare about 0.13 seconds after the interrupts are enabled, and from
                          then on the interrupt period will be accurate. If it is imperative that the
                          first output compare occur at exactly the specified time, then the code
                          for initialization of the output compare registers must be included in
                          the initialization routine. Otherwise, if this error in the initial timing
                          can be tolerated, it will save bytes of code space that might be desper­
                          ately needed for another portion of the program.
   204   205   206   207   208   209   210   211   212   213   214