ADS131A04 Example C Code
1.0.0
|
#include "ads131a04.h"
Macros | |
#define | WORD_LENGTH (WORD_LENGTH_BITS >> 3) |
#define | UPPER_BYTE(x) ((uint8_t) ((0xFF00 & x) >> 8)) |
#define | LOWER_BYTE(x) ((uint8_t) (0x00FF & x)) |
#define | COMBINE_BYTES(x, y) (((uint16_t) x << 8) | ((uint16_t) y & 0x00FF)) |
Functions | |
uint8_t | getRegisterValue (uint8_t address) |
void | adcStartup (void) |
uint8_t | readSingleRegister (uint8_t address) |
void | writeSingleRegister (uint8_t address, uint8_t data) |
bool | readData (adc_data_struct *dataStruct) |
uint16_t | sendCommand (uint16_t opcode) |
bool | lockRegisters (void) |
bool | unlockRegisters (void) |
uint16_t | calculateCRC (const uint8_t dataBytes[], uint8_t numberBytes, uint16_t initialValue) |
void | restoreRegisterDefaults (void) |
int32_t | signExtend (const uint8_t dataBytes[]) |
Variables | |
static uint8_t | registerMap [((uint8_t) 21)] |
static bool | registersLocked = true |
static uint8_t | dataTx [6 *(((uint8_t) 24) >> 3)] = { 0 } |
static uint8_t | dataRx [6 *(((uint8_t) 24) >> 3)] = { 0 } |
static uint8_t | cmdByteLength = 6 * ( ((uint8_t) 24) >> 3) |
#define COMBINE_BYTES | ( | x, | |
y | |||
) | (((uint16_t) x << 8) | ((uint16_t) y & 0x00FF)) |
#define LOWER_BYTE | ( | x | ) | ((uint8_t) (0x00FF & x)) |
#define UPPER_BYTE | ( | x | ) | ((uint8_t) ((0xFF00 & x) >> 8)) |
#define WORD_LENGTH (WORD_LENGTH_BITS >> 3) |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of Texas Instruments Incorporated nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
void adcStartup | ( | void | ) |
Example start up sequence for the ADS131A04.
Before calling this function, the device must be powered, the SPI/GPIO pins of the MCU must have already been configured, and (if applicable) the external clock source should be provided to CLKIN.
NOTE: You may want to modify this function to configure the ADC's initial register settings to your application's requirements.
uint16_t calculateCRC | ( | const uint8_t | dataBytes[], |
uint8_t | numberBytes, | ||
uint16_t | initialValue | ||
) |
Calculates the 16-bit CRC for the selected CRC polynomial.
dataBytes[] | pointer to first element in the data byte array |
numberBytes | length of data byte array to include in the CRC calculation |
initialValue | the seed value (or partial crc calculation), use 0xFFFF when beginning a new CRC computation |
NOTE: This calculation is shown as an example and has not been optimized for speed.
uint16_t getRegisterValue | ( | uint8_t | address | ) |
Getter function to access registerMap array from outside of this module.
NOTE: The internal registerMap arrays stores the last known register value, since the last read or write operation to that register. This function does not communicate with the device to retrieve the current register value. For the most up-to-date register data or reading the value of a hardware controlled register, it is recommend to use readSingleRegister() to read the device register.
address | is the 8-bit address of the register value to recall. |
bool lockRegisters | ( | void | ) |
Sends the LOCK command and then verifies that registers are locked.
bool readData | ( | adc_data_struct * | dataStruct | ) |
Read ADC data
dataStruct | pointer to data structure where results from reading data will be placed |
NOTE: This is currently the only function in this example that verifies if the CRC word on DOUT is correct.
uint8_t readSingleRegister | ( | uint8_t | address | ) |
Reads the contents of a single register at the specified address.
address | is the 8-bit address of the register to read. |
void restoreRegisterDefaults | ( | void | ) |
Updates the registerMap[] array to its default values.
NOTES:
uint16_t sendCommand | ( | uint16_t | opcode | ) |
Sends the specified SPI command to the ADC (NULL, RESET, STANDBY, or WAKEUP).
opcode | 16-bit SPI command. |
NOTE: Other ADC commands have their own dedicated functions to support additional functionality. This function will raise an assert if used with one of these commands (RREG, WREG, WREGS, LOCK,or UNLOCK).
int32_t signExtend | ( | const uint8_t | dataBytes[] | ) |
Internal function used by readData() to convert ADC data from multiple unsigned bytes into a single signed 32-bit word.
dataBytes | is a pointer to uint8_t[] where the first element is the MSB. |
bool unlockRegisters | ( | void | ) |
Sends the UNLOCK command and then verifies that registers are unlocked
void writeSingleRegister | ( | uint8_t | address, |
uint8_t | data | ||
) |
Writes data to a single register.
This command will be ignored if device registers are locked.
NOTE: This functions also performs a NULL command frame after the register write command to verify the new register value.
address | is the address of the register to write to. |
data | is the value to write. |
|
static |
|
static |
|
static |
|
static |
|
static |