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

📄 ad7705.c

📁 1
💻 C
字号:
#include <reg51.h>
#include"intrins.h"

#define Ain1 0x00
#define Ain2 0x01

sbit sclk = P1^7;
sbit drdy = P1^3;
sbit din  = P1^5;
sbit dout = P1^6;
sbit CS_AD7705 = P1^4;

void Writetoreg(unsigned char byteword);
unsigned int Read_16bitdata(void);
void Init_AD7705( unsigned char Channel );
unsigned int Average_Sample(unsigned int Num_Sample);
void Reset_AD7705(void);

void Writetoreg(unsigned char byteword)
{
	
	unsigned char i;

	while( !drdy );
	sclk = 1;
	CS_AD7705 = 0;
	
	for(i = 0; i < 8; i++)
	{
		sclk = 0;
		din = (bit)(byteword & 0x80);
		sclk = 1;
		byteword <<= 1;
	}

	CS_AD7705 = 1;	

}

unsigned int Read_16bitdata(void)
{
	
	unsigned char i;
	unsigned int Sample = 0;

	while( drdy );

	sclk = 1;
	CS_AD7705 = 0;

	for(i = 0; i < 16; i++)
	{
		Sample <<= 1;
		sclk = 0;		
		Sample += (0x0001 & (unsigned int)dout);
		sclk = 1;
	}

	CS_AD7705 = 1;

	while( !drdy );

	return Sample;

}

void Init_AD7705( unsigned char Channel )
{
	
	CS_AD7705 = 1;
	
	Writetoreg(0x21|Channel); /* Active Channel is Ain1(+)/Ain1(.), next operation as write to the clock register */
	Writetoreg(0x08); /* master clock enabled, 2.0000MHz Clock(CLKDIV = 1), set output rate to 200Hz*/
	Writetoreg(0x11|Channel); /* Active Channel is Ain1(+)/Ain1(.), next operation as write to the setup register */
	Writetoreg(0x7c); /* gain = 128, bipolar mode, buffer on, clear FSYNC and perform a Self Calibration*/	

}

unsigned int Average_Sample(unsigned int Num_Sample)
{
	
	unsigned int i;
	unsigned long int result = 0;

	for(i = 0; i < Num_Sample; i++)
	{
//	 	Init_AD7705();
		Writetoreg(0x39);
	 	result += Read_16bitdata();
	}

	result = result / Num_Sample;

	return (unsigned int)result;
}

void Reset_AD7705(void)
{
    unsigned char i;

	sclk = 1;
    for(i = 0;i < 40; i++)
    {  
        sclk = 0; 
        din = 1; 
        sclk = 1; 
    }

}

⌨️ 快捷键说明

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