改善了走时不准的问题,倒计时手动停止后转为正计时,增加了中英文切换的说明。

This commit is contained in:
423A35C7 2024-03-27 10:35:34 +08:00
parent 08a0e08f4d
commit fedca16fb5
3 changed files with 9 additions and 5 deletions

View File

@ -33,8 +33,9 @@ void
clock_init(void) { clock_init(void) {
// set 8253 timer-chip // set 8253 timer-chip
outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
outb(IO_TIMER1, TIMER_DIV(1000) % 256); // 经过多次试验发现原始的10毫秒一次中断即除以100是最准时的不要擅自改中断频率
outb(IO_TIMER1, TIMER_DIV(1000) / 256); outb(IO_TIMER1, TIMER_DIV(100) % 256);
outb(IO_TIMER1, TIMER_DIV(100) / 256);
// initialize time counter 'ticks' to zero // initialize time counter 'ticks' to zero
ticks = 0; ticks = 0;

View File

@ -9,7 +9,7 @@
#include <console.h> #include <console.h>
#include <kdebug.h> #include <kdebug.h>
#include <string.h> #include <string.h>
#define TICK_NUM 100 #define TICK_NUM 1
static void print_ticks() { static void print_ticks() {
cprintf("%d ticks\n",TICK_NUM); cprintf("%d ticks\n",TICK_NUM);
@ -167,7 +167,8 @@ trap_dispatch(struct trapframe *tf) {
switch (state) { switch (state) {
case STARTED: case STARTED:
if (ticks % TICK_NUM == 0) { if (ticks % TICK_NUM == 0) {
milliseconds += mode * TICK_NUM; // 10毫秒触发一次时钟中断TICK_NUM次中断触发一次更新所以毫秒数应该乘TICK_NUM * 10
milliseconds += mode * TICK_NUM * 10;
// 当时间倒计时到0时转成停止状态 // 当时间倒计时到0时转成停止状态
// 而正计时加了mode之后至少为1所以不会转换状态 // 而正计时加了mode之后至少为1所以不会转换状态
// 使用小于等于0而不是等于0是为了防止万一出现异常导致小于0的情况 // 使用小于等于0而不是等于0是为了防止万一出现异常导致小于0的情况
@ -239,6 +240,7 @@ trap_dispatch(struct trapframe *tf) {
break; break;
case 'e': case 'e':
state = STOPPED; state = STOPPED;
mode = COUNTUP;
cprintf(MSG_STOP, milliseconds / 1000, milliseconds % 1000); cprintf(MSG_STOP, milliseconds / 1000, milliseconds % 1000);
milliseconds = 0; milliseconds = 0;
break; break;

View File

@ -47,7 +47,8 @@
#define T_SWITCH_TOU 120 // user/kernel switch #define T_SWITCH_TOU 120 // user/kernel switch
#define T_SWITCH_TOK 121 // user/kernel switch #define T_SWITCH_TOK 121 // user/kernel switch
#define ENGLISH // 在VSCode中可以正常看到中文但在QEMU中看到的是乱码
#define ENGLISH // 使用中文则不需要这一行
#ifndef ENGLISH #ifndef ENGLISH
#define MSG_COUNTDOWN_STOP "倒计时到0了停止转到正计时。\n" #define MSG_COUNTDOWN_STOP "倒计时到0了停止转到正计时。\n"