不到一分钟时以百分之一秒显示计时器

问题描述 投票:-1回答:1

因此,在我目前的mbed RTOS代码中,我有一个计时器,从分钟显示的3分钟内倒计时:秒。我需要实现方式,所以当时间不到一分钟时,时间显示为百分之一秒第二,如59:59。我该怎么办?

这是我当前的代码(显示时间的相关代码在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);
}
c++ rtos mbed
1个回答
0
投票

将计时器更改为每100秒递减一次,从18000开始。然后:

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 ) ;
}
© www.soinside.com 2019 - 2024. All rights reserved.