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