ADS1220使用参考代码

void ADS1120_Init(void)

{

cs_ad=1;;

sclk=0;

ADReset();

}

void WriteOneByte(unsigned char command)

{

unsigned char i;

for(i = 0; i < 8;i++)

{

if(command&0x80)

mosi=1;

else

mosi=0;

command <<= 1;

sclk=1;

clk_delay(80);

sclk=0;

}

}

u8 ReadOneByte(void)

{

unsigned char result,i;

sclk=0;

for(i = 0;i<8; i++)

{

sclk=1;

result <<= 0x01;

if(miso)

result |= 0X01;

clk_delay(80);

sclk=0;

clk_delay(80);

}

return result;

}

u32 ReadData(void)

{

u32 result;

cs_ad=0;

WriteOneByte(0x10);

while(cs_rdy);

result = ReadOneByte();

result = (result<<8) | ReadOneByte();

result = (result<<8) | ReadOneByte();

cs_ad=1;

if (result & 0x800000)

    {

      result |= 0xff000000;

    }

return ( result ) ;

}

void ADReset(void)

{

cs_ad=0;

WriteOneByte(0x06);

cs_ad=1;

}

void ADPowerDown(void)

{

cs_ad=0;

WriteOneByte(0x02);

cs_ad=1;

}

void ADStartConversion(void)

{

cs_ad=0;

clk_delay(80);

WriteOneByte(0x08);

clk_delay(80);

   cs_ad=1;

   clk_delay(80);

}

void ReadRegister(void)

{

unsigned char i;

unsigned long Data;

cs_ad=0;

   clk_delay(80);

WriteOneByte(0x20|0x03);

// get the register content

for (i=0; i< 4; i++)

{

Data += ReadOneByte();

}

cs_ad=1;

clk_delay(80);    

}



void WriteRegister(unsigned char StartAddress, unsigned char NumRegs, unsigned char * pData)

{

unsigned char i;

cs_ad=0;

clk_delay(80);  

// send the command byte

WriteOneByte(0x40 | (((StartAddress<<2) & 0x0c) |((NumRegs-1)&0x03)));

// send the data bytes

for (i=0; i< NumRegs; i++)

{

WriteOneByte(*pData);

pData++;

}

cs_ad=1;

return;

}

void CofigAD(unsigned char channel) // 0-1-2-3

{

  //Disabling the PGA reduces overall power consumption and allows the common

  //mode voltage range (VCM) to span from AVSS 0.1 V to AVDD + 0.1 V.



switch(channel)

{

case 0: Init_Config[0] = 0x81;

break;

case 1: Init_Config[0] = 0x91;

break;

case 2: Init_Config[0] = 0xA1;

break;

case 3: Init_Config[0] = 0xB1;

break;

}

Init_Config[1] = 0X40;//Init_Config[1] = 0XC0;

Init_Config[2] = 0X05;//Init_Config[2] = 0X40;

Init_Config[3] = 0X00;

WriteRegister(0x00,0x04,Init_Config);

ReadRegister();

}

void SetChannelPGA(u8 channel, u8 gain)

{

switch(channel)

{

case 0:

if(gain==1)

channel0[0] = 0x81;

if(gain==2)

channel0[1] = 0x83;

if(gain==4)

channel0[2] = 0x85;

if(gain==8)

channel0[3] = 0x87;

if(gain==16)

channel0[4] = 0x89;

if(gain==32)

channel0[5] = 0x8B;

if(gain==64)

channel0[6] = 0x8D;

if(gain==128)

channel0[7] = 0x8F;

break;

case 1:

if(gain==1)

channel1[0] = 0x91;

if(gain==2)

channel1[1] = 0x93;

if(gain==4)

channel1[2] = 0x95;

if(gain==8)

channel1[3] = 0x97;

if(gain==16)

channel1[4] = 0x99;

if(gain==32)

channel1[5] = 0x9B;

if(gain==64)

channel1[6] = 0x9D;

if(gain==128)

channel0[7] = 0x9F;

break;

case 2: Init_Config[0] = 0xA1;

break;

case 3: Init_Config[0] = 0xB1;

break;

}

}

u32 GetAD(unsigned char channel) // 0-1-2-3

{

   u32 voltage=0;

  CofigAD(channel);



    ADStartConversion();

    voltage= ReadData();

  return voltage;

}

void clk_delay(u16 delay_time )     
 
{
 
while( delay_time>0 )
 
{
 
delay_time--;
 
}
 
}

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页