搜档网
当前位置:搜档网 › AD7705 430 驱动程序

AD7705 430 驱动程序

//AD7705_Write_1_BYTE用来向AD7705写一个字节,AD7705_Read_1_BYTE为读一个字节。具体写什么字节需要结合
//AD7705的芯片资料来看。
#include
#include "AD7705.H"
/*接口P1.0-DRDY P1.1-DOUT P1.2-DIN P1.3-CLK,硬件上RST接VCC CS 接GND*/
#define AD7705_DIN_LOW P1OUT&=~BIT2
#define AD7705_DIN_HIGH P1OUT|=BIT2
//#define AD7705_CS_LOW P2OUT&=~BIT0
//#define AD7705_CS_HIGH P2OUT|=BIT0
#define AD7705_CLK_LOW P1OUT&=~BIT3
#define AD7705_CLK_HIGH P1OUT|=BIT3
//#define AD7705_RST_LOW P2OUT&=~BIT5
//#define AD7705_RST_HIGH P2OUT|=BIT5
unsigned char AD7705_Channel=2; /*通道数:1 一个(AIN1+,AIN1-),2 两个(AIN1+,AIN1-)和(AIN2+,AIN2-)*/
void Delay_us(unsigned char us)
{
while(us--)
{
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
}
}

void AD7705_Reset(void) //软件复位,不改变寄存器的值
{
unsigned char i;

AD7705_DIN_HIGH;
for( i=0; i<36; i++ )
{
AD7705_CLK_LOW;
_NOP();
_NOP();
_NOP();
AD7705_CLK_HIGH;
_NOP();
_NOP();
_NOP();
}
//Delay_us(30);
delay_us(500);
}


void AD7705_Init_Channel1()/*初始化通道1*/
{
P1DIR&=0xFC; /*接口P1.0-DRDY P1.1-DOUT P1.2-DIN P1.3-CLK,硬件上RST接VCC CS 接GND*/
delay_us(1);
AD7705_Write_1_BYTE(0x20);/* Active Channel is Ain1(+)/Ain1(-), next operation as write to the clock register */
delay_us(1);
AD7705_Write_1_BYTE(0x08);/* master clock enabled, 1MHz Clock, set output rate to 20Hz*/
delay_us(1);
AD7705_Write_1_BYTE(0x10);/* Active Channel is Ain1(+)/Ain1(-), next operation as write to the setup register */
delay_us(1);
AD7705_Write_1_BYTE(0x44);/* 0x44 gain = 1, unbipolar mode, buffer on, clear FSYNC and perform a Self Calibration*/
delay_us(1);
}
void AD7705_Init_Channel2()/*初始化通道2*/
{
P1DIR&=0xFC; /*接口P1.0-DRDY P1.1-DOUT P1.2-DIN P1.3-CLK,硬件上RST接VCC CS 接GND*/
delay_us(1);
AD7705_Write_1_BYTE(0x21);/* Active Channel is Ain1(+)/Ain1(-), next operation as write to the clock register */
delay_us(1);
AD7705_Write_1_BYTE(0x08);/* master clock enabled, 2MHz Clock, set output rate to 50Hz*/
delay_us(1);
AD7705_Write_1_BYTE(0x11);/* Active Channel is Ain1(+)/Ain1(-), next operation as write to the setup register */
delay_us(1);
AD7705_Write_1_BYTE(0x44);/* 0x44 gain = 1, unbipolar mode, buffer on, clear FSYNC and perform a Self Calibration*/
delay_us(1);
}

void AD7705_Write_1_BYTE(unsigned char Data_byte)/*poll send 8bits,MSB first*/
{
unsigned char i;

//AD7705_CS_LOW;
_NOP();_NOP();_NOP();
AD7705_CLK_HIGH;
_NOP();_NOP();_NOP();
for(i = 0; i < 8; i++)
{
if((Data_byte&0x80)!=0)
{
AD7705_DIN_HIGH;
}
else
{
AD7705_D

IN_LOW;
}
AD7705_CLK_LOW;
_NOP();_NOP();_NOP();
AD7705_CLK_HIGH;
_NOP();_NOP();_NOP();
Data_byte <<= 1;
_NOP();_NOP();_NOP();
}
AD7705_DIN_HIGH;
_NOP();
_NOP();
// AD7705_CS_HIGH;
}

unsigned char AD7705_Read_1_BYTE() /*poll receive 8bits*/
{
unsigned char i,Data_read;

// AD7705_CS_LOW;
_NOP();_NOP();_NOP();
AD7705_CLK_HIGH;
_NOP();_NOP();_NOP();

for(i = 0; i < 8; i++)
{
AD7705_CLK_LOW;
_NOP();_NOP();_NOP();
_NOP();_NOP();_NOP();
Data_read <<= 1;
if(P1IN & BIT1)
Data_read |= 0x01;
else
Data_read &= 0xfe;
_NOP();_NOP();_NOP();
AD7705_CLK_HIGH;
_NOP();_NOP();_NOP();
}
_NOP();_NOP();_NOP();
//AD7705_CS_HIGH;
_NOP();_NOP();_NOP();
return(Data_read);
}

unsigned int AD7705_Data_Process_Channel1() /*读取通道1的数据*/
{
unsigned char AD7705_MSB_Data,AD7705_LSB_Data;
unsigned int T_Voltage ;
AD7705_Reset();
AD7705_Init_Channel1();
while(P1IN & BIT0);
AD7705_Write_1_BYTE(0x38); /*set the next operation for 16 bit read from the data register */
AD7705_MSB_Data = AD7705_Read_1_BYTE();
AD7705_LSB_Data = AD7705_Read_1_BYTE();
T_Voltage = (unsigned int)AD7705_MSB_Data*256 + (unsigned int)AD7705_LSB_Data;
return T_Voltage;
}
/*16位的,设输入电压为Vin,参考电压为Vref,输出数据为numb,则
Vin=numb*Vref/(2^16-1);*/
unsigned int AD7705_Data_Process_Channel2()/*读取通道2的数据*/
{
unsigned char AD7705_MSB_Data,AD7705_LSB_Data;
unsigned int T_Voltage ;
AD7705_Reset();
AD7705_Init_Channel2();
while(P1IN & BIT0);
AD7705_Write_1_BYTE(0x38); /*set the next operation for 16 bit read from the data register */
AD7705_MSB_Data = AD7705_Read_1_BYTE();
AD7705_LSB_Data = AD7705_Read_1_BYTE();
T_Voltage = (unsigned int)AD7705_MSB_Data*256 + (unsigned int)AD7705_LSB_Data;
return T_Voltage;
}

相关主题