Add Possibility to Measure External Batteries via Analog Pin

This commit is contained in:
seiichiro 2021-05-24 16:08:01 +02:00
parent 2038522e0f
commit 6b86aa707b
2 changed files with 30 additions and 0 deletions

View file

@ -26,6 +26,17 @@
*************************************************************************************************************************/ *************************************************************************************************************************/
// #define BTN_PIN PIN_PC2 // #define BTN_PIN PIN_PC2
/**************************************************************************************************************************
* External Battery Monitoring - If EXT_BAT_PIN is set to an analog pin of the ATTiny (e.g. PB4 or PB5), it will be used
* to read the Battery Voltage instead of the internal supply voltage. This can e.g. be used to monitor the voltage of an
* LiPo/LiIon cell connected to the Node via the PowerPack. The Devider should not put out more than 2.5V at the
* measurement point. Choose high resistor values (e.g. 2x 1MOhm for LiPo/LiIon) to minimize current drain through the
* resistors. The Send voltage will be measured voltage at the pin times EXT_BAT_RATIO.
*************************************************************************************************************************/
// #define EXT_BAT_PIN PIN_PB5
// #define EXT_BAT_RATIO 2
/************************************************************************************************************************** /**************************************************************************************************************************
* Number of active Sensors (used as long as HAS_NO_SENSOR is not enabled) * Number of active Sensors (used as long as HAS_NO_SENSOR is not enabled)
* Set to the correct number of enabled sensors below * Set to the correct number of enabled sensors below

View file

@ -220,6 +220,20 @@ void onEvent(ev_t ev) {
} }
// Get Battery Voltage // Get Battery Voltage
#ifdef EXT_BAT_PIN
uint16_t readSupplyVoltage() {
pinMode(EXT_BAT_PIN, INPUT);
analogReference(INTERNAL2V5);
ADC0.CTRLD = ADC_INITDLY_DLY256_gc;
ADC0_CTRLB = ADC_SAMPNUM_ACC64_gc;
uint16_t reading = analogRead(EXT_BAT_PIN);
reading = reading/64;
DEBUG_PRINT("ADC Reading: ");
DEBUG_PRINTLN(reading);
reading = (int16_t)(reading * (2500/1024.0) * EXT_BAT_RATIO);
return reading;
}
#else
uint16_t readSupplyVoltage() { //returns value in millivolts to avoid floating point uint16_t readSupplyVoltage() { //returns value in millivolts to avoid floating point
uint16_t temp = 0; uint16_t temp = 0;
analogReference(VDD); analogReference(VDD);
@ -233,6 +247,7 @@ uint16_t readSupplyVoltage() { //returns value in millivolts to avoid floating p
reading = intermediate / temp; reading = intermediate / temp;
return reading; return reading;
} }
#endif
// Read Sensors and Send Data // Read Sensors and Send Data
void do_send(osjob_t* j) { void do_send(osjob_t* j) {
@ -305,6 +320,10 @@ void setup()
for (int i = 0; i < (sizeof(disabledPins) / sizeof(disabledPins[0])) - 1; i++) for (int i = 0; i < (sizeof(disabledPins) / sizeof(disabledPins[0])) - 1; i++)
pinMode(disabledPins[i], INPUT_PULLUP); pinMode(disabledPins[i], INPUT_PULLUP);
#ifdef EXT_BAT_PIN
#endif
// Setup WS2812B LEDs // Setup WS2812B LEDs
#ifdef WS2812B_PIN #ifdef WS2812B_PIN
pinMode(WS2812B_PIN, OUTPUT); pinMode(WS2812B_PIN, OUTPUT);