Paril Jain / Mbed 2 deprecated CIS541_HW2_Timer

Dependencies:   TextLCD mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 DigitalOut myled(LED1);
00004 DigitalOut myled_(LED2);
00005 
00006 Semaphore minMutex;
00007 Semaphore secMutex;
00008 
00009 RawSerial pc(USBTX, USBRX); // tx, rx
00010 TextLCD lcd(p15, p16, p17, p18, p19, p20);
00011 
00012 void hundredth(void const *n);
00013 
00014 int sec, hsec, min;
00015 int pause, startFlag; 
00016 
00017 void minute(void const *n)
00018 {
00019     while(1)
00020     {
00021         minMutex.wait();
00022         min++;
00023     }
00024 }
00025 
00026 void second(void const *n)
00027 {
00028     while(1)
00029     {
00030         secMutex.wait();
00031         sec++;
00032         if(sec == 60)
00033         {
00034             minMutex.release();
00035             sec=0;
00036         }
00037     }
00038 }    
00039 
00040 void hundredth(void const *n)
00041 {
00042 //    if(startFlag == 1)
00043     {
00044         hsec++;
00045         if(hsec == 100)
00046         {
00047             secMutex.release();
00048             hsec = 0;
00049         }
00050     }
00051 }
00052 
00053 void display(void const *n)
00054 {
00055     while(1)
00056     {
00057         if(!pause)
00058         {
00059             lcd.locate(0,0);
00060             lcd.printf("%2d:%2d:%2d",min,sec,hsec);  
00061             Thread::wait(10);
00062         }
00063     }
00064 }    
00065  
00066 
00067 void key()
00068 {
00069     char c = pc.getc();
00070     if(c == 's' || c == 'S')
00071     {
00072         startFlag = !startFlag;
00073     }
00074     else if(c == 'p' || c == 'P')
00075     {
00076         pause = !pause;
00077     }
00078     else if(c == 'r' || c == 'R')
00079     {
00080         min = 0;
00081         sec = 0;
00082         hsec = 0;
00083     }
00084 }    
00085 
00086 int main()
00087 {
00088     hsec = 0;
00089     sec = 0;
00090     min = 0;
00091     pause = 0;
00092     startFlag = 0;
00093         
00094     RtosTimer hund(hundredth, osTimerPeriodic,(void *)0);
00095 
00096     Thread secondThread(second);
00097     Thread minuteThread(minute);
00098     Thread displayThread(display);
00099     
00100     secondThread.set_priority(osPriorityNormal);
00101     minuteThread.set_priority(osPriorityNormal);
00102     displayThread.set_priority(osPriorityIdle);
00103 
00104 
00105 
00106     hund.stop();
00107 
00108     pc.attach(&key, Serial::RxIrq);
00109     
00110     while(1)
00111     {
00112         if(startFlag)
00113         {
00114             hund.start(10);
00115         }
00116         else
00117         {
00118             hund.stop();
00119         }
00120         Thread::wait(10);
00121     }
00122 }