⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ad7705.c

📁 单片机实现AD7705采集
💻 C
字号:
#include <reg51.h>
#include<absacc.h>
#include <intrins.h>
#include<stdio.h>
#include<3310.h>
#define uchar unsigned char
#define uint  unsigned int
unsigned int voltage;
unsigned int ad7705timer;
uchar as_0[7];
sbit sclk_0=P3^1;//时钟脉冲
sbit rs=P1^1;
sbit dy=P1^2;
sbit shuju=P3^0;
//sbit din=P1^4;
void UART_INIT();//串口初始化
void chushi();
void a3310dis();
void writetoreg(uchar byteword);//写入数据
unsigned int read();//读数据
unsigned int Read();
void process(long x);

//======================
  void delay(int j)
   {
    int i;
    for(;j>0;j--)
	for(i=0;i<155;i++);
   }


  void write_uart(unsigned char a) //通过串口给ad7705发送命令字a
 {

  SCON=0;
   TI=0;
  SBUF=a;
  while(!TI);
 }
 unsigned char rearrange(unsigned char a) /*将8位数据a的顺序颠倒后赋给b。
        因为串口发送顺序和ad7705的接收顺序刚好相反*/
   {
     unsigned char i,b;
       b=a&0x01;
        for(i=1; i<8; i++)
     {
       b=b<<1;
       a=a>>1;
       b=b+(a&0x01);
     }
     return(b);
  }
   unsigned int ReadWord()
  {
    unsigned char high8,low8;
    unsigned int out;
    SCON=0;
   REN=1; //allow serial port to recieve data
   RI=0;
   while(!RI); //waiting the end of recieve of 8 bit
   high8=SBUF;
   RI=0;
   while(!RI);
   low8=SBUF;
   REN=0; 
   out=rearrange(high8);
   out=out<<8;
   out=out+rearrange(low8);
  return(out);
  }

//=============================
void process(long x)//数据处理
{
  as_0[5]=x/100000;
  x=x%100000;
  as_0[4]=x/10000;
  x=x%10000;
  as_0[3]=x/1000;
  x=x%1000;
  as_0[2]=x/100;
  x=x%100;
  as_0[1]=x/10;
  as_0[0]=x%10;
}
//=======================================
void a3310dis()//数据显示
{
   //   long i;
     //display1(6,2,number[as_0[5]]);
     display1(14,2,number[as_0[4]]);
     display1(26,2,number[as_0[3]]);
     display1(34,2,number[as_0[2]]);
     display1(42,2,number[as_0[1]]);	 
	 display1(50,2,number[as_0[0]]);
	// for(i=0;i<100;i++);
}
//====================================
//====================================
//主函数
void main()
{ 
    long a;
   init();//液晶初始化	
    clr();//清屏
   write_uart(0x04);
   write_uart(0x30);//50HZ转换频率*/
   write_uart(0x08);//选择设置寄存器
   write_uart(0x62);//自校准模式,增益为1
    _nop_();
     _nop_();
   dy=1;
  // rs=0;
  xianshi( 0,0,6,as);
  display1(20,2,number[10]);
   display1(58,2,number[11]);
   while(1)
   {  
        dy=0; 
		//rs=1;
        write_uart(0x1C);//自校准模式,增益为1
		dy=1;
	    a= ReadWord()/2.628;//读数据
 	   process(a);//数据处理
	    _nop_();
		_nop_();
	   a3310dis();//3310显示
	    _nop_();	
  	    _nop_();
		 _nop_();
		 delay(85);
       
   }
} 
/***************************************/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -