Skip to main content

ADC

The following functions are available for configuring and reading analog data with the ADC. The "X" in any function name must be replaced by the channel number (1,2,3 or 4):

configureADC

void configureADC(uint8_t channel, uint16_t fire_every_us, uint16_t fire_delay, adc_scale_t scale, void (*cb_function)(void) );

This function configures the ADC voltage range, at what interval the ADC reads a voltage and what function to call when new ADC data is ready. The function takes the following arguments:

  • channel ADC channel to read from. Must be 1, 2, 3 or 4.

  • fire_every_us 16-bit unsigned integer specifying how many μs to wait between ADC readings. The smallest value allowed is 1, which tells the ADC to fire every μs, or at 1 MHz. Since the ADC has a max data rate of 1 mega samples per second (MSPS), running at 1 MHz maximizes the ADC data rate and the remaining ADC channels cannot be used. The max value to 65535 which causes to teh ADC to read data every 65,535 μs, or at 15.26 Hz.

  • fire_delay 16-bit unsigned integer specifying how many μs to wail before firing. This controls the timing relationship for reading the voltage between multiple channels. If two channels are configured to fire every 10μs, then setting the fire_delay to 0 and 1 for the two active channels would have the two channels to fire 1μs apart and then wait 10μs before reading data again. Alternatively, setting fire_delay to 0 and 5 for the two active channels has the second channel read ADC data 5μs after the first channel collects data. In this setup, ADC data is read every 5μs, toggling the active channel each time.

  • scale Configures the input voltage range the for ADC. Valid options are:

    • BIPOLAR_1250mV : ±1.25V max range
    • BIPOLAR_2500mV: ±2.5V max range
    • BIPOLAR_5V: ±5V max range
    • BIPOLAR_10V: ±10V max range
  • cb_function Callback Function is called when ADC channel X has new data. Function must take no arguments, nor return anything. This should have the structure of

    void cb_function(void)
    Make sure to call readADCX_from_ISR()!!

    This custom function must call either readADCX_From_ISR or readADCXRAW_From_ISR (and X must match the channel). This clears the interrupt saying there is ADC data at the ready. If this is not called, the program will run the custom function over and over again and the Quarto will crash.

Example

configureADC1(5, 0, BIPOLAR_1250mV, getADC1); //±1.25V, runs every 5μs
configureADC2(10, 1, BIPOLAR_2500mV, getADC2); //±2.5V, runs every 10μs, 1μ after ADC1 is read
configureADC3(10, 7, BIPOLAR_5V, getADC3); //±5V, runs every 10μs, 6μ after ADC2 is read
configureADC4(10, 8, BIPOLAR_10V, getADC4);//±10V, runs every 10μs, 8μ after ADC2 is read

void getADC1(void) {
double newadc = readADC1_from_ISR(); //Read ADC data as a double
...
}
void getADC2(void) {
double newadc = readADC2_from_ISR(); //Read ADC data as a double
...
}
void getADC3(void) {
int16_t newadc = readADCRAW3_from_ISR(); //Read ADC data as an signed 16-bit integer
...
}
void getADC4(void) {
int16_t newadc = readADCRAW4_from_ISR(); //Read ADC data as an signed 16-bit integer
...
}

readADCX_from_ISR

double readADCX_from_ISR(void);

This function reads the ADC data when it is ready. It takes no arguments and returns the ADC voltage as a double-precision float.

Only call in ADC callback function!!

This function should only be called by a function configured as an ADC callback function, such as getADC1 in the previous example. Otherwise, this function can return invalid data and it can corrupt other ADC measurements.

Example

See the above example for example usage.

readADCXRAW_from_ISR

int16_t readADCXRAW_from_ISR(void);

This function is identical to readADCX_from_ISR except it returns the RAW ADC data as a signed 16-bit number. Scaling this number to voltage requires multiplying it by a number that depends on what ADC range the ADC is set to:

  • ADC Range BIPOLAR_1250mV: 1.28V/32768 = 3.90625e-5
  • ADC Range BIPOLAR_2500mV: 2.56V/32768 = 7.8125e-5
  • ADC Range BIPOLAR_5V: 5.12V/32768 = 1.5625e-4
  • ADC Range BIPOLAR_10V: 10.24V/32768 = 3.125e-4
Only call in ADC callback function!!

This function should only be called by a function configured as an ADC callback function, such as getADC1 in the above example. Otherwise, this function can return invalid data and it can corrupt other ADC measurements.

Example

configureADC1(5, 0, BIPOLAR_1250mV, getADC1_RAW); //±1.25V, runs every 5μs
configureADC1(5, 2, BIPOLAR_2500mV, getADC2_RAW); //±2.5V, runs every 5μs
int32_t adc_diff = 0;

void getADC1_RAW(void) {
int16_t newadc = readADC1RAW_from_ISR(); //Read ADC data as a double
adc_sum += newadc; // add new raw ADC1 measurement to adc_sum total
}
void getADC2_RAW(void) {
int16_t newadc = readADC2RAW_from_ISR(); //Read ADC data as a double
adc_sum -= 2 * newadc; // subtract new raw ADC2 measurement to adc_sum total
// multiply by two because ADC2 range is ±2.5V and ADC1 range is only ±1.25V
}

disableADC

void disableADC(uint8_t channel);

This function takes a single argument to select the channel to disable. It disables ADC collection on channel and the ADC callback function will no longer get called.

  • channel ADC channel to disable. Must be 1, 2, 3 or 4.

Example

void ADC_collection(bool on) {
if (on == true) {
configureADC(1,5, 0, BIPOLAR_1250mV, getADC1); //±1.25V, runs every 5μs
//turn on ADC collection on channel 1
} else {
disableADC(1); //turn off ADC collection on channel 1
}
}

void getADC1(void) {
double newadc = readADC1_from_ISR(); //Read ADC data as a double
...
}