ads1259
Dependents: Chromatograph_Mobile Chromatograph_Mobile
ADS1259.cpp@1:533ce2102fcc, 2020-06-22 (annotated)
- Committer:
- vitlog
- Date:
- Mon Jun 22 09:51:45 2020 +0000
- Revision:
- 1:533ce2102fcc
- Parent:
- 0:f9ba28ab9f4c
ne pomnyu chto pomenyal
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vitlog | 0:f9ba28ab9f4c | 1 | #include "ADS1259.h" |
vitlog | 0:f9ba28ab9f4c | 2 | #include "PGA280.h" |
vitlog | 0:f9ba28ab9f4c | 3 | #include <math.h> |
vitlog | 0:f9ba28ab9f4c | 4 | |
vitlog | 0:f9ba28ab9f4c | 5 | |
vitlog | 0:f9ba28ab9f4c | 6 | void ads1259_resetDevice ( unsigned char adr ) |
vitlog | 0:f9ba28ab9f4c | 7 | { |
vitlog | 0:f9ba28ab9f4c | 8 | RESET = 1; |
vitlog | 0:f9ba28ab9f4c | 9 | } |
vitlog | 0:f9ba28ab9f4c | 10 | |
vitlog | 0:f9ba28ab9f4c | 11 | void ads1259_powerDownDevice ( unsigned char adr ) |
vitlog | 0:f9ba28ab9f4c | 12 | { |
vitlog | 0:f9ba28ab9f4c | 13 | pga280_setAdress ( adr ); |
vitlog | 0:f9ba28ab9f4c | 14 | ADS1259_RESET = 0; |
vitlog | 0:f9ba28ab9f4c | 15 | //pga280_resetAdress (); |
vitlog | 0:f9ba28ab9f4c | 16 | } |
vitlog | 0:f9ba28ab9f4c | 17 | |
vitlog | 0:f9ba28ab9f4c | 18 | void ads1259_powerUpDevice ( unsigned char adr ) |
vitlog | 0:f9ba28ab9f4c | 19 | { |
vitlog | 0:f9ba28ab9f4c | 20 | pga280_setAdress ( adr ); |
vitlog | 0:f9ba28ab9f4c | 21 | ADS1259_RESET = 1; |
vitlog | 0:f9ba28ab9f4c | 22 | //pga280_resetAdress (); |
vitlog | 0:f9ba28ab9f4c | 23 | } |
vitlog | 0:f9ba28ab9f4c | 24 | |
vitlog | 0:f9ba28ab9f4c | 25 | void ads1259_conversionON ( unsigned char ch ) |
vitlog | 0:f9ba28ab9f4c | 26 | { |
vitlog | 0:f9ba28ab9f4c | 27 | pga280_setGPIO ( 5, ch ); |
vitlog | 0:f9ba28ab9f4c | 28 | } |
vitlog | 0:f9ba28ab9f4c | 29 | |
vitlog | 0:f9ba28ab9f4c | 30 | void ads1259_conversionOFF ( unsigned char ch ) |
vitlog | 0:f9ba28ab9f4c | 31 | { |
vitlog | 0:f9ba28ab9f4c | 32 | pga280_resetGPIO ( 5, ch); |
vitlog | 0:f9ba28ab9f4c | 33 | } |
vitlog | 0:f9ba28ab9f4c | 34 | |
vitlog | 0:f9ba28ab9f4c | 35 | char ads1259_readyDevice ( unsigned char ch ) |
vitlog | 0:f9ba28ab9f4c | 36 | /*Плохая функция проверки готовности устройства. |
vitlog | 0:f9ba28ab9f4c | 37 | Необходимо сделать прерывание, которое будт выставлять |
vitlog | 0:f9ba28ab9f4c | 38 | флаг или в котором будет происходить считывание*/ |
vitlog | 0:f9ba28ab9f4c | 39 | { |
vitlog | 0:f9ba28ab9f4c | 40 | char tmp; |
vitlog | 0:f9ba28ab9f4c | 41 | |
vitlog | 0:f9ba28ab9f4c | 42 | pga280_setAdress ( ch ); |
vitlog | 0:f9ba28ab9f4c | 43 | |
vitlog | 0:f9ba28ab9f4c | 44 | if ( ADS1259_RDYA == 0 ) |
vitlog | 0:f9ba28ab9f4c | 45 | tmp = true; |
vitlog | 0:f9ba28ab9f4c | 46 | else |
vitlog | 0:f9ba28ab9f4c | 47 | tmp = false; |
vitlog | 0:f9ba28ab9f4c | 48 | |
vitlog | 0:f9ba28ab9f4c | 49 | //pga280_resetAdress (); |
vitlog | 0:f9ba28ab9f4c | 50 | |
vitlog | 0:f9ba28ab9f4c | 51 | return tmp; |
vitlog | 0:f9ba28ab9f4c | 52 | } |
vitlog | 0:f9ba28ab9f4c | 53 | |
vitlog | 0:f9ba28ab9f4c | 54 | unsigned char ads1259_sendCommandDevice ( unsigned char command ) |
vitlog | 0:f9ba28ab9f4c | 55 | { |
vitlog | 0:f9ba28ab9f4c | 56 | //ADS1259_CS = 0; |
vitlog | 0:f9ba28ab9f4c | 57 | SPI1MasterTransferByte ( command ); |
vitlog | 0:f9ba28ab9f4c | 58 | //ADS1259_CS = 1; |
vitlog | 0:f9ba28ab9f4c | 59 | return true; |
vitlog | 0:f9ba28ab9f4c | 60 | } |
vitlog | 0:f9ba28ab9f4c | 61 | |
vitlog | 0:f9ba28ab9f4c | 62 | void ads1259_setCsAndReset ( void ) |
vitlog | 0:f9ba28ab9f4c | 63 | { |
vitlog | 0:f9ba28ab9f4c | 64 | //ADS1259_CS = 0; |
vitlog | 0:f9ba28ab9f4c | 65 | wait_ms ( 100 ); |
vitlog | 0:f9ba28ab9f4c | 66 | //ADS1259_CS = 1; |
vitlog | 0:f9ba28ab9f4c | 67 | wait_ms ( 100 ); |
vitlog | 0:f9ba28ab9f4c | 68 | } |
vitlog | 0:f9ba28ab9f4c | 69 | |
vitlog | 0:f9ba28ab9f4c | 70 | unsigned long ads1259_readRegisterDevice ( unsigned char reg, unsigned char num_reg, unsigned char ch ) |
vitlog | 0:f9ba28ab9f4c | 71 | /*Чтение количества регистров num_reg, начиная с текущего reg по адресу устройства ch*/ |
vitlog | 0:f9ba28ab9f4c | 72 | { |
vitlog | 0:f9ba28ab9f4c | 73 | unsigned long out_value = 0; |
vitlog | 0:f9ba28ab9f4c | 74 | |
vitlog | 0:f9ba28ab9f4c | 75 | uint32_t tmp[num_reg]; |
vitlog | 0:f9ba28ab9f4c | 76 | unsigned char i; |
vitlog | 0:f9ba28ab9f4c | 77 | |
vitlog | 0:f9ba28ab9f4c | 78 | if ( num_reg > 3 ) |
vitlog | 0:f9ba28ab9f4c | 79 | num_reg = 3; |
vitlog | 0:f9ba28ab9f4c | 80 | |
vitlog | 0:f9ba28ab9f4c | 81 | pga280_setGPIO ( 5, ch ); |
vitlog | 0:f9ba28ab9f4c | 82 | //ADS1259_CS = 0; |
vitlog | 0:f9ba28ab9f4c | 83 | SPI1MasterTransferByte ( RREG_ADS1259 | reg ); |
vitlog | 0:f9ba28ab9f4c | 84 | SPI1MasterTransferByte ( num_reg - 1 ); |
vitlog | 0:f9ba28ab9f4c | 85 | |
vitlog | 0:f9ba28ab9f4c | 86 | for ( i = 0; i < num_reg; i++ ) { |
vitlog | 0:f9ba28ab9f4c | 87 | tmp[i] = SPI1MasterTransferByte ( 0x00 ); |
vitlog | 0:f9ba28ab9f4c | 88 | } |
vitlog | 0:f9ba28ab9f4c | 89 | |
vitlog | 0:f9ba28ab9f4c | 90 | //ADS1259_CS = 1; |
vitlog | 0:f9ba28ab9f4c | 91 | pga280_resetGPIO ( 5, ch ); |
vitlog | 0:f9ba28ab9f4c | 92 | |
vitlog | 0:f9ba28ab9f4c | 93 | switch ( num_reg ) { |
vitlog | 0:f9ba28ab9f4c | 94 | case 1: |
vitlog | 0:f9ba28ab9f4c | 95 | |
vitlog | 0:f9ba28ab9f4c | 96 | out_value = tmp[0]; |
vitlog | 0:f9ba28ab9f4c | 97 | break; |
vitlog | 0:f9ba28ab9f4c | 98 | |
vitlog | 0:f9ba28ab9f4c | 99 | case 2: |
vitlog | 0:f9ba28ab9f4c | 100 | |
vitlog | 0:f9ba28ab9f4c | 101 | out_value = (tmp[0] << 8) | tmp[1]; |
vitlog | 0:f9ba28ab9f4c | 102 | break; |
vitlog | 0:f9ba28ab9f4c | 103 | |
vitlog | 0:f9ba28ab9f4c | 104 | case 3: |
vitlog | 0:f9ba28ab9f4c | 105 | |
vitlog | 0:f9ba28ab9f4c | 106 | out_value = ((tmp[0] << 16) | tmp[1] << 8) | tmp[2]; |
vitlog | 0:f9ba28ab9f4c | 107 | break; |
vitlog | 0:f9ba28ab9f4c | 108 | |
vitlog | 0:f9ba28ab9f4c | 109 | default: |
vitlog | 0:f9ba28ab9f4c | 110 | |
vitlog | 0:f9ba28ab9f4c | 111 | out_value = 0xFFFFFFFF; |
vitlog | 0:f9ba28ab9f4c | 112 | break; |
vitlog | 0:f9ba28ab9f4c | 113 | } |
vitlog | 0:f9ba28ab9f4c | 114 | |
vitlog | 0:f9ba28ab9f4c | 115 | return out_value; |
vitlog | 0:f9ba28ab9f4c | 116 | } |
vitlog | 0:f9ba28ab9f4c | 117 | |
vitlog | 0:f9ba28ab9f4c | 118 | unsigned char ads1259_readOneRegisterDevice ( unsigned char reg ) |
vitlog | 0:f9ba28ab9f4c | 119 | { |
vitlog | 0:f9ba28ab9f4c | 120 | unsigned char out_value = 0; |
vitlog | 0:f9ba28ab9f4c | 121 | //ADS1259_CS = 0; |
vitlog | 0:f9ba28ab9f4c | 122 | |
vitlog | 0:f9ba28ab9f4c | 123 | SPI1MasterTransferByte ( RREG_ADS1259 | reg ); |
vitlog | 0:f9ba28ab9f4c | 124 | SPI1MasterTransferByte ( 0 ); |
vitlog | 0:f9ba28ab9f4c | 125 | out_value = SPI1MasterReadByte (); |
vitlog | 0:f9ba28ab9f4c | 126 | |
vitlog | 0:f9ba28ab9f4c | 127 | //ADS1259_CS = 1; |
vitlog | 0:f9ba28ab9f4c | 128 | |
vitlog | 0:f9ba28ab9f4c | 129 | return out_value; |
vitlog | 0:f9ba28ab9f4c | 130 | } |
vitlog | 0:f9ba28ab9f4c | 131 | |
vitlog | 0:f9ba28ab9f4c | 132 | void ads1259_writeOneRegisterDevice ( unsigned char reg, unsigned char data ) |
vitlog | 0:f9ba28ab9f4c | 133 | { |
vitlog | 0:f9ba28ab9f4c | 134 | //ADS1259_CS = 0; |
vitlog | 0:f9ba28ab9f4c | 135 | |
vitlog | 0:f9ba28ab9f4c | 136 | SPI1MasterTransferByte ( WREG_ADS1259 | reg ); |
vitlog | 0:f9ba28ab9f4c | 137 | SPI1MasterTransferByte ( 0 ); |
vitlog | 0:f9ba28ab9f4c | 138 | SPI1MasterTransferByte ( data ); |
vitlog | 0:f9ba28ab9f4c | 139 | |
vitlog | 0:f9ba28ab9f4c | 140 | //ADS1259_CS = 1; |
vitlog | 0:f9ba28ab9f4c | 141 | } |
vitlog | 0:f9ba28ab9f4c | 142 | |
vitlog | 0:f9ba28ab9f4c | 143 | void ads1259_runReadDataContinuedModeDevice ( unsigned char ch ) |
vitlog | 0:f9ba28ab9f4c | 144 | { |
vitlog | 0:f9ba28ab9f4c | 145 | ads1259_sendCommandDevice ( SDATAC_ADS1259 ); // ?????????? ????? ?????? ?????? ? ?????????? ?????? |
vitlog | 0:f9ba28ab9f4c | 146 | ads1259_conversionON ( ch ); // ???????? ??? |
vitlog | 0:f9ba28ab9f4c | 147 | ads1259_sendCommandDevice ( RDATAC_ADS1259 ); // ?????????? ??????? ??????? ???????? ?????? ? ?????????? ?????? |
vitlog | 0:f9ba28ab9f4c | 148 | } |
vitlog | 0:f9ba28ab9f4c | 149 | |
vitlog | 0:f9ba28ab9f4c | 150 | uint32_t ads1259_readDataDevice ( unsigned char ch ) |
vitlog | 0:f9ba28ab9f4c | 151 | { |
vitlog | 0:f9ba28ab9f4c | 152 | uint32_t t_begin; // ????? ?????? |
vitlog | 0:f9ba28ab9f4c | 153 | uint32_t t_end; // ????? ????? |
vitlog | 0:f9ba28ab9f4c | 154 | uint32_t t_current; // ??????? ???????? |
vitlog | 0:f9ba28ab9f4c | 155 | uint32_t data_out = 0; // ???????? ?????? |
vitlog | 0:f9ba28ab9f4c | 156 | |
vitlog | 0:f9ba28ab9f4c | 157 | unsigned char data_ok; // ?????? ??????? |
vitlog | 0:f9ba28ab9f4c | 158 | |
vitlog | 0:f9ba28ab9f4c | 159 | uint32_t data[4]; |
vitlog | 0:f9ba28ab9f4c | 160 | |
vitlog | 0:f9ba28ab9f4c | 161 | ads1259_sendCommandDevice ( SDATAC_ADS1259 ); // ?????????? ????? ?????? ?????? ? ?????????? ?????? |
vitlog | 0:f9ba28ab9f4c | 162 | ads1259_conversionON ( ch ); // ???????? ??? |
vitlog | 0:f9ba28ab9f4c | 163 | ads1259_sendCommandDevice ( RDATA_ADS1259 ); // ?????????? ??????? ??????? ???????? ?????? ? ?????????? ?????? |
vitlog | 0:f9ba28ab9f4c | 164 | |
vitlog | 0:f9ba28ab9f4c | 165 | t_begin = 0; |
vitlog | 0:f9ba28ab9f4c | 166 | |
vitlog | 0:f9ba28ab9f4c | 167 | do { |
vitlog | 0:f9ba28ab9f4c | 168 | if ( ads1259_readyDevice ( ch ) == true ) { |
vitlog | 0:f9ba28ab9f4c | 169 | |
vitlog | 0:f9ba28ab9f4c | 170 | //ADS1259_CS = 0; |
vitlog | 0:f9ba28ab9f4c | 171 | |
vitlog | 0:f9ba28ab9f4c | 172 | data[0] = SPI1MasterReadByte (); |
vitlog | 0:f9ba28ab9f4c | 173 | data[1] = SPI1MasterReadByte (); |
vitlog | 0:f9ba28ab9f4c | 174 | data[2] = SPI1MasterReadByte (); |
vitlog | 0:f9ba28ab9f4c | 175 | |
vitlog | 0:f9ba28ab9f4c | 176 | //ADS1259_CS = 1; |
vitlog | 0:f9ba28ab9f4c | 177 | |
vitlog | 0:f9ba28ab9f4c | 178 | data_ok = true; |
vitlog | 0:f9ba28ab9f4c | 179 | break; |
vitlog | 0:f9ba28ab9f4c | 180 | |
vitlog | 0:f9ba28ab9f4c | 181 | } else { |
vitlog | 0:f9ba28ab9f4c | 182 | data_ok = false; |
vitlog | 0:f9ba28ab9f4c | 183 | } |
vitlog | 0:f9ba28ab9f4c | 184 | |
vitlog | 0:f9ba28ab9f4c | 185 | t_end++; |
vitlog | 0:f9ba28ab9f4c | 186 | t_current = t_end - t_begin; |
vitlog | 0:f9ba28ab9f4c | 187 | |
vitlog | 0:f9ba28ab9f4c | 188 | } while ( t_current < TIMEOUT ); |
vitlog | 0:f9ba28ab9f4c | 189 | |
vitlog | 0:f9ba28ab9f4c | 190 | if ( data_ok ) { |
vitlog | 0:f9ba28ab9f4c | 191 | data_out += (data[0] & 0xFF) << 16; |
vitlog | 0:f9ba28ab9f4c | 192 | data_out += (data[1] & 0xFF) << 8; |
vitlog | 0:f9ba28ab9f4c | 193 | data_out += (data[2] & 0xFF); |
vitlog | 0:f9ba28ab9f4c | 194 | } else { |
vitlog | 0:f9ba28ab9f4c | 195 | data_out = 0xFFFFFF; |
vitlog | 0:f9ba28ab9f4c | 196 | } |
vitlog | 0:f9ba28ab9f4c | 197 | |
vitlog | 0:f9ba28ab9f4c | 198 | ads1259_conversionOFF ( ch ); // ????????? ??? |
vitlog | 0:f9ba28ab9f4c | 199 | |
vitlog | 0:f9ba28ab9f4c | 200 | return data_out; |
vitlog | 0:f9ba28ab9f4c | 201 | } |
vitlog | 0:f9ba28ab9f4c | 202 | |
vitlog | 0:f9ba28ab9f4c | 203 | /* ***************************************************************************** |
vitlog | 0:f9ba28ab9f4c | 204 | End of File |
vitlog | 0:f9ba28ab9f4c | 205 | */ |