C语言定时器程序编写:Amm:ss定时器 C

关于C语言定时器程序编写的问题,在60 second time clock中经常遇到, 试图在 C 中制作一个计时器,以 mm:ss 格式计数,我不一定需要打印值,只需要它存在引用。

试图在 C 中制作一个计时器,以 mm:ss 格式计数,我不一定需要打印值,只需要它存在引用。

这个想法是一个“时间流逝”的时钟在一个十几岁的设备,每秒通过定时器上升 1 秒,在 60 秒,分钟的定时器滴答 1 和秒的定时器重置为 0 定时器运行在后台更新自己每秒作为一种连续秒表。

但是,与下图类似,计时器不会随意启动或停止,它在程序初始化时启动,只是每秒增加其计数器,分钟不需要转换,并且可以达到 99,因为此时程序应该已经结束。

enter image description here

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

我已经在去了,但是因为我不熟悉 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 秒已经过去;

89 秒 (1 分 9 秒)

Time: 01:19

360 秒 (6 分 0 秒)

Time: 06:00

27 秒 (0 分 27 秒)

Time: 00:27

4893 秒 (81 分 33 秒)

Time: 81:33

计时器应返回类似于上面。

Windows 系统。

任何人都可以帮忙吗?你可以编造任何你想要的变量或任何东西,因为我所知道的我所做的甚至没有任何进展。提前感谢。

1

结构用于启动一个布尔值 (以及程序中的其他可能的东西),该布尔值可用于控制定时器溢出是否计数,从而有效地暂停定时器。

绘制字符串的函数

 // 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 格式产生输出的代码很简单,因为上面的计时器完成了所有工作,剩下的就是格式化。传递的时间调用先前创建的函数,即总传递的时间范围,然后使用除法和模数找到 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>
0

不知道你的目的在这里,只是计数秒,直到你到达触发器,然后转换为 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);

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(582)
Ccd相机的像素:Opencv相机百万像素 Android
上一篇
男孩c调吉他谱:将男孩和女孩随机分配到 c# 组中
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(8条)