PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

158 lines
6.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. const int readPin = A9; // ADC0
  11. const int readPin2 = A3; // ADC1
  12. const int readPin3 = A2; // ADC0 or ADC1
  13. ADC *adc = new ADC(); // adc object
  14. void setup() {
  15. pinMode(LED_BUILTIN, OUTPUT);
  16. pinMode(readPin, INPUT);
  17. pinMode(readPin2, INPUT);
  18. pinMode(readPin3, INPUT);
  19. pinMode(A10, INPUT); //Diff Channel 0 Positive
  20. pinMode(A11, INPUT); //Diff Channel 0 Negative
  21. #ifdef ADC_DUAL_ADCS
  22. pinMode(A12, INPUT); //Diff Channel 3 Positive
  23. pinMode(A13, INPUT); //Diff Channel 3 Negative
  24. #endif
  25. Serial.begin(9600);
  26. ///// ADC0 ////
  27. // reference can be ADC_REFERENCE::REF_3V3, ADC_REFERENCE::REF_1V2 (not for Teensy LC) or ADC_REFERENCE::REF_EXT.
  28. //adc->adc0->setReference(ADC_REFERENCE::REF_1V2); // change all 3.3 to 1.2 if you change the reference to 1V2
  29. adc->adc0->setAveraging(16); // set number of averages
  30. adc->adc0->setResolution(16); // set bits of resolution
  31. // 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
  32. // see the documentation for more information
  33. // additionally the conversion speed can also be ADACK_2_4, ADACK_4_0, ADACK_5_2 and ADACK_6_2,
  34. // where the numbers are the frequency of the ADC clock in MHz and are independent on the bus speed.
  35. adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::VERY_LOW_SPEED); // change the conversion speed
  36. // it can be any of the ADC_MED_SPEED enum: VERY_LOW_SPEED, LOW_SPEED, MED_SPEED, HIGH_SPEED or VERY_HIGH_SPEED
  37. adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED); // change the sampling speed
  38. // always call the compare functions after changing the resolution!
  39. //adc->adc0->enableCompare(1.0/3.3*adc->getMaxValue(), 0); // measurement will be ready if value < 1.0V
  40. //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
  41. // If you enable interrupts, notice that the isr will read the result, so that isComplete() will return false (most of the time)
  42. adc->adc0->enableInterrupts(adc0_isr);
  43. adc->adc0->startContinuous(readPin);
  44. //adc->startContinuousDifferential(A10, A11, ADC_0);
  45. ////// ADC1 /////
  46. #ifdef ADC_DUAL_ADCS
  47. adc->adc1->setAveraging(16); // set number of averages
  48. adc->adc1->setResolution(16); // set bits of resolution
  49. adc->adc1->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED); // change the conversion speed
  50. adc->adc1->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED); // change the sampling speed
  51. //adc->adc0->setReference(ADC_REFERENCE::REF_1V2);
  52. // always call the compare functions after changing the resolution!
  53. //adc->adc0->enableCompare(1.0/3.3*adc->adc0->getMaxValue(), 0); // measurement will be ready if value < 1.0V
  54. //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
  55. // If you enable interrupts, note that the isr will read the result, so that isComplete() will return false (most of the time)
  56. //adc->adc1->enableInterrupts(adc1_isr);
  57. adc->adc1->startContinuous(readPin2);
  58. //adc->adc1->startContinuousDifferential(A12, A13);
  59. #endif
  60. delay(500);
  61. }
  62. int value = 0;
  63. int value2 = 0;
  64. char c=0;
  65. void loop() {
  66. if (Serial.available()) {
  67. c = Serial.read();
  68. if(c=='c') { // conversion active?
  69. Serial.print("Converting? ADC0: ");
  70. Serial.println(adc->adc0->isConverting());
  71. #ifdef ADC_DUAL_ADCS
  72. Serial.print("Converting? ADC1: ");
  73. Serial.println(adc->adc1->isConverting());
  74. #endif
  75. } else if(c=='s') { // stop conversion
  76. adc->adc0->stopContinuous();
  77. Serial.println("Stopped");
  78. } else if(c=='t') { // conversion successful?
  79. Serial.print("Conversion successful? ADC0: ");
  80. Serial.println(adc->adc0->isComplete());
  81. #ifdef ADC_DUAL_ADCS
  82. Serial.print("Conversion successful? ADC1: ");
  83. Serial.println(adc->adc1->isComplete());
  84. #endif
  85. } else if(c=='r') { // restart conversion
  86. Serial.println("Restarting conversions ");
  87. adc->adc0->startContinuous(readPin);
  88. //adc->startContinuousDifferential(A10, A11, ADC_0);
  89. } else if(c=='v') { // value
  90. Serial.print("Value ADC0: ");
  91. value = (uint16_t)adc->adc0->analogReadContinuous(); // the unsigned is necessary for 16 bits, otherwise values larger than 3.3/2 V are negative!
  92. Serial.println(value*3.3/adc->adc0->getMaxValue(), DEC);
  93. #ifdef ADC_DUAL_ADCS
  94. Serial.print("Value ADC1: ");
  95. value2 = (uint16_t)adc->adc1->analogReadContinuous(); // the unsigned is necessary for 16 bits, otherwise values larger than 3.3/2 V are negative!
  96. Serial.println(value2*3.3/adc->adc1->getMaxValue(), DEC);
  97. #endif
  98. } else if(c=='n') { // new single conversion on readPin3
  99. // this shows how even when both ADCs are busy with continuous measurements
  100. // you can still call analogRead, it will pause the conversion, get the value and resume the continuous conversion automatically.
  101. Serial.print("Single read on readPin3: ");
  102. Serial.println(adc->adc0->analogRead(readPin3)*3.3/adc->adc0->getMaxValue(), DEC);
  103. }
  104. }
  105. // Print errors, if any.
  106. if(adc->adc0->fail_flag != ADC_ERROR::CLEAR) {
  107. Serial.print("ADC0: "); Serial.println(getStringADCError(adc->adc0->fail_flag));
  108. }
  109. #ifdef ADC_DUAL_ADCS
  110. if(adc->adc1->fail_flag != ADC_ERROR::CLEAR) {
  111. Serial.print("ADC1: "); Serial.println(getStringADCError(adc->adc1->fail_flag));
  112. }
  113. #endif
  114. //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  115. delay(100);
  116. }
  117. void adc0_isr(void) {
  118. adc->adc0->analogReadContinuous();
  119. digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN)); // Toggle the led
  120. }
  121. #ifdef ADC_DUAL_ADCS
  122. void adc1_isr(void) {
  123. adc->adc1->analogReadContinuous();
  124. digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  125. }
  126. #endif