diff options
-rw-r--r-- | sw/ltc2400.cpp | 17 | ||||
-rw-r--r-- | sw/main.cpp | 15 |
2 files changed, 18 insertions, 14 deletions
diff --git a/sw/ltc2400.cpp b/sw/ltc2400.cpp index 2a76d87..169b7bd 100644 --- a/sw/ltc2400.cpp +++ b/sw/ltc2400.cpp @@ -86,10 +86,10 @@ void ltc2400_init() { cli(); - // Set bit order=MSB first (DORD=0) - // And SPI mode=0 (CPOL=0, CPHA=0) - SPCR &= ~(_BV(DORD) | _BV(CPOL) | _BV(CPHA)); - + // Set SPI Enable and Master mode, + // bit order=MSB first (DORD=0), + // SPI mode=0 (CPOL=0, CPHA=0) + // // clock divider: // SPR1 and SPR0 are the two LSB bits in SPCR // SPR1 SPR0 ~SPI2X Freq @@ -102,11 +102,10 @@ void ltc2400_init() // 0 1 1 fosc/16 // 1 0 1 fosc/64 // 1 1 1 fosc/128 - - constexpr uint8_t SPI_CLOCK_MASK = 0x03; // SPR1 = bit 1, SPR0 = bit 0 on SPCR - constexpr uint8_t SPI_2XCLOCK_MASK = 0x01; // SPI2X = bit 0 on SPSR - SPCR = (SPCR & ~SPI_CLOCK_MASK) | (SPR1 & SPI_CLOCK_MASK); - SPSR = (SPSR & ~SPI_2XCLOCK_MASK); // clear SPI2X + // + // Set SPR1 for /32 + SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR1); + SPSR = 0; // clear SPI2X sei(); } diff --git a/sw/main.cpp b/sw/main.cpp index ccf8b07..3c502cc 100644 --- a/sw/main.cpp +++ b/sw/main.cpp @@ -395,16 +395,19 @@ int main() // One second blink interval pins_set_status(time_now.seconds_ % 2 == 0); +#if 0 /* EEPROM has an endurance of at least 100'000 write/erase cycles. * (Datasheet 8.4 EEPROM Data Memory) * Storing every five hours gives us several years of endurance. * */ - if (last_store_time_seconds + 3600 * 5 >= time_now.seconds_) { + if (last_store_time_seconds + 3600 * 5 <= time_now.seconds_) { + send_debug("Store to EEPROM"); store_capacity_to_eeprom(); } +#endif const auto ltc2400_measure_interval = timer_t{0, 100000uL}; - if (last_ltc2400_measure + ltc2400_measure_interval > time_now) { + if (last_ltc2400_measure + ltc2400_measure_interval < time_now) { last_ltc2400_measure += ltc2400_measure_interval; if (ltc2400_conversion_ready()) { @@ -432,14 +435,16 @@ int main() } constexpr auto threshold_calculation_interval_s = 4; - if (last_threshold_calculation_seconds + threshold_calculation_interval_s > time_now.seconds_) { + if (last_threshold_calculation_seconds + threshold_calculation_interval_s < time_now.seconds_) { last_threshold_calculation_seconds += threshold_calculation_interval_s; + send_debug("Calc thresh"); handle_thresholds(time_now); } + constexpr auto ltc2400_print_interval_s = 10; - if (last_ltc2400_print_time_seconds + ltc2400_print_interval_s > time_now.seconds_) { + if (last_ltc2400_print_time_seconds + ltc2400_print_interval_s < time_now.seconds_) { last_ltc2400_print_time_seconds += ltc2400_print_interval_s; send_capacity(current_capacity, time_now); } @@ -449,7 +454,7 @@ int main() constexpr auto adc_interval_s = 20; switch (adc_state) { case adc_state_t::IDLE: - if (last_adc_measure_time_seconds + adc_interval_s > time_now.seconds_) { + if (last_adc_measure_time_seconds + adc_interval_s < time_now.seconds_) { send_debug("ADC meas"); last_adc_measure_time_seconds += adc_interval_s; SET_ADMUX(0); |