AD7798 ADC driver

Dependents:   CN0396 CN0397

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AD7798.h Source File

AD7798.h

00001 #ifndef _AD7798_H_
00002 #define _AD7798_H_
00003 
00004 #include <mbed.h>
00005 
00006 /******************************************************************************/
00007 /* AD7798                                                                   */
00008 /******************************************************************************/
00009 
00010 /*AD7798 Registers*/
00011 #define AD7798_REG_COMM     0 /* Communications Register(WO, 8-bit) */
00012 #define AD7798_REG_STAT     0 /* Status Register        (RO, 8-bit) */
00013 #define AD7798_REG_MODE     1 /* Mode Register          (RW, 16-bit */
00014 #define AD7798_REG_CONF     2 /* Configuration Register (RW, 16-bit)*/
00015 #define AD7798_REG_DATA     3 /* Data Register          (RO, 16-/24-bit) */
00016 #define AD7798_REG_ID       4 /* ID Register            (RO, 8-bit) */
00017 #define AD7798_REG_IO       5 /* IO Register            (RO, 8-bit) */
00018 #define AD7798_REG_OFFSET   6 /* Offset Register        (RW, 24-bit */
00019 #define AD7798_REG_FULLSCALE    7 /* Full-Scale Register    (RW, 24-bit */
00020 
00021 /* Communications Register Bit Designations (AD7798_REG_COMM) */
00022 #define AD7798_COMM_WEN     (1 << 7)            /* Write Enable */
00023 #define AD7798_COMM_WRITE   (0 << 6)            /* Write Operation */
00024 #define AD7798_COMM_READ    (1 << 6)            /* Read Operation */
00025 #define AD7798_COMM_ADDR(x) (((x) & 0x7) << 3)  /* Register Address */
00026 #define AD7798_COMM_CREAD   (1 << 2)            /* Continuous Read of Data Register */
00027 
00028 /* Status Register Bit Designations (AD7798_REG_STAT) */
00029 #define AD7798_STAT_RDY     (1 << 7) /* Ready */
00030 #define AD7798_STAT_ERR     (1 << 6) /* Error (Overrange, Underrange) */
00031 #define AD7798_STAT_CH3     (1 << 2) /* Channel 3 */
00032 #define AD7798_STAT_CH2     (1 << 1) /* Channel 2 */
00033 #define AD7798_STAT_CH1     (1 << 0) /* Channel 1 */
00034 
00035 
00036 #define AD7798_CH3      (1 << 2) /* Channel 3 */
00037 #define AD7798_CH2      (1 << 1) /* Channel 2 */
00038 #define AD7798_CH1      (1 << 0) /* Channel 1 */
00039 
00040 /* Mode Register Bit Designations (AD7798_REG_MODE) */
00041 #define AD7798_MODE_SEL(x)      (((x) & 0x7) << 13) /* Operation Mode Select */
00042 #define AD7798_MODE_PSW(x)      (1 << 12)           /* Power Switch Control Bit */
00043 #define AD7798_MODE_RATE(x)     ((x) & 0xF)         /* Filter Update Rate Select */
00044 
00045 /* AD7798_MODE_SEL(x) options */
00046 #define AD7798_MODE_CONT         0 /* Continuous Conversion Mode */
00047 #define AD7798_MODE_SINGLE       1 /* Single Conversion Mode */
00048 #define AD7798_MODE_IDLE         2 /* Idle Mode */
00049 #define AD7798_MODE_PWRDN        3 /* Power-Down Mode */
00050 #define AD7798_MODE_CAL_INT_ZERO 4 /* Internal Zero-Scale Calibration */
00051 #define AD7798_MODE_CAL_INT_FULL 5 /* Internal Full-Scale Calibration */
00052 #define AD7798_MODE_CAL_SYS_ZERO 6 /* System Zero-Scale Calibration */
00053 #define AD7798_MODE_CAL_SYS_FULL 7 /* System Full-Scale Calibration */
00054 
00055 /* Configuration Register Bit Designations (AD7798_REG_CONF) */
00056 #define AD7798_CONF_BO_EN     (1 << 13)             /* Burnout Current Enable */
00057 #define AD7798_CONF_UNIPOLAR  (1 << 12)             /* Unipolar/Bipolar Enable */
00058 #define AD7798_CONF_GAIN(x)   (((x) & 0x7) << 8)    /* Gain Select */
00059 #define AD7798_CONF_REFDET(x) (((x) & 0x1) << 5)    /* Reference detect function */
00060 #define AD7798_CONF_BUF       (1 << 4)              /* Buffered Mode Enable */
00061 #define AD7798_CONF_CHAN(x)   ((x) & 0x7)           /* Channel select */
00062 
00063 /* AD7798_CONF_GAIN(x) options */
00064 #define AD7798_GAIN_1       0
00065 #define AD7798_GAIN_2       1
00066 #define AD7798_GAIN_4       2
00067 #define AD7798_GAIN_8       3
00068 #define AD7798_GAIN_16      4
00069 #define AD7798_GAIN_32      5
00070 #define AD7798_GAIN_64      6
00071 #define AD7798_GAIN_128     7
00072 
00073 /* AD7798_CONF_REFDET(x) options */
00074 #define AD7798_REFDET_ENA   1
00075 #define AD7798_REFDET_DIS   0
00076 
00077 /* AD7798_CONF_CHAN(x) options */
00078 #define AD7798_CH_AIN1P_AIN1M   0 /* AIN1(+) - AIN1(-) */
00079 #define AD7798_CH_AIN2P_AIN2M   1 /* AIN2(+) - AIN2(-) */
00080 #define AD7798_CH_AIN3P_AIN3M   2 /* AIN3(+) - AIN3(-) */
00081 #define AD7798_CH_AIN1M_AIN1M   3 /* AIN1(-) - AIN1(-) */
00082 #define AD7798_CH_AVDD_MONITOR  7 /* AVDD Monitor */
00083 
00084 /* ID Register Bit Designations (AD7798_REG_ID) */
00085 #define AD7798_ID           0x8
00086 #define AD7798_ID_MASK      0xF
00087 
00088 /* IO (Excitation Current Sources) Register Bit Designations (AD7798_REG_IO) */
00089 #define AD7798_IOEN         (1 << 6)
00090 #define AD7798_IO1(x)       (((x) & 0x1) << 4)
00091 #define AD7798_IO2(x)       (((x) & 0x1) << 5)
00092 
00093 #define TRUE              1
00094 #define FALSE             0
00095 
00096 #define AD7798_UNIPOLAR     TRUE
00097 #define AD7798_BIPOLAR      FALSE
00098 
00099 #define AD7798_ENABLE       TRUE
00100 #define AD7798_DISABLE      FALSE
00101 
00102 /******************************************************************************/
00103 /* Functions Prototypes                                                       */
00104 /*****************************************************************************/
00105 
00106 class AD7798
00107 {
00108 public:
00109     AD7798( PinName CS, PinName MOSI = SPI_MOSI, PinName MISO = SPI_MISO, PinName SCK = SPI_SCK);
00110     /* Initialize AD7798 and check if the device is present*/
00111     uint8_t init(void);
00112     void frequency(int hz);
00113     /* Sends 32 consecutive 1's on SPI in order to reset the part. */
00114     void reset(void);
00115     /* Reads the value of the selected register. */
00116     uint16_t get_register_value(uint8_t regAddress, uint8_t size, bool toggle_cs = true);
00117     /* Writes a value to the register. */
00118     void set_register_value(uint8_t regAddress, uint16_t regValue, uint8_t size, bool toggle_cs = true);
00119     /* Sets the operating mode of AD7798. */
00120     void set_mode(uint8_t mode);
00121     /* Reads /RDY bit of Status register. */
00122     uint8_t ready(void);
00123     /* Selects the channel of AD7798. */
00124     void set_channel(uint8_t channel);
00125     /* Sets the gain of the In-Amp. */
00126     void set_gain(uint16_t gain);
00127     /* Enables or disables the reference detect function. */
00128     void set_reference(uint8_t state);
00129 
00130     void set_filter(uint8_t filter);
00131     void set_coding_mode(uint8_t mode);
00132     void set_burnout_current(uint8_t select);
00133 
00134     void read_data(uint8_t adcChannel, uint16_t *adcData);
00135 
00136     uint8_t spi_read(uint8_t *data, uint8_t bytes_number, bool toggle_cs = true);
00137     uint8_t spi_write(uint8_t *data, uint8_t bytes_number, bool toggle_cs = true);
00138 
00139 
00140 
00141     //DigitalIn miso;///< DigitalIn must be initialized before SPI to prevent pin MUX overwrite
00142     SPI ad7798;
00143     DigitalOut cs;
00144 
00145 private:
00146     const uint8_t _SPI_MODE = 0x03;
00147 };
00148 #endif  // _AD7798_H_