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

146                          8. INTERMEDIATE LEVEL PROJECTS

                        {
                            if(Digits[3] != 0)                      // If MSD Digit non zero
                            {
                                Segments = LEDS[Digits[3]];         // Send to PORT C
                                Enable3 = Enable;                   // Enable Digit 3
                                wait(0.005);                        // Wait 5ms
                                Enable3 = Disable;                  // Disable Digit 3
                            }

                            if(Digits[2] != 0 || (Digits[2] == 0 && Digits[3] != 0))
                            {
                                Segments = LEDS[Digits[2]];         // Send to PORT C
                                Enable2 = Enable;                   // Enable Digit 2
                                wait(0.005);                        // Wait 5ms
                                Enable2 = Disable;                  // Disable Digit 2
                            }

                            if(Digits[1] != 0 || (Digits[3] != 0 || Digits[2] != 0))
                            {
                                Segments = LEDS[Digits[1]];         // Send to PORT C
                                Enable1 = Enable;                   // Enable Digit 1
                                wait(0.005);                        // Wait 5ms
                                Enable1 = Disable;                  // Disable Digit 1
                            }

                            Segments = LEDS[Digits[0]];             // Send to PORT C
                            Enable0 = Enable;                       // Enable Digit 0
                            wait(0.005);                            // Wait 5ms
                            Enable0 = Disable;                      // Disable Digit 0
                        }
                    }

                 FIG. 8.11,  CONT’D



                                      8.4 POLLING AND INTERRUPTS

                   In many real-time applications, we may want to respond to external events as soon as they
                 happen. But this seems not to be possible while the CPU is executing some code. What we
                 basically want is to interrupt the CPU so that it stops what it has been doing and starts to
                 execute the code in our real-time routine. After completing the real-time routine, the CPU
                 should return and carry on what it has been doing before being interrupted. The concept that
                 stops the CPU and starts executing the real-time code is called Interrupt.
                   Basically, there are two ways that the CPU can respond to external events: polling and
                 interrupts.


                 8.4.1 Polling
                   In polling, the CPU checks the occurrence of an external event in a loop while executing
                 some other code. Fig. 8.12 shows a flow diagram where the CPU is carrying out some
   155   156   157   158   159   160   161   162   163   164   165