ADS1248

来源:互联网 发布:韩宜淘宝旗舰店 编辑:程序博客网 时间:2024/05/19 01:13

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有比例关系,只需要买一个精密电阻,理论上更省事。

我的原理图

/*******************************************************************************

* 文件名        : ads1248.h
* 作  者        :
* 版  本        :
* 日  期        : 2014-10-04
* 描  述        : ADS1248
*******************************************************************************/


/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __ADS1248_H
#define __ADS1248_H


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


//#define USE_INTER_VREF
/************************************************************************/
/*  ads1248 register map                                                */
/************************************************************************/
#define ADS_MUX0                 0X00
#define ADS_VBIAS                 0X01
#define ADS_MUX1                 0X02
#define ADS_SYS0                 0X03
#define ADS_OFC0                 0X04
#define ADS_OFC1                 0X05
#define ADS_OFC2                 0X06
#define ADS_FSC0                 0X07
#define ADS_FSC1                 0X08
#define ADS_FSC2                 0X09
#define ADS_IDAC0                 0X0A
#define ADS_IDAC1                 0X0B
#define ADS_GPIOCFG         0X0C
#define ADS_GPIODIR         0X0D
#define ADS_GPIODAT         0X0E


/************************************************************************/
/* ads1248 SPI commands                                                 */
/************************************************************************/
#define ADS_WAKEUP                    0x00
#define ADS_SLEEP                    0x02
#define ADS_SYNC                    0x04
#define ADS_RESET                    0x06
#define ADS_NOP                            0xFF
#define ADS_RDATA                    0x12
#define ADS_RDATAC                    0x14
#define ADS_SDATAC                    0x16
#define ADS_RREG                    0x20
#define ADS_WREG                    0x40
#define ADS_SYSOCAL                    0x60
#define ADS_SYSGCAL                    0x61
#define ADS_SELFOCAL            0x62


/************************************************************************/
/* ads1248 macroinstruction                                             */
/************************************************************************/
// about MUX0: Multiplexer Control Register 0
#define  P_AIN0                            0x00
#define  P_AIN1                            0x08
#define  P_AIN2                            0x10
#define  P_AIN3                            0x18
#define  P_AIN4                            0x20
#define  P_AIN5                            0x28
#define  P_AIN6                            0x30
#define  P_AIN7                            0x38
#define  N_AIN0                            0x00
#define  N_AIN1                            0x01
#define  N_AIN2                            0x02
#define  N_AIN3                            0x03
#define  N_AIN4                            0x04
#define  N_AIN5                            0x05
#define  N_AIN6                            0x06
#define  N_AIN7                            0x07


// about MUX1: Multiplexer Control Register 1
#define  CLK_Inter                                 0x00
#define  CLK_Exter                                 0x80
#define  REF_Inter_AlwaysOn                0x20  // selecte internal reference and always open  
#define  REF_Inter_AlwaysOff        0x00  // selecte internal reference and always off 
#define         SELT_REF0                                0x00                                
#define  SELT_REF1                                0x08
#define  SELT_Inter                                0x10
#define  SELT_Inter_REF0                0x18


// about SYS0 : System Control Register 0
#define         PGAGain_1                         0x00
#define         PGAGain_2                         0x10
#define         PGAGain_4                         0x20
#define         PGAGain_8                         0x30
#define         PGAGain_16                        0x40
#define         PGAGain_32                        0x50
#define         PGAGain_64                        0x60
#define         PGAGain_128                 0x70
#define         DataRate_5                        0x00
#define         DataRate_10                0x01
#define         DataRate_20                0x02
#define         DataRate_40                0x03
#define         DataRate_80                0x04
#define         DataRate_160                0x05
#define         DataRate_320                0x06
#define         DataRate_640                0x07
#define         DataRate_1000                0x08
#define         DataRate_2000                0x09


// about IDAC0: IDAC Control Register 0
#define  Drdy_Mode_EN                 0x08
#define  Drdy_Mode_DIS                 0x00


// the magnitude of the excitation current.
// The IDACs require the internal reference to be on.
#define  IMAG_Off                         0x00
#define  IMAG_50                        0x01
#define  IMAG_100                        0x02
#define  IMAG_250                        0x03
#define  IMAG_500                        0x04
#define  IMAG_750                        0x05
#define  IMAG_1000                        0x06
#define  IMAG_1500                        0x07


// about IDAC1: IDAC Control Register 1
// select the output pin for the first current source DAC.
#define         IDAC1_AIN0                        0x00
#define         IDAC1_AIN1                        0x10
#define         IDAC1_AIN2                        0x20
#define         IDAC1_AIN3                        0x30
#define         IDAC1_AIN4                        0x40
#define         IDAC1_AIN5                        0x50
#define         IDAC1_AIN6                        0x60
#define         IDAC1_AIN7                        0x70
#define         IDAC1_IEXT1                0x80
#define         IDAC1_IEXT2                0x90
#define         IDAC1_OFF                        0xC0


// select the output pin for the second current source DAC.
#define         IDAC2_AIN0                        0x00
#define         IDAC2_AIN1                        0x01
#define         IDAC2_AIN2                        0x02
#define         IDAC2_AIN3                        0x03
#define         IDAC2_AIN4                        0x04
#define         IDAC2_AIN5                        0x05
#define         IDAC2_AIN6                        0x06
#define         IDAC2_AIN7                        0x07
#define         IDAC2_IEXT1                0x08
#define         IDAC2_IEXT2                0x09
#define         IDAC2_OFF                        0x0C


void ADS1248_Init(void);
void ADS1248_Config(void);
u8 ADS1248_ReadByte(void);
u8 ADS1248_WriteByte(u8 data);
void ADS1248_SetIDAC(u8 idac1, u8 idac2, u8 idacImage);
void ADS1248_SetPGAGainAndDataRate(u8 pgaGain, u8 dataRate);
void ADS1248_SetInputChannel(u8 positiveChannel, u8 negativeChannel);
void ADS1248_SetReference(u8 interVrefOnOff, u8 refSelected);
s32 ADS1248_ReadADC();
float ADS1248_ResToTemp(float resistance);

#endif


/*******************************************************************************
* 文件名        : ads1248.c
* 作  者        :
* 版  本        :
* 日  期        : 2014-10-04
* 描  述        : ADS1248
*******************************************************************************/


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


#define ADS1248_SPI                        SPI3


#define ADS1248_CS                        GPIO_Pin_15
#define ADS1248_SCK                        GPIO_Pin_3
#define ADS1248_DO                        GPIO_Pin_4
#define ADS1248_DIN                        GPIO_Pin_5


#define ADS1248_RST                        GPIO_Pin_6
#define ADS1248_START                GPIO_Pin_8
#define ADS1248_READY                GPIO_Pin_9


#define RCC_APB2Periph_ADS1248_CS_PORT                RCC_APB2Periph_GPIOA
#define RCC_APB2Periph_ADS1248_PORT                        RCC_APB2Periph_GPIOB
#define RCC_APB1Periph_ADS1248_SPI                        RCC_APB1Periph_SPI3


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


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


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


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


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


/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


/*******************************************************************************
* @Function Name: ADS1248_Init
* @Description  : ADS1248初始化
* @param                : void
* @retval                   : void
* @date                           : 2014/10/04
*******************************************************************************/
void ADS1248_Init(void)
{
    ADS1248_Config(); // 配置SPI,GPIO和RCC


    ADS1248_RST_L();
    ADS1248_START_L();
    Delay(4);
    ADS1248_RST_H();
    ADS1248_START_H(); //连续
    Delay(20);
    ADS1248_CS_L();


    ADS1248_SetInputChannel(P_AIN0, N_AIN1);


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


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


/*******************************************************************************
* @Function Name: ADS1248_Config
* @Description  : SPI和GPIO初始化
* @param                : void
* @retval                   : void
* @date                           : 2014/10/04
*******************************************************************************/
void ADS1248_Config(void)
{
    SPI_InitTypeDef  SPI_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;


    /* Enable SPI and GPIO clocks */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADS1248_CS_PORT, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADS1248_PORT, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_ADS1248_SPI, ENABLE);


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


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


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


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


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


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


    /* Enable SPI  */
    SPI_Cmd(ADS1248_SPI, ENABLE);
}


/*******************************************************************************
* @Function Name: ADS1248_ReadByte
* @Description  : Reads a byte from the SPI Device
* @param                : void
* @retval                   : u8 Byte Read from the SPI Device
* @date                           : 2014/10/04
*******************************************************************************/
u8 ADS1248_ReadByte(void)
{
    return (ADS1248_WriteByte(ADS_NOP));
}


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


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


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


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


/*******************************************************************************
* @Function Name: ADS1248_SetIDAC
* @Description  :
* @param                : u8 idac1
* @param                : u8 idac2
* @param                : u8 idacImage
* @retval                   : void
* @date                           : 2014/10/04
*******************************************************************************/
void ADS1248_SetIDAC(u8 idac1, u8 idac2, u8 idacImage)
{
    ADS1248_WriteByte(ADS_WREG | ADS_IDAC0);
    ADS1248_WriteByte(0x01);
    ADS1248_WriteByte(idacImage);
    ADS1248_WriteByte(idac1 | idac2);
}


/*******************************************************************************
* @Function Name: ADS1248_SetPGAGainAndDataRate
* @Description  : Set PGA gain and data rate (SPS)
* @param                : u8 pgaGain
* @param                : u8 dataRate
* @retval                   : void
* @date                           : 2014/10/04
*******************************************************************************/
void ADS1248_SetPGAGainAndDataRate(u8 pgaGain, u8 dataRate)
{
    ADS1248_WriteByte(ADS_WREG | ADS_SYS0);
    ADS1248_WriteByte(0x00);
    ADS1248_WriteByte(pgaGain | dataRate);
}


/*******************************************************************************
* @Function Name: ADS1248_SetInputChannel
* @Description  :
* @param                : u8 positiveChannel
* @param                : u8 negativeChannel
* @retval                   : void
* @date                           : 2014/10/04
*******************************************************************************/
void ADS1248_SetInputChannel(u8 positiveChannel, u8 negativeChannel)
{
    ADS1248_WriteByte(ADS_WREG | ADS_MUX0);
    ADS1248_WriteByte(0x00);
    ADS1248_WriteByte(positiveChannel | negativeChannel);
}


/*******************************************************************************
* @Function Name: ADS1248_SetReference
* @Description  :
* @param                : u8 interVrefOnOff
* @param                : u8 refSelected
* @retval                   : void
* @date                           : 2014/10/04
*******************************************************************************/
void ADS1248_SetReference(u8 interVrefOnOff, u8 refSelected)
{
    ADS1248_WriteByte(ADS_WREG | ADS_MUX1);
    ADS1248_WriteByte(0x00);
    ADS1248_WriteByte(interVrefOnOff | refSelected);
}


/*******************************************************************************
* @Function Name: ADS1248_ReadREG
* @Description  : 读取寄存器的第byteToRead个字节
* @param                : u8 regName
* @param                : u8 byteToRead
* @retval                   : u8
* @date                           : 2014/10/05
*******************************************************************************/
u8 ADS1248_ReadREG(u8 regName, u8 byteToRead)
{
    ADS1248_WriteByte(ADS_RREG | regName);


    if(byteToRead == 1)
    {
        ADS1248_WriteByte(0x00);
    }
    else if(byteToRead == 2)
    {
        ADS1248_WriteByte(0x01);
        ADS1248_ReadByte();
    }


    return ADS1248_ReadByte();
}


/*******************************************************************************
* @Function Name: ADS1248_ReadADC
* @Description  : 返回有符号数值
* @retval                   : s32
* @date                           : 2014/10/04
*******************************************************************************/
s32 ADS1248_ReadADC()
{
    u8 temp[3];
    s32 adcVal = 0;


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


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


    ADS1248_WriteByte(ADS_RDATA);
    temp[0] = ADS1248_ReadByte();
    temp[1] = ADS1248_ReadByte();
    temp[2] = ADS1248_ReadByte();


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


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


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


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


    while(high >= low)
    {
        mid = (high + low) / 2;


        if(resistance >= pt100_table[mid])
        {
            if(resistance < pt100_table[mid + 1])
            {
                return (0.1 * low + (resistance - pt100_table[mid]) * 0.1 / ((pt100_table[mid + 1]) - pt100_table[mid]));
                break;
            }
            else
            {
                low = mid + 1;
            }
        }
        else if(resistance < pt100_table[mid])
        {
            high = mid - 1;
        }
    }
}


/*****************************************END OF FILE**********************************/


原创粉丝点击