else
{ //endtime
//a[8]--;
//beginsampling
if(int_times==begin_int_times){int_times++;sample_flag=1;}//giveabegin_samplingflag
if(a[MONTH]==4){a[MONTH]=a[9];alarm=10;}else{a[MONTH]=0;alarm=9;}//end_monthisgiventoa[MONTH]
switch(a[8])
{ case4:
if(a[MONTH]!
=0&&a[9]==a[2]) {a[8]--;a[alarm]=a[alarm+1];a[alarm+1]=a[alarm+2];}
a[alarm]|=0x80;setalarm(0x0b,a[alarm]);IE0=0;EA=1;break;//设定日报警
case3:
a[alarm]|=0x80;setalarm(0x0a,a[alarm]);a[8]--;a[alarm]=a[alarm+1];
IE0=0;EA=1;break;//sethouralarm
case2:
a[alarm]|=0x80;setalarm(0x09,a[alarm]);a[8]--;IE0=0;EA=1;break;//setminutealarm
case1:
sample_flag=2;break; //end_minutetimecomes,endsampling
default:
break;
}
}
//IE0=0;//中断申请标志清零
//EA=1;//开中断//send(0x11);
//}
//比较当前时刻和停止采样时刻是否一致,决定是否停止采样,
}
main()
{
//1{{{{{ definevariable;watchdogsetting}}}}}
//pcf8563
inti,k,j; ucharsetvalue;//,,c[12]
//ad
ucharsta_reg;
//usb
ucharfn[2];//fn=filename
sample_flag=0;int_times=0;
//watchdog
WDTPRG|=0x07;/*2.275s@FOSCA=11.059200MHz*/
/*watchdogstartsequence*/
WDTRST=0x1E; WDTRST=0xE1;
//2{{{{{initcom,setcommunicationparameter,setsystemtime,setsystemparameter
// (inludingbegin-timeandend-time,sampling-rate,receive-date-manner)}}}}}
//pcf563
////int11=1;
//ad
/*PRECONFIGURE...*///k=32;
SCLK=1; CS=0;DIN=1; //tokeepDIN=1for32sclocktoresetthepart
for(i=0;i<32;i++){SCLK=0;SCLK=1;}CS=1;
//usb
init_com();
fn[0]=INIT_TIME_FN; fn[1]=INIT_TIME_FN;
OpenFile(fn); SetPointer(0);ReadFile(15);//for(i=0;i<12;i++)c[i]=a[i];
//readU-paninittime(timenow(6),begintime(2:
报警小时,报警分),endtime(后面数据的个数,报警month,day,hour,minute,filename
(2))toa[i]
//| 报警方式:
3:
day,2:
hour,1:
minute
inittime(a); //按照0,1年2月3日4小时5分钟6星期传送数据
writedata(0x00,0x00);//启动时钟
//begin-time
if(a[7]==0x00)begin_int_times=0;elsebegin_int_times=1;
a[6]|=0x80; setalarm(0x0a,a[6]);//设定hour报警
init_int0();a[MONTH]=a[8];
//3{{{{{createfile}}}}}
i=9+a[8];CreateFile(a+i);////toasccodes-->send
//4{{{{ad7793parameterinit}}}}}
//initialregister
writetoreg(0x10);
writetoreg(0x00);writetoreg(0x97);//writetoconfreg:
0x1090:
gain:
16;unipolar;noVbias;
//internalref;buffer;channel1 //10-13
writetoreg(0x50);for(k=0;k<2;k++){sta_reg=readfromreg();send(sta_reg);}//read
writetoreg(0x28);writetoreg(0x03);//I/Oreg:
excitationcurrents:
1ma
writetoreg(0x68);sta_reg=readfromreg();send(sta_reg);//read
writetoreg(0x08);
writetoreg(0x80);writetoreg(0x0a);//writetomodereg:
0x8005:
50HZ;Internal64Kclock,
//notout;internalzerocalibration
while(DOUT_RDY);
writetoreg(0x08);
writetoreg(0xa0);writetoreg(0x0a);//writetomodereg:
0x8005:
50HZ;Internal64Kclock,
//notout;internalfull-scalecalibration
while(DOUT_RDY);
//settimerforwatchdog
//5{{{{sampling}}}}}{{{{{rolling}}}}}
while
(1)
{
//sampling
if(sample_flag==1){}//begin_sample
elseif(sample_flag==2){}//sleeping
/*for(j=0;j<20;j++)
{
//singleconversion
writetoreg(0x08);
writetoreg(0x20);writetoreg(0x0a);//writetomodereg:
0x8005:
50HZ;Internal64Kclock,
//notout;zerocalibration
while(DOUT_RDY);
for(i=0;i<10;i++)
{ writetoreg(0x58);for(k=0;k<3;k++){sta_reg=readfromreg();send(sta_reg);}
}
}
*/
/*
writetoreg(0x08);
writetoreg(0xa0);writetoreg(0x05);//writetomodereg:
50HZ;Internal64Kclock,
//notout;full-scallcalibration
while(readstatusreg());
writetoreg(0x08);
writetoreg(0x00);writetoreg(0x05);
*/
//readdata
//for(i=0;i<100;i++) single_conv();
// continuous_conv(100);
//continous_read();
//sendsamplingdatatou-panfile
//testsequence
if(receive()==0x80)readouttime();//pcf8563
/*watchdogresetsequence*/
WDTRST=0x1E; WDTRST=0xE1;
}
for(;;);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//--------------------------pcf8563-----------------------//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
ucharreceiveonetime(void)
{ ucharRX;RX=0;
if(RI==1){RI=0;RX=SBUF;} elseRX=0;
returnRX;
}
voidstarti2c()
{ EA=0;
SDA=1;SCL=1;SomeNOP();//INI
SDA=0;SomeNOP();//START
SCL=0;
}
voidstopi2c()
{
SCL=0;SDA=0;SomeNOP();//INI
SCL=1;SomeNOP();SDA=1;//STOP
EA=1;
}
bitwaitack()
{
ucharerrtime=255;//因故障接收方无ACK,超时值为255。
SDA=1;SomeNOP(); SCL=1;SomeNOP();
while(SDA){errtime--;if(!
errtime){stopi2c();//SystemError=0x11;
return0;}}
SCL=0;
return1;
}
voidsendack(){SDA=0;SomeNOP(); SCL=1;SomeNOP(); SCL=0; }
voidsendnack(){SDA=1;SomeNOP(); SCL=1;SomeNOP(); SCL=0; }
voidsendbyte(bytech)
{
uchari=8;
while(i--)
{SCL=0;_nop_(); SDA=(bit)(ch&0x80);ch<<=1;SomeNOP();
SCL=1;SomeNOP(); //上升沿有效
}
SCL=0;
}
bytereceivebyte(void)
{ uchari=8;byteddata=0; SDA=1;
while(i--){ddata<<=1; SCL=0;SomeNOP(); SCL=1;SomeNOP();ddata|=SDA;}
SCL=0;returnddata;
}
voidreaddata(ucharfirsttype,ucharcount,uchar*buff)
{
uchari;
starti2c();sendbyte(WRITE_ADR);//0xA2
waitack(); sendbyte(firsttype);
waitack(); starti2c(); sendbyte(READ_ADR);//0xA3
waitack();
for(i=0;i {buff[i]=receivebyte();if(i!
=count-1)sendack();}//除最后一个字节外,其他都要从MASTER发应答。
sendnack();stopi2c();
}
/**--------------------------------------------------------------------------------
调用方式:
voidSetPCF8563(uchartimetype,ucharvalue)﹫2001/08/07
函数说明:
调整时钟。
timetype是要改的时间类型,value是新设置的时间值(BCD格式)。
0x02:
秒/0x03:
分/0x04:
小时/0x05:
日/0x06:
星期/0x07:
月(世纪)/0x08:
年
---------------------------------------------------------------------------------*/
voidwritedata(uchartimetype,ucharvalue)
{ starti2c(); sendbyte(0xa2); waitack(); sendbyte(timetype);
waitack(); sendbyte(value); waitack(); stopi2c();
}
/**--------------------------------------------------------------------------------
调用方式:
voidSetAlarmHour(ucharcount)﹫2001/08/07
函数说明:
设置报警闹钟在一天的第count点报警。
例如:
count=23,则在晚上11点报警。
---------------------------------------------------------------------------------*/
voidsetalarm(ucharalarmtype,ucharcount)
{ writedata(0x01,0x02);writedata(alarmtype,count);}
/**--------------------------------------------------------------------------------
调用方式:
voidCleanAlarm(void)﹫2001/08/07
函数说明:
清除所有报警设置。
---------------------------------------------------------------------------------*/
voidcleanalarm(void)
{ writedata(0x01,0x00); writedata(0x09,0x80);
writedata(0x0A,0x80); writedata(0x0B,0x80); writedata(0x0C,0x80);
//writedata(0x0D,0x00);//writedata(0x0E,0x03);
}
voidinittime(uchar*c)
{ ucharsetvalue;
writedata(0x08,c[1]);//writeyear
if(c[0]&0x20==0)setvalue=c[2]&0x1f; elsesetvalue=c[2]&0x9f;
writedata(0x07,setvalue); //writemouthandcentrary
writedata(0x06,c[6]&0x07);writedata(0x05,c[3]&0x3f);
writedata(0x04,c[4]&0x3f);writedata(0