Eugene Mwangi
/
ads1256-test-program
This is a test program for the ADS1256 ADC transducer together with the DISCO-L475G STM32 board.
main.cpp@2:4b6ca4d9c0c7, 2019-08-21 (annotated)
- Committer:
- Eugene0469
- Date:
- Wed Aug 21 11:02:36 2019 +0000
- Revision:
- 2:4b6ca4d9c0c7
- Parent:
- 0:e5807af79fca
- Child:
- 3:d9b97ff44d28
Reduces the number of operating modes to one(input multiplexer mode) while retaining the commented sections of code.
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 | 0:e5807af79fca | 10 | |
Eugene0469 | 2:4b6ca4d9c0c7 | 11 | //#define DRTYPE 0 // 0 - readDataSync, 1 - readDataMux |
Eugene0469 | 2:4b6ca4d9c0c7 | 12 | //#define SINGLE_CHANNEL 0 //1 - single channel, 0 - multiple channel |
Eugene0469 | 2:4b6ca4d9c0c7 | 13 | #define NO_OF_SENSORS 4 // 1- 1 sensor, 2- 2 sensors, 3- 3 sensors, 4- 4 sensors |
Eugene0469 | 0:e5807af79fca | 14 | |
Eugene0469 | 0:e5807af79fca | 15 | DigitalIn ndrdy = D10; |
Eugene0469 | 0:e5807af79fca | 16 | DigitalOut cs = CHIP_SLCT; |
Eugene0469 | 0:e5807af79fca | 17 | |
Eugene0469 | 0:e5807af79fca | 18 | Serial pc(USBTX, USBRX); |
Eugene0469 | 0:e5807af79fca | 19 | |
Eugene0469 | 0:e5807af79fca | 20 | SPI spi(SPI_MOSI, SPI_MISO, SPI_SCLK); |
Eugene0469 | 0:e5807af79fca | 21 | ADS1256 ads(&spi, &ndrdy, &cs); |
Eugene0469 | 0:e5807af79fca | 22 | |
Eugene0469 | 0:e5807af79fca | 23 | //extern uint8_t ads.channel; /* The current channel*/ |
Eugene0469 | 0:e5807af79fca | 24 | int32_t ads_sum = 0; |
Eugene0469 | 0:e5807af79fca | 25 | |
Eugene0469 | 2:4b6ca4d9c0c7 | 26 | //#if SINGLE_CHANNEL |
Eugene0469 | 2:4b6ca4d9c0c7 | 27 | // /* |
Eugene0469 | 2:4b6ca4d9c0c7 | 28 | // @brief: Read Data from a single channel |
Eugene0469 | 2:4b6ca4d9c0c7 | 29 | // */ |
Eugene0469 | 2:4b6ca4d9c0c7 | 30 | // void readDataSingleChannel() |
Eugene0469 | 2:4b6ca4d9c0c7 | 31 | // { |
Eugene0469 | 2:4b6ca4d9c0c7 | 32 | // ads.readSingleChannel(); |
Eugene0469 | 2:4b6ca4d9c0c7 | 33 | // pc.printf("The value read is %d\n", ads.adcNow[ads.channel]); // Specifically for channel 0. |
Eugene0469 | 2:4b6ca4d9c0c7 | 34 | // wait_us(1000); |
Eugene0469 | 2:4b6ca4d9c0c7 | 35 | // } |
Eugene0469 | 2:4b6ca4d9c0c7 | 36 | //#else |
Eugene0469 | 2:4b6ca4d9c0c7 | 37 | /* |
Eugene0469 | 2:4b6ca4d9c0c7 | 38 | @brief: Read data from multiple channels |
Eugene0469 | 2:4b6ca4d9c0c7 | 39 | */ |
Eugene0469 | 2:4b6ca4d9c0c7 | 40 | // #if !DRTYPE |
Eugene0469 | 2:4b6ca4d9c0c7 | 41 | // /* |
Eugene0469 | 2:4b6ca4d9c0c7 | 42 | // @brief: Settling time using Synchronization. |
Eugene0469 | 2:4b6ca4d9c0c7 | 43 | // */ |
Eugene0469 | 2:4b6ca4d9c0c7 | 44 | // void readDataSync() |
Eugene0469 | 2:4b6ca4d9c0c7 | 45 | // { |
Eugene0469 | 2:4b6ca4d9c0c7 | 46 | // for(int i=ads.channel;i<CHANNEL_NUM;i++) |
Eugene0469 | 2:4b6ca4d9c0c7 | 47 | // { |
Eugene0469 | 2:4b6ca4d9c0c7 | 48 | // ads.channel = i; |
Eugene0469 | 2:4b6ca4d9c0c7 | 49 | // ads.waitDRDY(); |
Eugene0469 | 2:4b6ca4d9c0c7 | 50 | // ads.isr(); |
Eugene0469 | 2:4b6ca4d9c0c7 | 51 | // } |
Eugene0469 | 2:4b6ca4d9c0c7 | 52 | // ads.channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 53 | // for (int i=0; i<CHANNEL_NUM; i++) |
Eugene0469 | 2:4b6ca4d9c0c7 | 54 | // { |
Eugene0469 | 2:4b6ca4d9c0c7 | 55 | // ads_sum+= ads.adcNow[i]; |
Eugene0469 | 2:4b6ca4d9c0c7 | 56 | // } |
Eugene0469 | 2:4b6ca4d9c0c7 | 57 | // pc.printf("The total value read is %d\n", ads_sum); |
Eugene0469 | 2:4b6ca4d9c0c7 | 58 | // ads_sum=0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 59 | // wait_us(1000); |
Eugene0469 | 2:4b6ca4d9c0c7 | 60 | // } |
Eugene0469 | 2:4b6ca4d9c0c7 | 61 | // #else |
Eugene0469 | 2:4b6ca4d9c0c7 | 62 | // /* |
Eugene0469 | 2:4b6ca4d9c0c7 | 63 | // @brief: Settling time using the Input Multiplexer. |
Eugene0469 | 2:4b6ca4d9c0c7 | 64 | // */ |
Eugene0469 | 2:4b6ca4d9c0c7 | 65 | #if NO_OF_SENSORS == 1 |
Eugene0469 | 2:4b6ca4d9c0c7 | 66 | void readDataMux() |
Eugene0469 | 2:4b6ca4d9c0c7 | 67 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 68 | uint8_t channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 69 | ads.readDiffChannel(channel); |
Eugene0469 | 0:e5807af79fca | 70 | |
Eugene0469 | 2:4b6ca4d9c0c7 | 71 | for (int i=0; i<CHANNEL_NUM; i++) |
Eugene0469 | 2:4b6ca4d9c0c7 | 72 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 73 | ads_sum+= ads.adcNow[i]; |
Eugene0469 | 2:4b6ca4d9c0c7 | 74 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 75 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 76 | #elif NO_OF_SENSORS == 2 |
Eugene0469 | 2:4b6ca4d9c0c7 | 77 | void readDataMux() |
Eugene0469 | 2:4b6ca4d9c0c7 | 78 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 79 | uint8_t channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 80 | while(channel < (CHANNEL_NUM-2)) |
Eugene0469 | 2:4b6ca4d9c0c7 | 81 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 82 | ads.readDiffChannel(++channel); |
Eugene0469 | 2:4b6ca4d9c0c7 | 83 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 84 | channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 85 | for (int i=0; i<CHANNEL_NUM; i++) |
Eugene0469 | 2:4b6ca4d9c0c7 | 86 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 87 | ads_sum+= ads.adcNow[i]; |
Eugene0469 | 2:4b6ca4d9c0c7 | 88 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 89 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 90 | #elif NO_OF_SENSORS == 3 |
Eugene0469 | 2:4b6ca4d9c0c7 | 91 | void readDataMux() |
Eugene0469 | 0:e5807af79fca | 92 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 93 | uint8_t channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 94 | while(channel < CHANNEL_NUM-1) |
Eugene0469 | 0:e5807af79fca | 95 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 96 | ads.readDiffChannel(++channel); |
Eugene0469 | 0:e5807af79fca | 97 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 98 | channel = 0; |
Eugene0469 | 0:e5807af79fca | 99 | for (int i=0; i<CHANNEL_NUM; i++) |
Eugene0469 | 0:e5807af79fca | 100 | { |
Eugene0469 | 0:e5807af79fca | 101 | ads_sum+= ads.adcNow[i]; |
Eugene0469 | 0:e5807af79fca | 102 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 103 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 104 | #elif NO_OF_SENSORS == 4 |
Eugene0469 | 2:4b6ca4d9c0c7 | 105 | void readDataMux() |
Eugene0469 | 2:4b6ca4d9c0c7 | 106 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 107 | uint8_t channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 108 | while(channel < CHANNEL_NUM) |
Eugene0469 | 2:4b6ca4d9c0c7 | 109 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 110 | ads.readDiffChannel(++channel); |
Eugene0469 | 2:4b6ca4d9c0c7 | 111 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 112 | channel = 0; |
Eugene0469 | 2:4b6ca4d9c0c7 | 113 | for (int i=0; i<CHANNEL_NUM; i++) |
Eugene0469 | 2:4b6ca4d9c0c7 | 114 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 115 | ads_sum+= ads.adcNow[i]; |
Eugene0469 | 2:4b6ca4d9c0c7 | 116 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 117 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 118 | #else |
Eugene0469 | 2:4b6ca4d9c0c7 | 119 | void readDataMux() |
Eugene0469 | 2:4b6ca4d9c0c7 | 120 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 121 | pc.printf("INVALID NUMBER OF SENSORS INITIALIZED\n"); |
Eugene0469 | 2:4b6ca4d9c0c7 | 122 | } |
Eugene0469 | 2:4b6ca4d9c0c7 | 123 | #endif |
Eugene0469 | 2:4b6ca4d9c0c7 | 124 | |
Eugene0469 | 2:4b6ca4d9c0c7 | 125 | // #endif |
Eugene0469 | 2:4b6ca4d9c0c7 | 126 | //#endif |
Eugene0469 | 2:4b6ca4d9c0c7 | 127 | void process(void) |
Eugene0469 | 2:4b6ca4d9c0c7 | 128 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 129 | // #if SINGLE_CHANNEL |
Eugene0469 | 2:4b6ca4d9c0c7 | 130 | // readDataSingleChannel(); |
Eugene0469 | 2:4b6ca4d9c0c7 | 131 | // #else |
Eugene0469 | 2:4b6ca4d9c0c7 | 132 | // #if !DRTYPE |
Eugene0469 | 2:4b6ca4d9c0c7 | 133 | // readDataSync(); |
Eugene0469 | 2:4b6ca4d9c0c7 | 134 | // #else |
Eugene0469 | 2:4b6ca4d9c0c7 | 135 | readDataMux(); |
Eugene0469 | 0:e5807af79fca | 136 | pc.printf("The total value read is %d\n", ads_sum); |
Eugene0469 | 0:e5807af79fca | 137 | ads_sum=0; |
Eugene0469 | 0:e5807af79fca | 138 | wait_us(1000); |
Eugene0469 | 2:4b6ca4d9c0c7 | 139 | // #endif |
Eugene0469 | 2:4b6ca4d9c0c7 | 140 | // #endif |
Eugene0469 | 2:4b6ca4d9c0c7 | 141 | pc.printf("The value in adcNow[0] is %d\n", ads.adcNow[0]); |
Eugene0469 | 2:4b6ca4d9c0c7 | 142 | pc.printf("The value in adcNow[1] is %d\n", ads.adcNow[1]); |
Eugene0469 | 2:4b6ca4d9c0c7 | 143 | pc.printf("The value in adcNow[2] is %d\n", ads.adcNow[2]); |
Eugene0469 | 2:4b6ca4d9c0c7 | 144 | pc.printf("The value in adcNow[3] is %d\n", ads.adcNow[3]); |
Eugene0469 | 0:e5807af79fca | 145 | } |
Eugene0469 | 0:e5807af79fca | 146 | |
Eugene0469 | 0:e5807af79fca | 147 | int main() |
Eugene0469 | 0:e5807af79fca | 148 | { |
Eugene0469 | 0:e5807af79fca | 149 | /* @brief Set the configuration parameters: |
Eugene0469 | 0:e5807af79fca | 150 | channel == 0(0x01h), |
Eugene0469 | 0:e5807af79fca | 151 | PGA gain == 64, |
Eugene0469 | 0:e5807af79fca | 152 | Buffer == 1 (enabled), |
Eugene0469 | 0:e5807af79fca | 153 | Datarate == 2.5SPS. |
Eugene0469 | 0:e5807af79fca | 154 | Auto_calibration has also been enabled |
Eugene0469 | 0:e5807af79fca | 155 | */ |
Eugene0469 | 0:e5807af79fca | 156 | ads.cfgADC(); |
Eugene0469 | 2:4b6ca4d9c0c7 | 157 | pc.printf("Device configuration is successful\n"); |
Eugene0469 | 2:4b6ca4d9c0c7 | 158 | ads.setDiffChannel(); // Redundancy feature just to ensure that the channel is set correctly. |
Eugene0469 | 0:e5807af79fca | 159 | pc.printf("The set gain value is %d\n", ads.getGainVal()); |
Eugene0469 | 0:e5807af79fca | 160 | |
Eugene0469 | 0:e5807af79fca | 161 | /*TODO: Calibration*/ |
Eugene0469 | 0:e5807af79fca | 162 | ads.selfCal(); |
Eugene0469 | 0:e5807af79fca | 163 | ads.sysOffCal(); |
Eugene0469 | 0:e5807af79fca | 164 | |
Eugene0469 | 0:e5807af79fca | 165 | while(1) |
Eugene0469 | 0:e5807af79fca | 166 | { |
Eugene0469 | 2:4b6ca4d9c0c7 | 167 | process(); |
Eugene0469 | 2:4b6ca4d9c0c7 | 168 | wait_ms(500); |
Eugene0469 | 0:e5807af79fca | 169 | } |
Eugene0469 | 0:e5807af79fca | 170 | } |