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.
AD7190.cpp@2:dc4217ca1fff, 2014-01-28 (annotated)
- Committer:
- MarioPoneder
- Date:
- Tue Jan 28 13:18:17 2014 +0000
- Revision:
- 2:dc4217ca1fff
- Parent:
- 1:00d6e45e037a
doc done
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| MarioPoneder | 0:49fe1d7a6628 | 1 | #include "AD7190.h" |
| MarioPoneder | 0:49fe1d7a6628 | 2 | |
| MarioPoneder | 0:49fe1d7a6628 | 3 | /***************************************************************************//** |
| MarioPoneder | 2:dc4217ca1fff | 4 | * @brief Creates an instance of the AD7190 class. This class is designed |
| MarioPoneder | 1:00d6e45e037a | 5 | * for single slave operation. Therefore no chip-select pin is required. |
| MarioPoneder | 0:49fe1d7a6628 | 6 | * |
| MarioPoneder | 0:49fe1d7a6628 | 7 | * @param rdy RDY (DOUT/RDY) |
| MarioPoneder | 0:49fe1d7a6628 | 8 | * @param mosi DIN |
| MarioPoneder | 0:49fe1d7a6628 | 9 | * @param miso DOUT (DOUT/RDY), must be a different pin than rdy. |
| MarioPoneder | 0:49fe1d7a6628 | 10 | * @param sclk SCLK |
| MarioPoneder | 0:49fe1d7a6628 | 11 | *******************************************************************************/ |
| MarioPoneder | 0:49fe1d7a6628 | 12 | AD7190::AD7190(PinName rdy, PinName mosi, PinName miso, PinName sclk) |
| MarioPoneder | 0:49fe1d7a6628 | 13 | : _rdy(rdy), _rdyInt(rdy), _spi(mosi, miso, sclk) |
| MarioPoneder | 0:49fe1d7a6628 | 14 | { |
| MarioPoneder | 0:49fe1d7a6628 | 15 | ; |
| MarioPoneder | 0:49fe1d7a6628 | 16 | } |
| MarioPoneder | 0:49fe1d7a6628 | 17 | |
| MarioPoneder | 0:49fe1d7a6628 | 18 | /***************************************************************************//** |
| MarioPoneder | 0:49fe1d7a6628 | 19 | * @brief This interrupt routine gets called after every finished conversion |
| MarioPoneder | 0:49fe1d7a6628 | 20 | * in continous read mode. It reads 4 bytes of data, the sample takes |
| MarioPoneder | 0:49fe1d7a6628 | 21 | * 3 bytes and the status register 1 byte. |
| MarioPoneder | 2:dc4217ca1fff | 22 | * Moreover the sample callback function gets called. |
| MarioPoneder | 0:49fe1d7a6628 | 23 | * |
| MarioPoneder | 0:49fe1d7a6628 | 24 | * @return None, because this is an interrupt routine. |
| MarioPoneder | 0:49fe1d7a6628 | 25 | *******************************************************************************/ |
| MarioPoneder | 0:49fe1d7a6628 | 26 | void AD7190::SampleInterrupt(void) |
| MarioPoneder | 0:49fe1d7a6628 | 27 | { |
| MarioPoneder | 0:49fe1d7a6628 | 28 | static unsigned long buffer; |
| MarioPoneder | 0:49fe1d7a6628 | 29 | static unsigned char i; |
| MarioPoneder | 0:49fe1d7a6628 | 30 | this->_rdyInt.fall(NULL); |
| MarioPoneder | 0:49fe1d7a6628 | 31 | for(i = 1; i <= 4; i++) { |
| MarioPoneder | 0:49fe1d7a6628 | 32 | buffer = (buffer << 8) + _spi.write(0x00); |
| MarioPoneder | 0:49fe1d7a6628 | 33 | } |
| MarioPoneder | 0:49fe1d7a6628 | 34 | this->sampleCallbackFunction(buffer >> 8, buffer & 0x07); |
| MarioPoneder | 0:49fe1d7a6628 | 35 | this->_rdyInt.fall(this, &AD7190::SampleInterrupt); |
| MarioPoneder | 0:49fe1d7a6628 | 36 | } |
| MarioPoneder | 0:49fe1d7a6628 | 37 | |
| MarioPoneder | 0:49fe1d7a6628 | 38 | /***************************************************************************//** |
| MarioPoneder | 0:49fe1d7a6628 | 39 | * @brief Starts the interrupt-driven sampling process and sets the AD7190's |
| MarioPoneder | 0:49fe1d7a6628 | 40 | * continuous read mode flag. |
| MarioPoneder | 0:49fe1d7a6628 | 41 | * |
| MarioPoneder | 2:dc4217ca1fff | 42 | * @param sampleCallbackFunction Pointer to the sample callback function. |
| MarioPoneder | 2:dc4217ca1fff | 43 | * Example prototype: |
| MarioPoneder | 2:dc4217ca1fff | 44 | * @code void SampleCallback(unsigned long data, unsigned char channel); @endcode |
| MarioPoneder | 0:49fe1d7a6628 | 45 | * |
| MarioPoneder | 0:49fe1d7a6628 | 46 | * @return None. |
| MarioPoneder | 0:49fe1d7a6628 | 47 | *******************************************************************************/ |
| MarioPoneder | 0:49fe1d7a6628 | 48 | void AD7190::StartContinuousRead(pSampleCallback_t sampleCallbackFunction) |
| MarioPoneder | 0:49fe1d7a6628 | 49 | { |
| MarioPoneder | 0:49fe1d7a6628 | 50 | this->sampleCallbackFunction = sampleCallbackFunction; |
| MarioPoneder | 0:49fe1d7a6628 | 51 | this->WaitRdyGoLow(); |
| MarioPoneder | 0:49fe1d7a6628 | 52 | this->_spi.write(COMM_READ | COMM_CREAD | COMM_ADDR(REG_DATA)); |
| MarioPoneder | 0:49fe1d7a6628 | 53 | this->_rdyInt.fall(this, &AD7190::SampleInterrupt); |
| MarioPoneder | 0:49fe1d7a6628 | 54 | } |
| MarioPoneder | 0:49fe1d7a6628 | 55 | |
| MarioPoneder | 0:49fe1d7a6628 | 56 | /***************************************************************************//** |
| MarioPoneder | 0:49fe1d7a6628 | 57 | * @brief Stops the interrupt-driven sampling process and disables the AD7190's |
| MarioPoneder | 0:49fe1d7a6628 | 58 | * continuous read mode. |
| MarioPoneder | 0:49fe1d7a6628 | 59 | * |
| MarioPoneder | 0:49fe1d7a6628 | 60 | * @return None. |
| MarioPoneder | 0:49fe1d7a6628 | 61 | *******************************************************************************/ |
| MarioPoneder | 0:49fe1d7a6628 | 62 | void AD7190::StopContinuousRead(void) |
| MarioPoneder | 0:49fe1d7a6628 | 63 | { |
| MarioPoneder | 0:49fe1d7a6628 | 64 | this->_rdyInt.fall(NULL); |
| MarioPoneder | 0:49fe1d7a6628 | 65 | this->WaitRdyGoLow(); |
| MarioPoneder | 0:49fe1d7a6628 | 66 | this->_spi.write(COMM_READ | COMM_ADDR(REG_DATA)); |
| MarioPoneder | 0:49fe1d7a6628 | 67 | } |
| MarioPoneder | 0:49fe1d7a6628 | 68 | |
| MarioPoneder | 0:49fe1d7a6628 | 69 | /***************************************************************************//** |
| MarioPoneder | 0:49fe1d7a6628 | 70 | * @brief Writes the specified contents to the specified on-chip register. |
| MarioPoneder | 0:49fe1d7a6628 | 71 | * |
| MarioPoneder | 0:49fe1d7a6628 | 72 | * @param registerAddress Address of the register. |
| MarioPoneder | 0:49fe1d7a6628 | 73 | * @param registerValue Contents to write. |
| MarioPoneder | 0:49fe1d7a6628 | 74 | * |
| MarioPoneder | 0:49fe1d7a6628 | 75 | * @return None. |
| MarioPoneder | 0:49fe1d7a6628 | 76 | *******************************************************************************/ |
| MarioPoneder | 0:49fe1d7a6628 | 77 | void AD7190::SetRegisterValue(unsigned char registerAddress, unsigned long registerValue) |
| MarioPoneder | 0:49fe1d7a6628 | 78 | { |
| MarioPoneder | 0:49fe1d7a6628 | 79 | static unsigned char *dataPointer, bytesNr; |
| MarioPoneder | 0:49fe1d7a6628 | 80 | dataPointer = (unsigned char*)®isterValue; |
| MarioPoneder | 0:49fe1d7a6628 | 81 | |
| MarioPoneder | 0:49fe1d7a6628 | 82 | switch(registerAddress) { |
| MarioPoneder | 0:49fe1d7a6628 | 83 | case REG_ID: |
| MarioPoneder | 0:49fe1d7a6628 | 84 | case REG_GPOCON: |
| MarioPoneder | 0:49fe1d7a6628 | 85 | bytesNr = 1; |
| MarioPoneder | 0:49fe1d7a6628 | 86 | break; |
| MarioPoneder | 0:49fe1d7a6628 | 87 | case REG_MODE: |
| MarioPoneder | 0:49fe1d7a6628 | 88 | dataPointer += 2; |
| MarioPoneder | 0:49fe1d7a6628 | 89 | case REG_CONF: |
| MarioPoneder | 0:49fe1d7a6628 | 90 | registerValue |= (1 << 19); |
| MarioPoneder | 0:49fe1d7a6628 | 91 | case REG_OFFSET: |
| MarioPoneder | 0:49fe1d7a6628 | 92 | case REG_FULLSCALE: |
| MarioPoneder | 0:49fe1d7a6628 | 93 | bytesNr = 3; |
| MarioPoneder | 0:49fe1d7a6628 | 94 | break; |
| MarioPoneder | 0:49fe1d7a6628 | 95 | default: |
| MarioPoneder | 0:49fe1d7a6628 | 96 | bytesNr = 0; |
| MarioPoneder | 0:49fe1d7a6628 | 97 | } |
| MarioPoneder | 0:49fe1d7a6628 | 98 | |
| MarioPoneder | 0:49fe1d7a6628 | 99 | this->_spi.write(COMM_WRITE | COMM_ADDR(registerAddress)); |
| MarioPoneder | 0:49fe1d7a6628 | 100 | for(; bytesNr > 0; bytesNr--) { |
| MarioPoneder | 0:49fe1d7a6628 | 101 | this->_spi.write(*dataPointer); |
| MarioPoneder | 0:49fe1d7a6628 | 102 | registerAddress == REG_MODE ? dataPointer -- : dataPointer ++; |
| MarioPoneder | 0:49fe1d7a6628 | 103 | } |
| MarioPoneder | 0:49fe1d7a6628 | 104 | } |
| MarioPoneder | 0:49fe1d7a6628 | 105 | |
| MarioPoneder | 0:49fe1d7a6628 | 106 | /***************************************************************************//** |
| MarioPoneder | 0:49fe1d7a6628 | 107 | * @brief Reads the contents of the specified on-chip register. |
| MarioPoneder | 0:49fe1d7a6628 | 108 | * |
| MarioPoneder | 0:49fe1d7a6628 | 109 | * @param registerAddress Address of the register. |
| MarioPoneder | 0:49fe1d7a6628 | 110 | * |
| MarioPoneder | 0:49fe1d7a6628 | 111 | * @return Contents of the register. |
| MarioPoneder | 0:49fe1d7a6628 | 112 | *******************************************************************************/ |
| MarioPoneder | 0:49fe1d7a6628 | 113 | unsigned long AD7190::GetRegisterValue(unsigned char registerAddress) |
| MarioPoneder | 0:49fe1d7a6628 | 114 | { |
| MarioPoneder | 0:49fe1d7a6628 | 115 | static unsigned long buffer = 0; |
| MarioPoneder | 0:49fe1d7a6628 | 116 | static unsigned char bytesNr, i; |
| MarioPoneder | 0:49fe1d7a6628 | 117 | |
| MarioPoneder | 0:49fe1d7a6628 | 118 | switch (registerAddress) { |
| MarioPoneder | 0:49fe1d7a6628 | 119 | case REG_STAT: |
| MarioPoneder | 0:49fe1d7a6628 | 120 | case REG_ID: |
| MarioPoneder | 0:49fe1d7a6628 | 121 | case REG_GPOCON: |
| MarioPoneder | 0:49fe1d7a6628 | 122 | bytesNr = 1; |
| MarioPoneder | 0:49fe1d7a6628 | 123 | break; |
| MarioPoneder | 0:49fe1d7a6628 | 124 | case REG_MODE: |
| MarioPoneder | 0:49fe1d7a6628 | 125 | case REG_CONF: |
| MarioPoneder | 0:49fe1d7a6628 | 126 | case REG_DATA: |
| MarioPoneder | 0:49fe1d7a6628 | 127 | case REG_OFFSET: |
| MarioPoneder | 0:49fe1d7a6628 | 128 | case REG_FULLSCALE: |
| MarioPoneder | 0:49fe1d7a6628 | 129 | bytesNr = 3; |
| MarioPoneder | 0:49fe1d7a6628 | 130 | break; |
| MarioPoneder | 0:49fe1d7a6628 | 131 | default: |
| MarioPoneder | 0:49fe1d7a6628 | 132 | bytesNr = 0; |
| MarioPoneder | 0:49fe1d7a6628 | 133 | } |
| MarioPoneder | 0:49fe1d7a6628 | 134 | |
| MarioPoneder | 0:49fe1d7a6628 | 135 | this->_spi.write(COMM_READ | COMM_ADDR(registerAddress)); |
| MarioPoneder | 0:49fe1d7a6628 | 136 | for(i = 1; i <= bytesNr; i++) { |
| MarioPoneder | 0:49fe1d7a6628 | 137 | buffer = (buffer << 8) + _spi.write(0x00); |
| MarioPoneder | 0:49fe1d7a6628 | 138 | } |
| MarioPoneder | 0:49fe1d7a6628 | 139 | return buffer; |
| MarioPoneder | 0:49fe1d7a6628 | 140 | } |
| MarioPoneder | 0:49fe1d7a6628 | 141 | |
| MarioPoneder | 0:49fe1d7a6628 | 142 | /***************************************************************************//** |
| MarioPoneder | 0:49fe1d7a6628 | 143 | * @brief Initialises the SPI bus, resets the AD7190 and checks if it is |
| MarioPoneder | 0:49fe1d7a6628 | 144 | * responding. |
| MarioPoneder | 0:49fe1d7a6628 | 145 | * |
| MarioPoneder | 0:49fe1d7a6628 | 146 | * @return True, if the ADC is respondig |
| MarioPoneder | 0:49fe1d7a6628 | 147 | *******************************************************************************/ |
| MarioPoneder | 0:49fe1d7a6628 | 148 | bool AD7190::Init(void) |
| MarioPoneder | 0:49fe1d7a6628 | 149 | { |
| MarioPoneder | 0:49fe1d7a6628 | 150 | unsigned char regVal; |
| MarioPoneder | 0:49fe1d7a6628 | 151 | |
| MarioPoneder | 0:49fe1d7a6628 | 152 | this->_spi.format(8, 3); |
| MarioPoneder | 0:49fe1d7a6628 | 153 | this->_spi.frequency(10000000); |
| MarioPoneder | 0:49fe1d7a6628 | 154 | this->Reset(); |
| MarioPoneder | 0:49fe1d7a6628 | 155 | wait_ms(1); |
| MarioPoneder | 0:49fe1d7a6628 | 156 | regVal = this->GetRegisterValue(REG_ID); |
| MarioPoneder | 0:49fe1d7a6628 | 157 | |
| MarioPoneder | 0:49fe1d7a6628 | 158 | return (regVal & ID_MASK) == ID_AD7190 ; |
| MarioPoneder | 0:49fe1d7a6628 | 159 | } |
| MarioPoneder | 0:49fe1d7a6628 | 160 | |
| MarioPoneder | 0:49fe1d7a6628 | 161 | /***************************************************************************//** |
| MarioPoneder | 0:49fe1d7a6628 | 162 | * @brief Resets the AD7190. |
| MarioPoneder | 0:49fe1d7a6628 | 163 | * |
| MarioPoneder | 0:49fe1d7a6628 | 164 | * @return None. |
| MarioPoneder | 0:49fe1d7a6628 | 165 | *******************************************************************************/ |
| MarioPoneder | 0:49fe1d7a6628 | 166 | void AD7190::Reset(void) |
| MarioPoneder | 0:49fe1d7a6628 | 167 | { |
| MarioPoneder | 0:49fe1d7a6628 | 168 | static unsigned char i; |
| MarioPoneder | 0:49fe1d7a6628 | 169 | this->_spi.write(0x01); |
| MarioPoneder | 0:49fe1d7a6628 | 170 | for(i = 0; i < 6; i++) { |
| MarioPoneder | 0:49fe1d7a6628 | 171 | this->_spi.write(0xFF); |
| MarioPoneder | 0:49fe1d7a6628 | 172 | } |
| MarioPoneder | 0:49fe1d7a6628 | 173 | } |
| MarioPoneder | 0:49fe1d7a6628 | 174 | |
| MarioPoneder | 0:49fe1d7a6628 | 175 | /***************************************************************************//** |
| MarioPoneder | 0:49fe1d7a6628 | 176 | * @brief Waits for the RDY (DOUT/RDY) pin to go low. |
| MarioPoneder | 0:49fe1d7a6628 | 177 | * |
| MarioPoneder | 0:49fe1d7a6628 | 178 | * @return None. |
| MarioPoneder | 0:49fe1d7a6628 | 179 | *******************************************************************************/ |
| MarioPoneder | 0:49fe1d7a6628 | 180 | void AD7190::WaitRdyGoLow(void) |
| MarioPoneder | 0:49fe1d7a6628 | 181 | { |
| MarioPoneder | 0:49fe1d7a6628 | 182 | for(; this->_rdy;); |
| MarioPoneder | 0:49fe1d7a6628 | 183 | } |