AnalogReadConversion

From chipKIT
Jump to: navigation, search

Synopsis

uint32_t analogReadConversion()

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 analog read 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


analogReadConversion() returns the conversion result directly from the ADC1BUF0 register. Prior to calling this fuction you must call analogReadConversionStart() and then periodically check to see if the result is ready testing if analogReadConversionComplete() returns true;

Return Value

Value Description
0-1024 ADC Result (typically 0-1024 or 10bits)

Note:

  • If either __ALT_ADC_IMPL__ or __PIC32MZECADC__ are defined, 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

analogReadConversion(), analogReadConversionStart(), analogReadConversionComplete()