AD7606-51例程

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Ad7606.h源码#ifndef_AD7076_H_#define_AD7076_H_#includereg52.h#includeintrins.h#includeother.h/*sbitDB0=P0^0sbitDB1=P0^1sbitDB2=P0^2sbitDB3=P0^3sbitDB4=P0^4sbitDB5=P0^5sbitDB6=P0^6sbitDB7=P0^7sbitDB8=P2^0sbitDB9=P2^1sbitDB10=P2^2sbitDB11=P2^3sbitDB12=P2^4sbitDB13=P2^5sbitDB14=P2^6sbitDB15=P2^7*/#definesampling_0times0#definesampling_2times1#definesampling_4times2#definesampling_8times3#definesampling_16times4#definesampling_32times5#definesampling_64times6sbitOS0=P1^0;sbitOS1=P1^1;sbitOS2=P1^2;sbitrage=P1^3;sbitconvst=P1^4;sbitbusy=P1^5;sbitrst=P1^6;sbitrd_and_cs=P1^7;sbitgreen_led=P3^6;sbitred_led=P3^7;structDB_data_struct{ucharDB_data_H;ucharDB_data_L;};voidAD7606_startconvst(void);//voidAD7076_stopconvst(void);voidAD7606_reset(void);voidAD7606_setinputvoltage(ucharvol);voidAD7606_setOS(ucharuCoS);structDB_data_struct*AD7606_read_data(void);voidAD7606_init(void);#endifAd7606.c源码:#includeAD7076.h/**名称:AD7606_startconvst()*功能:启动转换*/voidAD7606_startconvst(void){convst=0;delay_us(1);convst=1;}/**名称:AD7606_reset()*功能:复位模块*/voidAD7606_reset(void){rst=0;rst=1;//delay_us(1);rst=0;}/**名称:AD7606_setinputvoltage()*功能:设置测试电压范围*rage=1为-10V~+10V*rage=0位-5V~+5V*/voidAD7606_setinputvoltage(ucharvol){if(vol==1){rage=1;}else{rage=0;}}/**名称:AD7606_setOS()*功能:设置过采样频率*/voidAD7606_setOS(ucharuCoS){switch(uCoS){casesampling_0times://无过采样OS0=0;OS1=0;OS2=0;break;casesampling_2times://2倍过采样OS0=1;OS1=0;OS2=0;break;casesampling_4times://4倍过采样OS0=0;OS1=1;OS2=0;break;casesampling_8times://8倍过采样OS0=1;OS1=1;OS2=0;break;casesampling_16times://16倍过采样OS0=0;OS1=0;OS2=1;break;casesampling_32times://32倍过采样OS0=1;OS1=0;OS2=1;break;casesampling_64times://64倍过采样OS0=0;OS1=1;OS2=1;break;default:break;}}/**名称:AD7606_read_data()*功能:读取数据*返回值:返回一个结构体指针,该指针为指向结构体数组的首地址*/structDB_data_struct*AD7606_read_data(void){uchari;//ucharDB_data_H,DB_data_L;structDB_data_structDB_data[8];//申请一个结构体数组,用于存储8个通道的16位数据for(i=0;i8;i++){rd_and_cs=0;//在并行模式下,RD和CS同时置低,将使得数据更新在并行总线上DB_data[i].DB_data_L=P0;DB_data[i].DB_data_H=P2;rd_and_cs=1;//DB_data[i]=(DB_data_H8)|DB_data_L;}returnDB_data;}/**名称:AD7606_init()*功能:初始化AD7606模块*初始化为无过采样,即采样频率为200ksps,采样电压范围为-5V~+5V*/voidAD7606_init(void){AD7606_setOS(sampling_0times);AD7606_setinputvoltage(0);AD7606_reset();AD7606_startconvst();}Other.h源码:#ifndef_OTHER_H_#define_OTHER_H_#defineuintunsignedint#defineucharunsignedcharvoiddelay_ms(uinttimer);voiddelay_us(uchartimer);ucharbinary_to_decimal(ucharbinary);#endifOther.c源码:#includestdio.h#includeother.hvoiddelay_ms(uinttimer)//毫秒延时程序{uchari;while(timer--){for(i=200;i0;i--);for(i=120;i0;i--);}}voiddelay_us(uchartimer)//微妙延时程序{while(timer--);}/**名称:binary_to_decimal()*功能:二进制数据转换为十进制数据*//*ucharbinary_to_decimal(ucharbinary){uchardecimal,i,j,binary_tmp;ucharsumtmp=1;for(i=4;i0;i--){binary_tmp=binary&0x08;if(binary_tmp){if(i1){for(j=i;j1;j--){sumtmp=sumtmp*2;}decimal+=sumtmp;sumtmp=1;}elseif(i==1){decimal+=1;}else{;}}binary=binary1;}returndecimal;}*/Main.c源码:/**ThisisaprogramfortestingAD7606usedSTC89C52RC*ThelineconnectingAD7606withSTC89C52RCis:*STC89C52RC|AD7606*P0|DB[0:7]*P2|DB[8:15]*OS0|P1^0*OS1|P1^1*OS2|P1^2*rage|P1^3*convst|P1^4*busy|P1^5*rst|P1^6*rd_and_cs|P1^7*green_led|P3^6*red_led|P3^7*GND|GND*+5V|VCC*green_led|P3_6*red_led|P3_7*Author:jinmingchen*Compang:Hongweiindongguan*Date:Sep1,2015*/#includereg52.h#includeintrins.h#includestdio.h#includeAD7076.h#includeother.h//ucharintroduce1[]=Thisisaprogram;//ucharintroduce2[]=fortestingAD7606\r\n;ucharsampling[]=Thesamplingdatais:;ucharnewline[]=\r\n;ucharspace[]=;uchar*DB_str[16]={0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F};/*uchar*data_to_string(ucharData){uchartmp;tmp=Data&0x0f;str[1]=DB_str[binary_to_decimal(tmp)];tmp=Data4;str[0]=DB_str[binary_to_decimal(tmp)];returnstr;}*/voidserial_init(void){SCON=0x50;//UART为模式一,8位数据,允许接收TMOD|=0x20;//定时器1为模式二,8位自动重装PCON|=0x80;//SMOD=1TH1=0xfd;//波特率为19200,晶振频率为11.0592MHzIE=0x00;TR1=1;TI=1;}/*//向串口发送一个函数voidserial_send_byte(ucharch){SBUF=ch;while(TI==0);TI=0;}*///向串口发送一串字符数据voidserial_send_string(uchar*p){while(*p!='\0'){SBUF=*p;while(TI==0);TI=0;p++;}}intmain(){structDB_data_struct*DB_data;uchark,tmp;ucharDB_data_cmp[8];serial_init();AD7606_init();while(1){//DB_data=DB_str;//serial_send_string(introduce1);//serial_send_string(introduce2);AD7606_startconvst();while((busy==1))//当busy为低电平时,数据转换完毕,此时可以读取数据{//serial_send_byte(busy);//delay_ms(500);;}DB_data=AD7606_read_data();serial_send_string(sampling);for(k=0;k8;k++){tmp=(DB_data-DB_data_H4)&0x0f;serial_send_string(DB_str[tmp]);tmp=(DB_data-DB_data_H)&0x0f;serial_send_string(DB_str[tmp]);tmp=(DB_data-DB_data_L4)&0x0f;serial_send_string(DB_str[tmp]);tmp=(DB_data-DB_data_L)&0x0f;serial_send_string(DB_str[tmp]);serial_send_string(space);DB_data_cmp[k]=DB_data-DB_data_H;DB_data+=1;}if((DB_data_cmp[0]0x7D)&&(DB_data_cmp[1]0x7D)&&(DB_data_cmp[2]0x7D)&&(DB_data_cmp[3]0x7D)\&&(DB_data_cmp[4]0x7D)&&(DB_data_cmp[5]0x7D)&&(DB_data_cmp[6]0x7D)&&(DB_data_cmp[7]0x7D)){//电压检测为预定值,则绿灯亮red_led=0;green_led=1;}else{//电压检测为非预定值,则红灯亮green_led

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功