PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.0KB

  1. /* Example for synchonized measurements using both ADC present in Teensy 3.1
  2. * You can change the number of averages, bits of resolution and also the comparison value or range.
  3. */
  4. #ifdef ADC_DUAL_ADCS
  5. #include <ADC.h>
  6. #include <ADC_util.h>
  7. const int readPin = A9;
  8. const int readPin2 = A3;
  9. ADC *adc = new ADC(); // adc object
  10. elapsedMicros time;
  11. void setup() {
  12. pinMode(LED_BUILTIN, OUTPUT);
  13. pinMode(readPin, INPUT);
  14. pinMode(readPin2, INPUT);
  15. Serial.begin(9600);
  16. ///// ADC0 ////
  17. adc->adc0->setAveraging(1); // set number of averages
  18. adc->adc0->setResolution(8); // set bits of resolution
  19. adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed
  20. adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED); // change the sampling speed
  21. ////// ADC1 /////
  22. adc->adc1->setAveraging(1); // set number of averages
  23. adc->adc1->setResolution(8); // set bits of resolution
  24. adc->adc1->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed
  25. adc->adc1->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED); // change the sampling speed
  26. adc->startSynchronizedContinuous(readPin, readPin2);
  27. // You can also try:
  28. //adc->startSynchronizedContinuousDifferential(A10, A11, A12, A13);
  29. // Read the values in the loop() with readSynchronizedContinuous()
  30. delay(100);
  31. Serial.println("end setup");
  32. }
  33. int value = 0;
  34. int value2 = 0;
  35. ADC::Sync_result result;
  36. void loop() {
  37. // You can also try:
  38. //result = adc->analogSynchronizedRead(readPin, readPin2);
  39. //result = adc->analogSynchronizedReadDifferential(A10, A11, A12, A13);
  40. result = adc->readSynchronizedContinuous();
  41. // if using 16 bits and single-ended is necessary to typecast to unsigned,
  42. // otherwise values larger than 3.3/2 will be interpreted as negative
  43. result.result_adc0 = (uint16_t)result.result_adc0;
  44. result.result_adc1 = (uint16_t)result.result_adc1;
  45. //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  46. //Serial.print("Value ADC0: ");
  47. Serial.print(time, DEC);
  48. Serial.print(" ");
  49. Serial.print(result.result_adc0*3.3/adc->adc0->getMaxValue(), DEC);
  50. Serial.print(" ");
  51. Serial.println(result.result_adc1*3.3/adc->adc1->getMaxValue(), DEC);
  52. // Print errors, if any.
  53. if(adc->adc0->fail_flag != ADC_ERROR::CLEAR) {
  54. Serial.print("ADC0: "); Serial.println(getStringADCError(adc->adc0->fail_flag));
  55. }
  56. #ifdef ADC_DUAL_ADCS
  57. if(adc->adc1->fail_flag != ADC_ERROR::CLEAR) {
  58. Serial.print("ADC1: "); Serial.println(getStringADCError(adc->adc1->fail_flag));
  59. }
  60. #endif
  61. digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  62. //delay(100);
  63. }
  64. /*
  65. With a AWG I generated a sine wave of 1 Hz and 2 V amplitude.
  66. I measure synchronously on pins A0 (ADC0) and A2 (ADC1), sampling at 20 Hz (every 50ms).
  67. The relative error: mean(value(A0)-value(A2))/mean(value(A0)) is approx 0.02%
  68. */
  69. #else // make sure the example can run for any boards (automated testing)
  70. void setup() {}
  71. void loop() {}
  72. #endif // ADC_DUAL_ADCS