This is a Driver for the Analog Devices AD7989 18 Bit ADC.
AD7989.h@8:b143706d6c43, 2019-02-04 (annotated)
- Committer:
- Ryan Vasquez
- Date:
- Mon Feb 04 11:09:56 2019 -0600
- Revision:
- 8:b143706d6c43
- Parent:
- 7:8717395d66e7
fixed the for loop in the averaging function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rvasquez6089 | 0:64eaedfa13a2 | 1 | #ifndef AD7989_H |
rvasquez6089 | 0:64eaedfa13a2 | 2 | #define AD7989_H |
rvasquez6089 | 0:64eaedfa13a2 | 3 | |
Ryan Vasquez |
3:c3f4ce50902f | 4 | #include "mbed.h" |
rvasquez6089 | 6:e018ebbbc7eb | 5 | /** An abstraction for an 18 bit ADC |
rvasquez6089 | 4:be5656dbc656 | 6 | * @note Not quite sure this is thread or interrupt safe. Treat this as a standard mbed SPI object |
rvasquez6089 | 4:be5656dbc656 | 7 | * There are mutex locks when the SPI bus is being used. |
rvasquez6089 | 6:e018ebbbc7eb | 8 | * |
rvasquez6089 | 6:e018ebbbc7eb | 9 | * Example: |
rvasquez6089 | 5:558b7506e684 | 10 | * @code |
rvasquez6089 | 5:558b7506e684 | 11 | * #include "mbed.h" |
rvasquez6089 | 5:558b7506e684 | 12 | * #include "AD7989_Driver.h" |
rvasquez6089 | 5:558b7506e684 | 13 | * |
rvasquez6089 | 5:558b7506e684 | 14 | * SPI spi(p5, p6, p7); // mosi, miso, sclk |
rvasquez6089 | 5:558b7506e684 | 15 | * DigitalOut cs(p8); |
rvasquez6089 | 5:558b7506e684 | 16 | * Digitalout convert(p9); |
rvasquez6089 | 5:558b7506e684 | 17 | * AD7989_Driver ADCDEV(&spi, &cs, &convert); |
rvasquez6089 | 5:558b7506e684 | 18 | * double voltage; |
rvasquez6089 | 5:558b7506e684 | 19 | * int main() |
rvasquez6089 | 5:558b7506e684 | 20 | * { |
rvasquez6089 | 5:558b7506e684 | 21 | * while(1) |
rvasquez6089 | 5:558b7506e684 | 22 | * { |
rvasquez6089 | 5:558b7506e684 | 23 | * voltage = ADCDEV.sample(); |
rvasquez6089 | 5:558b7506e684 | 24 | * printf("Voltage = %f Volts", voltage); |
rvasquez6089 | 5:558b7506e684 | 25 | * } |
rvasquez6089 | 5:558b7506e684 | 26 | * } |
rvasquez6089 | 6:e018ebbbc7eb | 27 | * @endcode |
rvasquez6089 | 4:be5656dbc656 | 28 | */ |
rvasquez6089 | 0:64eaedfa13a2 | 29 | class AD7989_Driver |
rvasquez6089 | 0:64eaedfa13a2 | 30 | { |
rvasquez6089 | 0:64eaedfa13a2 | 31 | public: |
rvasquez6089 | 0:64eaedfa13a2 | 32 | /** Constructor for AD7989_Drive object |
rvasquez6089 | 0:64eaedfa13a2 | 33 | * |
rvasquez6089 | 0:64eaedfa13a2 | 34 | * @param SPI_BUS this is the SPI bus the AD7989 is one |
rvasquez6089 | 0:64eaedfa13a2 | 35 | * @param Chip_Select this is the DigitalOut outline connected to CS pin |
rvasquez6089 | 0:64eaedfa13a2 | 36 | * @param Convert this is the DigitalOut pin that is connected to the CONV pin |
rvasquez6089 | 0:64eaedfa13a2 | 37 | */ |
rvasquez6089 | 0:64eaedfa13a2 | 38 | AD7989_Driver(SPI *SPI_BUS, DigitalOut *Chip_Select, DigitalOut *Convert); |
rvasquez6089 | 0:64eaedfa13a2 | 39 | /** Function returns the voltage on the AD7989 as a double |
rvasquez6089 | 0:64eaedfa13a2 | 40 | * The value the function will return will be from -5.0V to 5.0V |
rvasquez6089 | 0:64eaedfa13a2 | 41 | */ |
rvasquez6089 | 0:64eaedfa13a2 | 42 | double sample(); |
rvasquez6089 | 7:8717395d66e7 | 43 | /** Averages a number of samples the user specifies |
rvasquez6089 | 0:64eaedfa13a2 | 44 | * |
Ryan Vasquez |
2:60f5474814ac | 45 | * @param num_avgs The number of samples to average |
rvasquez6089 | 0:64eaedfa13a2 | 46 | */ |
rvasquez6089 | 0:64eaedfa13a2 | 47 | double sample_avg(int num_avgs); |
rvasquez6089 | 0:64eaedfa13a2 | 48 | |
rvasquez6089 | 0:64eaedfa13a2 | 49 | float volts; |
rvasquez6089 | 0:64eaedfa13a2 | 50 | private: |
rvasquez6089 | 0:64eaedfa13a2 | 51 | static const float zero_point; |
rvasquez6089 | 0:64eaedfa13a2 | 52 | SPI *ADC_SPI; |
rvasquez6089 | 0:64eaedfa13a2 | 53 | DigitalOut *CS; |
rvasquez6089 | 0:64eaedfa13a2 | 54 | DigitalOut *ADC_CONV; |
rvasquez6089 | 0:64eaedfa13a2 | 55 | }; |
rvasquez6089 | 0:64eaedfa13a2 | 56 | |
Ryan Vasquez |
2:60f5474814ac | 57 | #endif |