Page 193 - ARM Based Microcontroller Projects Using MBED
P. 193

8.15  PROJECT 12—EVENT COUNTER                   179
                      BEGIN
                             Configure LED1 as output and BUTTON1 as input
                             Display heading
                             DO FOREVER
                                    Reset Timer
                                    Generate a random number 1 to 10 into r
                                    Wait randomly r seconds
                                    Start the Timer
                                    Flash the LED for 50ms
                                    Wait until button is pressed
                                    Stop the Timer
                                    Calculate the elapsed time
                                    Display the elapsed time
                                    Wait 3 seconds
                             ENDDO
                      END
            FIG. 8.40  Program PDL.

              An infinite loop is formed using a while statement. Inside this loop, a random number is
            generated between 1 and 10 and this is used as an argument in the wait statement. Therefore,
            the program waits randomly between 1 and 10s. The LED is then flashed for 50ms and the
            program starts the Timer and waits until the button is pressed. When the button is pressed,
            the Timer is stopped and the elapsed time is read in milliseconds and stored in variable Du-
            ration. In all, 50ms is subtracted from the elapsed time since the duration between the ON
            and OFF times is 50ms. The Duration is then displayed on the PC screen. The program re-
            peats after 3s of delay.
              Fig. 8.42 shows a typical output from the program.


                              8.15 PROJECT 12—EVENT COUNTER

            8.15.1 Description

              This is an event counter project. The User button is used to simulate the occurrence of ex-
            ternal events such that every time the button is pressed a count is incremented by one and the
            total count is displayed on the PC screen continuously.

            8.15.2 Aim
              The aim of this project is to show how the various serial communication functions can
            be used.
            8.15.3 The PDL

              Fig. 8.43 shows the program PDL.
   188   189   190   191   192   193   194   195   196   197   198