ad7793编程例子.docx

上传人:b****5 文档编号:7962596 上传时间:2023-01-27 格式:DOCX 页数:14 大小:19.24KB
下载 相关 举报
ad7793编程例子.docx_第1页
第1页 / 共14页
ad7793编程例子.docx_第2页
第2页 / 共14页
ad7793编程例子.docx_第3页
第3页 / 共14页
ad7793编程例子.docx_第4页
第4页 / 共14页
ad7793编程例子.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

ad7793编程例子.docx

《ad7793编程例子.docx》由会员分享,可在线阅读,更多相关《ad7793编程例子.docx(14页珍藏版)》请在冰豆网上搜索。

ad7793编程例子.docx

ad7793编程例子

ad7793编程例子

ad7793编程

#include

#include

#include

#include

#defineucharunsignedchar

#defineTIMES200

//pcf8563

#definebyteunsignedchar

#defineSomeNOP();_nop_();_nop_();_nop_();_nop_();

#defineMONTH15

#defineREAD_ADR0xa3

#defineWRITE_ADR0xa2

//usb

#defineINIT_TIME_FN0x30//asc--0

#defineMAXDATA20

//ad

sbitCS=P1^3;sbitCLK=P1^2;sbitSCLK=P1^0;sbitDIN=P1^4;sbitDOUT_RDY=P1^1;//pcf8563

sbitCLKOUT=P2^0;sbitSCL=P2^1;sbitSDA=P2^2;sbitint11=P2^3;

voidsend(uchar);ucharreceive(void);voidinit_com(void);

//initad1

voidwritetoreg(uchar);ucharreadfromreg(void);bitreadstatusreg(void);uchar*

readsingle(void);

//voidset_s_conv(void);voidset_c_conv(void);voidset_c_read(void);voidsingle_conv(void);voidcontinuous_conv(intt);voidcontinous_read();voidset_timer();

//pcf8563

ucharreceiveonetime(void);voidstarti2c();voidstopi2c();bitwaitack();voidsendack();voidsendnack();voidsendbyte(bytech);bytereceivebyte();

voidwritedata(uchartimetype,ucharvalue);voidreaddata(uchar

firsttype,ucharcount,uchar*buff);

voidsetalarm(ucharalarmtype,ucharcount);voidcleanalarm();

voidinittime(uchar*c);voidinit_int0();voidreadouttime();//usb

volatileuchara[MAXDATA],begin_int_times,int_times,sample_flag;//0x627525presentes

voltageofAVDD=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

else

{//endtime

//a[8]--;

//beginsampling

if(int_times==begin_int_times){int_times++;sample_flag=1;}//givea

begin_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:

报警小时,报警分),end

time(后面数据的个数,报警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;Internal64K

clock,

//notout;internalzerocalibration

while(DOUT_RDY);

writetoreg(0x08);

writetoreg(0xa0);writetoreg(0x0a);//writetomodereg:

0x8005:

50HZ;Internal64K

clock,

//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;Internal

64Kclock,

//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(0x03,c[5]&0x7f);

}

voidinit_int0()

{EA=1;//开中断

IT0=0;//设置中断1为电平触发方式

EX0=1;//允许外部中断0

}

//voidINTERRUPT(_IE0_)int0_isr()

voidreadouttime()

{ucharc[7],setvalue;inti;

readdata(0x03,6,c);

c[0]&=0x7f;c[1]&=0x3f;c[2]&=0x3f;c[3]&=0x07;

setvalue=c[4];setvalue&=0x80;c[4]&=0x1f;

//c[7]=c[5];

c[6]=c[5];if(setvalue)c[5]=0x19;elsec[5]=0x20;

for(i=0;i<7;i++)send(c[i]);

}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++////+++++++++++++++++++++++++++++++++++++++++++++++++++++////--------------------------usb-----------------------////+++++++++++++++++++++++++++++++++++++++++++++++++++++////+++++++++++++++++++++++++++++++++++++++++++++++++++++//voidinit_com(void)

{/*串口初始化*/

TMOD=0x22;

//TH1=0xf4;TL1=0xf4;PCON=0x00;//SMOD=02400bpsTH1=0xff;TL1=0xff;PCON=0x80;//SMOD=157600bpsTR1=1;SCON=0xd8;//0x50

ET1=0;ES=0;

}

voidsend(ucharad_data)

{SBUF=ad_data;while(TI==0);TI=0;}

ucharreceive(void)

{ucharRX;RX=0;while(RI==0);RI=0;RX=SBUF;

returnRX;

}

voidCreateFile(uchar*fn)

{uchari;//ucharc[5];

send(0x0

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1