AnalogReadConversionComplete

From chipKIT
Jump to: navigation, search

Synopsis

uint32_t analogReadConversionComplete()

Parameter

None

Description

NOTE:

  • DO NOT mix blocking and non-blocking analog reads as it could hang your board.
  • This functionality was added in chipKIT-core 1.4.0 and is specific to chipKIT boards only. If you need your code to be compatible with Arduino boards you should use the original analogRead() function. There are workarounds that are available for Arduino but at this writing there is no standardized support in the Arduino wiring libraries for non-blocking analogRead().


In an effort to increase the number of times per second the loop() function is called while using analogRead functions, non­-blocking equivalent functions have been implemented into the chipKIT-­core. Test have shown that these non-blocking analogRead functions are about 2x faster on a PIC32MX (Lenny, FubarinoSD) based chipKIT board and about 4.9x faster on a PIC32MZ (Wi-Fire) based boards.

chipKIT Board PIC32 Clock Freq loop() Frequency Blocking Analog Read loop() Frequency Non-Blocking Analog Read Improvement
Lenny MX 40MHz 49.14 kHz 127.39 kHz 2.59
Fubarino MX 80MHz 90.91kHz 217.39 kHz 2.39
Wi-Fire MZEFG 200MHz 233.10 kHz 1162.79 kHz 4.98

analogReadConversionComplete() is directly accessing the AD1CON1.DONE flag. This flag indicates that the conversion from analog to digital is finished and ready to be read by analogReadConversion(). In other words when you call the analogConversionStart() function you are telling the hardware to take an analog sample and convert it to digital. This task takes time and therefore you may not want to wait around for that to finish. This flag allows you to continue with code exectuation and only periodlically check to see if the conversion is complete. When the conversion is complete you would then call analogReadConversion() to get the result.

Return Value

Value Description
False Result Not Ready
True Result Ready

Note: If either __ALT_ADC_IMPL__ or __PIC32MZECADC__ are defined, analogReadConversionComplete() will always return True and analogReadConversion() will be blocking when called.

Conforming To

The non-blocking analogReadxxx Functions are specific to ChipKit 1.4.0 and later and exceed the Arduino 1.6.x specification.

Example

This example starts an analog read in setup() and then checks to see if the result is ready in the loop() function. If a result is ready the ADC value is assigned to the 'value' variable and a new non-blocking analog read is started. If a result is not ready the code simply continues to execute and does not wait. The ability to continue is why we call this non-blocking.

#define ADC_TEST_PIN 16
 
void setup() {
    analogReadConversionStart(ADC_TEST_PIN);
}
 
void loop() {
    uint32_t value;
    if ( analogReadConversionComplete() ) {
      value = analogReadConversion();
      analogReadConversionStart(ADC_TEST_PIN);
    }
}


See Also

analogReadConversionComplete(), analogReadConversionStart(), analogReadConversion()