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

154                          8. INTERMEDIATE LEVEL PROJECTS

                    /*****************************************************************************

                                        4-DIGIT MULTIPLEXED LED EVENT COUNTER
                                        =====================================
                    In this program two 2-digit 7-Segment LEDs are connected to PORT C of the
                    Nucleo-F411RE development board to form a 4-digit 7-Segment display. The
                    program counts up by one each time the User Button is pressed. The button
                    simulates the occurence of external events.
                    Author: Dogan Ibrahim
                    Date  : August 2018
                    File  : EventCounter
                    *****************************************************************************/
                    #include "mbed.h"
                    Ticker tim;

                    PortOut Segments(PortC, 0xFF);
                    int LEDS[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
                    DigitalIn button(BUTTON1);
                    int W, Y, CNT = 0, Flag = 3;
                    int Digits[4];

                    //
                    // Digit Enable bits
                    //
                    DigitalOut Enable3(PC_8);
                    DigitalOut Enable2(PC_9);
                    DigitalOut Enable1(PC_10);
                    DigitalOut Enable0(PC_11);
                    #define Enable 1
                    #define Disable 0
                    //
                    // Thsi is the Interrupt Service Routine (ISR) which is called at every
                    // 5ms by Ticker
                    //
                    void Refresh()
                    {
                    //
                    // Extract digits of CNT into Digits[]. Digits[3] holds the MSD digit
                    // and Digits[0] holds the LSD digit
                    //
                        Digits[3] = CNT / 1000;
                        Y = CNT - 1000*Digits[3];
                        Digits[2] = Y / 100;
                        W = Y - 100*Digits[2];
                        Digits[1] = W /10;
                        Digits[0] = W % 10;

                        if(Flag == 3)                               // If to refresh Digit 3
                        {
                            Enable0 = Disable;
                            Flag = 2;
                 FIG. 8.17  Program listing.
                                                                                          (Continued)
   163   164   165   166   167   168   169   170   171   172   173