3D printed Arduino Airsoft Chronograph
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * adc.cpp
  3. *
  4. * OpenChrono BB speed measurement device.
  5. *
  6. * Copyright (c) 2022 Thomas Buck <thomas@xythobuz.de>
  7. *
  8. * OpenChrono is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * OpenChrono is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with OpenChrono. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. * https://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
  22. */
  23. #include <Arduino.h>
  24. #include "adc.h"
  25. long readVcc(void) {
  26. // Read 1.1V reference against AVcc
  27. // set the reference to Vcc and the measurement to the internal 1.1V reference
  28. #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  29. ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  30. #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
  31. ADMUX = _BV(MUX5) | _BV(MUX0);
  32. #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
  33. ADMUX = _BV(MUX3) | _BV(MUX2);
  34. #else
  35. ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  36. #endif
  37. delay(2); // Wait for Vref to settle
  38. ADCSRA |= _BV(ADSC); // Start conversion
  39. while (bit_is_set(ADCSRA,ADSC)); // measuring
  40. uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
  41. uint8_t high = ADCH; // unlocks both
  42. long result = (high<<8) | low;
  43. result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
  44. return result; // Vcc in millivolts
  45. }