📄 timer_funcs.c
字号:
#include <REGX52.h>
#include "xkcan_defs.h"
#include "timer_defs.h"
#include "max7219.h"
#include "DIG_defs.h"
ubyte volatile timer_tick_500ms;
uint volatile timer_tick;
ubyte volatile timer_tick_5s;
ubyte volatile timer_half;
ubyte idata timer_set_hr;
ubyte idata timer_set_min;
ubyte idata timer_set_sec;
ubyte idata timer_exp_set_hr;
ubyte idata timer_exp_set_min;
ubyte idata timer_exp_set_sec;
ubyte volatile idata timer_hr;
ubyte volatile idata timer_min;
ubyte volatile idata timer_sec;
ubyte volatile idata timer_exp_hr;
ubyte volatile idata timer_exp_min;
ubyte volatile idata timer_exp_sec;
void TIMER_Init(void)
{
// Use T2 as a 25ms timer
ET2 = 0; // 关闭T2的中断
T2MOD = 0x00; // T2工作在模式0
T2CON = 0x00; //
RCAP2L = 0xb0; // 产生25ms中断 |24 MHz 0x10000 - 0xC350 = 0x3CB0
RCAP2H = 0x3c;
TL2 = RCAP2L;
TH2 = RCAP2H;
ET2 = 1; // 打开T2的中断
timer_tick = 0;
timer_half = 0;
timer_clock_pause = 1;
timer_exp_clock_pause = 1;
timer_tick_500ms = 0;
}
void timer2_int(void) interrupt 5 using 1
{
EA = 0;
TF2 = 0;
timer_tick++;
if ( (timer_tick % 20) == 0 )
{
timer_tick_500ms = 1;
}
if ( (timer_tick % 200) == 0 )
{
timer_tick_5s = 1;
}
EA = 1;
}
void TIMER_Reset()
{
TR2 = 0;
timer_tick = 0;
TR2 = 1;
}
void TIMER_Start()
{
TR2 = 1; // 打开T2
}
void TIMER_Stop()
{
TR2 = 0;
timer_tick = 0;
timer_hr = timer_set_hr;
timer_min = timer_set_min;
timer_sec = timer_set_sec;
}
void TIMER_Restart()
{
TIMER_Stop();
TIMER_Start(); // 打开T2
}
void TIMER_Pause()
{
TR2 = 0; // 打开T2
}
uint TIMER_Count()
{
uint t;
t = timer_tick ;
return t;
}
void CLOCK_Pause()
{
timer_clock_pause = 1;
}
void CLOCK_Start()
{
timer_clock_pause = 0;
}
void CLOCK_Stop()
{
timer_hr = timer_set_hr;
timer_min = timer_set_min;
timer_sec = timer_set_sec;
timer_clock_pause = 1;
}
void CLOCK_Reset()
{
CLOCK_Stop();
CLOCK_Start();
}
void CLOCK_Exp_Init()
{
flag_timer_exp_set = 0;
flag_timer_exp_timeup = 0;
timer_exp_clock_pause = 1;
}
void CLOCK_Exp_Pause()
{
timer_exp_clock_pause = 1;
}
void CLOCK_Exp_Start()
{
timer_exp_clock_pause = 0;
}
void CLOCK_Exp_Stop()
{
if (flag_timer_exp_set == 1)
{
timer_exp_hr = timer_exp_set_hr;
timer_exp_min = timer_exp_set_min;
timer_exp_sec = timer_exp_set_sec;
}
timer_exp_clock_pause = 1;
}
void CLOCK_Exp_Reset()
{
CLOCK_Exp_Stop();
CLOCK_Exp_Init();
}
void CLOCK_Set(ubyte hr, ubyte min, ubyte sec)
{
CLOCK_Stop();
timer_set_hr = hr;
timer_set_min = min;
timer_set_sec = sec;
timer_half = 0;
timer_hr = timer_set_hr;
timer_min = timer_set_min;
timer_sec = timer_set_sec;
}
void CLOCK_Exp_Set(ubyte hr, ubyte min, ubyte sec)
{
CLOCK_Exp_Stop();
timer_exp_set_hr = hr;
timer_exp_set_min = min;
timer_exp_set_sec = sec;
timer_exp_hr = timer_exp_set_hr;
timer_exp_min = timer_exp_set_min;
timer_exp_sec = timer_exp_set_sec;
flag_timer_exp_set = 1;
}
void UpdateClock()
{
if (timer_clock_pause == 1)
{
return;
}
// The normal Clock
if ( timer_half == 0 )
{
timer_sec++;
if ( timer_sec == 60 )
{
timer_min++;
timer_sec = 0;
}
if ( timer_min == 60 )
{
timer_hr++;
timer_min = 0;
}
timer_half = 1;
}
else
{
timer_half = 0;
}
// The Countdown clock for experiment
if (timer_exp_clock_pause == 1)
{
return;
}
if ( timer_half == 1 )
{
timer_exp_sec--;
if ( timer_exp_sec == 0xff )
{
timer_exp_min--;
timer_exp_sec = 59;
}
if ( timer_exp_min == 0xff )
{
timer_exp_hr--;
timer_exp_min = 59;
}
if ( timer_exp_hr == 0xff )
{
timer_exp_min = 0;
timer_exp_sec = 0;
timer_exp_hr = 0;
flag_timer_exp_timeup = 1;
}
}
}
void UpdateTimeDisplay( void )
{
if ( timer_exp_clock_pause == 0 )
{
DIG_Update ( _7219_DIG0, timer_exp_min / 10 );
DIG_Update ( _7219_DIG1, timer_exp_min % 10 );
DIG_Update ( _7219_DIG2, timer_exp_sec / 10 );
DIG_Update ( _7219_DIG3, (timer_half<<_7219_SEG_DP)|timer_exp_sec % 10 );
}
else
{
DIG_Update ( _7219_DIG0, timer_min / 10 );
DIG_Update ( _7219_DIG1, timer_min % 10 );
DIG_Update ( _7219_DIG2, timer_sec / 10 );
DIG_Update ( _7219_DIG3, (timer_half<<_7219_SEG_DP)|timer_sec % 10 );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -