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.

adc_pdb.ino 4.1KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Example for triggering the ADC with PDB
  2. * Valid for Teensy 3.0 and 3.1
  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. Serial.begin(9600);
  14. Serial.println("Begin setup");
  15. ///// ADC0 ////
  16. adc->adc0->setAveraging(1); // set number of averages
  17. adc->adc0->setResolution(8); // set bits of resolution
  18. adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::VERY_HIGH_SPEED); // change the conversion speed
  19. adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED); // change the sampling speed
  20. ////// ADC1 /////
  21. #ifdef ADC_DUAL_ADCS
  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::VERY_HIGH_SPEED); // change the conversion speed
  25. adc->adc1->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED); // change the sampling speed
  26. #endif
  27. Serial.println("End setup");
  28. }
  29. char c=0;
  30. int value;
  31. int value2;
  32. void loop() {
  33. #ifdef ADC_USE_PDB
  34. if (Serial.available()) {
  35. c = Serial.read();
  36. if(c=='v') { // value
  37. Serial.print("Value ADC0: ");
  38. value = (uint16_t)adc->adc0->readSingle(); // the unsigned is necessary for 16 bits, otherwise values larger than 3.3/2 V are negative!
  39. Serial.println(value*3.3/adc->adc0->getMaxValue(), DEC);
  40. #ifdef ADC_DUAL_ADCS
  41. Serial.print("Value ADC1: ");
  42. value2 = (uint16_t)adc->adc1->readSingle(); // the unsigned is necessary for 16 bits, otherwise values larger than 3.3/2 V are negative!
  43. Serial.println(value2*3.3/adc->adc1->getMaxValue(), DEC);
  44. #endif
  45. } else if(c=='s') { // start pdb, before pressing enter write the frequency in Hz
  46. uint32_t freq = Serial.parseInt();
  47. if (freq == 0) {
  48. Serial.println("Stop pdb.");
  49. adc->adc0->stopPDB();
  50. #ifdef ADC_DUAL_ADCS
  51. adc->adc1->stopPDB();
  52. #endif
  53. }
  54. else {
  55. Serial.print("Start pdb with frequency ");
  56. Serial.print(freq);
  57. Serial.println(" Hz.");
  58. adc->adc0->stopPDB();
  59. adc->adc0->startSingleRead(readPin); // call this to setup everything before the pdb starts, differential is also possible
  60. adc->adc0->enableInterrupts(adc0_isr);
  61. adc->adc0->startPDB(freq); //frequency in Hz
  62. #ifdef ADC_DUAL_ADCS
  63. adc->adc1->stopPDB();
  64. adc->adc1->startSingleRead(readPin2); // call this to setup everything before the pdb starts
  65. adc->adc1->enableInterrupts(adc1_isr);
  66. adc->adc1->startPDB(freq); //frequency in Hz
  67. #endif
  68. }
  69. } else if(c=='p') { // pbd stats
  70. Serial.print("Frequency: ");
  71. Serial.println(adc->adc0->getPDBFrequency());
  72. }
  73. }
  74. // Print errors, if any.
  75. if(adc->adc0->fail_flag != ADC_ERROR::CLEAR) {
  76. Serial.print("ADC0: "); Serial.println(getStringADCError(adc->adc0->fail_flag));
  77. }
  78. #ifdef ADC_DUAL_ADCS
  79. if(adc->adc1->fail_flag != ADC_ERROR::CLEAR) {
  80. Serial.print("ADC1: "); Serial.println(getStringADCError(adc->adc1->fail_flag));
  81. }
  82. #endif
  83. adc->resetError();
  84. //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  85. delay(10);
  86. #endif // ADC_USE_PDB
  87. }
  88. // Make sure to call readSingle() to clear the interrupt.
  89. void adc0_isr() {
  90. adc->adc0->readSingle();
  91. //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN) );
  92. }
  93. #ifdef ADC_DUAL_ADCS
  94. void adc1_isr() {
  95. adc->adc1->readSingle();
  96. //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN) );
  97. }
  98. #endif
  99. #ifdef ADC_USE_PDB
  100. // pdb interrupt is enabled in case you need it.
  101. void pdb_isr(void) {
  102. PDB0_SC &=~PDB_SC_PDBIF; // clear interrupt
  103. //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN) );
  104. }
  105. #endif