-1

So in my current RTOS code for mbed I have a timer that counts down from 3 minutes displayed in the format of minutes:seconds.I need to implement way so when the time gets to under a minute, the time is displayed in hundredths of a second, such as 59:59. How would I do that?

Here is my current code (the relevant code for displaying time is under void lcd_func (void const *args)):

#include "mbed.h"
#include "cmsis_os.h"

#include "scoreboard.h"
#define deb 180

C12832 lcd(p5, p7, p6, p8, p11);
LM75B sensor(p28,p27);

InterruptIn By1(p15); // Up on the Joystick
InterruptIn By2(p16); // Right on the Joystick
InterruptIn By3(p12); // Down on the Joystick
InterruptIn Team(p14); // Push on the Joystick
InterruptIn Play(p13); // Left to activate clock

// declaration of IDs handle for various threads
osThreadId score_ID, LCD_ID, time_ID, temp_ID; 

// definition of the thread
osThreadDef(score_func, osPriorityNormal, DEFAULT_STACK_SIZE);  
osThreadDef(lcd_func, osPriorityNormal, DEFAULT_STACK_SIZE);   
osThreadDef(time_func, osPriorityNormal, DEFAULT_STACK_SIZE);  
osThreadDef(temp_func, osPriorityNormal, DEFAULT_STACK_SIZE);

// message from ISrs
osMessageQDef(queue, 1, uint32_t);
osMessageQId(queue_ID);

// service routines for Joystick Up, Right and Down
void By1_isr() {osMessagePut(queue_ID, 1, 0);}
void By2_isr() {osMessagePut(queue_ID, 2, 0);}
void By3_isr() {osMessagePut(queue_ID, 3, 0);}
void Team_isr();
void Time_isr();
Timer t;
int minutes,seconds,zero,faults;
int main()
{
    t.start();//start the timer
    By1.rise(&By1_isr);
    By2.rise(&By2_isr); 
    By3.rise(&By3_isr); 
    Team.rise(&Team_isr); 
    Play.rise(&Time_isr);
    queue_ID = osMessageCreate(osMessageQ(queue), NULL);
    score_ID = osThreadCreate(osThread(score_func), NULL);
    LCD_ID = osThreadCreate(osThread(lcd_func), NULL);
    time_ID = osThreadCreate(osThread(time_func), NULL);
    temp_ID = osThreadCreate(osThread(temp_func), NULL);
}

void Team_isr() 
{
    if(t.read_ms() > deb) {
        score.h0v1 = !score.h0v1;
        osSignalSet(LCD_ID, 0x2);
        t.reset();

        }
}

void Time_isr() 
{
    if (score.running == 0)
    {
        score.running = 1;
     }
    else
    {
    faults++;
        score.running = 0;
        }
    osSignalSet(time_ID, 0x3);

}

void Timer1_Update (void const *args)
{
    score.time_count -= 1;
    osSignalSet(LCD_ID, 0x2);
} 


void Destroy(float val)
{
    osThreadTerminate(time_ID);
    osThreadTerminate(score_ID);
    osThreadTerminate(LCD_ID);
    lcd.cls();
    lcd.locate(0,3);
    lcd.printf("Gamed Terminated!\n");
    lcd.printf("(temperature reached %2.1f)\n", val);
    osThreadTerminate(temp_ID);
}

void score_func (void const *args)
{

    score.h0v1 = 0; // home by default
    score.time_count = 180;
    score.home_count = 0;
    score.visitors_count = 0;
    uint32_t val;
    while (1) {
        osEvent score_sig = osMessageGet(queue_ID, osWaitForever);
        if (score_sig.status == osEventMessage) 
            val = score_sig.value.v;
        if (score.h0v1 == 0)
            score.home_count += val;
        else
            score.visitors_count += val;
        osSignalSet(LCD_ID, 0x2);
    }
}

void lcd_func (void const *args)
{
    while(1) {
        minutes=score.time_count/60;
        seconds=score.time_count%60;
        if (seconds<10)
        {
        lcd.cls();
        lcd.locate(0,3);
        lcd.printf("Time remaining: %2d:%d%d\n",minutes,zero,seconds);
            }
            else{
        lcd.cls();
        lcd.locate(0,3);
        lcd.printf("Time remaining: %2d:%2d\n",minutes,seconds);}
        if (score.h0v1 == 0) 
            lcd.printf("*Home: %2d      Visitors: %2d\n", 
                        score.home_count, score.visitors_count);
        else
            lcd.printf(" Home: %2d     *Visitors: %2d\n", 
                        score.home_count, score.visitors_count);
        osSignalWait(0x2, osWaitForever);
    }        
}

void time_func (void const *args)
{
    osTimerDef (Timer1, Timer1_Update);     
    osTimerId Timer1_ID;  
    // Activate time
    Timer1_ID = osTimerCreate (osTimer(Timer1), osTimerPeriodic, NULL);
    while(1) {
        osSignalWait(0x3, osWaitForever);
        if (score.running == 0)
            osTimerStop (Timer1_ID); 
        else
            osTimerStart (Timer1_ID, 1000UL); 
    }
}

void temp_func (void const *args)
{
    float temp;   
    if (sensor.open()) {
        sensor.alertTemp(40.0);
        while (1) {
            temp = (float)sensor.temp();
            if (temp > 30.0) 
                Destroy(temp);
            osDelay(5000);
        }
    }
    else
    osThreadTerminate(temp_ID);
}
super95
  • 103
  • 1
  • 10
  • 1
    Your question is about a very specific part of your code - there is no need to post your entire application. Moreover your question has little to do with mbed or RTOS; it is rather generic and would get a larger audience perhaps is tagged differently. – Clifford Mar 12 '19 at 08:30

1 Answers1

0

Change your timer to decrement every 100th and start at 18000. Then:

minutes = score.time_count / 6000;
seconds = (score.time_count % 6000) / 100 ;
hundredths = score.time_count % 100 ;

...

if( minutes == 0 )
{
    lcd.printf( "Time remaining: 00:%2d:%2d\n", seconds, hundredths ) ;
}
else
{
    lcd.printf( "Time remaining: %2d:%2d:00\n", minutes, seconds ) ;
}
Clifford
  • 88,407
  • 13
  • 85
  • 165