PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

142 lines
5.0KB

  1. /* Example for analogRead
  2. * You can change the number of averages, bits of resolution and also the comparison value or range.
  3. */
  4. #include <ADC.h>
  5. #include <ADC_util.h>
  6. const int readPin = A9; // ADC0
  7. const int readPin2 = A2; // ADC1
  8. ADC *adc = new ADC(); // adc object;
  9. void setup() {
  10. pinMode(LED_BUILTIN, OUTPUT);
  11. pinMode(readPin, INPUT);
  12. pinMode(readPin2, INPUT);
  13. pinMode(A10, INPUT); //Diff Channel 0 Positive
  14. pinMode(A11, INPUT); //Diff Channel 0 Negative
  15. #ifdef ADC_DUAL_ADCS
  16. pinMode(A12, INPUT); //Diff Channel 3 Positive
  17. pinMode(A13, INPUT); //Diff Channel 3 Negative
  18. #endif
  19. Serial.begin(9600);
  20. Serial.println("Begin setup");
  21. ///// ADC0 ////
  22. // reference can be ADC_REFERENCE::REF_3V3, ADC_REFERENCE::REF_1V2 (not for Teensy LC) or ADC_REFERENCE::REF_EXT.
  23. //adc->adc0->setReference(ADC_REFERENCE::REF_1V2); // change all 3.3 to 1.2 if you change the reference to 1V2
  24. adc->adc0->setAveraging(16); // set number of averages
  25. adc->adc0->setResolution(16); // set bits of resolution
  26. // it can be any of the ADC_CONVERSION_SPEED enum: VERY_LOW_SPEED, LOW_SPEED, MED_SPEED, HIGH_SPEED_16BITS, HIGH_SPEED or VERY_HIGH_SPEED
  27. // see the documentation for more information
  28. // additionally the conversion speed can also be ADACK_2_4, ADACK_4_0, ADACK_5_2 and ADACK_6_2,
  29. // where the numbers are the frequency of the ADC clock in MHz and are independent on the bus speed.
  30. adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::VERY_LOW_SPEED); // change the conversion speed
  31. // it can be any of the ADC_MED_SPEED enum: VERY_LOW_SPEED, LOW_SPEED, MED_SPEED, HIGH_SPEED or VERY_HIGH_SPEED
  32. adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED); // change the sampling speed
  33. // always call the compare functions after changing the resolution!
  34. //adc->adc0->enableCompare(1.0/3.3*adc->adc0->getMaxValue(), 0); // measurement will be ready if value < 1.0V
  35. //adc->adc0->enableCompareRange(1.0*adc->adc0->getMaxValue()/3.3, 2.0*adc->adc0->getMaxValue()/3.3, 0, 1); // ready if value lies out of [1.0,2.0] V
  36. // If you enable interrupts, notice that the isr will read the result, so that isComplete() will return false (most of the time)
  37. //adc->adc0->enableInterrupts(adc0_isr);
  38. ////// ADC1 /////
  39. #ifdef ADC_DUAL_ADCS
  40. adc->adc1->setAveraging(16); // set number of averages
  41. adc->adc1->setResolution(16); // set bits of resolution
  42. adc->adc1->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED); // change the conversion speed
  43. adc->adc1->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED); // change the sampling speed
  44. //adc->adc1->setReference(ADC_REFERENCE::REF_1V2);
  45. // always call the compare functions after changing the resolution!
  46. //adc->adc1->enableCompare(1.0/3.3*adc->adc1->getMaxValue(), 0); // measurement will be ready if value < 1.0V
  47. //adc->adc1->enableCompareRange(1.0*adc->adc1->getMaxValue()/3.3, 2.0*adc->adc1->getMaxValue()/3.3, 0, 1); // ready if value lies out of [1.0,2.0] V
  48. // If you enable interrupts, note that the isr will read the result, so that isComplete() will return false (most of the time)
  49. //adc->adc1->enableInterrupts(adc1_isr);
  50. #endif
  51. Serial.println("End setup");
  52. }
  53. int value;
  54. int value2;
  55. void loop() {
  56. // Single reads
  57. value = adc->adc0->analogRead(readPin); // read a new value, will return ADC_ERROR_VALUE if the comparison is false.
  58. Serial.print("Pin: ");
  59. Serial.print(readPin);
  60. Serial.print(", value ADC0: ");
  61. Serial.println(value*3.3/adc->adc0->getMaxValue(), DEC);
  62. #ifdef ADC_DUAL_ADCS
  63. value2 = adc->adc1->analogRead(readPin2);
  64. Serial.print("Pin: ");
  65. Serial.print(readPin2);
  66. Serial.print(", value ADC1: ");
  67. Serial.println(value2*3.3/adc->adc1->getMaxValue(), DEC);
  68. #endif
  69. // Differential reads
  70. #if ADC_DIFF_PAIRS > 0
  71. #ifdef ADC_USE_PGA
  72. double V_per_bit = 3.3/adc->adc0->getPGA()/adc->adc0->getMaxValue();
  73. #else
  74. double V_per_bit = 3.3/adc->adc0->getMaxValue();
  75. #endif
  76. value = adc->adc0->analogReadDifferential(A10, A11); // read a new value, will return ADC_ERROR_VALUE if the comparison is false.
  77. Serial.print(" Value A10-A11: ");
  78. // Divide by the maximum possible value and the PGA level
  79. Serial.println(value*V_per_bit, DEC);
  80. #ifdef ADC_DUAL_ADCS
  81. value2 = adc->adc1->analogReadDifferential(A12, A13);
  82. Serial.print(" Value A12-A13: ");
  83. Serial.println(value2*V_per_bit, DEC);
  84. #endif
  85. #endif
  86. // Print errors, if any.
  87. if(adc->adc0->fail_flag != ADC_ERROR::CLEAR) {
  88. Serial.print("ADC0: "); Serial.println(getStringADCError(adc->adc0->fail_flag));
  89. }
  90. #ifdef ADC_DUAL_ADCS
  91. if(adc->adc1->fail_flag != ADC_ERROR::CLEAR) {
  92. Serial.print("ADC1: "); Serial.println(getStringADCError(adc->adc1->fail_flag));
  93. }
  94. #endif
  95. digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  96. delay(50);
  97. }
  98. // If you enable interrupts make sure to call readSingle() to clear the interrupt.
  99. void adc0_isr() {
  100. adc->adc0->readSingle();
  101. }