[嵌入式开发模块]AD转换芯片ADS8344驱动模块

古城微笑少年丶 2022-02-13 18:45 546阅读 0赞

文章目录

  • 芯片简介
  • 代码
    • 头文件
    • 源文件
  • 使用及示例代码

芯片简介

ADS8344是一个8通道16位模数转换器,使用SPI接口。
watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpbl9zdHJvbmc_size_16_color_FFFFFF_t_70
其可以使用单端或差分输入,单端模式时,每个CHX独立作为采样输入,测其相对于模拟地的差值。差分模式时,CH0和CH1、CH2和CH3……分别作为一组,测两者之间的差值(前者为正)。

采样值是一个16位无符号整型,0对应着模拟地(以及更低),65535(最大值)对应着参考电压Vref(以及更高),中间值则将模拟地和参考电压间的电压值平分。

比如要是要是COM接着的是0V,Vref接着5V,而采样值测出为34532,那么实际测量到的电压= (5V - 0V)/65535*34532 + 0V ≈ 2.63V

通信方式对应于时钟分为多种:
watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpbl9zdHJvbmc_size_16_color_FFFFFF_t_70 1
watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpbl9zdHJvbmc_size_16_color_FFFFFF_t_70 2
watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpbl9zdHJvbmc_size_16_color_FFFFFF_t_70 3

概括来说,CS引脚断言后,芯片会以收到的第一个1作为控制字节的开始,收完控制字节后开始转换,转换中BUSY引脚会变化,然后根据选择的模式会略有不同。

这里,驱动使用了外部时钟模式,这种模式下,一次转换需要最少25个时钟周期,去掉控制字节的8个,就是17个时钟周期。这是应该控制字节后的第一个时钟周期是用于转换的,第二个时钟周期才开始传输结果。因为大部分单片机都是以字节为单位进行通信的,因此一次转换就需要32个时钟周期。最后7个bit会被填充0,真正的数据是倒数第8个bit起,往前16个bit。

因为十分直白明了,就介绍到这了,代码也不多。

代码

头文件

  1. /*
  2. ****************************************************************************************
  3. *
  4. * ADS8344 DRIVER MODULE
  5. * ADS8344驱动模块
  6. *
  7. * File : ADS8344.h
  8. * By : Lin Shijun(http://blog.csdn.net/lin_strong)
  9. * Date : 2019/04/28
  10. * Version : V1.0
  11. * Note : It only support Single-Ended Channel and external clock mode in this version.
  12. * To use the interface:
  13. * a. register the SPI functions to the module by regFuncSPI and regFuncCS.
  14. * b. call sampleChannelSGLEXT to get one sample at specified channel.
  15. * Note that the return value of sample is a uint16_t type. 0 means the input
  16. * equal to or lower than GND, 65535 means equal to or higher than Vref.
  17. * For example, assume that GND is 0V, Vref is 5V, sample result is 34532, than
  18. * input = (5V - 0V)/65535*34532 + 0V = 2.63V
  19. ****************************************************************************************
  20. */
  21. #ifndef _ADS8344_H
  22. #define _ADS8344_H
  23. /*
  24. ****************************************************************************************
  25. * INCLUDES
  26. ****************************************************************************************
  27. */
  28. #include <stdint.h>
  29. /*
  30. ****************************************************************************************
  31. * CONSTANTS
  32. ****************************************************************************************
  33. */
  34. // Single-Ended Channel Selection // A2 A1 A0
  35. #define ADS8344SGL_CH0 0 // 0 0 0
  36. #define ADS8344SGL_CH1 4 // 1 0 0
  37. #define ADS8344SGL_CH2 1 // 0 0 1
  38. #define ADS8344SGL_CH3 5 // 1 0 1
  39. #define ADS8344SGL_CH4 2 // 0 1 0
  40. #define ADS8344SGL_CH5 6 // 1 1 0
  41. #define ADS8344SGL_CH6 3 // 0 1 1
  42. #define ADS8344SGL_CH7 7 // 1 1 1
  43. // Differential Channel Control // A2 A1 A0
  44. #define ADS8344DIF_CH0 0 // 0 0 0
  45. #define ADS8344DIF_CH2 1 // 0 0 1
  46. #define ADS8344DIF_CH4 2 // 0 1 0
  47. #define ADS8344DIF_CH6 3 // 0 1 1
  48. // Power-Down Selection.
  49. // Power-down between conversions. When each conversion is finished, the converter enters a
  50. // low-power mode. At the start of the next conversion, the device instantly powers up to
  51. // full power. There is no need for additional delays to assure full operation and the very
  52. // first conversion is valid.
  53. #define ADS8344PD_Powerdown 0
  54. // Selects Internal Clock Mode.
  55. #define ADS8344PD_InternalClk 2
  56. // No power-down between conversions, device always powered. Selects external clock mode.
  57. #define ADS8344PD_ExternalClk 3
  58. /*
  59. ****************************************************************************************
  60. * INTERFACES
  61. ****************************************************************************************
  62. */
  63. // description: Register SPI call back functions
  64. // parameter : spi_rb callback function to read byte using SPI(keep MOSI low!!!!!)
  65. // spi_wb callback function to write byte using SPI
  66. // return :
  67. // note : you should register SPI functions through this interface before use the driver.
  68. void ADS8344_regFuncSPI(uint8_t (*spi_rb)(void), void (*spi_wb)(uint8_t wb));
  69. // description: Registers call back function for ADS8344 select & deselect.
  70. // parameter : cs_sel callback function for ADS8344 select
  71. // cs_desel callback fucntion for ADS8344 deselect
  72. // return :
  73. // note : you should register CS functions through this interface before use the driver.
  74. void ADS8344_regFuncCS(void(*cs_sel)(void), void(*cs_desel)(void));
  75. // description: sample the channel 'ch' with Single-Ended Channel and external clock mode.
  76. // parameter : ch the channel to sample, see ADS8344SGL_CHX
  77. // return : the sample result.
  78. // note :
  79. uint16_t ADS8344_sampleChannelSGLEXT(uint8_t ch);
  80. #endif

源文件

  1. /*
  2. ****************************************************************************************
  3. *
  4. * ADS8344 DRIVER MODULE
  5. * ADS8344驱动模块
  6. *
  7. * File : ADS8344.c
  8. * By : Lin Shijun(http://blog.csdn.net/lin_strong)
  9. * Date : 2019/04/28
  10. * Version : V1.0
  11. * Note :
  12. ****************************************************************************************
  13. */
  14. /*
  15. ****************************************************************************************
  16. * INCLUDES
  17. ****************************************************************************************
  18. */
  19. #include "ADS8344.h"
  20. /*
  21. ****************************************************************************************
  22. * CONSTANTS
  23. ****************************************************************************************
  24. */
  25. #define ADS8344MASK_S 0x80
  26. #define ADS8344MASK_SGL 0x04
  27. /*
  28. ****************************************************************************************
  29. * LOVAL OBJECTS
  30. ****************************************************************************************
  31. */
  32. static uint8_t _spi_rb_default(void){
  33. return 0;}
  34. static void _spi_wb_default(uint8_t wb){
  35. (void)wb;}
  36. static void _nullFunc(void){
  37. }
  38. static uint8_t (*_spiRead)(void) = _spi_rb_default;
  39. static void (*_spiWrite)(uint8_t wb) = _spi_wb_default;
  40. static void(*_csSel)(void) = _nullFunc;
  41. static void(*_csDesel)(void) = _nullFunc;
  42. /*
  43. ****************************************************************************************
  44. * INTERFACE IMPLEMENTATIONS
  45. ****************************************************************************************
  46. */
  47. void ADS8344_regFuncSPI(uint8_t (*spi_rb)(void), void (*spi_wb)(uint8_t wb)){
  48. _spiRead = spi_rb;
  49. _spiWrite = spi_wb;
  50. }
  51. void ADS8344_regFuncCS(void(*cs_sel)(void), void(*cs_desel)(void)){
  52. _csSel = cs_sel;
  53. _csDesel = cs_desel;
  54. }
  55. uint16_t ADS8344_sampleChannelSGLEXT(uint8_t ch){
  56. uint16_t ret;
  57. _csSel();
  58. // write control byte
  59. _spiWrite(ADS8344MASK_S | (ch << 4) | ADS8344MASK_SGL | ADS8344PD_ExternalClk);
  60. ret = _spiRead() << 8;
  61. ret |= _spiRead();
  62. ret <<= 1;
  63. ret |= (_spiRead() >> 7);
  64. _csDesel();
  65. return ret;
  66. }

单元测试代码就不放了。

使用及示例代码

没多少代码,很简单的一个模块。
使用的步骤就是,先按要求实现SPI相关接口并进行注册,让模块知道怎么调用SPI。然后再使用ADS8344_sampleChannelSGLEXT接口获得采样值就行了。

  1. #include <stdio.h>
  2. #include "SPI.h"
  3. #include "ADS8344.h"
  4. // 已隐去不重要部分
  5. #define ADS8344_SPI SPI2
  6. #define ADS8344_CSDDR DDRH_DDRH7
  7. #define ADS8344_CS PTH_PTH7
  8. static void Delay(void);
  9. static void INIT_PLL(void);
  10. static uint8_t ADS8344_readByte(void){
  11. return SPI_ExchangeChar(ADS8344_SPI, 0x00);
  12. }
  13. static void ADS8344_writeByte(uint8_t b){
  14. SPI_ExchangeChar(ADS8344_SPI, b);
  15. }
  16. static void ADS8344_SelectCS(void){
  17. ADS8344_CS = 0;
  18. }
  19. static void ADS8344_DeselectCS(void){
  20. ADS8344_CS = 1;
  21. }
  22. void main(void) {
  23. // 已隐去不重要部分
  24. ADS8344_CSDDR = 1;
  25. MODRR_MODRR6 = 1; // route the SPI2 to port H
  26. // 初始化SPI
  27. (void)SPI_Init(ADS8344_SPI);
  28. (void)SPI_Enable(ADS8344_SPI);
  29. // 注册SPI相关接口给模块使用
  30. ADS8344_regFuncSPI(ADS8344_readByte,ADS8344_writeByte);
  31. ADS8344_regFuncCS(ADS8344_SelectCS,ADS8344_DeselectCS);
  32. for(;;) {
  33. Delay();
  34. // 采样一次通道CH1并打印
  35. printf("%5u\r\n", ADS8344_sampleChannelSGLEXT(ADS8344SGL_CH1));
  36. }
  37. }

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpbl9zdHJvbmc_size_16_color_FFFFFF_t_70 4

发表评论

表情:
评论列表 (有 0 条评论,546人围观)

还没有评论,来说两句吧...

相关阅读