Skip to content

Commit

Permalink
Changed pins
Browse files Browse the repository at this point in the history
  • Loading branch information
ArminJo committed Oct 8, 2024
1 parent fe3f2f4 commit 04fe7a0
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 22 deletions.
9 changes: 8 additions & 1 deletion UltimateBatteryTester/ADCUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@
#define ADC_GND_CHANNEL_MUX 15
#define ADC_CHANNEL_MUX_MASK 0x0F

#elif defined(__AVR_ATmega644P__)
#define ADC_TEMPERATURE_CHANNEL_MUX // not existent
#define ADC_1_1_VOLT_CHANNEL_MUX 0x1E
#define ADC_GND_CHANNEL_MUX 0x1F
#define ADC_CHANNEL_MUX_MASK 0x0F

#elif defined(__AVR_ATmega32U4__)
#define ADC_TEMPERATURE_CHANNEL_MUX 0x27
#define ADC_1_1_VOLT_CHANNEL_MUX 0x1E
Expand Down Expand Up @@ -208,7 +214,8 @@ void resetCounterForVCCUndervoltageMultipleTimes();
bool isVCCUndervoltage();
bool isVCCEmergencyUndervoltage();
bool isVCCOvervoltage();
bool isVCCOvervoltageSimple();
bool isVCCOvervoltageSimple(); // Version using readVCCVoltageMillivoltSimple()
bool isVCCTooHighSimple(); // Version not using readVCCVoltageMillivoltSimple()

#endif // defined(__AVR__) ...

Expand Down
31 changes: 25 additions & 6 deletions UltimateBatteryTester/ADCUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ void resetCounterForVCCUndervoltageMultipleTimes() {
* Raw reading of 1.1 V is 221 at 5.1 V.
* Raw reading of 1.1 V is 214 at 5.25 V (+5 %).
* Raw reading of 1.1 V is 204 at 5.5 V (+10 %).
* Raw reading of 1.1 V is 1126000 / VCC_MILLIVOLT
* @return true if 5 % overvoltage reached
*/
bool isVCCOvervoltage() {
Expand All @@ -726,6 +727,21 @@ bool isVCCOvervoltageSimple() {
return (sVCCVoltageMillivolt > VCC_OVERVOLTAGE_THRESHOLD_MILLIVOLT);
}

// Version not using readVCCVoltageMillivoltSimple()
bool isVCCTooHighSimple() {
ADMUX = ADC_1_1_VOLT_CHANNEL_MUX | (DEFAULT << SHIFT_VALUE_FOR_REFERENCE);
// ADCSRB = 0; // Only active if ADATE is set to 1.
// ADSC-StartConversion ADIF-Reset Interrupt Flag - NOT free running mode
ADCSRA = (_BV(ADEN) | _BV(ADSC) | _BV(ADIF) | ADC_PRESCALE128); // 128 -> 104 microseconds per ADC conversion at 16 MHz --- Arduino default
// wait for single conversion to finish
loop_until_bit_is_clear(ADCSRA, ADSC);

// Get value
uint16_t tRawValue = ADCL | (ADCH << 8);

return tRawValue < 1126000 / VCC_OVERVOLTAGE_THRESHOLD_MILLIVOLT;
}

/*
* Temperature sensor is enabled by selecting the appropriate channel.
* Different formula for 328P and 328PB!
Expand All @@ -737,18 +753,21 @@ float getCPUTemperatureSimple(void) {
return 0.0;
#else
// use internal 1.1 volt as reference. 4 times oversample. Assume the signal has noise, but never verified :-(
uint16_t tTempRaw = readADCChannelWithReferenceOversample(ADC_TEMPERATURE_CHANNEL_MUX, INTERNAL, 2);
uint16_t tTemperatureRaw = readADCChannelWithReferenceOversample(ADC_TEMPERATURE_CHANNEL_MUX, INTERNAL, 2);
#if defined(LOCAL_DEBUG)
Serial.print(F("TempRaw="));
Serial.println(tTempRaw);
Serial.println(tTemperatureRaw);
#endif

#if defined(__AVR_ATmega328PB__)
tTempRaw -= 245;
return (float)tTempRaw;
tTemperatureRaw -= 245;
return (float)tTemperatureRaw;
#elif defined(__AVR_ATtiny85__)
tTemperatureRaw -= 273; // 273 and 1.1666 are values from the datasheet
return (float)tTemperatureRaw / 1.1666;
#else
tTempRaw -= 317;
return (float) tTempRaw / 1.22;
tTemperatureRaw -= 317;
return (float) tTemperatureRaw / 1.22;
#endif
#endif
}
Expand Down
13 changes: 12 additions & 1 deletion UltimateBatteryTester/EasyButtonAtInt01.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* EasyButtonAtInt01.hpp
* EasyButtonAtInt01.h
*
* Arduino library for handling push buttons connected between ground and INT0 and / or INT1 pin.
* INT0 and INT1 are connected to Pin 2 / 3 on most Arduinos (ATmega328), to PB6 / PA3 on ATtiny167 and on ATtinyX5 we have only INT0 at PB2.
Expand Down Expand Up @@ -170,6 +170,16 @@
#define INT1_OUT_PORT (PORTB)
# endif // defined(USE_BUTTON_1)

#elif defined(USE_INT2_FOR_BUTTON_0) // Hack for ATmega 644
# if defined(USE_BUTTON_1)
#error If USE_INT2_FOR_BUTTON_0 is defined, only USE_BUTTON_0 is allowed, USE_BUTTON_1 must be disabled!
# endif
// dirty hack, but INT0 and INT1 are occupied by second USART
#define INT0_PIN 2 // PB2 / INT2
#define INT0_DDR_PORT (DDRB)
#define INT0_IN_PORT (PINB)
#define INT0_OUT_PORT (PORTB)

#elif defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__)
// from here we use only ATtinyCore / PAx / PBx numbers, since on Digispark board and core library there is used a strange enumeration of pins
#define INT0_PIN 14 // PB6 / INT0 is connected to USB+ on DigisparkPro boards and labeled with 3 (D3)
Expand Down Expand Up @@ -383,6 +393,7 @@ void __attribute__ ((weak)) handleINT1Interrupt();

/* Version 3.4.1 - 12/2023
* - Avoid wrong double press detection if calling checkForDoublePress() after release of button.
* - Hack for ATmega 644.
*
* Version 3.4.0 - 10/2023
* - Added NO_INITIALIZE_IN_CONSTRUCTOR macro to enable late initializing.
Expand Down
22 changes: 20 additions & 2 deletions UltimateBatteryTester/EasyButtonAtInt01.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ void EasyButton::init(bool aIsButtonAtINT0) {
sPointerToButton0ForISR = this;
# if defined(USE_ATTACH_INTERRUPT)
attachInterrupt(digitalPinToInterrupt(INT0_PIN), &handleINT0Interrupt, CHANGE);

# elif defined(USE_INT2_FOR_BUTTON_0)
EICRA |= _BV(ISC20); // interrupt on any logical change
EIFR |= _BV(INTF2);// clear interrupt bit
EIMSK |= _BV(INT2);// enable interrupt on next change

# else
EICRA |= _BV(ISC00); // interrupt on any logical change
EIFR |= _BV(INTF0);// clear interrupt bit
Expand Down Expand Up @@ -722,8 +728,8 @@ void __attribute__ ((weak)) handleINT1Interrupt() {
// ISR for PIN PD2
// Cannot make the vector itself weak, since the vector table is already filled by weak vectors resulting in ignoring my weak one:-(
//ISR(INT0_vect, __attribute__ ((weak))) {
# if defined(USE_BUTTON_0)
ISR(INT0_vect) {
# if defined(USE_INT2_FOR_BUTTON_0)
ISR(INT2_vect) {
# if defined(MEASURE_EASY_BUTTON_INTERRUPT_TIMING)
digitalWriteFast(INTERRUPT_TIMING_OUTPUT_PIN, HIGH);
# endif
Expand All @@ -732,6 +738,18 @@ ISR(INT0_vect) {
digitalWriteFast(INTERRUPT_TIMING_OUTPUT_PIN, LOW);
# endif
}
# else
# if defined(USE_BUTTON_0)
ISR(INT0_vect) {
# if defined(MEASURE_EASY_BUTTON_INTERRUPT_TIMING)
digitalWriteFast(INTERRUPT_TIMING_OUTPUT_PIN, HIGH);
# endif
handleINT0Interrupt();
# if defined(MEASURE_EASY_BUTTON_INTERRUPT_TIMING)
digitalWriteFast(INTERRUPT_TIMING_OUTPUT_PIN, LOW);
# endif
}
# endif
# endif

# if defined(USE_BUTTON_1)
Expand Down
14 changes: 8 additions & 6 deletions UltimateBatteryTester/UltimateBatteryTester.ino
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,23 @@

/*
* Pin and ADC definitions
* Start/Stop button is connected to INT0 pin 2
*
* Pin 2 / INT0 is used for Start/Stop button
* Pin 3 to 8 are used for parallel LCD connection
*/
#define ADC_CHANNEL_VOLTAGE 0 // A0 for voltage measurement
#define ADC_CHANNEL_CURRENT 1 // A1 for current measurement
#define VOLTAGE_RANGE_EXTENSION_PIN A2 // This pin is low to extend the voltage range from 2.2 volt to 4.4 volt
#define LOAD_HIGH_PIN A3 // This pin is high to switch on the high load (3 ohm)
// A4 + A5, the hardware I2C pins on Arduino, are used for Serial LCD
#define ONLY_PLOTTER_OUTPUT_PIN A4 // If powered by Li-ion, verbose output to Arduino Serial Monitor is disabled, if connected to ground. This is intended for Arduino Plotter mode.
#define ADC_CHANNEL_LOGGER_CURRENT 5 // A5 for current measurement for Logger
#define LOAD_LOW_PIN 12 // This pin is high to switch on the low load (10 ohm). A4 is occupied by I2C for serial LCD display.
#define BUZZER_PIN 9
#define ADC_CHANNEL_LOGGER_CURRENT 4 // A4 for current measurement for Logger
#define BUZZER_PIN A5
// Mode pins
#define ONLY_PLOTTER_OUTPUT_PIN 9 // If powered by Li-ion, verbose output to Arduino Serial Monitor is disabled, if connected to ground. This is intended for Arduino Plotter mode.
#define ONLY_LOGGER_MODE_PIN 10 // If connected to ground, current is measured at the shunt at A4 and voltage still at A0.
// If powered by USB verbose verbose output to Arduino Serial Monitor is disabled, if NOT connected to ground.
#define CUTOFF_LEVEL_PIN 11 // If connected to ground, "cut off is low" is displayed and discharge ends at a lower voltage. E.g. Li-ion discharge ends at 3000 mV instead of 3500 mV
#define LOAD_LOW_PIN 12 // This pin is high to switch on the low load (10 ohm). A4 is occupied by I2C for serial LCD display.

/*
* Imports and definitions for start/stop button at pin 2
Expand Down Expand Up @@ -508,7 +509,8 @@ void setup() {
digitalWrite(VOLTAGE_RANGE_EXTENSION_PIN, LOW);

Serial.begin(115200);
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_PORT_USBVIRTUAL) || defined(SERIAL_USB) /*stm32duino*/|| defined(USBCON) /*STM32_stm32*/|| defined(SERIALUSB_PID) || defined(ARDUINO_attiny3217)
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_PORT_USBVIRTUAL) || defined(SERIAL_USB) /*stm32duino*/|| defined(USBCON) /*STM32_stm32*/ \
|| defined(SERIALUSB_PID) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_attiny3217)
delay(4000); // To be able to connect Serial monitor after reset or power up and before first print out. Do not wait for an attached Serial Monitor!
#endif

Expand Down
15 changes: 9 additions & 6 deletions UltimateBatteryTester/digitalWriteFast.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
#ifndef __digitalWriteFast_h_
#define __digitalWriteFast_h_ 1

//#define THROW_ERROR_IF_NOT_FAST // If activated, an error is thrown if pin is not a compile time constant
void NonConstantsUsedForPinModeFast( void ) __attribute__ (( error("Parameter for pinModeFast() function is not constant") ));
void NonConstantsUsedForDigitalWriteFast( void ) __attribute__ (( error("Parameter for digitalWriteFast() function is not constant") ));
void NonConstantsUsedForDigitalToggleFast( void ) __attribute__ (( error("Parameter for digitalToggleFast() function is not constant") ));
int NonConstantsUsedForDigitalReadFast( void ) __attribute__ (( error("Parameter for digitalReadFast() function is not constant") ));

#if !defined(MEGATINYCORE) // megaTinyCore has it own digitalWriteFast function set, except digitalToggleFast().

//#define SANGUINO_PINOUT // define for Sanguino pinout

// general macros/defines
Expand Down Expand Up @@ -312,12 +320,6 @@

#endif


void NonConstantsUsedForPinModeFast( void ) __attribute__ (( error("Parameter for pinModeFast() function is not constant") ));
void NonConstantsUsedForDigitalWriteFast( void ) __attribute__ (( error("Parameter for digitalWriteFast() function is not constant") ));
void NonConstantsUsedForDigitalToggleFast( void ) __attribute__ (( error("Parameter for digitalToggleFast() function is not constant") ));
int NonConstantsUsedForDigitalReadFast( void ) __attribute__ (( error("Parameter for digitalReadFast() function is not constant") ));

#if !defined(digitalWriteFast)
# if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR)) && defined(__digitalPinToPortReg)
# if defined(THROW_ERROR_IF_NOT_FAST)
Expand Down Expand Up @@ -416,4 +418,5 @@ if (__builtin_constant_p(P)) { \
# endif
#endif // !defined(digitalToggleFast)

#endif // !defined(MEGATINYCORE)
#endif //__digitalWriteFast_h_

0 comments on commit 04fe7a0

Please sign in to comment.