Rob Toulson / Mbed 2 deprecated PE_09-05_TimerDurationTest

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Program Example 9.5: Tests Timer duration, displaying current time values to terminal
00002                                                                      */ 
00003 #include "mbed.h"
00004 
00005 Timer t;
00006 float s=0;                              //seconds cumulative count
00007 float m=0;             //minutes cumulative count
00008 DigitalOut diag (LED1);
00009 Serial pc(USBTX, USBRX);
00010 
00011 int main() {
00012   pc.printf("\r\nTimer Duration Test\n\r");
00013   pc.printf("-------------------\n\n\r");
00014   t.reset();            //reset Timer
00015   t.start();            // start Timer
00016   while(1){
00017     if (t.read()>=(s+1)){   //has Timer passed next whole second?
00018       diag = 1;             //If yes, flash LED and print a message
00019       wait (0.05);
00020       diag = 0;
00021       s++ ;
00022       //print the number of seconds exceeding whole minutes
00023       pc.printf("%1.0f seconds\r\n",(s-60*(m-1)));   
00024     }
00025     if (t.read()>=60*m){
00026       printf("%1.0f minutes \n\r",m);   
00027       m++ ;
00028     }
00029     if (t.read()<s){     //test for overflow
00030       pc.printf("\r\nTimer has overflowed!\n\r");
00031       for(;;){}         //lock into an endless loop doing nothing
00032     }
00033   }            //end of while
00034 }
00035