发个参考网络加自己修改的 STM32+ADS1248+PT100代码,仅供参考~

2019-12-09 19:13发布

本帖最后由 68336016 于 2014-10-7 19:53 编辑

头文件部分宏定义参考了网络内容,其它代码自己写的,仅供没用过ADS1248的朋友参考

仅供参考……


用STM32硬件SPI驱动ADS1248,读出了AD值,怎么使用自己稍微看下文档就知道了。
因为我没有买到精密的Rcomp,目前测的电压直接就是PT100的电压,PGA增益只能设置小些。
读数还算稳定,温度小数点后第二位有时会跳动,我暂时是能用就行,没时间多折腾。

头文件里的宏定义#define USE_INTER_VREF决定是用   ADS1248内部2.048V  还是  恒流源在Rbias上的压降 当作AD基准源。

如果想用内部基准源,Rcomp(R41,R44,R46,R47)必须得用精密电阻,Rbias(R42)无所谓。
但是要考虑内部基准源精度及稳定性,还有内部恒流源不一定准确。

如果用恒流源在Rbias上的压降当作AD基准源,Rcomp,Rbias都得精密电阻。
AD值只跟Rbias有比例关系,只需要买一个精密电阻,理论上更省事。

我的原理图
QQ截图20141007193106.jpg (310.46 KB, 下载次数: 3) 下载附件 2014-10-7 19:31 上传

头文件
  1. /*******************************************************************************
  2. * 文件名        : ads1248.h
  3. * 作  者        :
  4. * 版  本        :
  5. * 日  期        : 2014-10-04
  6. * 描  述        : ADS1248
  7. *******************************************************************************/

  8. /* Define to prevent recursive inclusion -------------------------------------*/
  9. #ifndef __ADS1248_H
  10. #define __ADS1248_H

  11. /* Includes ------------------------------------------------------------------*/
  12. #include "stm32f10x_lib.h"

  13. //#define USE_INTER_VREF
  14. /************************************************************************/
  15. /*  ads1248 register map                                                */
  16. /************************************************************************/
  17. #define ADS_MUX0                 0X00
  18. #define ADS_VBIAS                 0X01
  19. #define ADS_MUX1                 0X02
  20. #define ADS_SYS0                 0X03
  21. #define ADS_OFC0                 0X04
  22. #define ADS_OFC1                 0X05
  23. #define ADS_OFC2                 0X06
  24. #define ADS_FSC0                 0X07
  25. #define ADS_FSC1                 0X08
  26. #define ADS_FSC2                 0X09
  27. #define ADS_IDAC0                 0X0A
  28. #define ADS_IDAC1                 0X0B
  29. #define ADS_GPIOCFG         0X0C
  30. #define ADS_GPIODIR         0X0D
  31. #define ADS_GPIODAT         0X0E

  32. /************************************************************************/
  33. /* ads1248 SPI commands                                                 */
  34. /************************************************************************/
  35. #define ADS_WAKEUP                    0x00
  36. #define ADS_SLEEP                    0x02
  37. #define ADS_SYNC                    0x04
  38. #define ADS_RESET                    0x06
  39. #define ADS_NOP                            0xFF
  40. #define ADS_RDATA                    0x12
  41. #define ADS_RDATAC                    0x14
  42. #define ADS_SDATAC                    0x16
  43. #define ADS_RREG                    0x20
  44. #define ADS_WREG                    0x40
  45. #define ADS_SYSOCAL                    0x60
  46. #define ADS_SYSGCAL                    0x61
  47. #define ADS_SELFOCAL            0x62

  48. /************************************************************************/
  49. /* ads1248 macroinstruction                                             */
  50. /************************************************************************/
  51. // about MUX0: Multiplexer Control Register 0
  52. #define  P_AIN0                            0x00
  53. #define  P_AIN1                            0x08
  54. #define  P_AIN2                            0x10
  55. #define  P_AIN3                            0x18
  56. #define  P_AIN4                            0x20
  57. #define  P_AIN5                            0x28
  58. #define  P_AIN6                            0x30
  59. #define  P_AIN7                            0x38
  60. #define  N_AIN0                            0x00
  61. #define  N_AIN1                            0x01
  62. #define  N_AIN2                            0x02
  63. #define  N_AIN3                            0x03
  64. #define  N_AIN4                            0x04
  65. #define  N_AIN5                            0x05
  66. #define  N_AIN6                            0x06
  67. #define  N_AIN7                            0x07

  68. // about MUX1: Multiplexer Control Register 1
  69. #define  CLK_Inter                                 0x00
  70. #define  CLK_Exter                                 0x80
  71. #define  REF_Inter_AlwaysOn                0x20  // selecte internal reference and always open  
  72. #define  REF_Inter_AlwaysOff        0x00  // selecte internal reference and always off
  73. #define         SELT_REF0                                0x00                               
  74. #define  SELT_REF1                                0x08
  75. #define  SELT_Inter                                0x10
  76. #define  SELT_Inter_REF0                0x18

  77. // about SYS0 : System Control Register 0
  78. #define         PGAGain_1                         0x00
  79. #define         PGAGain_2                         0x10
  80. #define         PGAGain_4                         0x20
  81. #define         PGAGain_8                         0x30
  82. #define         PGAGain_16                        0x40
  83. #define         PGAGain_32                        0x50
  84. #define         PGAGain_64                        0x60
  85. #define         PGAGain_128                 0x70
  86. #define         DataRate_5                        0x00
  87. #define         DataRate_10                0x01
  88. #define         DataRate_20                0x02
  89. #define         DataRate_40                0x03
  90. #define         DataRate_80                0x04
  91. #define         DataRate_160                0x05
  92. #define         DataRate_320                0x06
  93. #define         DataRate_640                0x07
  94. #define         DataRate_1000                0x08
  95. #define         DataRate_2000                0x09

  96. // about IDAC0: IDAC Control Register 0
  97. #define  Drdy_Mode_EN                 0x08
  98. #define  Drdy_Mode_DIS                 0x00

  99. // the magnitude of the excitation current.
  100. // The IDACs require the internal reference to be on.
  101. #define  IMAG_Off                         0x00
  102. #define  IMAG_50                        0x01
  103. #define  IMAG_100                        0x02
  104. #define  IMAG_250                        0x03
  105. #define  IMAG_500                        0x04
  106. #define  IMAG_750                        0x05
  107. #define  IMAG_1000                        0x06
  108. #define  IMAG_1500                        0x07

  109. // about IDAC1: IDAC Control Register 1
  110. // select the output pin for the first current source DAC.
  111. #define         IDAC1_AIN0                        0x00
  112. #define         IDAC1_AIN1                        0x10
  113. #define         IDAC1_AIN2                        0x20
  114. #define         IDAC1_AIN3                        0x30
  115. #define         IDAC1_AIN4                        0x40
  116. #define         IDAC1_AIN5                        0x50
  117. #define         IDAC1_AIN6                        0x60
  118. #define         IDAC1_AIN7                        0x70
  119. #define         IDAC1_IEXT1                0x80
  120. #define         IDAC1_IEXT2                0x90
  121. #define         IDAC1_OFF                        0xC0

  122. // select the output pin for the second current source DAC.
  123. #define         IDAC2_AIN0                        0x00
  124. #define         IDAC2_AIN1                        0x01
  125. #define         IDAC2_AIN2                        0x02
  126. #define         IDAC2_AIN3                        0x03
  127. #define         IDAC2_AIN4                        0x04
  128. #define         IDAC2_AIN5                        0x05
  129. #define         IDAC2_AIN6                        0x06
  130. #define         IDAC2_AIN7                        0x07
  131. #define         IDAC2_IEXT1                0x08
  132. #define         IDAC2_IEXT2                0x09
  133. #define         IDAC2_OFF                        0x0C

  134. void ADS1248_Init(void);
  135. void ADS1248_Config(void);
  136. u8 ADS1248_ReadByte(void);
  137. u8 ADS1248_WriteByte(u8 data);
  138. void ADS1248_SetIDAC(u8 idac1, u8 idac2, u8 idacImage);
  139. void ADS1248_SetPGAGainAndDataRate(u8 pgaGain, u8 dataRate);
  140. void ADS1248_SetInputChannel(u8 positiveChannel, u8 negativeChannel);
  141. void ADS1248_SetReference(u8 interVrefOnOff, u8 refSelected);
  142. s32 ADS1248_ReadADC();
  143. float ADS1248_ResToTemp(float resistance);
  144. #endif
复制代码

源文件
  1. /*******************************************************************************
  2. * 文件名        : ads1248.c
  3. * 作  者        :
  4. * 版  本        :
  5. * 日  期        : 2014-10-04
  6. * 描  述        : ADS1248
  7. *******************************************************************************/

  8. /* Includes ------------------------------------------------------------------*/
  9. #include "ads1248.h"
  10. /* Private typedef -----------------------------------------------------------*/
  11. /* Private define ------------------------------------------------------------*/
  12. #define ADS1248_CS_PORT                GPIOA        //片选脚所在端口
  13. #define ADS1248_PORT                GPIOB        //其它脚所在端口

  14. #define ADS1248_SPI                        SPI3

  15. #define ADS1248_CS                        GPIO_Pin_15
  16. #define ADS1248_SCK                        GPIO_Pin_3
  17. #define ADS1248_DO                        GPIO_Pin_4
  18. #define ADS1248_DIN                        GPIO_Pin_5

  19. #define ADS1248_RST                        GPIO_Pin_6
  20. #define ADS1248_START                GPIO_Pin_8
  21. #define ADS1248_READY                GPIO_Pin_9

  22. #define RCC_APB2Periph_ADS1248_CS_PORT                RCC_APB2Periph_GPIOA
  23. #define RCC_APB2Periph_ADS1248_PORT                        RCC_APB2Periph_GPIOB
  24. #define RCC_APB1Periph_ADS1248_SPI                        RCC_APB1Periph_SPI3

  25. #define ADS1248_CS_H()                        ADS1248_CS_PORT->BSRR = ADS1248_CS
  26. #define ADS1248_CS_L()                        ADS1248_CS_PORT->BRR = ADS1248_CS

  27. #define ADS1248_RST_H()                        ADS1248_PORT->BSRR = ADS1248_RST
  28. #define ADS1248_RST_L()                        ADS1248_PORT->BRR = ADS1248_RST

  29. #define ADS1248_START_H()            ADS1248_PORT->BSRR = ADS1248_START
  30. #define ADS1248_START_L()            ADS1248_PORT->BRR = ADS1248_START

  31. #define ADS1248_READY_PIN()                ADS1248_PORT->IDR & ADS1248_READY //低电平表示READY

  32. /* Private macro -------------------------------------------------------------*/
  33. /* Private variables ---------------------------------------------------------*/
  34. const float pt100_table[1001] =/*PT100 0-100度对应的电阻值,0.1度间隔*/
  35. {
  36.     100.0000, 100.0391, 100.0782, 100.1172, 100.1563, 100.1954, 100.2345, 100.2736, 100.3126, 100.3517,
  37.     100.3908, 100.4298, 100.4689, 100.5080, 100.5470, 100.5861, 100.6252, 100.6642, 100.7033, 100.7424,
  38.     100.7814, 100.8205, 100.8595, 100.8986, 100.9377, 100.9767, 101.0158, 101.0548, 101.0939, 101.1329,
  39.     101.1720, 101.2110, 101.2501, 101.2891, 101.3282, 101.3672, 101.4062, 101.4453, 101.4843, 101.5234,
  40.     101.5624, 101.6014, 101.6405, 101.6795, 101.7185, 101.7576, 101.7966, 101.8356, 101.8747, 101.9137,
  41.     101.9527, 101.9917, 102.0308, 102.0698, 102.1088, 102.1478, 102.1868, 102.2259, 102.2649, 102.3039,
  42.     102.3429, 102.3819, 102.4209, 102.4599, 102.4989, 102.5380, 102.5770, 102.6160, 102.6550, 102.6940,
  43.     102.7330, 102.7720, 102.8110, 102.8500, 102.8890, 102.9280, 102.9670, 103.0060, 103.0450, 103.0840,
  44.     103.1229, 103.1619, 103.2009, 103.2399, 103.2789, 103.3179, 103.3569, 103.3958, 103.4348, 103.4738,
  45.     103.5128, 103.5518, 103.5907, 103.6297, 103.6687, 103.7077, 103.7466, 103.7856, 103.8246, 103.8636,
  46.     103.9025, 103.9415, 103.9805, 104.0194, 104.0584, 104.0973, 104.1363, 104.1753, 104.2142, 104.2532,
  47.     104.2921, 104.3311, 104.3701, 104.4090, 104.4480, 104.4869, 104.5259, 104.5648, 104.6038, 104.6427,
  48.     104.6816, 104.7206, 104.7595, 104.7985, 104.8374, 104.8764, 104.9153, 104.9542, 104.9932, 105.0321,
  49.     105.0710, 105.1100, 105.1489, 105.1878, 105.2268, 105.2657, 105.3046, 105.3435, 105.3825, 105.4214,
  50.     105.4603, 105.4992, 105.5381, 105.5771, 105.6160, 105.6549, 105.6938, 105.7327, 105.7716, 105.8105,
  51.     105.8495, 105.8884, 105.9273, 105.9662, 106.0051, 106.0440, 106.0829, 106.1218, 106.1607, 106.1996,
  52.     106.2385, 106.2774, 106.3163, 106.3552, 106.3941, 106.4330, 106.4719, 106.5108, 106.5496, 106.5885,
  53.     106.6274, 106.6663, 106.7052, 106.7441, 106.7830, 106.8218, 106.8607, 106.8996, 106.9385, 106.9774,
  54.     107.0162, 107.0551, 107.0940, 107.1328, 107.1717, 107.2106, 107.2495, 107.2883, 107.3272, 107.3661,
  55.     107.4049, 107.4438, 107.4826, 107.5215, 107.5604, 107.5992, 107.6381, 107.6769, 107.7158, 107.7546,
  56.     107.7935, 107.8324, 107.8712, 107.9101, 107.9489, 107.9877, 108.0266, 108.0654, 108.1043, 108.1431,
  57.     108.1820, 108.2208, 108.2596, 108.2985, 108.3373, 108.3762, 108.4150, 108.4538, 108.4926, 108.5315,
  58.     108.5703, 108.6091, 108.6480, 108.6868, 108.7256, 108.7644, 108.8033, 108.8421, 108.8809, 108.9197,
  59.     108.9585, 108.9974, 109.0362, 109.0750, 109.1138, 109.1526, 109.1914, 109.2302, 109.2690, 109.3078,
  60.     109.3467, 109.3855, 109.4243, 109.4631, 109.5019, 109.5407, 109.5795, 109.6183, 109.6571, 109.6959,
  61.     109.7347, 109.7734, 109.8122, 109.8510, 109.8898, 109.9286, 109.9674, 110.0062, 110.0450, 110.0838,
  62.     110.1225, 110.1613, 110.2001, 110.2389, 110.2777, 110.3164, 110.3552, 110.3940, 110.4328, 110.4715,
  63.     110.5103, 110.5491, 110.5879, 110.6266, 110.6654, 110.7042, 110.7429, 110.7817, 110.8204, 110.8592,
  64.     110.8980, 110.9367, 110.9755, 111.0142, 111.0530, 111.0917, 111.1305, 111.1693, 111.2080, 111.2468,
  65.     111.2855, 111.3242, 111.3630, 111.4017, 111.4405, 111.4792, 111.5180, 111.5567, 111.5954, 111.6342,
  66.     111.6729, 111.7117, 111.7504, 111.7891, 111.8279, 111.8666, 111.9053, 111.9441, 111.9828, 112.0215,
  67.     112.0602, 112.0990, 112.1377, 112.1764, 112.2151, 112.2538, 112.2926, 112.3313, 112.3700, 112.4087,
  68.     112.4474, 112.4861, 112.5248, 112.5636, 112.6023, 112.6410, 112.6797, 112.7184, 112.7571, 112.7958,
  69.     112.8345, 112.8732, 112.9119, 112.9506, 112.9893, 113.0280, 113.0667, 113.1054, 113.1441, 113.1828,
  70.     113.2215, 113.2602, 113.2988, 113.3375, 113.3762, 113.4149, 113.4536, 113.4923, 113.5309, 113.5696,
  71.     113.6083, 113.6470, 113.6857, 113.7243, 113.7630, 113.8017, 113.8404, 113.8790, 113.9177, 113.9564,
  72.     113.9950, 114.0337, 114.0724, 114.1110, 114.1497, 114.1884, 114.2270, 114.2657, 114.3043, 114.3430,
  73.     114.3817, 114.4203, 114.4590, 114.4976, 114.5363, 114.5749, 114.6136, 114.6522, 114.6909, 114.7295,
  74.     114.7681, 114.8068, 114.8454, 114.8841, 114.9227, 114.9614, 115.0000, 115.0386, 115.0773, 115.1159,
  75.     115.1545, 115.1932, 115.2318, 115.2704, 115.3091, 115.3477, 115.3863, 115.4249, 115.4636, 115.5022,
  76.     115.5408, 115.5794, 115.6180, 115.6567, 115.6953, 115.7339, 115.7725, 115.8111, 115.8497, 115.8883,
  77.     115.9270, 115.9656, 116.0042, 116.0428, 116.0814, 116.1200, 116.1586, 116.1972, 116.2358, 116.2744,
  78.     116.3130, 116.3516, 116.3902, 116.4288, 116.4674, 116.5060, 116.5446, 116.5831, 116.6217, 116.6603,
  79.     116.6989, 116.7375, 116.7761, 116.8147, 116.8532, 116.8918, 116.9304, 116.9690, 117.0076, 117.0461,
  80.     117.0847, 117.1233, 117.1619, 117.2004, 117.2390, 117.2776, 117.3161, 117.3547, 117.3933, 117.4318,
  81.     117.4704, 117.5090, 117.5475, 117.5861, 117.6247, 117.6632, 117.7018, 117.7403, 117.7789, 117.8174,
  82.     117.8560, 117.8945, 117.9331, 117.9716, 118.0102, 118.0487, 118.0873, 118.1258, 118.1644, 118.2029,
  83.     118.2414, 118.2800, 118.3185, 118.3571, 118.3956, 118.4341, 118.4727, 118.5112, 118.5497, 118.5883,
  84.     118.6268, 118.6653, 118.7038, 118.7424, 118.7809, 118.8194, 118.8579, 118.8965, 118.9350, 118.9735,
  85.     119.0120, 119.0505, 119.0890, 119.1276, 119.1661, 119.2046, 119.2431, 119.2816, 119.3201, 119.3586,
  86.     119.3971, 119.4356, 119.4741, 119.5126, 119.5511, 119.5896, 119.6281, 119.6666, 119.7051, 119.7436,
  87.     119.7821, 119.8206, 119.8591, 119.8976, 119.9361, 119.9746, 120.0131, 120.0516, 120.0900, 120.1285,
  88.     120.1670, 120.2055, 120.2440, 120.2824, 120.3209, 120.3594, 120.3979, 120.4364, 120.4748, 120.5133,
  89.     120.5518, 120.5902, 120.6287, 120.6672, 120.7056, 120.7441, 120.7826, 120.8210, 120.8595, 120.8980,
  90.     120.9364, 120.9749, 121.0133, 121.0518, 121.0902, 121.1287, 121.1672, 121.2056, 121.2441, 121.2825,
  91.     121.3210, 121.3594, 121.3978, 121.4363, 121.4747, 121.5132, 121.5516, 121.5901, 121.6285, 121.6669,
  92.     121.7054, 121.7438, 121.7822, 121.8207, 121.8591, 121.8975, 121.9360, 121.9744, 122.0128, 122.0513,
  93.     122.0897, 122.1281, 122.1665, 122.2049, 122.2434, 122.2818, 122.3202, 122.3586, 122.3970, 122.4355,
  94.     122.4739, 122.5123, 122.5507, 122.5891, 122.6275, 122.6659, 122.7043, 122.7427, 122.7811, 122.8195,
  95.     122.8579, 122.8963, 122.9347, 122.9731, 123.0115, 123.0499, 123.0883, 123.1267, 123.1651, 123.2035,
  96.     123.2419, 123.2803, 123.3187, 123.3571, 123.3955, 123.4338, 123.4722, 123.5106, 123.5490, 123.5874,
  97.     123.6257, 123.6641, 123.7025, 123.7409, 123.7792, 123.8176, 123.8560, 123.8944, 123.9327, 123.9711,
  98.     124.0095, 124.0478, 124.0862, 124.1246, 124.1629, 124.2013, 124.2396, 124.2780, 124.3164, 124.3547,
  99.     124.3931, 124.4314, 124.4698, 124.5081, 124.5465, 124.5848, 124.6232, 124.6615, 124.6999, 124.7382,
  100.     124.7766, 124.8149, 124.8533, 124.8916, 124.9299, 124.9683, 125.0066, 125.0450, 125.0833, 125.1216,
  101.     125.1600, 125.1983, 125.2366, 125.2749, 125.3133, 125.3516, 125.3899, 125.4283, 125.4666, 125.5049,
  102.     125.5432, 125.5815, 125.6199, 125.6582, 125.6965, 125.7348, 125.7731, 125.8114, 125.8497, 125.8881,
  103.     125.9264, 125.9647, 126.0030, 126.0413, 126.0796, 126.1179, 126.1562, 126.1945, 126.2328, 126.2711,
  104.     126.3094, 126.3477, 126.3860, 126.4243, 126.4626, 126.5009, 126.5392, 126.5775, 126.6157, 126.6540,
  105.     126.6923, 126.7306, 126.7689, 126.8072, 126.8455, 126.8837, 126.9220, 126.9603, 126.9986, 127.0368,
  106.     127.0751, 127.1134, 127.1517, 127.1899, 127.2282, 127.2665, 127.3048, 127.3430, 127.3813, 127.4195,
  107.     127.4578, 127.4961, 127.5343, 127.5726, 127.6109, 127.6491, 127.6874, 127.7256, 127.7639, 127.8021,
  108.     127.8404, 127.8786, 127.9169, 127.9551, 127.9934, 128.0316, 128.0699, 128.1081, 128.1464, 128.1846,
  109.     128.2228, 128.2611, 128.2993, 128.3376, 128.3758, 128.4140, 128.4523, 128.4905, 128.5287, 128.5670,
  110.     128.6052, 128.6434, 128.6816, 128.7199, 128.7581, 128.7963, 128.8345, 128.8728, 128.9110, 128.9492,
  111.     128.9874, 129.0256, 129.0638, 129.1021, 129.1403, 129.1785, 129.2167, 129.2549, 129.2931, 129.3313,
  112.     129.3695, 129.4077, 129.4459, 129.4841, 129.5223, 129.5605, 129.5987, 129.6369, 129.6751, 129.7133,
  113.     129.7515, 129.7897, 129.8279, 129.8661, 129.9043, 129.9425, 129.9807, 130.0188, 130.0570, 130.0952,
  114.     130.1334, 130.1716, 130.2098, 130.2479, 130.2861, 130.3243, 130.3625, 130.4006, 130.4388, 130.4770,
  115.     130.5152, 130.5533, 130.5915, 130.6297, 130.6678, 130.7060, 130.7442, 130.7823, 130.8205, 130.8586,
  116.     130.8968, 130.9350, 130.9731, 131.0113, 131.0494, 131.0876, 131.1257, 131.1639, 131.2020, 131.2402,
  117.     131.2783, 131.3165, 131.3546, 131.3928, 131.4309, 131.4691, 131.5072, 131.5453, 131.5835, 131.6216,
  118.     131.6597, 131.6979, 131.7360, 131.7742, 131.8123, 131.8504, 131.8885, 131.9267, 131.9648, 132.0029,
  119.     132.0411, 132.0792, 132.1173, 132.1554, 132.1935, 132.2317, 132.2698, 132.3079, 132.3460, 132.3841,
  120.     132.4222, 132.4603, 132.4985, 132.5366, 132.5747, 132.6128, 132.6509, 132.6890, 132.7271, 132.7652,
  121.     132.8033, 132.8414, 132.8795, 132.9176, 132.9557, 132.9938, 133.0319, 133.0700, 133.1081, 133.1462,
  122.     133.1843, 133.2224, 133.2604, 133.2985, 133.3366, 133.3747, 133.4128, 133.4509, 133.4889, 133.5270,
  123.     133.5651, 133.6032, 133.6413, 133.6793, 133.7174, 133.7555, 133.7935, 133.8316, 133.8697, 133.9078,
  124.     133.9458, 133.9839, 134.0220, 134.0600, 134.0981, 134.1361, 134.1742, 134.2123, 134.2503, 134.2884,
  125.     134.3264, 134.3645, 134.4025, 134.4406, 134.4786, 134.5167, 134.5547, 134.5928, 134.6308, 134.6689,
  126.     134.7069, 134.7450, 134.7830, 134.8211, 134.8591, 134.8971, 134.9352, 134.9732, 135.0112, 135.0493,
  127.     135.0873, 135.1253, 135.1634, 135.2014, 135.2394, 135.2774, 135.3155, 135.3535, 135.3915, 135.4295,
  128.     135.4676, 135.5056, 135.5436, 135.5816, 135.6196, 135.6577, 135.6957, 135.7337, 135.7717, 135.8097,
  129.     135.8477, 135.8857, 135.9237, 135.9617, 135.9997, 136.0377, 136.0757, 136.1137, 136.1517, 136.1897,
  130.     136.2277, 136.2657, 136.3037, 136.3417, 136.3797, 136.4177, 136.4557, 136.4937, 136.5317, 136.5697,
  131.     136.6077, 136.6456, 136.6836, 136.7216, 136.7596, 136.7976, 136.8355, 136.8735, 136.9115, 136.9495,
  132.     136.9875, 137.0254, 137.0634, 137.1014, 137.1393, 137.1773, 137.2153, 137.2532, 137.2912, 137.3292,
  133.     137.3671, 137.4051, 137.4431, 137.4810, 137.5190, 137.5569, 137.5949, 137.6329, 137.6708, 137.7088,
  134.     137.7467, 137.7847, 137.8226, 137.8606, 137.8985, 137.9365, 137.9744, 138.0123, 138.0503, 138.0882,
  135.     138.1262, 138.1641, 138.2020, 138.2400, 138.2779, 138.3158, 138.3538, 138.3917, 138.4296, 138.4676,
  136.     138.5055
  137. };

  138. /* Private function prototypes -----------------------------------------------*/
  139. /* Private functions ---------------------------------------------------------*/

  140. /*******************************************************************************
  141. * @Function Name: ADS1248_Init
  142. * @Description  : ADS1248初始化
  143. * @param                : void
  144. * @retval                   : void
  145. * @date                           : 2014/10/04
  146. *******************************************************************************/
  147. void ADS1248_Init(void)
  148. {
  149.     ADS1248_Config(); // 配置SPI,GPIO和RCC

  150.     ADS1248_RST_L();
  151.     ADS1248_START_L();
  152.     Delay(4);
  153.     ADS1248_RST_H();
  154.     ADS1248_START_H(); //连续
  155.     Delay(20);
  156.     ADS1248_CS_L();

  157.     ADS1248_SetInputChannel(P_AIN0, N_AIN1);

  158. #ifdef  USE_INTER_VREF
  159.     ADS1248_SetReference(REF_Inter_AlwaysOn, SELT_Inter);            // 设置内部基准为参考源
  160. #else  /*   USE REF0  */
  161.     ADS1248_SetReference(REF_Inter_AlwaysOn, SELT_REF0);            // 设置外部REF0为参考源
  162. #endif

  163.     Delay(10);
  164.     ADS1248_SetIDAC(IDAC1_AIN0, IDAC2_AIN1, IMAG_1000);                        // 设置电流源
  165.     ADS1248_SetPGAGainAndDataRate(PGAGain_8, DataRate_20);                // 设置PGA倍数和传输速率
  166. }

  167. /*******************************************************************************
  168. * @Function Name: ADS1248_Config
  169. * @Description  : SPI和GPIO初始化
  170. * @param                : void
  171. * @retval                   : void
  172. * @date                           : 2014/10/04
  173. *******************************************************************************/
  174. void ADS1248_Config(void)
  175. {
  176.     SPI_InitTypeDef  SPI_InitStructure;
  177.     GPIO_InitTypeDef GPIO_InitStructure;

  178.     /* Enable SPI and GPIO clocks */
  179.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADS1248_CS_PORT, ENABLE);
  180.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADS1248_PORT, ENABLE);
  181.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_ADS1248_SPI, ENABLE);

  182.     /* Configure SPI pins: SCK, MISO and MOSI */
  183.     GPIO_InitStructure.GPIO_Pin = ADS1248_SCK | ADS1248_DO | ADS1248_DIN;
  184.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  185.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  186.     GPIO_Init(ADS1248_PORT, &GPIO_InitStructure);

  187.     /* Configure ADS1248 Control Pins: RST, START */
  188.     GPIO_InitStructure.GPIO_Pin = ADS1248_RST | ADS1248_START;
  189.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  190.     GPIO_Init(ADS1248_PORT, &GPIO_InitStructure);

  191.     /* Configure I/O for ADS1248 Chip select */
  192.     GPIO_InitStructure.GPIO_Pin = ADS1248_CS;
  193.     GPIO_Init(ADS1248_CS_PORT, &GPIO_InitStructure);

  194.     /* Configure ADS1248 Status Pins: READY */
  195.     GPIO_InitStructure.GPIO_Pin =  ADS1248_READY;
  196.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  197.     GPIO_Init(ADS1248_PORT, &GPIO_InitStructure);

  198.     /* Deselect the FLASH: Chip Select high */
  199.     ADS1248_CS_H();

  200.     /* SPI configuration 初始化阶段*/
  201.     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  202.     SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  203.     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  204.     SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  205.     SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  206.     SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  207.     SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; //PCLK1/32=24/32=0.75MHz,ADS1248的SCLK最快约2MHz
  208.     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  209.     SPI_InitStructure.SPI_CRCPolynomial = 7;
  210.     SPI_Init(ADS1248_SPI, &SPI_InitStructure);

  211.     /* Enable SPI  */
  212.     SPI_Cmd(ADS1248_SPI, ENABLE);
  213. }

  214. /*******************************************************************************
  215. * @Function Name: ADS1248_ReadByte
  216. * @Description  : Reads a byte from the SPI Device
  217. * @param                : void
  218. * @retval                   : u8 Byte Read from the SPI Device
  219. * @date                           : 2014/10/04
  220. *******************************************************************************/
  221. u8 ADS1248_ReadByte(void)
  222. {
  223.     return (ADS1248_WriteByte(ADS_NOP));
  224. }

  225. /*******************************************************************************
  226. * @Function Name: ADS1248_WriteByte
  227. * @Description  : Sends a byte through the SPI interface and return the byte received from the SPI bus.
  228. * @param                : u8 data byte to send
  229. * @retval                   : u8 The value of the received byte
  230. * @date                           : 2014/10/04
  231. *******************************************************************************/
  232. u8 ADS1248_WriteByte(u8 data)
  233. {
  234.     /* Loop while DR register in not emplty */
  235.     while(SPI_I2S_GetFlagStatus(ADS1248_SPI, SPI_I2S_FLAG_TXE) == RESET);

  236.     /* Send u8 through the SPI bus peripheral */
  237.     SPI_I2S_SendData(ADS1248_SPI, data);

  238.     /* Wait to receive a u8 */
  239.     while(SPI_I2S_GetFlagStatus(ADS1248_SPI, SPI_I2S_FLAG_RXNE) == RESET);

  240.     /* Return the u8 read from the SPI bus */
  241.     return SPI_I2S_ReceiveData(ADS1248_SPI);
  242. }

  243. /*******************************************************************************
  244. * @Function Name: ADS1248_SetIDAC
  245. * @Description  :
  246. * @param                : u8 idac1
  247. * @param                : u8 idac2
  248. * @param                : u8 idacImage
  249. * @retval                   : void
  250. * @date                           : 2014/10/04
  251. *******************************************************************************/
  252. void ADS1248_SetIDAC(u8 idac1, u8 idac2, u8 idacImage)
  253. {
  254.     ADS1248_WriteByte(ADS_WREG | ADS_IDAC0);
  255.     ADS1248_WriteByte(0x01);
  256.     ADS1248_WriteByte(idacImage);
  257.     ADS1248_WriteByte(idac1 | idac2);
  258. }

  259. /*******************************************************************************
  260. * @Function Name: ADS1248_SetPGAGainAndDataRate
  261. * @Description  : Set PGA gain and data rate (SPS)
  262. * @param                : u8 pgaGain
  263. * @param                : u8 dataRate
  264. * @retval                   : void
  265. * @date                           : 2014/10/04
  266. *******************************************************************************/
  267. void ADS1248_SetPGAGainAndDataRate(u8 pgaGain, u8 dataRate)
  268. {
  269.     ADS1248_WriteByte(ADS_WREG | ADS_SYS0);
  270.     ADS1248_WriteByte(0x00);
  271.     ADS1248_WriteByte(pgaGain | dataRate);
  272. }

  273. /*******************************************************************************
  274. * @Function Name: ADS1248_SetInputChannel
  275. * @Description  :
  276. * @param                : u8 positiveChannel
  277. * @param                : u8 negativeChannel
  278. * @retval                   : void
  279. * @date                           : 2014/10/04
  280. *******************************************************************************/
  281. void ADS1248_SetInputChannel(u8 positiveChannel, u8 negativeChannel)
  282. {
  283.     ADS1248_WriteByte(ADS_WREG | ADS_MUX0);
  284.     ADS1248_WriteByte(0x00);
  285.     ADS1248_WriteByte(positiveChannel | negativeChannel);
  286. }

  287. /*******************************************************************************
  288. * @Function Name: ADS1248_SetReference
  289. * @Description  :
  290. * @param                : u8 interVrefOnOff
  291. * @param                : u8 refSelected
  292. * @retval                   : void
  293. * @date                           : 2014/10/04
  294. *******************************************************************************/
  295. void ADS1248_SetReference(u8 interVrefOnOff, u8 refSelected)
  296. {
  297.     ADS1248_WriteByte(ADS_WREG | ADS_MUX1);
  298.     ADS1248_WriteByte(0x00);
  299.     ADS1248_WriteByte(interVrefOnOff | refSelected);
  300. }

  301. /*******************************************************************************
  302. * @Function Name: ADS1248_ReadREG
  303. * @Description  : 读取寄存器的第byteToRead个字节
  304. * @param                : u8 regName
  305. * @param                : u8 byteToRead
  306. * @retval                   : u8
  307. * @date                           : 2014/10/05
  308. *******************************************************************************/
  309. u8 ADS1248_ReadREG(u8 regName, u8 byteToRead)
  310. {
  311.     ADS1248_WriteByte(ADS_RREG | regName);

  312.     if(byteToRead == 1)
  313.     {
  314.         ADS1248_WriteByte(0x00);
  315.     }
  316.     else if(byteToRead == 2)
  317.     {
  318.         ADS1248_WriteByte(0x01);
  319.         ADS1248_ReadByte();
  320.     }

  321.     return ADS1248_ReadByte();
  322. }

  323. /*******************************************************************************
  324. * @Function Name: ADS1248_ReadADC
  325. * @Description  : 返回有符号数值
  326. * @retval                   : s32
  327. * @date                           : 2014/10/04
  328. *******************************************************************************/
  329. s32 ADS1248_ReadADC()
  330. {
  331.     u8 temp[3];
  332.     s32 adcVal = 0;

  333. //    while(ADS1248_READY_PIN()) //DRDY引脚高电平表示数据没准备好
  334. //    {
  335. //    }

  336.     if(ADS1248_READY_PIN()) //DRDY引脚高电平表示数据没准备好
  337.     {
  338.         return 0;
  339.     }

  340.     ADS1248_WriteByte(ADS_RDATA);
  341.     temp[0] = ADS1248_ReadByte();
  342.     temp[1] = ADS1248_ReadByte();
  343.     temp[2] = ADS1248_ReadByte();

  344.     adcVal = (s32)(temp[0] << 24) + (temp[1] << 16) + (temp[2] << 8); // 左移8位,放大256倍识别正负号

  345.     return (adcVal >> 8); // 右移8位恢复原值
  346. }

  347. /*******************************************************************************
  348. * @Function Name: ADS1248_ResToTemp
  349. * @Description  : 二分法查表,将PT100的电阻值转为对应温度0-100
  350. * @param                : float resistance
  351. * @retval                   : float
  352. * @date                           : 2014/10/06
  353. *******************************************************************************/
  354. float ADS1248_ResToTemp(float resistance)
  355. {
  356.     u16 low = 0;
  357.     u16 mid = sizeof(pt100_table) / sizeof(pt100_table[0]) / 2;
  358.     u16 high = sizeof(pt100_table) / sizeof(pt100_table[0]) - 1;

  359.     if((resistance < pt100_table[0]) || (resistance > pt100_table[high]))
  360.     {
  361.         return -1; // 超限,不做特殊处理
  362.     }

  363.     while(high >= low)
  364.     {
  365.         mid = (high + low) / 2;

  366.         if(resistance >= pt100_table[mid])
  367.         {
  368.             if(resistance < pt100_table[mid + 1])
  369.             {
  370.                 return (0.1 * low + (resistance - pt100_table[mid]) * 0.1 / ((pt100_table[mid + 1]) - pt100_table[mid]));
  371.                 break;
  372.             }
  373.             else
  374.             {
  375.                 low = mid + 1;
  376.             }
  377.         }
  378.         else if(resistance < pt100_table[mid])
  379.         {
  380.             high = mid - 1;
  381.         }
  382.     }
  383. }

  384. /*****************************************END OF FILE**********************************/
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
99条回答
steven0419
1楼-- · 2019-12-17 11:50
不错的资料,谢谢楼主了。
zhcj66
2楼-- · 2019-12-17 16:34
lzADS1248 抗干扰能力怎么样?还有稳定数据读数是多少位,记得当时测试ADS1247读取数据稳定在16bit
--璞--
3楼-- · 2019-12-17 18:43
 精彩回答 2  元偷偷看……
l.htlht
4楼-- · 2019-12-18 00:37
收藏了,多谢楼主。
gzhua20088ssj
5楼-- · 2019-12-18 06:10
楼主这个PT100温度采集看来还不错,没有机会验证,有没有完整的电路图呢
dyl
6楼-- · 2019-12-18 06:26
你好,我想做一个温度检测功能,想用你的方案。老板给的时间太短了,可以买你的方案吗?

一周热门 更多>