最近在做一个项目,是使用AD芯片,采集一个电压信号 ,使用了AD77171A芯片
程序从别的地方找了,参照改了下,代码如下
- /********************************************************************************
- Author : Akshay Kumar B U (akshay.kumar@analog.com)
- India Applications Support Team
- Date : December 2013
- File name : AD7171.c
- Description : This is an executable file containing all definitions of functions used for operating AD7171 in SPI mode.
- Hardware Platform : ADuCM360 + AD7171
- ********************************************************************************/
- /* ADuCM360 interface with AD7171
- SCLK - P1.1
- PDRST - P1.2
- DOUT/RDY - P1.3
- */
- //引脚说明 ; AD7171_SCLK = PBout(12), Read Data= Pb.6
- #include "ad7171.h"
- #include "led.h"
- #include "delay.h"
- // UART-based external variables
- u8 ucTxBufferEmpty = 0; // Used to indicate that the UART Tx buffer is empty
- u8 szTemp[64] = ""; // Used to store string before printing to UART
- u8 nLen = 0;
- u8 AD_i = 0;
- u8 ucWaitForUart = 0; // Used by calibration routines to wait for user input
- //Initializing AD7171 to default settings
- void InitializeAD7171(void)
- {
- AD7171_SCLK =0;
- delay_us(1); // 100ns delay
- delay_ms(1); //1ms delay
- }
- void SpiFunction(u16 *InputBuf, u16 *OutputBuf, u16 NoOfBytes)
- {
- int i,j;
- u16 TempInput = 0x00;
- //unsigned int TempOutput = 0x00;
- u16 ReceivedBit = 0x00;
- for(i=0; i< NoOfBytes; i++)
- {
- //TempOutput = *OutputBuf;
- for(j=0; j<8; j++)
- {
- ////////////WRITE DATA/////////////
-
- TempInput<<=1;
-
- AD7171_SCLK =0;//ADuCM360GpioOutput(AD7171_SCLK, PIN_LOW);
- delay_us(1);//Delay(100);
- AD7171_SCLK =1;
- delay_us(1);//Delay(100);
-
- //if(0x80==(TempOutput & 0x80))
- // {
- // ADuCM360GpioOutput(AD7171_SDIN, PIN_HIGH);
- // }
- // else
- // {
- // ADuCM360GpioOutput(AD7171_SDIN, PIN_LOW);
- // }
- // TempOutput<<=1;
- // Delay(1);
-
- ////////////WRITE DATA ENDS/////////
-
-
- ///////////READ DATA///////
-
- ReceivedBit = PBin(6);//ADuCM360GpioInput(AD7171_DOUTRDY);
- if(0x1==ReceivedBit)
- {
- TempInput |= 1;
- }
-
- ////////////READ DATA ENDS/////////
-
- }
- *InputBuf = TempInput;
- OutputBuf++;
- InputBuf++;
-
- }
-
- AD7171_SCLK =1;//ADuCM360GpioOutput(AD7171_SCLK, PIN_HIGH);
- delay_us(1);//Delay(100);
- }
- void SpiFunction2(u16 *InputBuf, u16 *OutputBuf, u16 NoOfBytes)
- {
- int i,j;
- u16 TempInput = 0x00;
- //unsigned int TempOutput = 0x00;
- u16 ReceivedBit = 0x00;
- for(i=0; i< NoOfBytes; i++)
- {
- //TempOutput = *OutputBuf;
- for(j=0; j<8; j++)
- {
- TempInput<<=1;
- AD7171_SCLK =0;//ADuCM360GpioOutput(AD7171_SCLK, PIN_LOW);
- delay_us(1);//Delay(100);
- ReceivedBit = PBin(6);//ADuCM360GpioInput(AD7171_DOUTRDY);
- if(0x1==ReceivedBit)
- {
- TempInput |= 1;
- }
- AD7171_SCLK =1;
- delay_us(1);//Delay(100);
-
- }
- /*
-
- for (i = 0; i < 16; i++)
- {
- SCLK = 0;
- usData = usData << 1;
- usData |= DAT;
- SCLK = 1;
- }
-
- */
- *InputBuf = TempInput;
- OutputBuf++;
- InputBuf++;
- }
-
- AD7171_SCLK =1;//ADuCM360GpioOutput(AD7171_SCLK, PIN_HIGH);
- delay_us(1);//Delay(100);
- }
- //Function to Read data from AD7171
- u16 ReadFromAD7171(void)
- {
- u16 ready = 0x000001;
- u16 DataReg = 0x000000;
- u16 InputRegister[10];
- u16 OutputRegister[10];
- u16 StatusBar = 0x00;
- int i=0;
- AD7171_SCLK =1; //ADuCM360GpioOutput (AD7171_SCLK, PIN_HIGH);
-
- while(ready) // Waits untill the DOUT/RDY pin goes low to indicate
- { // data is converted and is ready to be read.
- ready = PBin(6);//ADuCM360GpioInput (AD7171_DOUTRDY);
- }
- delay_ms(1);//Delay(1000);
-
- SpiFunction2(InputRegister, OutputRegister, NoOfByte); //SPI read write
- for(i=0; i<2; i++)
- {
- DataReg <<= 8;
- DataReg |= InputRegister[i];
- }
-
- StatusBar = (InputRegister[2] & 0x0000FF);
-
- // ErrorCheck(InputRegister[2]); // Performs Error Check in AD7171 Received Data. 'ErrorBit' is set to 1 if any error is present.
- delay_us(1);
-
- do
- {
- ready = PBin(6);//ADuCM360GpioInput (AD7171_DOUTRDY);
- } while(ready != 0x01);
- return DataReg;
-
- }
- /*
- // Performs Error Check in the received data
- void ErrorCheck(unsigned int Status)
- {
- if(0x20 ==(Status & 0x20))
- {
- InitializeAD7171(); //Initializes the AD7171 device when the Error bit is set
- //or when the pattern bits PAT2,PAT1 and PAT0 are not equal to 101
- //or when ID bits ID1 and ID0 are not equal to 01.
- // sprintf ( (char*)szTemp, "Error Present as Error Bit in Status Register is set high \r\n");
- // nLen = strlen((char*)szTemp);
- if (nLen <64)
- SendString();
- }
- if (0x05 != (Status & 0x05))
- {
- InitializeAD7171();
- // sprintf ( (char*)szTemp, "Error due to mismatch in Pattern Bits \r\n");
- // nLen = strlen((char*)szTemp);
- if (nLen <64)
- SendString();
- }
- if (0x08 != (Status & 0x08))
- {
- InitializeAD7171();
- // sprintf ( (char*)szTemp, "Error due to mismatch in ID bits of Status Bar \r\n");
- // nLen = strlen((char*)szTemp);
- if (nLen <64)
- SendString();
- }
- else if (0x0D ==(Status & 0x0D))
- {
- // sprintf ( (char*)szTemp, "No Error \r\n");
- // nLen = strlen((char*)szTemp);
- if (nLen <64)
- SendString();
- }
- }
- */
复制代码
但是发现读到的数值 ,算出的结果,比实际的值要小些,还没找到问题
|