From 04fe7a05e626554162b3a900123f7621ca0c2d38 Mon Sep 17 00:00:00 2001 From: Armin Date: Thu, 4 Apr 2024 15:51:26 +0200 Subject: [PATCH] Changed pins --- UltimateBatteryTester/ADCUtils.h | 9 +++++- UltimateBatteryTester/ADCUtils.hpp | 31 +++++++++++++++---- UltimateBatteryTester/EasyButtonAtInt01.h | 13 +++++++- UltimateBatteryTester/EasyButtonAtInt01.hpp | 22 +++++++++++-- .../UltimateBatteryTester.ino | 14 +++++---- UltimateBatteryTester/digitalWriteFast.h | 15 +++++---- 6 files changed, 82 insertions(+), 22 deletions(-) diff --git a/UltimateBatteryTester/ADCUtils.h b/UltimateBatteryTester/ADCUtils.h index ce283c2..c406217 100644 --- a/UltimateBatteryTester/ADCUtils.h +++ b/UltimateBatteryTester/ADCUtils.h @@ -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 @@ -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__) ... diff --git a/UltimateBatteryTester/ADCUtils.hpp b/UltimateBatteryTester/ADCUtils.hpp index 0cff66e..73030b4 100644 --- a/UltimateBatteryTester/ADCUtils.hpp +++ b/UltimateBatteryTester/ADCUtils.hpp @@ -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() { @@ -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! @@ -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 } diff --git a/UltimateBatteryTester/EasyButtonAtInt01.h b/UltimateBatteryTester/EasyButtonAtInt01.h index 5eb880f..2491db1 100644 --- a/UltimateBatteryTester/EasyButtonAtInt01.h +++ b/UltimateBatteryTester/EasyButtonAtInt01.h @@ -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. @@ -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) @@ -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. diff --git a/UltimateBatteryTester/EasyButtonAtInt01.hpp b/UltimateBatteryTester/EasyButtonAtInt01.hpp index 8b5295d..d0ee2b7 100644 --- a/UltimateBatteryTester/EasyButtonAtInt01.hpp +++ b/UltimateBatteryTester/EasyButtonAtInt01.hpp @@ -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 @@ -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 @@ -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) diff --git a/UltimateBatteryTester/UltimateBatteryTester.ino b/UltimateBatteryTester/UltimateBatteryTester.ino index c1b7ef4..3359713 100644 --- a/UltimateBatteryTester/UltimateBatteryTester.ino +++ b/UltimateBatteryTester/UltimateBatteryTester.ino @@ -98,7 +98,8 @@ /* * 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 @@ -106,14 +107,14 @@ #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 @@ -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 diff --git a/UltimateBatteryTester/digitalWriteFast.h b/UltimateBatteryTester/digitalWriteFast.h index 1bb8eb5..a36a71f 100644 --- a/UltimateBatteryTester/digitalWriteFast.h +++ b/UltimateBatteryTester/digitalWriteFast.h @@ -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 @@ -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) @@ -416,4 +418,5 @@ if (__builtin_constant_p(P)) { \ # endif #endif // !defined(digitalToggleFast) +#endif // !defined(MEGATINYCORE) #endif //__digitalWriteFast_h_