#include "GeneralOP.h"
#include "DS1302.h"
#include <pic.h>
#include "MyExterns.c"
#include "GetTypes.H"
#include "TimeDate.h"
#include "DS18B20.h"
const UCHAR MonthDate[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void UpdateType()
{
UCHAR tmp=TypePort & 0xF0 ;
#pragma switch direct
switch(ScanIndex)
{
case 0:
tmp = tmp | (ScanTypes[0] & 0x0F);
//TypePort= (TypePort & 0xF0) + (ScanTypes[0]&0x0F);
break;
case 1:
tmp = tmp | (ScanTypes[0] >> 4);
//TypePort= (TypePort & 0xF0) + (ScanTypes[0]>>4);
break;
case 2:
tmp = tmp | (ScanTypes[1] & 0x0F);
//TypePort= (TypePort & 0xF0) + (ScanTypes[1]&0x0F);
break;
case 3:
//default:
tmp = tmp | (ScanTypes[1] >> 4);
break;
//TypePort= (TypePort & 0xF0) + (ScanTypes[1]>>4);
}
TypePort=tmp;
}
void UpdateTime()
{
UCHAR FebTmp=1;
Time[DS_Second]++;
// Action over time delay
if(ActionOverTime)
ActionOverTime--;
if(60<=Time[DS_Second])
{
// sleep over time delay
if(SleepOverTime)
SleepOverTime--;
bEnvSet=1;
Time[DS_Second]=0;
Time[DS_Minute]++;
bSaveTime=1;
if(60<=Time[DS_Minute])
{
bSaveHour=1;
Time[DS_Minute]=0;
Time[DS_Hour]++;
if(24<=Time[DS_Hour])
{
bSaveDate=1;
Time[DS_Hour]=0;
Time[DS_Date]++;
Time[DS_Day]++;
if(8<=Time[DS_Day])
{
Time[DS_Day]=1;
}
if((Time[DS_Year]&0x03)==0 && (Time[DS_Month]==2))
// Big Feb
FebTmp++;
if((MonthDate[Time[DS_Month]]+FebTmp)<=Time[DS_Date])
{
bSaveMonth=1;
Time[DS_Date]=1;
Time[DS_Month]++;
if(13==Time[DS_Month])
{
bSaveYear=1;
Time[DS_Month]=1;
Time[DS_Year]++;
if(99==Time[DS_Year])
Time[DS_Year]=0;
}
}
}
}
}
}
// Save time to EE
void SaveTime()
{
//UCHAR i=0;
//eeprom_write(i,0x33);
if(bSaveHour)
{
bSaveHour=0;
eeprom_write(HourAddr,Time[DS_Hour]);
}
eeprom_write(MinuteOffset+Time[DS_Hour],Time[DS_Minute]);
if(bSaveDate)
{
bSaveDate=0;
eeprom_write(DateAddr,Time[DS_Date]);
eeprom_write(DayAddr,Time[DS_Day]);
}
if(bSaveMonth)
{
bSaveMonth=0;
eeprom_write(MonthAddr,Time[DS_Month]);
}
if(bSaveYear)
{
bSaveYear=0;
eeprom_write(YearAddr,Time[DS_Year]);
}
/*
for(i=DS_Minute;i<(DS_Year+1);i++)
{
eeprom_write(i,Time[i]);
}
//*/
}
// Load Time from EE
void LoadTime()
{
UCHAR i;
for(i=HourAddr;i<=YearAddr;i++)
{
Time[i]=eeprom_read(i);
}
Time[DS_Minute]=eeprom_read(Time[DS_Hour]+MinuteOffset);
}
//Update Scan Port
void UpdatePort()
{
switch(ScanIndex)
{
case 0:
Com4=1;
Com1=0;
break;
case 1:
Com1=1;
Com2=0;
break;
case 2:
Com2=1;
Com3=0;
break;
default:
Com3=1;
Com4=0;
break;
}
}
void KeyServer()
//void KeyServer()
{
if(KeyUp)
{
if(Key)
{
KeyDown=1;
KeyUp=0;
}
else
{
KeyUp=0;
KeyDown=0;
KeyDetect=0;
if(KeyTime)
{
DbClick=1;
KeyTime=0;
}
else
{
KeyTime=20;
}
}
}
if(KeyDown)
{
if(Key)
{
if(!(KeyDelay&0x80))
KeyDelay++;
}
else
{
KeyUp=1;
KeyDown=0;
}
}
if(KeyTime)
{
KeyTime--;
if(!KeyTime)
Click=1;
}
if((!KeyDetect) && Key)
{
KeyDetect=1;
}
if(KeyDetect)
KeyDown=1;
}
unsigned char Hex2BCD(unsigned char in)
{
unsigned char i=0;
if(in>99)
return 0x99;
while(in>9)
{
i++;
in-=10;
}
return in|(i<<4);
}
unsigned char BCD2Hex(unsigned char in)
{
unsigned char i=in>>4;
unsigned char j=0;
while(i--)
{
j+=10;
}
return j+(in&0x0F);
}
void WaitNumberDisplay()
{
while(DisplayBusy)
{
if(Click)
DisplayIndex=0;
}
Click=0;
KeyDelay=0;
}
void TouchDetect()
{
if(TP1==85)
return ;
if(EnvTP>MaxEnvTP)
bHotModel=1;
else
bHotModel=0;
if(!bHotModel)
{
if((TP1-EnvTP)>=TPOver && TP1>OldTP1)
{
bRightTouch=1;
bEnvSet=0;
}
if((TP2-EnvTP)>=TPOver && TP2>OldTP2)
{
bLeftTouch=1;
bEnvSet=0;
}
if(bLeftTouch && bRightTouch)
{
bRightTouch=0;
bLeftTouch=0;
bDoubleTouch=1;
bEnvSet=0;
}
}
if(TP1<EnvTP)
EnvTP=TP1;
if(TP2<EnvTP)
EnvTP=TP2;
if(TP2==TP1 && TP1==OldTP1 && bEnvSet)
EnvTP=TP1;
OldTP1=TP1;
OldTP2=TP2;
}
void TransAction()
{
#ifdef __DEBUG
if(DbClick)
{
//TXREG=0xDB;
DbClick=0;
KeyDelay=0;
//bB20ReadTp=1;
bReadB20RAM=1;
}
#else
if(DbClick)
{
DbClick=0;
KeyDelay=0;
bSupperMode=1;
}
#endif
if(Click)
{
//TXREG=KeyDelay;
Click=0;
if(KeyDelay>LongPress)
{
//bB20SetAlarm=1;
bDisHourMin=1;
//bDisTime=1;
}
else
{
bB20ReadTp=1;
}
KeyDelay=0;
}
if(bLeftTouch)
{
bLeftTouch=0;
bB20ReadTp=1;
/*
DisBuf1[0]= LeftMask1 & 0xF7;
DisBuf2[0]= LeftMask2 & 0x7F;
WaitForAction(DateMask,WeekDayMask);
SleepOverTime=SleepTime;
bB20Convert=0;
bB20AlarmSearch=0;
//*/
}
if(bRightTouch)
{
bRightTouch=0;
bDisHourMin=1;
/*
DisBuf1[0]= RightMask1 & 0xF7;
DisBuf2[0]= RightMask2 & 0x7F;
WaitForAction(HourMinMask,HourMinMask);
SleepOverTime=SleepTime;
bB20Convert=0;
bB20AlarmSearch=0;
//*/
}
if(bDoub