Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ads1299.cpp@0:99e31762ab2f, 2019-05-29 (annotated)
- Committer:
- eguliyev
- Date:
- Wed May 29 03:43:46 2019 +0000
- Revision:
- 0:99e31762ab2f
how to read texas instrument adc1299 using nucleo f4
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| eguliyev | 0:99e31762ab2f | 1 | #include "mbed.h" |
| eguliyev | 0:99e31762ab2f | 2 | #include "ads1299.h" |
| eguliyev | 0:99e31762ab2f | 3 | |
| eguliyev | 0:99e31762ab2f | 4 | SPI spi(SPI_MOSI,SPI_MISO,SPI_SCK); |
| eguliyev | 0:99e31762ab2f | 5 | DigitalOut CS(PA_4); |
| eguliyev | 0:99e31762ab2f | 6 | InterruptIn DRDY(PA_2); |
| eguliyev | 0:99e31762ab2f | 7 | |
| eguliyev | 0:99e31762ab2f | 8 | long ADS129X_data[9]; |
| eguliyev | 0:99e31762ab2f | 9 | bool ADS129X_newData; |
| eguliyev | 0:99e31762ab2f | 10 | void ADS129X_dataReadyISR(); |
| eguliyev | 0:99e31762ab2f | 11 | |
| eguliyev | 0:99e31762ab2f | 12 | void SPI_INIT(void){ |
| eguliyev | 0:99e31762ab2f | 13 | spi.format(8,1); |
| eguliyev | 0:99e31762ab2f | 14 | spi.frequency(1000000); |
| eguliyev | 0:99e31762ab2f | 15 | } |
| eguliyev | 0:99e31762ab2f | 16 | |
| eguliyev | 0:99e31762ab2f | 17 | void WAKEUP() { |
| eguliyev | 0:99e31762ab2f | 18 | CS = 0; |
| eguliyev | 0:99e31762ab2f | 19 | spi.write(ADS129X_CMD_WAKEUP); |
| eguliyev | 0:99e31762ab2f | 20 | wait_us(2); |
| eguliyev | 0:99e31762ab2f | 21 | CS = 1; |
| eguliyev | 0:99e31762ab2f | 22 | wait_us(2); |
| eguliyev | 0:99e31762ab2f | 23 | } |
| eguliyev | 0:99e31762ab2f | 24 | |
| eguliyev | 0:99e31762ab2f | 25 | void STANDBY() { |
| eguliyev | 0:99e31762ab2f | 26 | CS = 0; |
| eguliyev | 0:99e31762ab2f | 27 | spi.write(ADS129X_CMD_STANDBY); |
| eguliyev | 0:99e31762ab2f | 28 | wait_us(2); |
| eguliyev | 0:99e31762ab2f | 29 | CS = 1; |
| eguliyev | 0:99e31762ab2f | 30 | } |
| eguliyev | 0:99e31762ab2f | 31 | |
| eguliyev | 0:99e31762ab2f | 32 | void RSET() { |
| eguliyev | 0:99e31762ab2f | 33 | CS = 0; |
| eguliyev | 0:99e31762ab2f | 34 | spi.write(ADS129X_CMD_RESET); |
| eguliyev | 0:99e31762ab2f | 35 | wait_us(2); |
| eguliyev | 0:99e31762ab2f | 36 | CS =1; |
| eguliyev | 0:99e31762ab2f | 37 | wait_ms(10); |
| eguliyev | 0:99e31762ab2f | 38 | } |
| eguliyev | 0:99e31762ab2f | 39 | |
| eguliyev | 0:99e31762ab2f | 40 | void START() { |
| eguliyev | 0:99e31762ab2f | 41 | // CS = 0; |
| eguliyev | 0:99e31762ab2f | 42 | //spi.write(ADS129X_CMD_START); |
| eguliyev | 0:99e31762ab2f | 43 | // wait_us(2); |
| eguliyev | 0:99e31762ab2f | 44 | // CS = 1; |
| eguliyev | 0:99e31762ab2f | 45 | RDATA(); |
| eguliyev | 0:99e31762ab2f | 46 | DRDY.fall(&ADS129X_dataReadyISR); |
| eguliyev | 0:99e31762ab2f | 47 | } |
| eguliyev | 0:99e31762ab2f | 48 | |
| eguliyev | 0:99e31762ab2f | 49 | |
| eguliyev | 0:99e31762ab2f | 50 | void RDATAC() { |
| eguliyev | 0:99e31762ab2f | 51 | CS =0;; |
| eguliyev | 0:99e31762ab2f | 52 | spi.write(ADS129X_CMD_RDATAC); |
| eguliyev | 0:99e31762ab2f | 53 | wait_us(2); |
| eguliyev | 0:99e31762ab2f | 54 | CS = 1; |
| eguliyev | 0:99e31762ab2f | 55 | wait_us(2); //must way at least 4 tCLK cycles before sending another command (Datasheet, pg. 39) |
| eguliyev | 0:99e31762ab2f | 56 | } |
| eguliyev | 0:99e31762ab2f | 57 | |
| eguliyev | 0:99e31762ab2f | 58 | void SDATAC() { |
| eguliyev | 0:99e31762ab2f | 59 | CS = 0; |
| eguliyev | 0:99e31762ab2f | 60 | spi.write(ADS129X_CMD_SDATAC); //SDATAC |
| eguliyev | 0:99e31762ab2f | 61 | wait_us(2); |
| eguliyev | 0:99e31762ab2f | 62 | CS=1; |
| eguliyev | 0:99e31762ab2f | 63 | } |
| eguliyev | 0:99e31762ab2f | 64 | |
| eguliyev | 0:99e31762ab2f | 65 | void RDATA() { |
| eguliyev | 0:99e31762ab2f | 66 | CS =0; |
| eguliyev | 0:99e31762ab2f | 67 | spi.write(ADS129X_CMD_RDATA); |
| eguliyev | 0:99e31762ab2f | 68 | wait_us(2); |
| eguliyev | 0:99e31762ab2f | 69 | CS=1; |
| eguliyev | 0:99e31762ab2f | 70 | } |
| eguliyev | 0:99e31762ab2f | 71 | |
| eguliyev | 0:99e31762ab2f | 72 | char RREG(char _address) { |
| eguliyev | 0:99e31762ab2f | 73 | char opcode1 = ADS129X_CMD_RREG | (_address & 0x1F); //001rrrrr; _RREG = 00100000 and _address = rrrrr |
| eguliyev | 0:99e31762ab2f | 74 | CS = 0; //Low to communicate |
| eguliyev | 0:99e31762ab2f | 75 | spi.write(opcode1); //RREG |
| eguliyev | 0:99e31762ab2f | 76 | spi.write(0x00); //opcode2 |
| eguliyev | 0:99e31762ab2f | 77 | wait_us(1); |
| eguliyev | 0:99e31762ab2f | 78 | char data = spi.write(0x00); // read (Datasheet, pg.39) |
| eguliyev | 0:99e31762ab2f | 79 | wait_us(2); |
| eguliyev | 0:99e31762ab2f | 80 | CS =1; //High to end communication |
| eguliyev | 0:99e31762ab2f | 81 | return data; |
| eguliyev | 0:99e31762ab2f | 82 | } |
| eguliyev | 0:99e31762ab2f | 83 | |
| eguliyev | 0:99e31762ab2f | 84 | void RREG(char _address, char _numRegisters, char *_data) { |
| eguliyev | 0:99e31762ab2f | 85 | char opcode1 = ADS129X_CMD_RREG | (_address & 0x1F); //001rrrrr; _RREG = 00100000 and _address = rrrrr |
| eguliyev | 0:99e31762ab2f | 86 | CS = 0; |
| eguliyev | 0:99e31762ab2f | 87 | spi.write(ADS129X_CMD_SDATAC); //SDATAC |
| eguliyev | 0:99e31762ab2f | 88 | spi.write(opcode1); //RREG |
| eguliyev | 0:99e31762ab2f | 89 | spi.write(_numRegisters-1); //opcode2 |
| eguliyev | 0:99e31762ab2f | 90 | for(char i = 0; i < _numRegisters; i++){ |
| eguliyev | 0:99e31762ab2f | 91 | *(_data+i) = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 92 | } |
| eguliyev | 0:99e31762ab2f | 93 | wait_us(2); |
| eguliyev | 0:99e31762ab2f | 94 | CS = 1; |
| eguliyev | 0:99e31762ab2f | 95 | } |
| eguliyev | 0:99e31762ab2f | 96 | |
| eguliyev | 0:99e31762ab2f | 97 | void WREG(char _address, char _value) { |
| eguliyev | 0:99e31762ab2f | 98 | char opcode1 = ADS129X_CMD_WREG | (_address & 0x1F); //001rrrrr; _RREG = 00100000 and _address = rrrrr ds |
| eguliyev | 0:99e31762ab2f | 99 | CS=0; //Low to communicate |
| eguliyev | 0:99e31762ab2f | 100 | spi.write(opcode1); |
| eguliyev | 0:99e31762ab2f | 101 | spi.write(0x00); // opcode2; only write one register |
| eguliyev | 0:99e31762ab2f | 102 | spi.write(_value); |
| eguliyev | 0:99e31762ab2f | 103 | wait_us(2); |
| eguliyev | 0:99e31762ab2f | 104 | CS = 1; //Low to communicate |
| eguliyev | 0:99e31762ab2f | 105 | } |
| eguliyev | 0:99e31762ab2f | 106 | |
| eguliyev | 0:99e31762ab2f | 107 | char getDeviceId() { |
| eguliyev | 0:99e31762ab2f | 108 | CS=0; //Low to communicate |
| eguliyev | 0:99e31762ab2f | 109 | spi.write(ADS129X_CMD_RREG); //RREG |
| eguliyev | 0:99e31762ab2f | 110 | spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 111 | char data = spi.write(0x00); // byte to read (hopefully 0b???11110) |
| eguliyev | 0:99e31762ab2f | 112 | wait_us(2); |
| eguliyev | 0:99e31762ab2f | 113 | CS=1; //Low to communicate |
| eguliyev | 0:99e31762ab2f | 114 | return data; |
| eguliyev | 0:99e31762ab2f | 115 | } |
| eguliyev | 0:99e31762ab2f | 116 | |
| eguliyev | 0:99e31762ab2f | 117 | void Set_IRQ(){ |
| eguliyev | 0:99e31762ab2f | 118 | // DRDY.fall(&ADS129X_dataReadyISR); |
| eguliyev | 0:99e31762ab2f | 119 | } |
| eguliyev | 0:99e31762ab2f | 120 | |
| eguliyev | 0:99e31762ab2f | 121 | void ADS129X_dataReadyISR() { |
| eguliyev | 0:99e31762ab2f | 122 | |
| eguliyev | 0:99e31762ab2f | 123 | CS = 0; |
| eguliyev | 0:99e31762ab2f | 124 | |
| eguliyev | 0:99e31762ab2f | 125 | // status |
| eguliyev | 0:99e31762ab2f | 126 | ((char*) ADS129X_data)[0*4+3] = 0; |
| eguliyev | 0:99e31762ab2f | 127 | ((char*) ADS129X_data)[0*4+2] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 128 | ((char*) ADS129X_data)[0*4+1] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 129 | ((char*) ADS129X_data)[0*4+0] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 130 | // channel 1 |
| eguliyev | 0:99e31762ab2f | 131 | ((char*) ADS129X_data)[1*4+3] = 0; |
| eguliyev | 0:99e31762ab2f | 132 | ((char*) ADS129X_data)[1*4+2] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 133 | ((char*) ADS129X_data)[1*4+1] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 134 | ((char*) ADS129X_data)[1*4+0] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 135 | // channel 2 |
| eguliyev | 0:99e31762ab2f | 136 | ((char*) ADS129X_data)[2*4+3] = 0; |
| eguliyev | 0:99e31762ab2f | 137 | ((char*) ADS129X_data)[2*4+2] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 138 | ((char*) ADS129X_data)[2*4+1] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 139 | ((char*) ADS129X_data)[2*4+0] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 140 | // channel 3 |
| eguliyev | 0:99e31762ab2f | 141 | ((char*) ADS129X_data)[3*4+3] = 0; |
| eguliyev | 0:99e31762ab2f | 142 | ((char*) ADS129X_data)[3*4+2] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 143 | ((char*) ADS129X_data)[3*4+1] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 144 | ((char*) ADS129X_data)[3*4+0] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 145 | // channel 4 |
| eguliyev | 0:99e31762ab2f | 146 | ((char*) ADS129X_data)[4*4+3] = 0; |
| eguliyev | 0:99e31762ab2f | 147 | ((char*) ADS129X_data)[4*4+2] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 148 | ((char*) ADS129X_data)[4*4+1] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 149 | ((char*) ADS129X_data)[4*4+0] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 150 | // channel 5 |
| eguliyev | 0:99e31762ab2f | 151 | ((char*) ADS129X_data)[5*4+3] = 0; |
| eguliyev | 0:99e31762ab2f | 152 | ((char*) ADS129X_data)[5*4+2] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 153 | ((char*) ADS129X_data)[5*4+1] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 154 | ((char*) ADS129X_data)[5*4+0] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 155 | // channel 6 |
| eguliyev | 0:99e31762ab2f | 156 | ((char*) ADS129X_data)[6*4+3] = 0; |
| eguliyev | 0:99e31762ab2f | 157 | ((char*) ADS129X_data)[6*4+2] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 158 | ((char*) ADS129X_data)[6*4+1] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 159 | ((char*) ADS129X_data)[6*4+0] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 160 | // channel 7 |
| eguliyev | 0:99e31762ab2f | 161 | ((char*) ADS129X_data)[7*4+3] = 0; |
| eguliyev | 0:99e31762ab2f | 162 | ((char*) ADS129X_data)[7*4+2] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 163 | ((char*) ADS129X_data)[7*4+1] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 164 | ((char*) ADS129X_data)[7*4+0] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 165 | // channel 8 |
| eguliyev | 0:99e31762ab2f | 166 | ((char*) ADS129X_data)[8*4+3] = 0; |
| eguliyev | 0:99e31762ab2f | 167 | ((char*) ADS129X_data)[8*4+2] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 168 | ((char*) ADS129X_data)[8*4+1] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 169 | ((char*) ADS129X_data)[8*4+0] = spi.write(0x00); |
| eguliyev | 0:99e31762ab2f | 170 | CS = 1; |
| eguliyev | 0:99e31762ab2f | 171 | ADS129X_newData = true; |
| eguliyev | 0:99e31762ab2f | 172 | } |
| eguliyev | 0:99e31762ab2f | 173 | |
| eguliyev | 0:99e31762ab2f | 174 | bool getData(long *buffer) { |
| eguliyev | 0:99e31762ab2f | 175 | if(ADS129X_newData) { |
| eguliyev | 0:99e31762ab2f | 176 | ADS129X_newData = false; |
| eguliyev | 0:99e31762ab2f | 177 | for (int i = 0; i < 9; i++) { |
| eguliyev | 0:99e31762ab2f | 178 | buffer[i] = ADS129X_data[i]; |
| eguliyev | 0:99e31762ab2f | 179 | } |
| eguliyev | 0:99e31762ab2f | 180 | return true; |
| eguliyev | 0:99e31762ab2f | 181 | } |
| eguliyev | 0:99e31762ab2f | 182 | return false; |
| eguliyev | 0:99e31762ab2f | 183 | } |
| eguliyev | 0:99e31762ab2f | 184 | |
| eguliyev | 0:99e31762ab2f | 185 | |
| eguliyev | 0:99e31762ab2f | 186 | |
| eguliyev | 0:99e31762ab2f | 187 | void configChannel(char _channel, bool _powerDown, char _gain, char _mux) { |
| eguliyev | 0:99e31762ab2f | 188 | char value = ((_powerDown & 1)<<7) | ((_gain & 7)<<4) | (_mux & 7); |
| eguliyev | 0:99e31762ab2f | 189 | WREG(ADS129X_REG_CH1SET + (_channel-1), value); |
| eguliyev | 0:99e31762ab2f | 190 | } |
| eguliyev | 0:99e31762ab2f | 191 | |
| eguliyev | 0:99e31762ab2f | 192 | |
| eguliyev | 0:99e31762ab2f | 193 | |
| eguliyev | 0:99e31762ab2f | 194 | |
| eguliyev | 0:99e31762ab2f | 195 | |
| eguliyev | 0:99e31762ab2f | 196 | |
| eguliyev | 0:99e31762ab2f | 197 | |
| eguliyev | 0:99e31762ab2f | 198 | |
| eguliyev | 0:99e31762ab2f | 199 | |
| eguliyev | 0:99e31762ab2f | 200 | |
| eguliyev | 0:99e31762ab2f | 201 | |
| eguliyev | 0:99e31762ab2f | 202 | |
| eguliyev | 0:99e31762ab2f | 203 | |
| eguliyev | 0:99e31762ab2f | 204 | |
| eguliyev | 0:99e31762ab2f | 205 | |
| eguliyev | 0:99e31762ab2f | 206 | |
| eguliyev | 0:99e31762ab2f | 207 | |
| eguliyev | 0:99e31762ab2f | 208 | |
| eguliyev | 0:99e31762ab2f | 209 | |
| eguliyev | 0:99e31762ab2f | 210 | |
| eguliyev | 0:99e31762ab2f | 211 | |
| eguliyev | 0:99e31762ab2f | 212 | |
| eguliyev | 0:99e31762ab2f | 213 | |
| eguliyev | 0:99e31762ab2f | 214 | |
| eguliyev | 0:99e31762ab2f | 215 | |
| eguliyev | 0:99e31762ab2f | 216 | |
| eguliyev | 0:99e31762ab2f | 217 | |
| eguliyev | 0:99e31762ab2f | 218 | |
| eguliyev | 0:99e31762ab2f | 219 | |
| eguliyev | 0:99e31762ab2f | 220 | |
| eguliyev | 0:99e31762ab2f | 221 | |
| eguliyev | 0:99e31762ab2f | 222 |