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ů.

conversionSpeed.ino 5.0KB

před 3 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 = A2; // ADC0 or ADC1
  11. volatile uint32_t value = 0;
  12. #if defined(ADC_TEENSY_4)
  13. const uint32_t NUM_SAMPLES = 10000;
  14. #else
  15. const uint32_t NUM_SAMPLES = 1000;
  16. #endif
  17. uint32_t num_samples = NUM_SAMPLES;
  18. elapsedMicros timeElapsed;
  19. volatile uint32_t num_iter = 0;
  20. ADC *adc = new ADC(); // adc object
  21. void setup() {
  22. pinMode(LED_BUILTIN, OUTPUT);
  23. pinMode(readPin, INPUT_PULLUP);
  24. Serial.begin(9600);
  25. delay(100);
  26. Serial.print("F_CPU: "); Serial.print(F_CPU/1e6); Serial.println(" MHz.");
  27. Serial.print("ADC_F_BUS: "); Serial.print(ADC_F_BUS/1e6); Serial.println(" MHz.");
  28. // Single-shot conversions. The fastest way to do conversions is with continuous mode (see below)
  29. Serial.println("Single-shot conversion speeds. Value should be 1.");
  30. for(auto average : averages_list) {
  31. adc->adc0->setAveraging(average); // set number of averages
  32. for (auto resolution : resolutions_list) {
  33. adc->adc0->setResolution(resolution); // set bits of resolution
  34. for (auto conv_speed : conversion_speed_list) {
  35. adc->adc0->setConversionSpeed(conv_speed); // change the conversion speed
  36. for (auto samp_speed: sampling_speed_list) {
  37. adc->adc0->setSamplingSpeed(samp_speed); // change the sampling speed
  38. adc->adc0->wait_for_cal();
  39. num_samples = (int)NUM_SAMPLES/average;
  40. timeElapsed = 0;
  41. value = 0;
  42. for(uint32_t i=0; i<num_samples; i++) {
  43. value += adc->adc0->analogRead(readPin);
  44. }
  45. double time_us = (float)timeElapsed/num_samples;
  46. Serial.print("Average: "); Serial.print(average);
  47. Serial.print(", Resolution: "); Serial.print(resolution);
  48. Serial.print(", Conversion speed: "); Serial.print(getConversionEnumStr(conv_speed));
  49. Serial.print(", Sampling speed: "); Serial.print(getSamplingEnumStr(samp_speed));
  50. Serial.print(". Time: "); Serial.print(time_us); Serial.print(" us.");
  51. Serial.print(" Value: "); Serial.println(value/num_samples*1.0/adc->adc0->getMaxValue(), 6);
  52. }
  53. }
  54. }
  55. }
  56. // Continuous mode
  57. Serial.println("Continuous mode conversion speeds. Value should be 1.");
  58. adc->adc0->enableInterrupts(adc0_isr);
  59. for(auto average : averages_list) {
  60. adc->adc0->setAveraging(average); // set number of averages
  61. for (auto resolution : resolutions_list) {
  62. adc->adc0->setResolution(resolution); // set bits of resolution
  63. for (auto conv_speed : conversion_speed_list) {
  64. adc->adc0->setConversionSpeed(conv_speed); // change the conversion speed
  65. for (auto samp_speed: sampling_speed_list) {
  66. adc->adc0->setSamplingSpeed(samp_speed); // change the sampling speed
  67. adc->adc0->wait_for_cal();
  68. num_samples = NUM_SAMPLES/average;
  69. value = 0;
  70. num_iter = 0;
  71. timeElapsed = 0;
  72. adc->adc0->startContinuous(readPin);
  73. while (num_iter<num_samples) {}
  74. double time_us = (float)timeElapsed/num_samples;
  75. adc->adc0->stopContinuous();
  76. Serial.print("Average: "); Serial.print(average);
  77. Serial.print(", Resolution: "); Serial.print(resolution);
  78. Serial.print(", Conversion speed: "); Serial.print(getConversionEnumStr(conv_speed));
  79. Serial.print(", Sampling speed: "); Serial.print(getSamplingEnumStr(samp_speed));
  80. Serial.print(". Time: "); Serial.print(time_us); Serial.print(" us.");
  81. Serial.print(" Value: "); Serial.println(value/num_samples*1.0/adc->adc0->getMaxValue(), 6);
  82. }
  83. }
  84. }
  85. }
  86. }
  87. void loop() {
  88. // Print errors, if any.
  89. if(adc->adc0->fail_flag != ADC_ERROR::CLEAR) {
  90. Serial.print("ADC0: "); Serial.println(getStringADCError(adc->adc0->fail_flag));
  91. }
  92. #ifdef ADC_DUAL_ADCS
  93. if(adc->adc1->fail_flag != ADC_ERROR::CLEAR) {
  94. Serial.print("ADC1: "); Serial.println(getStringADCError(adc->adc1->fail_flag));
  95. }
  96. #endif
  97. digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  98. delay(100);
  99. }
  100. void adc0_isr(void) {
  101. if(num_iter<num_samples) {
  102. value += (uint16_t)adc->adc0->analogReadContinuous();
  103. num_iter++;
  104. } else { // clear interrupt
  105. adc->adc0->analogReadContinuous();
  106. }
  107. //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  108. }