📄 gettime.c
字号:
#include <p30f6014.h>
#include <dsp.h>
#include "common.h"
#include "timers.h"
#include "procsignal.h"
#include "gettime.h"
typedef void (*pt2Function)(void);
//局部定义
#define MIN_INTEGRATE_LIMIT 35
#define HR_INTEGRATE_LIMIT 30
#define YR_INTEGRATE_LIMIT 40
#define SEARCH_LIMIT 15
//全局变量定义
unsigned char SecondsCount; //24 小时UTC(世界协调时)时间
unsigned char MinutesCount;
unsigned char HoursCount;
unsigned char YearCount;
int MinEnergy;
int HrEnergy;
int YrEnergy;
char LocalHoursCount;
char LocalHoursOffset;
void InitGetTime( void );
void ProcDataBit( void );
void IncTime( void );
static void SecProcNOP( void );
static void SecProcM1( void );
static void SecProcM2( void );
static void SecProcM4( void );
static void SecProcM8( void );
static void SecProcM10( void );
static void SecProcM20( void );
static void SecProcM40( void );
static void SecProcH1( void );
static void SecProcH2( void );
static void SecProcH4( void );
static void SecProcH8( void );
static void SecProcH10( void );
static void SecProcH20( void );
static void SecProcY1( void );
static void SecProcY2( void );
static void SecProcY4( void );
static void SecProcY8( void );
static void SecProcY10( void );
static void SecProcY20( void );
static void SecProcY40( void );
static void SecProcY80( void );
static void SecProcChkMin( void );
static void SecProcChkHrs( void );
static void SecProcChkYrs( void );
static void ResetMinIntArray(void);
static void ResetHrIntArray(void);
static void ResetYrIntArray(void);
static void UpdateMinBit(unsigned char bitmask);
static void UpdateHrBit(unsigned char bitmask);
static void UpdateYrBit(unsigned char bitmask);
const pt2Function SecProcJmpTable[60] =
{
SecProcNOP, //00
SecProcNOP, //01
SecProcNOP, //02 DST2
SecProcNOP, //03
SecProcY1, //04 年 1
SecProcY2, //05 年 2
SecProcY4, //06 年 4
SecProcY8, //07 年 8
SecProcNOP, //08
SecProcNOP, //09 P1
SecProcM1, //10 分 1
SecProcM2, //11 分 2
SecProcM4, //12 分 4
SecProcM8, //13 分 8
SecProcNOP, //14
SecProcM10, //15 分 10
SecProcM20, //16 分 20
SecProcM40, //17 分 40
SecProcChkMin, //18
SecProcNOP, //19 P2
SecProcH1, //20 小时 1
SecProcH2, //21 小时 2
SecProcH4, //22 小时 4
SecProcH8, //23 小时 8
SecProcNOP, //24
SecProcH10, //25 小时 10
SecProcH20, //26 小时 20
SecProcChkHrs, //27
SecProcNOP, //28
SecProcNOP, //29 P3
SecProcNOP, //30 天 1
SecProcNOP, //31 天 2
SecProcNOP, //32 天 4
SecProcNOP, //33 天 8
SecProcNOP, //34
SecProcNOP, //35 天 10
SecProcNOP, //36 天 20
SecProcNOP, //37 天 40
SecProcNOP, //38 天 80
SecProcNOP, //39 P4
SecProcNOP, //40 天 100
SecProcNOP, //41 天 200
SecProcNOP, //42
SecProcNOP, //43
SecProcNOP, //44
SecProcNOP, //45
SecProcNOP, //46
SecProcNOP, //47
SecProcNOP, //48
SecProcNOP, //49 P5
SecProcNOP, //50 DUT+/-
SecProcY10, //51 年 10
SecProcY20, //52 年 20
SecProcY40, //53 年 40
SecProcY80, //54 年 80
SecProcNOP, //55 DST1
SecProcChkYrs,//56 .1 DUT
SecProcNOP, //57 .2 DUT
SecProcNOP, //58 .4 DUT
SecProcNOP //59 P6
};
static char CurrentBitValue;
static char MinIntArray[60];
static char HrIntArray[24];
static char YrIntArray[100];
static unsigned char MIntCount;
static unsigned char HIntCount;
static unsigned char YIntCount;
const unsigned char BINtoBCD[100] =
{ //把二进制转为BCD
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99
};
//初始化获取时间子程序
void InitGetTime( void )
{
SecondsCount = 0;
MinEnergy = 0;
HrEnergy = 0;
YrEnergy = 0;
SemA.Mode24Hr = TRUE;
SemA.MinFound = FALSE;
SemA.HrFound = FALSE;
SemA.YrFound = FALSE;
}
//每秒增加一次UTC时间
void IncTime( void )
{
SecondsCount++;
if(SecondsCount>=60)
{
SecondsCount = 0;
MinutesCount++;
if(MinutesCount>=60)
{
MinutesCount = 0;
HoursCount++;
if(HoursCount>=24)
HoursCount = 0;
}
}
char tmp = (char)HoursCount + LocalHoursOffset;
if(tmp<0)
tmp += 24;
if(tmp>=24)
tmp -= 24;
if(tmp>=12)
SemA.PMAM = TRUE;
else
SemA.PMAM = FALSE;
if(tmp>12)
LocalHoursCount = tmp - 12;
else
LocalHoursCount = tmp;
if(0==LocalHoursCount)
LocalHoursCount = 12;
if( !SemA.MinLocked || !SemA.SecLocked )
{
ResetMinIntArray();
ResetHrIntArray();
ResetYrIntArray();
}
}
//处理数据位
void ProcDataBit( void )
{
if(SemA.MinLocked)
{
CurrentBitValue = CalcBit(); //获取最新的数据位值
SecProcJmpTable[SecondsCount](); //跳转到相关的子程序
}
}
//由跳转表调用的函数
static void SecProcNOP( void ){}
static void SecProcM1( void ){ UpdateMinBit(BIT0); }
static void SecProcM2( void ){ UpdateMinBit(BIT1); }
static void SecProcM4( void ){ UpdateMinBit(BIT2); }
static void SecProcM8( void ){ UpdateMinBit(BIT3); }
static void SecProcM10( void ){ UpdateMinBit(BIT4); }
static void SecProcM20( void ){ UpdateMinBit(BIT5); }
static void SecProcM40( void ){ UpdateMinBit(BIT6); }
static void SecProcH1( void ){ UpdateHrBit(BIT0); }
static void SecProcH2( void ){ UpdateHrBit(BIT1); }
static void SecProcH4( void ){ UpdateHrBit(BIT2); }
static void SecProcH8( void ){ UpdateHrBit(BIT3); }
static void SecProcH10( void ){ UpdateHrBit(BIT4); }
static void SecProcH20( void ){ UpdateHrBit(BIT5); }
static void SecProcY1( void ){ UpdateYrBit(BIT0); }
static void SecProcY2( void ){ UpdateYrBit(BIT1); }
static void SecProcY4( void ){ UpdateYrBit(BIT2); }
static void SecProcY8( void ){ UpdateYrBit(BIT3); }
static void SecProcY10( void ){ UpdateYrBit(BIT4); }
static void SecProcY20( void ){ UpdateYrBit(BIT5); }
static void SecProcY40( void ){ UpdateYrBit(BIT6); }
static void SecProcY80( void ){ UpdateYrBit(BIT7); }
//检查处于最大能量处的分钟数组,寻找最佳的分钟值
static void SecProcChkMin( void )
{
int i;
char val;
char max = 0;
char maxpos = 0;
for(i=0; i<60; i++)
{
val = MinIntArray[i];
if( val > max )
{
max = val;
maxpos = i;
}
}
MinEnergy = max;
if( max>MIN_INTEGRATE_LIMIT )
{
MinutesCount = MIntCount + maxpos;
if(MinutesCount>=60)
MinutesCount -= 60;
if(MinutesCount <= HIntCount)
{
ResetHrIntArray(); //重新开始小时
}
ResetMinIntArray();
SemA.MinFound = TRUE;
}
else
{
if(!SemA.MinFound)
{
MinutesCount = MIntCount + maxpos;
if(MinutesCount>=60)
MinutesCount -= 60;
}
MIntCount++;
if(MIntCount > SEARCH_LIMIT)
ResetMinIntArray();
}
}
//检查处于最大能量处的小时数组,寻找最佳的小时值
static void SecProcChkHrs( void )
{
int i;
char val;
char max = 0;
char maxpos;
for(i=0; i<24; i++)
{
val = HrIntArray[i];
if( val >= max )
{
max = val;
maxpos = i;
}
}
HrEnergy = max;
if( max>HR_INTEGRATE_LIMIT )
{
HoursCount = maxpos;
SemA.HrFound = TRUE;
ResetHrIntArray();
}
else
{
if(!SemA.HrFound )
HoursCount = maxpos;
HIntCount++;
if(HIntCount > SEARCH_LIMIT)
ResetHrIntArray();
}
}
//检查处于最大能量处的年数组,寻找最佳的年值
static void SecProcChkYrs( void )
{
int i;
char val;
char max = 0;
char maxpos;
for(i=0; i<100; i++)
{
val = YrIntArray[i];
if( val >= max )
{
max = val;
maxpos = i;
}
}
YrEnergy = max;
if( max>YR_INTEGRATE_LIMIT )
{
YearCount = maxpos;
ResetYrIntArray();
SemA.YrFound = TRUE;
}
else
{
if(!SemA.YrFound )
YearCount = maxpos;
YIntCount++;
if(YIntCount > SEARCH_LIMIT)
ResetYrIntArray();
}
}
//更新分钟位
void UpdateMinBit(unsigned char bitmask)
{
int i;
unsigned char bcdval;
unsigned char ptr = MIntCount;
for(i=0; i<60; i++)
{
bcdval = BINtoBCD[ptr];
if( bcdval & bitmask)
MinIntArray[i] += CurrentBitValue;
else
MinIntArray[i] -= CurrentBitValue;
ptr++;
if(ptr>=60)
ptr = 0;
}
}
//更新小时位
void UpdateHrBit(unsigned char bitmask)
{
int i;
unsigned char bcdval;
for(i=0; i<24; i++)
{
bcdval = BINtoBCD[i];
if( bcdval & bitmask)
HrIntArray[i] += CurrentBitValue;
else
HrIntArray[i] -= CurrentBitValue;
}
}
//更新年位
void UpdateYrBit(unsigned char bitmask)
{
int i;
unsigned char bcdval;
for(i=0; i<100; i++)
{
bcdval = BINtoBCD[i];
if( bcdval & bitmask)
YrIntArray[i] += CurrentBitValue;
else
YrIntArray[i] -= CurrentBitValue;
}
}
//复位分钟数组
void ResetMinIntArray(void)
{
int i;
for(i=0; i<60; i++)
MinIntArray[i] = 0;
MIntCount = 0;
MinEnergy = 0;
}
//复位小时数组
void ResetHrIntArray(void)
{
int i;
for(i=0; i<24; i++)
HrIntArray[i] = 0;
HIntCount = 0;
HrEnergy = 0;
}
//复位年数组
void ResetYrIntArray(void)
{
int i;
for(i=0; i<100; i++)
YrIntArray[i] = 0;
YIntCount = 0;
YrEnergy = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -