A mm:ss 计时器 C

A mm:ss timer C

试图在 C 中制作一个以 mm:ss 格式计数的计时器,我不一定需要打印该值,只要让它存在以供参考即可。

这个想法是在小型设备上设置一个 "time elapsed" 时钟,每经过一秒,计时器就会增加 1 秒,在 60 秒时,分钟计时器会增加 1,秒计时器会重置为0. 计时器在后台运行,每秒更新一次,充当一种连续的秒表。

类似于下图,但是定时器不会随意启动或停止,它在程序初始化时启动,只是每秒增加它的计数器,分钟不需要转换,可以上升到99,这个时候节目应该结束了。

一个例子是一个时钟,显示自从开始游戏关卡以来经过的时间,向用户显示他们完成关卡所用的时间。

我已经尝试了下面的内容,但由于我不熟悉 C 和基于 C 的语言的工作方式,所以我不确定我是否在朝着正确的方向前进。

int minutes = 0;
int seconds = 0, trigger = 1000;
clock_t start = clock();
do {
  if(seconds == 60) {
    seconds = 0;
    minutes += 1;
  }
  clock_t difference = clock() - start;
  seconds = difference * 1000 / CLOCKS_PER_SEC;
  i++;
} while ( seconds < trigger );

例如,假设已经过去了x秒;

计时器应该return与上面类似。

Windows系统.

有人可以帮忙吗?你可以编造任何你想要的变量或任何东西,据我所知,我所做的甚至没​​有任何结果。提前致谢。

不确定你在这里的目的,只是计算秒数直到你到达触发器然后转换为 mm:ss?您可以只计算达到触发器的秒数,并将触发器表示为 mm:ss,这基本上是一样的。

猜一猜,这就是您要找的东西吗?

unsigned int minutes = 0;
unsigned int seconds = 0;

unsigned int trigger = 360;
unsigned int elapsed_seconds = 0;

/* Count the seconds until #trigger seconds are reached (?)*/
clock_t start = clock();

do {

  clock_t difference = clock() - start;
  elapsed_seconds = difference * 1000 / CLOCKS_PER_SEC;

  /* Stop at trigger (?)*/
} while ( elapsed_seconds < trigger );

/* Express in minutes and seconds */
minutes = elapsed_seconds / 60;
seconds = elapsed_seconds % 60;

printf("Time: %02d:%02d\n", minutes, seconds);

可以使用 Teensy 系统使用其溢出和中断来启动计时器。下面的代码将设置并启动一个计时器,该计时器将计数。一个结构用于启动一个布尔值(以及程序中的其他可能的东西),该值可用于控制计时器的溢出是否计数,有效地暂停计时器。

绘制字符串的函数

 // Render a string of printable ASCII characters into the screen buffer.
 // Parameters:
 // x - The horizontal position of the top-left corner of the displayed text.
 // y - The vertical position of the top-left corner of the displayed text.
 // character - The ASCII code of the character to render. Valid values range from 
 0x20 == 32 to 0x7f == 127.
 // colour - The colour, FG_COLOUR or BG_COLOUR. If colour is BG_COLOUR,
 the text is rendered as an inverse video block.

void draw_string(int top_left_x, int top_left_y, char *text, colour_t colour) {
    // Draw each character until the null terminator is reached
    for ( uint8_t x = top_left_x, i = 0; text[i] != 0; x += CHAR_WIDTH, i++ ) {
        draw_char(x, top_left_y, text[i], colour);
        // Add a column of spaces here if you want to space out the lettering.
        // (see lcd.c for a hint on how to do this)
    }
}

下面是一个格式化函数使用上面的函数绘制一个字符串,格式化函数用来得到mm:ss布局

// a formatting function to assist with printing to the screen
void draw_formatted(int x, int y, const char * format, ...) {
    va_list args;
    va_start(args, format);
    char buffer[1000];
    vsprintf(buffer, format, args);
    draw_string(x, y, buffer, FG_COLOUR);
}

格式化排序后,应创建一个结构来初始化操作计时器所需的值,并启动计时器

// initiates a struct for the timer, which includes a minute, second and 
// validator accessed using tim
struct val_store {
    bool timer_validator;
    uint8_t time_passed
    uint8_t min;
    uint8_t sec;
} tim;

// initiates timer parameters, essentially setting up the timer to be able to function, 
// sets timer to begin, this should be included in the initial setup of a program
TCCR1A = 0;
TCCR1B = 2; 
TIMSK1 = 1;
sei();
tim.timer_validator = true;

下面是定时器的隐藏部分,这是心脏,它创造了整个问题的基础价值,非常重要,除非TCCR1A,TCCR1B,TIMSK1和sei,否则它不会起作用() 是预先启动的,值可以分别从 0、2、1 变化,但是,下面定时器中使用的值必须使用位图进行相应调整

// Create a volatile global variable called over_flow_count
volatile unsigned int over_flow_count = 0;
//  interrupt service routine to process timer overflow
//  interrupts for Timer 1.
ISR(TIMER1_OVF_vect) {
    // checks if timer is active or not
    if (tim.timer_validator) {
    over_flow_count++;
    }
}
// elapsed time since program start
// use float instead of double to save memory
float elapsed_time(void) {
    float current_time = (float)
        ( ( over_flow_count * 65536.0 + TCNT1 ) * 8.0  / 8000000 );
    return current_time;
}

最后,与上述计时器一起使用以生成 mm:ss 格式输出的代码很简单,因为上面的计时器完成了所有工作,剩下的就是格式化。 time passed 调用之前创建的函数,这是总的通过时间范围,然后使用除法和模求出 min 和 sec,并使用之前创建的格式化函数进行格式化

tim.time_passed = elapsed_time();
tim.min = time_passed / 60;
tim.sec = time_passed % 60;
draw_formatted(x, y, "Time: %02d:%02d", tim.min, tim.sec);

可以使用类似的方法创建暂停

if (BIT_VALUE(PINB, 0)) {
    // buffer before and after to prevent a single press activating
    // multiple instances of a joystick press, cheap interrupt
    _delay_ms(250);

    // pauses timer by setting the boolean to false, preventing the if statement passing
    tim.timer_validator = false;

    while(true) {
        clear_screen();
        #Put processes to occur while paused here

        // checks to see if the pause should resume
        if (BIT_VALUE(PINB, 0)) {
            break;
        }

    // continue timer
    tim.timer_validator = true;

    // once again a buffer for good measure
    _delay_ms(250);
    }

如果程序创建正确,计时器应该会自动更新! 希望对您有所帮助!

相关包含,并非所有包含都可以使用。

// includes
#include <assert.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <cpu_speed.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h>