Eugene Mwangi
/
ads1256-test-program
This is a test program for the ADS1256 ADC transducer together with the DISCO-L475G STM32 board.
main.cpp@4:8a130c60169b, 2019-08-21 (annotated)
- Committer:
- Eugene0469
- Date:
- Wed Aug 21 11:07:25 2019 +0000
- Revision:
- 4:8a130c60169b
- Parent:
- 3:d9b97ff44d28
Merges the branches of my local repository
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Eugene0469 | 0:e5807af79fca | 1 | #include "mbed.h" |
Eugene0469 | 0:e5807af79fca | 2 | #include "ADS1256.h" |
Eugene0469 | 0:e5807af79fca | 3 | |
Eugene0469 | 0:e5807af79fca | 4 | #define SPI_MOSI D11 |
Eugene0469 | 0:e5807af79fca | 5 | #define SPI_MISO D12 |
Eugene0469 | 0:e5807af79fca | 6 | #define SPI_SCLK D13 |
Eugene0469 | 0:e5807af79fca | 7 | #define CHIP_SLCT D9 |
Eugene0469 | 0:e5807af79fca | 8 | #define CHANNEL_NUM 4 |
Eugene0469 | 0:e5807af79fca | 9 | |
Eugene0469 | 2:4b6ca4d9c0c7 | 10 | #define NO_OF_SENSORS 4 // 1- 1 sensor, 2- 2 sensors, 3- 3 sensors, 4- 4 sensors |
Eugene0469 | 0:e5807af79fca | 11 | |
Eugene0469 | 0:e5807af79fca | 12 | DigitalIn ndrdy = D10; |
Eugene0469 | 0:e5807af79fca | 13 | DigitalOut cs = CHIP_SLCT; |
Eugene0469 | 0:e5807af79fca | 14 | |
Eugene0469 | 0:e5807af79fca | 15 | Serial pc(USBTX, USBRX); |
Eugene0469 | 0:e5807af79fca | 16 | |
Eugene0469 | 0:e5807af79fca | 17 | SPI spi(SPI_MOSI, SPI_MISO, SPI_SCLK); |
Eugene0469 | 0:e5807af79fca | 18 | ADS1256 ads(&spi, &ndrdy, &cs); |
Eugene0469 | 0:e5807af79fca | 19 | |
Eugene0469 | 0:e5807af79fca | 20 | int32_t ads_sum = 0; |
Eugene0469 | 0:e5807af79fca | 21 | |
Eugene0469 | 3:d9b97ff44d28 | 22 | /* |
Eugene0469 | 3:d9b97ff44d28 | 23 | @brief: Settling time using the Input Multiplexer. |
Eugene0469 | 3:d9b97ff44d28 | 24 | */ |
Eugene0469 | 2:4b6ca4d9c0c7 | 25 | #if NO_OF_SENSORS == 1 |
Eugene0469 | 2:4b6ca4d9c0c7 | 26 | void readDataMux() |
Eugene0469 | 2:4b6ca4d9c0c7 | 27 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 28 | uint8_t channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 29 | ads.readDiffChannel(channel); |
Eugene0469 | 0:e5807af79fca | 30 | |
Eugene0469 | 2:4b6ca4d9c0c7 | 31 | for (int i=0; i<CHANNEL_NUM; i++) |
Eugene0469 | 2:4b6ca4d9c0c7 | 32 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 33 | ads_sum+= ads.adcNow[i]; |
Eugene0469 | 2:4b6ca4d9c0c7 | 34 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 35 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 36 | #elif NO_OF_SENSORS == 2 |
Eugene0469 | 2:4b6ca4d9c0c7 | 37 | void readDataMux() |
Eugene0469 | 2:4b6ca4d9c0c7 | 38 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 39 | uint8_t channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 40 | while(channel < (CHANNEL_NUM-2)) |
Eugene0469 | 2:4b6ca4d9c0c7 | 41 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 42 | ads.readDiffChannel(++channel); |
Eugene0469 | 2:4b6ca4d9c0c7 | 43 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 44 | channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 45 | for (int i=0; i<CHANNEL_NUM; i++) |
Eugene0469 | 2:4b6ca4d9c0c7 | 46 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 47 | ads_sum+= ads.adcNow[i]; |
Eugene0469 | 2:4b6ca4d9c0c7 | 48 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 49 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 50 | #elif NO_OF_SENSORS == 3 |
Eugene0469 | 2:4b6ca4d9c0c7 | 51 | void readDataMux() |
Eugene0469 | 0:e5807af79fca | 52 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 53 | uint8_t channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 54 | while(channel < CHANNEL_NUM-1) |
Eugene0469 | 0:e5807af79fca | 55 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 56 | ads.readDiffChannel(++channel); |
Eugene0469 | 0:e5807af79fca | 57 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 58 | channel = 0; |
Eugene0469 | 0:e5807af79fca | 59 | for (int i=0; i<CHANNEL_NUM; i++) |
Eugene0469 | 0:e5807af79fca | 60 | { |
Eugene0469 | 0:e5807af79fca | 61 | ads_sum+= ads.adcNow[i]; |
Eugene0469 | 0:e5807af79fca | 62 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 63 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 64 | #elif NO_OF_SENSORS == 4 |
Eugene0469 | 2:4b6ca4d9c0c7 | 65 | void readDataMux() |
Eugene0469 | 2:4b6ca4d9c0c7 | 66 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 67 | uint8_t channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 68 | while(channel < CHANNEL_NUM) |
Eugene0469 | 2:4b6ca4d9c0c7 | 69 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 70 | ads.readDiffChannel(++channel); |
Eugene0469 | 2:4b6ca4d9c0c7 | 71 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 72 | channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 73 | for (int i=0; i<CHANNEL_NUM; i++) |
Eugene0469 | 2:4b6ca4d9c0c7 | 74 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 75 | ads_sum+= ads.adcNow[i]; |
Eugene0469 | 2:4b6ca4d9c0c7 | 76 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 77 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 78 | #else |
Eugene0469 | 2:4b6ca4d9c0c7 | 79 | void readDataMux() |
Eugene0469 | 2:4b6ca4d9c0c7 | 80 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 81 | pc.printf("INVALID NUMBER OF SENSORS INITIALIZED\n"); |
Eugene0469 | 2:4b6ca4d9c0c7 | 82 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 83 | #endif |
Eugene0469 | 3:d9b97ff44d28 | 84 | |
Eugene0469 | 2:4b6ca4d9c0c7 | 85 | void process(void) |
Eugene0469 | 2:4b6ca4d9c0c7 | 86 | { |
Eugene0469 | 3:d9b97ff44d28 | 87 | |
Eugene0469 | 2:4b6ca4d9c0c7 | 88 | readDataMux(); |
Eugene0469 | 0:e5807af79fca | 89 | pc.printf("The total value read is %d\n", ads_sum); |
Eugene0469 | 0:e5807af79fca | 90 | ads_sum=0; |
Eugene0469 | 0:e5807af79fca | 91 | wait_us(1000); |
Eugene0469 | 3:d9b97ff44d28 | 92 | |
Eugene0469 | 2:4b6ca4d9c0c7 | 93 | pc.printf("The value in adcNow[0] is %d\n", ads.adcNow[0]); |
Eugene0469 | 2:4b6ca4d9c0c7 | 94 | pc.printf("The value in adcNow[1] is %d\n", ads.adcNow[1]); |
Eugene0469 | 2:4b6ca4d9c0c7 | 95 | pc.printf("The value in adcNow[2] is %d\n", ads.adcNow[2]); |
Eugene0469 | 2:4b6ca4d9c0c7 | 96 | pc.printf("The value in adcNow[3] is %d\n", ads.adcNow[3]); |
Eugene0469 | 0:e5807af79fca | 97 | } |
Eugene0469 | 0:e5807af79fca | 98 | |
Eugene0469 | 0:e5807af79fca | 99 | int main() |
Eugene0469 | 0:e5807af79fca | 100 | { |
Eugene0469 | 0:e5807af79fca | 101 | /* @brief Set the configuration parameters: |
Eugene0469 | 0:e5807af79fca | 102 | channel == 0(0x01h), |
Eugene0469 | 0:e5807af79fca | 103 | PGA gain == 64, |
Eugene0469 | 0:e5807af79fca | 104 | Buffer == 1 (enabled), |
Eugene0469 | 0:e5807af79fca | 105 | Datarate == 2.5SPS. |
Eugene0469 | 0:e5807af79fca | 106 | Auto_calibration has also been enabled |
Eugene0469 | 0:e5807af79fca | 107 | */ |
Eugene0469 | 0:e5807af79fca | 108 | ads.cfgADC(); |
Eugene0469 | 2:4b6ca4d9c0c7 | 109 | pc.printf("Device configuration is successful\n"); |
Eugene0469 | 2:4b6ca4d9c0c7 | 110 | ads.setDiffChannel(); // Redundancy feature just to ensure that the channel is set correctly. |
Eugene0469 | 0:e5807af79fca | 111 | pc.printf("The set gain value is %d\n", ads.getGainVal()); |
Eugene0469 | 0:e5807af79fca | 112 | |
Eugene0469 | 0:e5807af79fca | 113 | /*TODO: Calibration*/ |
Eugene0469 | 0:e5807af79fca | 114 | ads.selfCal(); |
Eugene0469 | 0:e5807af79fca | 115 | ads.sysOffCal(); |
Eugene0469 | 0:e5807af79fca | 116 | |
Eugene0469 | 0:e5807af79fca | 117 | while(1) |
Eugene0469 | 0:e5807af79fca | 118 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 119 | process(); |
Eugene0469 | 2:4b6ca4d9c0c7 | 120 | wait_ms(500); |
Eugene0469 | 0:e5807af79fca | 121 | } |
Eugene0469 | 0:e5807af79fca | 122 | } |