ad7793编程例子Word文档下载推荐.docx
《ad7793编程例子Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《ad7793编程例子Word文档下载推荐.docx(11页珍藏版)》请在冰豆网上搜索。
voidset_c_conv(void);
voidset_c_read(void);
voidsingle_conv(void);
voidcontinuous_conv(intt);
voidcontinous_read();
voidset_timer();
ucharreceiveonetime(void);
voidstarti2c();
voidstopi2c();
bitwaitack();
voidsendack();
voidsendnack();
voidsendbyte(bytech);
bytereceivebyte();
voidwritedata(uchartimetype,ucharvalue);
voidreaddata(ucharfirsttype,ucharcount,uchar*buff);
voidsetalarm(ucharalarmtype,ucharcount);
voidcleanalarm();
voidinittime(uchar*c);
voidinit_int0();
voidreadouttime();
volatileuchara[MAXDATA],begin_int_times,int_times,sample_flag;
//0x627525presentesvoltageofAVDD=2.7V//ucharpre_data;
voidCreateFile(uchar*fn);
voidWritetoFile(uchar*wdata,ucharlength);
voidOpenFile(uchar*fn);
voidSetPointer(ucharpointer);
bitReadFile(ucharlength);
voidReturntoRoot(void);
//uchar*ToASC(ucharc);
//pcf8563timerint
voidalarm()interrupt0using2
{
intalarm;
EA=0;
//禁止中断
//if(int_time==0)
{
if(int_times<
begin_int_times){int_times++;
a[7]|=0x80;
setalarm(0x09,a[7]);
IE0=0;
EA=1;
}//setbegin_minutealarm
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;
break;
//设定日报警
case3:
setalarm(0x0a,a[alarm]);
a[8]--;
//sethouralarm
case2:
setalarm(0x09,a[alarm]);
//setminutealarm
case1:
sample_flag=2;
break;
//end_minutetimecomes,endsampling
default:
}
//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;
/*PRECONFIGURE...*///k=32;
SCLK=1;
CS=0;
DIN=1;
//tokeepDIN=1for32sclocktoresetthepart
for(i=0;
i<
32;
i++){SCLK=0;
}CS=1;
init_com();
fn[0]=INIT_TIME_FN;
fn[1]=INIT_TIME_FN;
OpenFile(fn);
SetPointer(0);
ReadFile(15);
//for(i=0;
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(0xa0);
internalfull-scalecalibration
//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(0x20);
zerocalibration
10;
i++)
writetoreg(0x58);
3;
*/
/*
writetoreg(0x05);
50HZ;
full-scallcalibration
while(readstatusreg());
//readdata
//for(i=0;
100;
i++)
single_conv();
//
continuous_conv(100);
//continous_read();
//sendsamplingdatatou-panfile
//testsequence
if(receive()==0x80)readouttime();
/*watchdogresetsequence*/
for(;
;
);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//--------------------------pcf8563-----------------------//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
ucharreceiveonetime(void)
ucharRX;
RX=0;
if(RI==1){RI=0;
RX=SBUF;
}
elseRX=0;
returnRX;
voidstarti2c()
SDA=1;
SCL=1;
SomeNOP();
//INI
SDA=0;
//START
SCL=0;
voidstopi2c()
//INI
//STOP
EA=1;
bitwaitack()
ucharerrtime=255;
//因故障接收方无ACK,超时值为255。
SomeNOP();
while(SDA){errtime--;
if(!
errtime){stopi2c();
//SystemError=0x11;
return0;
}}
return1;
voidsendack(){SDA=0;
voidsendnack(){SDA=1;
voidsendbyte(bytech)
uchari=8;
while(i--)
{SCL=0;
SDA=(bit)(ch&
0x80);
ch<
<
=1;
//上升沿有效
bytereceivebyte(void)
byteddata=0;
while(i--){ddata<
ddata|=SDA;
returnddata;
voidreaddata(ucharfirsttype,ucharcount,uchar*buff)
uchari;
starti2c();
sendbyte(WRITE_ADR);
//0xA2
waitack();
sendbyte(firsttype);
sendbyte(READ_ADR);
//0xA3
for(i=0;
count;
{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)
sendbyte(0xa2);
sendbyte(timetype);
sendbyte(value);
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)
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]&
writedata(0