From e636b3d8bf8b3400e9b2eec57fbf416fd7c31806 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Tue, 10 Jun 2025 16:11:31 +0800 Subject: Battery check and readme. --- README.txt | 20 +++++++++++++++----- main.c | 44 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/README.txt b/README.txt index 1449783..501c565 100644 --- a/README.txt +++ b/README.txt @@ -1,5 +1,7 @@ CURRENT MEASUREMENTS +Inidividual parts: + R503 FPM: When VCC is connected to a 3.31V supply, FPM draws 13.8mA of quiescent current. When @@ -9,7 +11,6 @@ ATmega328P: MOSFET: - FS5106B high-torque servo: One of them draws 6.1mA when connected and stabilizes at 4.6mA. The other drew @@ -23,12 +24,21 @@ TARGET CURRENT DRAW 3. 900uA for 3 months 3. 694uA for 4 months +LINEAR REGULATORS + + 1. When the ATmega328P is in normal mode, the MCU + FPM + Servo + linear + regulators draw 30.6mA of quiescent current at 5.07V. + 2. When the ATmega328P is in power down mode, the MCU + FPM + Servo + linear + regulators draw 26.2mA of quiescent current at 5.07V. + 3. When the ATmega328P is in power down mode, the MCU + linear regulators draw + 13.7mA of quiescent current at 5.07V (without MOSFETS). + +BOOST CONVERTER + + + REMARKS 1. Can't use 3.3V FPM modules with Arduino Uno. The RX and TX lines have resistors. - 2. When the ATmega328P is in normal mode, the MCU + FPM + Servo + linear - regulators draw 30.6mA of quiescent current at 5.07V. - 3. When the ATmega328P is in power down mode, the MCU + FPM + Servo + linear - regulators draw 26.0mA of quiescent current at 5.07V. diff --git a/main.c b/main.c index d79dc0d..7329e11 100644 --- a/main.c +++ b/main.c @@ -33,6 +33,35 @@ #define PWM_MAX 2500 #define PWM_TOP 20000 +#define VCC_MIN 4900 + +#define LED_PIN PB5 +#define LED_DDR DDRB +#define LED_PORT PORTB + +/* Measure vcc by measuring known internal 1.1v bandgap + * reference voltage against AVCC. + */ +uint16_t getvcc(void) +{ + uint16_t vcc; + + ADMUX |= (1 << REFS0); + ADMUX |= (1 << MUX3) | (1 << MUX2) | (1 << MUX1); + ADCSRA |= (1 << ADEN) | (1 << ADPS2) | (1 << ADPS0); + + // https://www.sciencetronics.com/greenphotons/?p=1521 + _delay_us(500); + + ADCSRA |= (1 << ADSC); + while (ADCSRA & (1 << ADSC)) + ; + vcc = (1100UL * 1023 / ADC); + + ADCSRA &= ~(1 << ADEN); + return vcc; +} + int main(void) { /* disable watchdog timer */ @@ -42,6 +71,10 @@ int main(void) WDTCSR |= (1 << WDCE) | (1 << WDE); WDTCSR = 0x00; + /* battery check */ + LED_DDR |= (1 << LED_PIN); + LED_PORT &= ~(1 << LED_PIN); + /* init input ports */ INPUT_DDR &= ~((1 << BACK_LOCK_PIN) | (1 << BACK_UNLOCK_PIN) | (1 << FRONT_LOCK_PIN) | (1 << FRONT_UNLOCK_PIN) | @@ -69,6 +102,9 @@ int main(void) sei(); for (;;) { + if (getvcc() < VCC_MIN) + LED_PORT |= (1 << LED_PIN); + sleep_bod_disable(); set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_mode(); @@ -102,24 +138,18 @@ static inline int is_pressed(uint8_t btn) ISR(FPM_INT_VEC) { - cli(); - if (fpm_match()) { unlock(); fpm_led(BREATHE, BLUE, 1); } else { fpm_led(BREATHE, RED, 1); } - - sei(); } ISR(BTN_INT_VEC) { uint16_t id; - cli(); - if (is_pressed(FRONT_LOCK_PIN)) { lock(); fpm_led(FLASH, RED, 1); @@ -137,6 +167,4 @@ ISR(BTN_INT_VEC) } else fpm_led(BREATHE, RED, 1); } - - sei(); } -- cgit v1.2.3