[求助] LTC1867芯片单极模式只能采样0~2V

清风烈酒   2018-1-29 17:00 楼主
悬赏 10 分 芯积分未解决
在stm32f107基础上使用LTC1867芯片采样,设置单极性后只能采样要0~2V的电压,基准为4V。请大神帮忙!!!!
  1. #define ADC_CS_H GPIO_SetBits(GPIOA,GPIO_Pin_4);
  2. #define ADC_CS_L GPIO_ResetBits(GPIOA,GPIO_Pin_4);
  3. #define ADC_SCK_H GPIO_SetBits(GPIOA,GPIO_Pin_5);
  4. #define ADC_SCK_L GPIO_ResetBits(GPIOA,GPIO_Pin_5);
  5. //#define ADC_MISO GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6)
  6. #define ADC_MISO GPIOA->IDR&(1<<6)
  7. #define ADC_MOSI_H GPIO_SetBits(GPIOA,GPIO_Pin_7);
  8. #define ADC_MOSI_L GPIO_ResetBits(GPIOA,GPIO_Pin_7);
  9. #define Instructions_LTC1867_CH0 0x80
  10. #define Instructions_LTC1867_CH1 0xC0
  11. #define Instructions_LTC1867_CH2 0x90
  12. #define Instructions_LTC1867_CH3 0xD0
  13. #define Instructions_LTC1867_CH4 0xA0
  14. #define Instructions_LTC1867_CH5 0xE0
  15. #define Instructions_LTC1867_CH6 0xB0
  16. #define Instructions_LTC1867_CH7 0xF0
  17. #define LTC1867_SLEEP_MODE 0x02
  18. #define LTC1867_EXIT_SLEEP_MODE 0x00
  19. #define LTC1867_UNIPOLAR_MODE 0x04
  20. #define LTC1867_BIPOLAR_MODE 0x00
  21. /******************************************
  22. *功能:SPI初始化
  23. *******************************************/
  24. void SPI_GPIO_Init(void)
  25. {
  26. GPIO_InitTypeDef GPIO_InitStructure;
  27. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  28. //----------------------GPIO----------------------
  29. // Configure SPI pins: CS
  30. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  31. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  32. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  33. GPIO_Init(GPIOA, &GPIO_InitStructure);
  34. // Configure SPI pins: SCK
  35. GPIO_InitStructure.GPIO_Pin = 0;
  36. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  37. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  38. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  39. GPIO_Init(GPIOA, &GPIO_InitStructure);
  40. // Configure SPI pins: MISO
  41. GPIO_InitStructure.GPIO_Pin = 0;
  42. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  43. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  44. GPIO_Init(GPIOA, &GPIO_InitStructure);
  45. // Configure _SPI pins: MOSI
  46. GPIO_InitStructure.GPIO_Pin = 0;
  47. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  48. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  49. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  50. GPIO_Init(GPIOA, &GPIO_InitStructure);
  51. //设置为空闲状态
  52. ADC_CS_H;
  53. ADC_SCK_H;
  54. }
  55. /************************************************
  56. *功能: LTC1867ACGN选择1~8通道
  57. *************************************************/
  58. u8 LTC1867ACGN_SetChnl(u8 chnl)
  59. {
  60. u8 cmd;
  61. switch(chnl)
  62. {
  63. case 1:
  64. cmd = Instructions_LTC1867_CH0|LTC1867_UNIPOLAR_MODE;break;
  65. case 2:
  66. cmd = Instructions_LTC1867_CH1|LTC1867_UNIPOLAR_MODE;break;
  67. case 3:
  68. cmd = Instructions_LTC1867_CH2|LTC1867_UNIPOLAR_MODE;break;
  69. case 4:
  70. cmd = Instructions_LTC1867_CH3|LTC1867_UNIPOLAR_MODE;break;
  71. case 5:
  72. cmd = Instructions_LTC1867_CH4|LTC1867_UNIPOLAR_MODE;break;
  73. case 6:
  74. cmd = Instructions_LTC1867_CH5|LTC1867_UNIPOLAR_MODE;break;
  75. case 7:
  76. cmd = Instructions_LTC1867_CH6|LTC1867_UNIPOLAR_MODE;break;
  77. case 8:
  78. cmd = Instructions_LTC1867_CH7|LTC1867_UNIPOLAR_MODE;break;
  79. default:
  80. cmd = Instructions_LTC1867_CH0|LTC1867_UNIPOLAR_MODE;break;
  81. }
  82. return cmd;
  83. }
  84. /**********************************
  85. *功能: SPI读写数据,给定通道选择参数
  86. ***********************************/
  87. u16 SPI_WriteRead(u8 chnl)
  88. {
  89. u8 i = 0;
  90. u16 dat = 0;
  91. u8 cmd = 0;
  92. cmd = LTC1867ACGN_SetChnl(chnl);
  93. ADC_CS_L;
  94. delay_5us(10); //50
  95. for(i=0;i<7;i++)
  96. {
  97. if(cmd&(0x80))
  98. {
  99. ADC_MOSI_H;
  100. }else{
  101. ADC_MOSI_L;
  102. }
  103. cmd <<= 1;
  104. ADC_SCK_L;
  105. delay_5us(1); //5
  106. ADC_SCK_H;
  107. delay_5us(1); //5
  108. }
  109. ADC_CS_H;
  110. delay_5us(2); //10
  111. ADC_CS_L;
  112. for(i=0;i<16;i++)
  113. {
  114. ADC_SCK_L;
  115. delay_5us(1); //2
  116. if(ADC_MISO) dat |= 1;
  117. dat <<= 1;
  118. ADC_SCK_H;
  119. delay_5us(1); //2
  120. }
  121. ADC_CS_H;
  122. return dat;
  123. }
本帖最后由 清风烈酒 于 2018-1-30 12:58 编辑

回复评论 (9)

使用的外部4.069基准吗?
虾扯蛋,蛋扯虾,虾扯蛋扯虾
点赞  2018-1-29 18:07
引用: littleshrimp 发表于 2018-1-29 18:07
使用的外部4.069基准吗?

使用的内部基准,我用万用表测了REFCOMP引脚有4.07V的电压
点赞  2018-1-30 09:33
引用: 清风烈酒 发表于 2018-1-30 09:33
使用的内部基准,我用万用表测了REFCOMP引脚有4.07V的电压

看你的情况像是单端模式没有设置成功,差分模式下会出现你说的这种情况,你再检查一下设置看看
虾扯蛋,蛋扯虾,虾扯蛋扯虾
点赞  2018-1-30 10:05
引用: littleshrimp 发表于 2018-1-30 10:05
看你的情况像是单端模式没有设置成功,差分模式下会出现你说的这种情况,你再检查一下设置看看

QQ截图20180130101810.png
上表我用的是下面8行的配置,上面八行是差分输入吧。
我这边实际电压0V对应采集值0V,实际电压2V对应采集值为2V,2.01V开始就变成了0V,实际电压4V对应的采集值是1.996V。
点赞  2018-1-30 10:21
麻烦大神解析,感激不尽
点赞  2018-1-30 10:25
引用: 清风烈酒 发表于 2018-1-30 10:21
上表我用的是下面8行的配置,上面八行是差分输入吧。
我这边实际电压0V对应采集值0V,[/ba ...

你看一下0V,2V,4V对应的ADC CODE分别是多少
再参照一下官方的转换函数看下是不是计算的时候出了问题?
  1. /*!
  2. LTC1867: 16-Bit 8-Channel 200ksps ADC
  3.  
  4. @verbatim
  5.  
  6. The LTC1863/LTC1867 are pin-compatible, 8-channel 12-/16-bit A/D converters with
  7. serial I/O, and an internal reference. The ADCs typically draw only 1.3mA from a
  8. single 5V supply. The 8-channel input multiplexer can be configured for either
  9. single-ended or differential inputs and unipolar or bipolar conversions (or
  10. combinations thereof). The automatic nap and sleep modes benefit power sensitive
  11. applications.
  12.  
  13. The LTC1867's DC performance is outstanding with a +/-2LSB INL specification and
  14. no missing codes over temperature. The signal-to-noise ratio (SNR) for the
  15. LTC1867 is typically 89dB, with the internal reference.
  16.  
  17. @endverbatim
  18.  
  19. [url]http://www.linear.com/product/LTC1867[/url]
  20.  
  21. [url]http://www.linear.com/product/LTC1867#demoboards[/url]
  22.  
  23. REVISION HISTORY
  24. $Revision: 2026 $
  25. $Date: 2013-10-14 13:52:48 -0700 (Mon, 14 Oct 2013) $
  26.  
  27. Copyright (c) 2013, Linear Technology Corp.(LTC)
  28. All rights reserved.
  29.  
  30. Redistribution and use in source and binary forms, with or without
  31. modification, are permitted provided that the following conditions are met:
  32.  
  33. 1. Redistributions of source code must retain the above copyright notice, this
  34.    list of conditions and the following disclaimer.
  35. 2. Redistributions in binary form must reproduce the above copyright notice,
  36.    this list of conditions and the following disclaimer in the documentation
  37.    and/or other materials provided with the distribution.
  38.  
  39. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  40. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  41. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  42. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  43. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  44. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  46. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  47. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  48. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49.  
  50. The views and conclusions contained in the software and documentation are those
  51. of the authors and should not be interpreted as representing official policies,
  52. either expressed or implied, of Linear Technology Corp.
  53.  
  54. The Linear Technology Linduino is not affiliated with the official Arduino team.
  55. However, the Linduino is only possible because of the Arduino team's commitment
  56. to the open-source community.  Please, visit [url]http://www.arduino.cc[/url] and
  57. [url]http://store.arduino.cc[/url] , and consider a purchase that will help fund their
  58. ongoing work.
  59. */
  60.  
  61. //! @defgroup LTC1867 LTC1867: 16-Bit 8-Channel 200ksps ADC
  62.  
  63. /*! @file
  64.     @ingroup LTC1867
  65.     Library for LTC1867: 16-Bit 8-Channel 200ksps ADC
  66. */
  67.  
  68. #include <Arduino.h>
  69. #include <stdint.h>
  70. #include "Linduino.h"
  71. #include "LT_SPI.h"
  72. #include "LTC1867.h"
  73. #include <SPI.h>
  74.  
  75.  
  76. // Reads the ADC  and returns 16-bit data
  77. void LTC1867_read(uint8_t cs, uint8_t adc_command, uint16_t *adc_code)
  78. {
  79.   spi_transfer_word(cs, (uint16_t)(adc_command<<8), adc_code);
  80. }
  81.  
  82. // Calculates the LTC1867 input's unipolar voltage given the binary data and lsb weight.
  83. float LTC1867_unipolar_code_to_voltage(uint16_t adc_code, float LTC1867_lsb, int32_t LTC1867_offset_unipolar_code)
  84. {
  85.   float adc_voltage;
  86.   adc_voltage=((float)(adc_code+LTC1867_offset_unipolar_code))*LTC1867_lsb;   //! 1) Calculate voltage from ADC code, lsb, offset.
  87.   return(adc_voltage);
  88. }
  89.  
  90. // Calculates the LTC1867 input's bipolar voltage given the two's compliment data and lsb weight
  91. float LTC1867_bipolar_code_to_voltage(uint16_t adc_code, float LTC1867_lsb, int32_t LTC1867_offset_bipolar_code)
  92. {
  93.   float adc_voltage, sign = 1.0;
  94.   if (adc_code>>15)
  95.   {
  96.     adc_code = (adc_code ^ 0xFFFF)+1;                                           //! 1) Convert ADC code from two's complement to binary
  97.     sign = -1;
  98.   }
  99.   adc_voltage=((float)(adc_code+LTC1867_offset_bipolar_code))*LTC1867_lsb*sign; //! 2) Calculate voltage from ADC code, lsb, offset.
  100.   return(adc_voltage);
  101. }
  102.  
  103. // Calibrate the lsb
  104. void LTC1867_cal_voltage(uint16_t zero_unipolar_code, uint16_t zero_bipolar_code, uint16_t fs_code, float zero_voltage, float fs_voltage, float *LTC1867_lsb, int32_t *LTC1867_offset_unipolar_code, int32_t *LTC1867_offset_bipolar_code)
  105. {
  106.   float temp_offset;
  107.   *LTC1867_lsb = (fs_voltage-zero_voltage)/((float)(fs_code - zero_unipolar_code));                     //! 1) Calculate the LSB
  108.    
  109.   temp_offset = (zero_voltage/ *LTC1867_lsb) - zero_unipolar_code;                                      //! 2) Calculate Unipolar offset
  110.   temp_offset = (temp_offset > (floor(temp_offset) + 0.5)) ? ceil(temp_offset) : floor(temp_offset);    //! 3) Round
  111.   *LTC1867_offset_unipolar_code = (int32_t)temp_offset;                                                 //! 4) Cast as int32_t
  112.    
  113.   temp_offset = (zero_voltage / *LTC1867_lsb) - zero_bipolar_code ;                                     //! 5) Calculate Bipolar offset
  114.   temp_offset = (temp_offset > (floor(temp_offset) + 0.5)) ? ceil(temp_offset) : floor(temp_offset);    //! 6) Round
  115.   *LTC1867_offset_bipolar_code = (int32_t)temp_offset;                                                  //! 7) cast as int32_t
  116. }
虾扯蛋,蛋扯虾,虾扯蛋扯虾
点赞  2018-1-30 10:54
引用: littleshrimp 发表于 2018-1-30 10:54
你看一下0V,2V,4V对应的ADC CODE分别是多少
再参照一下官方的转换函数看下是不是计算的时候出了问题?
...

用基准源给定2V,AD读到的值是0xFE06,给定4V也是0xFE06左右。
官方提供的源码有点看不懂啊
点赞  2018-1-30 12:20
引用: 清风烈酒 发表于 2018-1-30 12:20
用基准源给定2V,AD读到的值是0xFE06,给定4V也是0xFE06左右。
官方提供的源码有点看不懂啊

再看看输出数据有没有篡位
虾扯蛋,蛋扯虾,虾扯蛋扯虾
点赞  2018-1-30 17:34
引用: littleshrimp 发表于 2018-1-30 17:34
再看看输出数据有没有篡位

也没有啊,请问下如果使用内部基准,基准电压是4V吗?REFCOMP有4V输出,VREF为2.4v。我用外用表量了进来的电压和给定的电压差不多,但是2~4V就有问题了。用U32保存数据也没有溢出的情况请帮我看下这个原理图有没有问题。
  • QQ截图20180131104133.png
点赞  2018-1-31 10:45
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复