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.

168 lines
5.5KB

  1. /* Example for analogContinuousRead
  2. * It measures continuously the voltage on pin A9,
  3. * Write v and press enter on the serial console to get the value
  4. * Write c and press enter on the serial console to check that the conversion is taking place,
  5. * Write t to check if the voltage agrees with the comparison in the setup()
  6. * Write s to stop the conversion, you can restart it writing r.
  7. */
  8. #include <ADC.h>
  9. #include <ADC_util.h>
  10. ADC *adc = new ADC(); // adc object
  11. #if defined(ADC_TEENSY_LC) // teensy LC
  12. #define PINS 13
  13. #define PINS_DIFF 2
  14. uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12};
  15. uint8_t adc_pins_diff[] = {A10, A11};
  16. #elif defined(ADC_TEENSY_3_0) // teensy 3.0
  17. #define PINS 14
  18. #define PINS_DIFF 4
  19. uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13};
  20. uint8_t adc_pins_diff[] = {A10, A11, A12, A13};
  21. #elif defined(ADC_TEENSY_3_1) || defined(ADC_TEENSY_3_2) // teensy 3.1/3.2
  22. #define PINS 21
  23. #define PINS_DIFF 4
  24. uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13,
  25. A14, A15, A16, A17, A18, A19, A20};
  26. uint8_t adc_pins_diff[] = {A10, A11, A12, A13};
  27. #elif defined(ADC_TEENSY_3_5) // Teensy 3.5
  28. #define PINS 27
  29. #define PINS_DIFF 2
  30. uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10,
  31. A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21, A22, A23, A24, A25, A26};
  32. uint8_t adc_pins_diff[] = {A10, A11};
  33. #elif defined(ADC_TEENSY_3_6) // Teensy 3.6
  34. #define PINS 25
  35. #define PINS_DIFF 2
  36. uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10,
  37. A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21, A22, A23, A24};
  38. uint8_t adc_pins_diff[] = {A10, A11};
  39. #elif defined(ADC_TEENSY_4_0) // Teensy 4.0
  40. #define PINS 14
  41. #define DIG_PINS 10
  42. #define PINS_DIFF 0
  43. uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13};
  44. uint8_t adc_pins_dig[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9};
  45. uint8_t adc_pins_diff[] = {};
  46. #elif defined(ADC_TEENSY_4_1) // Teensy 4.1
  47. #define PINS 18
  48. #define DIG_PINS 10
  49. #define PINS_DIFF 0
  50. uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17};
  51. uint8_t adc_pins_dig[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9};
  52. uint8_t adc_pins_diff[] = {};
  53. #endif // defined
  54. void setup()
  55. {
  56. pinMode(LED_BUILTIN, OUTPUT);
  57. for (int i = 0; i < PINS; i++)
  58. {
  59. pinMode(adc_pins[i], INPUT);
  60. }
  61. Serial.begin(9600);
  62. ///// ADC0 ////
  63. adc->adc0->setAveraging(16); // set number of averages
  64. adc->adc0->setResolution(12); // set bits of resolution
  65. adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED); // change the conversion speed
  66. adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED); // change the sampling speed
  67. ////// ADC1 /////
  68. #ifdef ADC_DUAL_ADCS
  69. adc->adc1->setAveraging(16); // set number of averages
  70. adc->adc1->setResolution(12); // set bits of resolution
  71. adc->adc1->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED); // change the conversion speed
  72. adc->adc1->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED); // change the sampling speed
  73. #endif
  74. delay(500);
  75. }
  76. int value = 0;
  77. int pin = 0;
  78. void loop()
  79. {
  80. for (int i = 0; i < PINS; i++)
  81. {
  82. value = adc->analogRead(adc_pins[i]); // read a new value, will return ADC_ERROR_VALUE if the comparison is false.
  83. Serial.print("A");
  84. Serial.print(i);
  85. Serial.print(": ");
  86. Serial.print(value * 3.3 / adc->adc0->getMaxValue(), 2);
  87. Serial.print(". ");
  88. if (i == 9)
  89. {
  90. Serial.println();
  91. }
  92. else if (i == 11)
  93. {
  94. Serial.print("\t");
  95. }
  96. else if (i == 13)
  97. {
  98. Serial.print("\t");
  99. }
  100. else if (i == 22)
  101. {
  102. Serial.println();
  103. }
  104. }
  105. Serial.println();
  106. #if ADC_DIFF_PAIRS > 0
  107. Serial.print("Differential pairs: ");
  108. for (int i = 0; i < PINS_DIFF; i += 2)
  109. {
  110. value = adc->analogReadDifferential(adc_pins_diff[i], adc_pins_diff[i + 1]); // read a new value, will return ADC_ERROR_VALUE if the comparison is false.
  111. Serial.print(i);
  112. Serial.print(": ");
  113. Serial.print(value * 3.3 / adc->adc0->getMaxValue(), 2);
  114. Serial.print(". ");
  115. }
  116. Serial.println();
  117. #endif
  118. // the actual parameters for the temperature sensor depend on the board type and
  119. // on the actual batch. The printed value is only an approximation
  120. //Serial.print("Temperature sensor (approx.): ");
  121. //value = adc->analogRead(ADC_INTERNAL_SOURCE::TEMP_SENSOR); // read a new value, will return ADC_ERROR_VALUE if the comparison is false.
  122. //Serial.print(": ");
  123. //float volts = value*3.3/adc->adc0->getMaxValue();
  124. //Serial.print(25-(volts-0.72)/1.7*1000, 2); // slope is 1.6 for T3.0
  125. //Serial.println(" C.");
  126. // Print errors, if any.
  127. if (adc->adc0->fail_flag != ADC_ERROR::CLEAR)
  128. {
  129. Serial.print("ADC0: ");
  130. Serial.println(getStringADCError(adc->adc0->fail_flag));
  131. }
  132. #ifdef ADC_DUAL_ADCS
  133. if (adc->adc1->fail_flag != ADC_ERROR::CLEAR)
  134. {
  135. Serial.print("ADC1: ");
  136. Serial.println(getStringADCError(adc->adc1->fail_flag));
  137. }
  138. #endif
  139. adc->resetError();
  140. Serial.println();
  141. Serial.println();
  142. //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  143. delay(50);
  144. }