Page 164 - ARM Based Microcontroller Projects Using MBED
P. 164
150 8. INTERMEDIATE LEVEL PROJECTS
Mbed supports three types of timers [in addition to the real-time clock (RTC)]: Timer,
Timeout, and Ticker. In this section, we shall be looking at the details of Ticker function
which can be used to generate timer interrupts at regular intervals. We shall be looking at
the use of other timer functions in later sections of this chapter.
Using the Ticker
The ticker function is used to call an ISR periodically at regular intervals. Table 8.1 presents
the various Ticker functions. The steps to use the Ticker function are as follows:
• Define a Ticket-type variable
• Attach the Ticker variable to a function (this is your ISR) and define its period
• The ISR will be called periodically at the specified times
8.5.6 Program Listing
The program listing (program: SevenSegMux6) is given in Fig. 8.15. At the beginning of
the program, tim is defined of type Ticker, lower byte of PORT C is grouped together into a
variable called Segments. Array Digits is declared at the beginning of the program so that it is
a global integer array. All the digits are disabled, and function called Refresh has been at-
tached to the Ticker with a period of 5ms so that the function is called periodically at every
5ms. The main program runs in a loop, increments variable CNT, and waits for 1s. Function
Refresh implements the display functions. Here, variable Flag is used to decide which digit
should be refreshed. If Flag is 3 then digit 3 is refreshed, if it is 2 then digit 2 is refreshed, and
so on. Notice that the wait statements have been removed from the program since the Refresh
function is called at every 5ms. Also, although digit 3 is enabled in digit 3 refresh cycle, it is
disabled in digit 2 refresh cycle. This is done for the other digits as well.
8.6 PROJECT 4—FOUR-DIGIT 7-SEGMENT LED EVENT COUNTER
8.6.1 Description
In this project, the Ticker timer interrupts are used to refresh the four-digit 7-segment dis-
play as in the previous project. The program counts up by one every time the on-board User
button is pressed, therefore simulating the occurrence of external events.
TABLE 8.1 Ticker Functions
Timeout Function Description
attach Attach a user function to a Ticker, specifying the repeat interval in seconds
attach_us Attach a user function to a Ticker, specifying the repeat interval in microseconds
detach Detach the function from Ticker