Page 418 - Programming Microcontrollers in C
P. 418

Delays Revisited    403

                          inituart(38400);
                          Enable_Fast_Interrupts();
                          vector(handler,FAST_AUTOVECTOR);
                          while(count++<300)
                          {
                                 if((semaphore=attach_semaphore())==-1)
                                 {
                                         puts(“semaphore attachment error\n”);
                                         exit(0);
                                 }


                                 delay(1000,semaphore);
                                 wait_for_semaphore(semaphore);
                                 printd(count);
                                 putchar(‘\r’);
                          }
                   }
                              Listing 8-4: Delay Test Routine

                              The delay test routine first initializes the on-board UART so that
                          signals can be sent to a terminal and then enables the fast interrupts
                          and places the handler address in the FAST_AUTOVECTOR location
                          as needed. The main test attaches a semaphore, executes a delay of
                          1000 milliseconds, waits for the delay to expire and prints a value to
                          the screen. This sequence is placed in a loop that is executed 300 times.
                          Note that it is important that attach_semaphore() be checked
                          for a –1 return. If not, you could attempt to attach too many semaphores,
                          and your code would not catch the error. In the above case, the program
                          was exited with the exit() function. It is not usually necessary to
                          abort the program when there is no semaphore when one is needed.
                          You can merely wait for a semaphore with any value between 0 and 9.
                          When that semaphore is released, you can proceed with a request to
                          attach a semaphore and it should then succeed.
                              The attach semaphore is in the calling program in this case. The
                          semaphore number is sent to the delay program where it is in turn
                          passed to the pit interrupt service routine. When the interrupt service
                          routine, after the proper delay, is executed, the specified semaphore
                          is released. Therefore, in the calling program, the wait for semaphore
                          routine is executed until the semaphore is released. If needed, the
                          semaphore status can be polled synchronously by the program to
                          determine when the delay time has expired. In other words, the
   413   414   415   416   417   418   419   420   421   422   423