找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1489|回复: 1
收起左侧

stm32下 ,ad7171芯片调试笔记

[复制链接]
ID:826132 发表于 2020-10-6 12:11 | 显示全部楼层 |阅读模式
最近在做一个项目,是使用AD芯片,采集一个电压信号 ,使用了AD77171A芯片
程序从别的地方找了,参照改了下,代码如下
  1. /********************************************************************************
  2. Author : Akshay Kumar B U (akshay.kumar@analog.com)
  3.                                         India Applications Support Team

  4. Date : December 2013

  5. File name : AD7171.c

  6. Description : This is an executable file containing all definitions of functions used for operating AD7171 in SPI mode.

  7. Hardware Platform : ADuCM360 + AD7171        
  8. ********************************************************************************/

  9. /* ADuCM360 interface with AD7171
  10. SCLK                                - P1.1
  11. PDRST                                - P1.2
  12. DOUT/RDY                - P1.3
  13. */
  14. //引脚说明 ; AD7171_SCLK =  PBout(12), Read Data= Pb.6
  15. #include "ad7171.h"
  16. #include "led.h"
  17. #include "delay.h"


  18. // UART-based external variables
  19. u8 ucTxBufferEmpty  = 0;       // Used to indicate that the UART Tx buffer is empty
  20. u8 szTemp[64] = "";            // Used to store string before printing to UART
  21. u8 nLen = 0;
  22. u8 AD_i = 0;
  23. u8 ucWaitForUart = 0;          // Used by calibration routines to wait for user input





  24. //Initializing AD7171 to default settings
  25. void InitializeAD7171(void)
  26. {
  27.         AD7171_SCLK =0;
  28.         delay_us(1);                          // 100ns delay
  29.         delay_ms(1);    //1ms delay
  30. }


  31. void SpiFunction(u16 *InputBuf, u16 *OutputBuf, u16 NoOfBytes)
  32. {
  33. int i,j;
  34. u16 TempInput = 0x00;
  35. //unsigned int TempOutput = 0x00;        
  36. u16        ReceivedBit = 0x00;
  37.         for(i=0; i< NoOfBytes; i++)
  38.         {
  39.                 //TempOutput = *OutputBuf;
  40.                 for(j=0; j<8; j++)
  41.                 {
  42.                                 ////////////WRITE DATA/////////////
  43.                         
  44.                         TempInput<<=1;
  45.                         
  46.                         AD7171_SCLK =0;//ADuCM360GpioOutput(AD7171_SCLK, PIN_LOW);
  47.                         delay_us(1);//Delay(100);        
  48.                         AD7171_SCLK =1;
  49.                         delay_us(1);//Delay(100);
  50.                         
  51.                 //if(0x80==(TempOutput & 0x80))
  52.                 //        {
  53.                 //        ADuCM360GpioOutput(AD7171_SDIN, PIN_HIGH);
  54.                 //        }
  55.                 //        else
  56.                 //        {
  57.                 //                ADuCM360GpioOutput(AD7171_SDIN, PIN_LOW);
  58.                 //        }
  59.                 //  TempOutput<<=1;        
  60.                 //        Delay(1);
  61.                
  62.                         ////////////WRITE DATA ENDS/////////
  63.                         
  64.                         
  65.                         ///////////READ DATA///////
  66.                                    
  67.                         ReceivedBit = PBin(6);//ADuCM360GpioInput(AD7171_DOUTRDY);        
  68.                                 if(0x1==ReceivedBit)
  69.                                         {
  70.                                                 TempInput |= 1;
  71.                                         }
  72.                                                                
  73.                         ////////////READ DATA ENDS/////////
  74.                         
  75.                 }
  76.                 *InputBuf = TempInput;
  77.                 OutputBuf++;
  78.                 InputBuf++;
  79.                         
  80.         }
  81.         
  82.         AD7171_SCLK =1;//ADuCM360GpioOutput(AD7171_SCLK, PIN_HIGH);
  83.         delay_us(1);//Delay(100);        
  84. }

  85. void SpiFunction2(u16 *InputBuf, u16 *OutputBuf, u16 NoOfBytes)
  86. {
  87. int i,j;
  88. u16 TempInput = 0x00;
  89. //unsigned int TempOutput = 0x00;        
  90. u16        ReceivedBit = 0x00;
  91.         for(i=0; i< NoOfBytes; i++)
  92.         {
  93.                 //TempOutput = *OutputBuf;
  94.                 for(j=0; j<8; j++)
  95.                 {
  96.                         TempInput<<=1;                        
  97.                         AD7171_SCLK =0;//ADuCM360GpioOutput(AD7171_SCLK, PIN_LOW);
  98.                         delay_us(1);//Delay(100);        
  99.                         ReceivedBit = PBin(6);//ADuCM360GpioInput(AD7171_DOUTRDY);        
  100.                                 if(0x1==ReceivedBit)
  101.                                         {
  102.                                                 TempInput |= 1;
  103.                                         }
  104.       AD7171_SCLK =1;                                       
  105.                         delay_us(1);//Delay(100);                                                               
  106.         
  107.                 }
  108.                 /*
  109.         
  110.         for (i = 0; i < 16; i++)
  111.         {
  112.                 SCLK = 0;
  113.                 usData = usData << 1;
  114.                 usData |= DAT;
  115.                 SCLK = 1;
  116.         }        
  117.                
  118.                 */
  119.                 *InputBuf = TempInput;
  120.                 OutputBuf++;
  121.                 InputBuf++;                        
  122.         }
  123.         
  124.         AD7171_SCLK =1;//ADuCM360GpioOutput(AD7171_SCLK, PIN_HIGH);
  125.         delay_us(1);//Delay(100);        
  126. }

  127. //Function to Read data from AD7171
  128. u16 ReadFromAD7171(void)                 
  129. {
  130.         u16 ready = 0x000001;        
  131.         u16 DataReg = 0x000000;
  132.         u16 InputRegister[10];
  133.         u16 OutputRegister[10];
  134.         u16 StatusBar = 0x00;
  135.         int i=0;        
  136.                 AD7171_SCLK =1; //ADuCM360GpioOutput  (AD7171_SCLK, PIN_HIGH);
  137.                                 
  138.                 while(ready)                                              // Waits untill the DOUT/RDY pin goes low to indicate
  139.                 {                                                   // data is converted and is ready to be read.
  140.                         ready = PBin(6);//ADuCM360GpioInput (AD7171_DOUTRDY);        
  141.                 }

  142.                 delay_ms(1);//Delay(1000);
  143.         
  144.                 SpiFunction2(InputRegister, OutputRegister, NoOfByte);                                   //SPI read write               
  145.                         for(i=0; i<2; i++)
  146.         {                        
  147.                                         DataReg        <<= 8;
  148.                                         DataReg |= InputRegister[i];                                    
  149.                                 }                        
  150.                
  151.                          StatusBar = (InputRegister[2] & 0x0000FF);
  152.                                                         
  153. //                ErrorCheck(InputRegister[2]);    // Performs Error Check in AD7171 Received Data. 'ErrorBit' is set to 1 if any error is present.
  154.     delay_us(1);
  155.                
  156.                 do              
  157.                 {
  158.                         ready = PBin(6);//ADuCM360GpioInput  (AD7171_DOUTRDY);        
  159.                 }        while(ready != 0x01);               
  160.     return DataReg;        
  161.         
  162. }


  163. /*

  164. // Performs Error Check in the received data
  165. void ErrorCheck(unsigned int Status)
  166. {
  167.         if(0x20 ==(Status & 0x20))
  168.     {                                                               
  169.               InitializeAD7171();             //Initializes the AD7171 device when the Error bit is set      
  170.                                                                                                                                                                 //or when the pattern bits PAT2,PAT1 and PAT0 are not equal to 101                 
  171.                                                    //or when ID bits ID1 and ID0 are not equal to 01.
  172. //                                   sprintf ( (char*)szTemp, "Error Present as Error Bit in Status Register is set high \r\n");
  173. //                   nLen = strlen((char*)szTemp);
  174.                    if (nLen <64)
  175.                                  SendString();
  176.           }
  177.         if (0x05 != (Status & 0x05))
  178.                 {
  179.                         InitializeAD7171();
  180. //                    sprintf ( (char*)szTemp, "Error due to mismatch in Pattern Bits \r\n");
  181. //                   nLen = strlen((char*)szTemp);
  182.                    if (nLen <64)
  183.                                  SendString();                                   
  184.           }
  185.         if (0x08 != (Status & 0x08))
  186.                 {
  187.                         InitializeAD7171();
  188. //                    sprintf ( (char*)szTemp, "Error due to mismatch in ID bits of Status Bar \r\n");
  189. //                   nLen = strlen((char*)szTemp);
  190.                    if (nLen <64)
  191.                                  SendString();                                   
  192.           }
  193.         else if (0x0D ==(Status & 0x0D))
  194.                 {
  195. //                        sprintf ( (char*)szTemp, "No Error \r\n");
  196. //                   nLen = strlen((char*)szTemp);
  197.                    if (nLen <64)
  198.                                  SendString();
  199.                 }
  200. }


  201. */
复制代码

但是发现读到的数值 ,算出的结果,比实际的值要小些,还没找到问题

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:826132 发表于 2020-10-6 12:13 | 显示全部楼层
首次发贴,自己顶下。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表