diff options
| author | Selene ToyKeeper | 2019-07-26 20:51:38 -0600 |
|---|---|---|
| committer | Selene ToyKeeper | 2019-07-26 20:51:38 -0600 |
| commit | 849fcfd3e1b828c6f35d62792a308917b2ef0053 (patch) | |
| tree | 0da7ccd286a2a3d30fa144681b4bd71a756f9531 /spaghetti-monster/fsm-adc.c | |
| parent | merged Mateminco MF01S / MT18 branch, adding support for the new light (diff) | |
| download | anduril-849fcfd3e1b828c6f35d62792a308917b2ef0053.tar.gz anduril-849fcfd3e1b828c6f35d62792a308917b2ef0053.tar.bz2 anduril-849fcfd3e1b828c6f35d62792a308917b2ef0053.zip | |
merged a sanitized copy of the Emisar D4v2 branch; history summarized below:
------------------------------------------------------------
revno: 457
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Thu 2019-07-18 22:42:50 -0600
message:
make sure no thermal events are handled while off
(even though it shouldn't be possible, since it only happens after 15 minutes,
and the light is only awake in muggle-off mode for 1 second)
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
------------------------------------------------------------
revno: 456
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Thu 2019-07-18 20:09:58 -0600
message:
don't measure temperature while asleep
(fixes bug on D4 V2, where it would start flashing after 15 minutes turned off in muggle mode)
(also should speed up response to battery voltage changes while asleep)
modified:
ToyKeeper/spaghetti-monster/fsm-adc.c
------------------------------------------------------------
revno: 455
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Thu 2019-07-18 20:05:56 -0600
message:
made strobe brightness configurable per build target
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
------------------------------------------------------------
revno: 454
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Wed 2019-07-10 10:22:44 -0600
message:
made the "therm faster" code only affect ramping down,
and fixed a couple minor logic issues in the thermal regulation code
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
ToyKeeper/spaghetti-monster/fsm-adc.c
------------------------------------------------------------
revno: 452
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sat 2019-07-06 08:18:25 -0600
message:
fixed spelling error in D4 / D4V2 files
modified:
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4.h
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h
------------------------------------------------------------
revno: 451
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sat 2019-07-06 08:16:57 -0600
message:
fixed placement of #ifdefs on reboot loop prevention
modified:
ToyKeeper/spaghetti-monster/fsm-main.c
------------------------------------------------------------
revno: 450
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sat 2019-07-06 08:15:59 -0600
message:
made level_calc.py also find and show the channel transition points,
so I won't have to do this manually all the time
modified:
bin/level_calc.py
------------------------------------------------------------
revno: 449 [merge]
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sat 2019-07-06 05:31:37 -0600
message:
merged from fsm branch to get factory reset function
modified:
ToyKeeper/spaghetti-monster/anduril/anduril-manual.txt
ToyKeeper/spaghetti-monster/anduril/anduril.c
ToyKeeper/spaghetti-monster/anduril/anduril.txt
ToyKeeper/spaghetti-monster/anduril/cfg-fw3a.h
ToyKeeper/spaghetti-monster/fsm-main.c
ToyKeeper/spaghetti-monster/fsm-misc.c
ToyKeeper/spaghetti-monster/fsm-misc.h
------------------------------------------------------------
revno: 448
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sat 2019-07-06 05:13:40 -0600
message:
started adding some ideas for later (maybe), but not currently used...
... may remove again later
modified:
ToyKeeper/spaghetti-monster/fsm-adc.c
------------------------------------------------------------
revno: 447
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sat 2019-07-06 05:13:11 -0600
message:
adjusted D4 / D4v2 thermal response again; it really doesn't want to behave
modified:
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4.h
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h
------------------------------------------------------------
revno: 446
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Mon 2019-06-17 03:18:06 -0600
message:
made d4 ramp slightly smoother at clock speed change
modified:
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4.h
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h
------------------------------------------------------------
revno: 445
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Mon 2019-06-17 03:17:40 -0600
message:
improved D4S thermal response; less prone to overshooting and oscillation
modified:
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4s.h
------------------------------------------------------------
revno: 444
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Thu 2019-06-06 23:12:26 -0600
message:
adjusted Emisar D4 thermal response values...
(this light really doesn't want to behave thermally,
so the values here don't quite get an ideal response,
but they're a lot better than they were)
modified:
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4.h
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h
------------------------------------------------------------
revno: 443
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Thu 2019-06-06 23:11:08 -0600
message:
made anduril raise brightness slower when doing thermal regulation,
made hard turbo drop only activate when it's actually at turbo
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
------------------------------------------------------------
revno: 442
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Thu 2019-06-06 23:09:54 -0600
message:
made more thermal parameters configurable...
- the speed of thermal history rotation (every 1s, 2s, or 4s)
- prediction strength
also made minimum time between temperature warnings factor in the lowpass, so the default is 5s again instead of 7s
modified:
ToyKeeper/spaghetti-monster/fsm-adc.c
------------------------------------------------------------
revno: 441
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Wed 2019-06-05 03:39:34 -0600
message:
adjusted d4v2 ramp to hit 1x7135 exactly at level 4/7, turned off hard turbo drop because it doesn't seem to be needed any more
modified:
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h
------------------------------------------------------------
revno: 440 [merge]
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Wed 2019-06-05 03:33:21 -0600
message:
merged updates from fsm branch
added:
ToyKeeper/spaghetti-monster/anduril/cfg-ff-e01.h
ToyKeeper/spaghetti-monster/fireflies-ui/cfg-ff-e07-2.h
renamed:
ToyKeeper/spaghetti-monster/fireflies-ui/cfg-ff-edc-thrower.h => ToyKeeper/spaghetti-monster/fireflies-ui/cfg-ff-e01.h
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
ToyKeeper/spaghetti-monster/fireflies-ui/Makefile
ToyKeeper/spaghetti-monster/fireflies-ui/build-all.sh
ToyKeeper/spaghetti-monster/fireflies-ui/fireflies-ui.c
ToyKeeper/spaghetti-monster/fireflies-ui/meta
ToyKeeper/spaghetti-monster/fsm-events.h
ToyKeeper/spaghetti-monster/fireflies-ui/cfg-ff-e01.h
------------------------------------------------------------
revno: 439
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-31 02:10:02 -0600
message:
added D4v2-219 build for reduced power (only 75% FET)
added:
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2-219.h
------------------------------------------------------------
revno: 438
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-31 01:57:14 -0600
message:
made MCU stay awake at boot long enough to get accurate voltage reading, sped up return-to-sleep delays, made RGB "voltage" preview mode last twice as long
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
------------------------------------------------------------
revno: 437
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-31 01:56:18 -0600
message:
sped up button release timeout slightly to reduce lag when turning off light
modified:
ToyKeeper/spaghetti-monster/fsm-events.h
------------------------------------------------------------
revno: 436 [merge]
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Thu 2019-05-23 00:43:14 -0600
message:
merged from fsm branch to get a bunch of recent updates
modified:
... a lot of files
------------------------------------------------------------
revno: 435
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Tue 2019-05-21 14:25:28 -0600
message:
no muggle mode on D4v2, by request
modified:
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h
------------------------------------------------------------
revno: 434
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Mon 2019-05-20 00:16:26 -0600
message:
improved RGB aux LED previews for voltage mode and blinking mode
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
------------------------------------------------------------
revno: 433
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-10 23:53:47 -0600
message:
fixed D4v2 ramp shape and thermal limit
modified:
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h
------------------------------------------------------------
revno: 432 [merge]
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-10 23:39:48 -0600
message:
merged from lantern branch, to get newer candle mode
added:
ToyKeeper/spaghetti-monster/anduril/cfg-blf-lantern.h
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
ToyKeeper/spaghetti-monster/fsm-ramping.c
ToyKeeper/spaghetti-monster/fsm-ramping.h
------------------------------------------------------------
revno: 431 [merge]
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-10 23:29:28 -0600
message:
merged from upstream fsm branch
added:
ToyKeeper/spaghetti-monster/anduril/cfg-fw3a-219.h
modified:
ToyKeeper/spaghetti-monster/anduril/anduril-manual.txt
------------------------------------------------------------
revno: 430
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-10 23:27:55 -0600
message:
use only red, green, and blue for voltage display... don't do color mixes
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
------------------------------------------------------------
revno: 429
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-10 23:27:30 -0600
message:
flash-tiny1634-fuses had a typo
modified:
bin/flash-tiny1634-fuses.sh
------------------------------------------------------------
revno: 428
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sat 2019-05-04 18:03:28 -0600
message:
fixed builds without sleep ticks enabled
modified:
ToyKeeper/spaghetti-monster/fsm-wdt.c
------------------------------------------------------------
revno: 427
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sat 2019-05-04 18:01:17 -0600
message:
made aux LEDs turn on at boot time, instead of waiting until sleep LVP measures the battery
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
------------------------------------------------------------
revno: 426
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sat 2019-05-04 18:00:34 -0600
message:
implemented LVP in standby mode
(which allows LVP to turn off high-drain aux LEDs while asleep,
and allows RGB readout to change with battery charge)
modified:
ToyKeeper/spaghetti-monster/fsm-adc.c
ToyKeeper/spaghetti-monster/fsm-adc.h
ToyKeeper/spaghetti-monster/fsm-wdt.c
------------------------------------------------------------
revno: 425
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sat 2019-05-04 03:14:33 -0600
message:
enable tenclick thermal config in D4v2 target
modified:
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h
------------------------------------------------------------
revno: 424
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sat 2019-05-04 03:14:16 -0600
message:
added RGB support to lockout mode, improved RGB behavior, set defaults to low because the high mode is way too bright
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
------------------------------------------------------------
revno: 423
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sat 2019-05-04 02:36:55 -0600
message:
got RGB aux LED modes working -- (off, low, high, blinking) and (7 colors + rainbow + voltage)
(off mode only, for now, no lockout)
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
------------------------------------------------------------
revno: 422
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 18:25:23 -0600
message:
Turn off aux LEDs when voltage is low. They use too much power on high mode.
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
------------------------------------------------------------
revno: 421
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 18:24:15 -0600
message:
Calibrated D4v2 voltage.
modified:
ToyKeeper/hwdef-Emisar_D4v2.h
------------------------------------------------------------
revno: 420
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 17:48:51 -0600
message:
fixed RGB aux LED low mode
modified:
ToyKeeper/hwdef-Emisar_D4v2.h
ToyKeeper/spaghetti-monster/fsm-misc.c
------------------------------------------------------------
revno: 419
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 17:35:47 -0600
message:
started on RGB aux LED support in Anduril, but it's just an early test right now
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h
------------------------------------------------------------
revno: 418
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 17:35:05 -0600
message:
adjust ramping auto-aux-LED code to include RGB version (shut off when main LEDs are on)
modified:
ToyKeeper/spaghetti-monster/fsm-ramping.c
------------------------------------------------------------
revno: 417
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 17:33:08 -0600
message:
started on tiny1634 RGB aux LED support
(seems to work except for the "low" output mode)
modified:
ToyKeeper/hwdef-Emisar_D4v2.h
ToyKeeper/spaghetti-monster/fsm-misc.c
ToyKeeper/spaghetti-monster/fsm-misc.h
------------------------------------------------------------
revno: 416
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 16:22:15 -0600
message:
fixed D4v2 PWM speed and delay speed, fixed hardware setup reference comments
modified:
ToyKeeper/hwdef-Emisar_D4v2.h
------------------------------------------------------------
revno: 415
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 16:20:50 -0600
message:
fixed attiny1634 clock speed adjustments
modified:
ToyKeeper/tk-attiny.h
------------------------------------------------------------
revno: 414
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 15:38:52 -0600
message:
renamed SWITCH_PIN_D (pin data) to SWITCH_PORT, to match AVR manual terminology
(even though the value should be PINA or PINB or PINC)
modified:
ToyKeeper/hwdef-Emisar_D4v2.h
ToyKeeper/spaghetti-monster/fsm-pcint.c
ToyKeeper/tk-attiny.h
------------------------------------------------------------
revno: 413
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 01:48:10 -0600
message:
added tiny1634 flashing scripts
added:
bin/flash-tiny1634-fuses.sh
bin/flash-tiny1634.sh
------------------------------------------------------------
revno: 411
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 01:42:35 -0600
message:
started adding tiny1634 support to FSM ... so much kludge, such need clean
modified:
ToyKeeper/spaghetti-monster/fsm-adc.c
ToyKeeper/spaghetti-monster/fsm-main.c
ToyKeeper/spaghetti-monster/fsm-pcint.c
ToyKeeper/spaghetti-monster/fsm-wdt.c
ToyKeeper/tk-attiny.h
------------------------------------------------------------
revno: 410
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 01:40:15 -0600
message:
added D4v2-1634 config and hwdef files
modified:
ToyKeeper/hwdef-Emisar_D4v2.h
ToyKeeper/spaghetti-monster/anduril/cfg-emisar-d4v2.h
------------------------------------------------------------
revno: 409
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Fri 2019-05-03 01:39:24 -0600
message:
made build-all.sh detect attiny type from cfg file, and use it
modified:
ToyKeeper/spaghetti-monster/anduril/build-all.sh
------------------------------------------------------------
revno: 403 [merge]
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sun 2019-04-14 18:14:51 -0600
message:
merged updates from upstream
added/modified:
... lots of files
------------------------------------------------------------
revno: 402
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Sun 2019-04-14 18:08:08 -0600
message:
merged in some aux LED code updates from anduril
modified:
ToyKeeper/spaghetti-monster/rampingios/rampingiosv3.c
------------------------------------------------------------
revno: 401
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Thu 2018-12-27 16:23:09 -0700
message:
tk-attiny.h: started adding other MCU support
also removed redundant eeprom size hints
modified:
ToyKeeper/tk-attiny.h
------------------------------------------------------------
revno: 400
committer: Selene ToyKeeper <git@toykeeper.net>
branch nick: emisar-d4v2
timestamp: Thu 2018-12-27 16:21:59 -0700
message:
made dynamic clocking safer and more portable
(uses library function instead of direct register access)
(lib disables interrupts properly, which I wasn't doing)
modified:
ToyKeeper/spaghetti-monster/anduril/anduril.c
ToyKeeper/spaghetti-monster/fsm-events.c
ToyKeeper/spaghetti-monster/fsm-misc.c
ToyKeeper/spaghetti-monster/spaghetti-monster.h
Diffstat (limited to 'spaghetti-monster/fsm-adc.c')
| -rw-r--r-- | spaghetti-monster/fsm-adc.c | 137 |
1 files changed, 108 insertions, 29 deletions
diff --git a/spaghetti-monster/fsm-adc.c b/spaghetti-monster/fsm-adc.c index bcf49ed..6832e32 100644 --- a/spaghetti-monster/fsm-adc.c +++ b/spaghetti-monster/fsm-adc.c @@ -20,27 +20,76 @@ #ifndef FSM_ADC_C #define FSM_ADC_C -#ifdef USE_VOLTAGE_DIVIDER -// 1.1V / pin7 -#define ADMUX_VOLTAGE ADMUX_VOLTAGE_DIVIDER -#else -// VCC / 1.1V reference -#define ADMUX_VOLTAGE ADMUX_VCC -#endif +inline void set_admux_therm() { + #if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85) || (ATTINY == 1634) + ADMUX = ADMUX_THERM; + #elif (ATTINY == 841) + ADMUXA = ADMUXA_THERM; + ADMUXB = ADMUXB_THERM; + #else + #error Unrecognized MCU type + #endif +} + +inline void set_admux_voltage() { + #if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85) || (ATTINY == 1634) + #ifdef USE_VOLTAGE_DIVIDER + // 1.1V / pin7 + ADMUX = ADMUX_VOLTAGE_DIVIDER; + #else + // VCC / 1.1V reference + ADMUX = ADMUX_VCC; + #endif + #elif (ATTINY == 841) + #ifdef USE_VOLTAGE_DIVIDER + ADMUXA = ADMUXA_VOLTAGE_DIVIDER; + ADMUXB = ADMUXB_VOLTAGE_DIVIDER; + #else + ADMUXA = ADMUXA_VCC; + ADMUXB = ADMUXB_VCC; + #endif + #else + #error Unrecognized MCU type + #endif +} + +inline void ADC_start_measurement() { + #if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85) || (ATTINY == 841) || (ATTINY == 1634) + ADCSRA |= (1 << ADSC) | (1 << ADIE); + #else + #error unrecognized MCU type + #endif +} + +// set up ADC for reading battery voltage inline void ADC_on() { - // read voltage on VCC by default - ADMUX = ADMUX_VOLTAGE; - #ifdef USE_VOLTAGE_DIVIDER - // disable digital input on divider pin to reduce power consumption - DIDR0 |= (1 << VOLTAGE_ADC_DIDR); + #if (ATTINY == 25) || (ATTINY == 45) || (ATTINY == 85) || (ATTINY == 1634) + set_admux_voltage(); + #ifdef USE_VOLTAGE_DIVIDER + // disable digital input on divider pin to reduce power consumption + DIDR0 |= (1 << VOLTAGE_ADC_DIDR); + #else + // disable digital input on VCC pin to reduce power consumption + //DIDR0 |= (1 << ADC_DIDR); // FIXME: unsure how to handle for VCC pin + #endif + #if (ATTINY == 1634) + ACSRA |= (1 << ACD); // turn off analog comparator to save power + #endif + // enable, start, prescale + ADCSRA = (1 << ADEN) | (1 << ADSC) | ADC_PRSCL; + // end tiny25/45/85 + #elif (ATTINY == 841) + ADCSRB = 0; // Right adjusted, auto trigger bits cleared. + //ADCSRA = (1 << ADEN ) | 0b011; // ADC on, prescaler division factor 8. + set_admux_voltage(); + // enable, start, prescale + ADCSRA = (1 << ADEN) | (1 << ADSC) | ADC_PRSCL; + //ADCSRA |= (1 << ADSC); // start measuring #else - // disable digital input on VCC pin to reduce power consumption - //DIDR0 |= (1 << ADC_DIDR); // FIXME: unsure how to handle for VCC pin + #error Unrecognized MCU type #endif - // enable, start, prescale - ADCSRA = (1 << ADEN) | (1 << ADSC) | ADC_PRSCL; } inline void ADC_off() { @@ -89,6 +138,9 @@ ISR(ADC_vect) { // thermal declarations #ifdef USE_THERMAL_REGULATION + #ifndef THERMAL_UPDATE_SPEED + #define THERMAL_UPDATE_SPEED 2 + #endif #define NUM_THERMAL_VALUES_HISTORY 8 #define ADC_STEPS 4 static uint8_t history_step = 0; // don't update history as often @@ -96,7 +148,7 @@ ISR(ADC_vect) { static uint8_t temperature_timer = 0; static uint8_t overheat_lowpass = 0; static uint8_t underheat_lowpass = 0; - #define TEMPERATURE_TIMER_START (THERMAL_WARNING_SECONDS*ADC_CYCLES_PER_SECOND) // N seconds between thermal regulation events + #define TEMPERATURE_TIMER_START ((THERMAL_WARNING_SECONDS-2)*ADC_CYCLES_PER_SECOND) // N seconds between thermal regulation events #define OVERHEAT_LOWPASS_STRENGTH (ADC_CYCLES_PER_SECOND*2) // lowpass for 2 seconds #define UNDERHEAT_LOWPASS_STRENGTH (ADC_CYCLES_PER_SECOND*2) // lowpass for 2 seconds #else @@ -110,6 +162,12 @@ ISR(ADC_vect) { pseudo_rand_seed += measurement; #endif + #if defined(TICK_DURING_STANDBY) && defined(USE_SLEEP_LVP) + // only measure battery voltage while asleep + if (go_to_standby) adc_step = 1; + else + #endif + adc_step = (adc_step + 1) & (ADC_STEPS-1); #ifdef USE_LVP @@ -120,7 +178,7 @@ ISR(ADC_vect) { if (voltage == 0) { for(uint8_t i=0; i<NUM_VOLTAGE_VALUES; i++) voltage_values[i] = measurement; - voltage = 42; // Life, the Universe, and Everything (*) + voltage = 42; // the answer to life, the universe, and the voltage of a full li-ion cell } else { uint16_t total = 0; uint8_t i; @@ -181,7 +239,7 @@ ISR(ADC_vect) { // temperature else if (adc_step == 3) { // Convert ADC units to Celsius (ish) - int16_t temp = measurement - 275 + THERM_CAL_OFFSET + therm_cal_offset; + int16_t temp = measurement - 275 + THERM_CAL_OFFSET + (int16_t)therm_cal_offset; // prime on first execution if (reset_thermal_history) { @@ -202,14 +260,18 @@ ISR(ADC_vect) { // guess what the temperature will be in a few seconds int16_t pt; { - uint8_t i; int16_t diff; int16_t t = temperature; // algorithm tweaking; not really intended to be modified // how far ahead should we predict? + #ifndef THERM_PREDICTION_STRENGTH #define THERM_PREDICTION_STRENGTH 4 - // how proportional should the adjustments be? + #endif + // how proportional should the adjustments be? (not used yet) + #ifndef THERM_RESPONSE_MAGNITUDE + #define THERM_RESPONSE_MAGNITUDE 128 + #endif // acceptable temperature window size in C #define THERM_WINDOW_SIZE 5 // highest temperature allowed @@ -219,13 +281,22 @@ ISR(ADC_vect) { // if it's time to rotate the thermal history, do it history_step ++; - if (0 == (history_step & 7)) { + #if (THERMAL_UPDATE_SPEED == 4) // new value every 4s + #define THERM_HISTORY_STEP_MAX 15 + #elif (THERMAL_UPDATE_SPEED == 2) // new value every 2s + #define THERM_HISTORY_STEP_MAX 7 + #elif (THERMAL_UPDATE_SPEED == 1) // new value every 1s + #define THERM_HISTORY_STEP_MAX 3 + #elif (THERMAL_UPDATE_SPEED == 0) // new value every 0.5s + #define THERM_HISTORY_STEP_MAX 1 + #endif + if (0 == (history_step & THERM_HISTORY_STEP_MAX)) { // rotate measurements and add a new one - for (i=0; i<NUM_THERMAL_VALUES_HISTORY-1; i++) { + for (uint8_t i=0; i<NUM_THERMAL_VALUES_HISTORY-1; i++) { temperature_history[i] = temperature_history[i+1]; } + temperature_history[NUM_THERMAL_VALUES_HISTORY-1] = t; } - temperature_history[NUM_THERMAL_VALUES_HISTORY-1] = t; // guess what the temp will be several seconds in the future // diff = rate of temperature change @@ -244,7 +315,8 @@ ISR(ADC_vect) { // cancel counters if appropriate if (pt > THERM_FLOOR) { underheat_lowpass = 0; // we're probably not too cold - } else if (pt < THERM_CEIL) { + } + if (pt < THERM_CEIL) { overheat_lowpass = 0; // we're probably not too hot } @@ -261,6 +333,7 @@ ISR(ADC_vect) { overheat_lowpass = 0; temperature_timer = TEMPERATURE_TIMER_START; // how far above the ceiling? + //int16_t howmuch = (pt - THERM_CEIL) * THERM_RESPONSE_MAGNITUDE / 128; int16_t howmuch = pt - THERM_CEIL; // try to send out a warning emit(EV_temperature_high, howmuch); @@ -276,6 +349,7 @@ ISR(ADC_vect) { underheat_lowpass = 0; temperature_timer = TEMPERATURE_TIMER_START; // how far below the floor? + //int16_t howmuch = (THERM_FLOOR - pt) * THERM_RESPONSE_MAGNITUDE / 128; int16_t howmuch = THERM_FLOOR - pt; // try to send out a warning (unless voltage is low) // (LVP and underheat warnings fight each other) @@ -291,16 +365,21 @@ ISR(ADC_vect) { // set the correct type of measurement for next time #ifdef USE_THERMAL_REGULATION #ifdef USE_LVP - if (adc_step < 2) ADMUX = ADMUX_VOLTAGE; - else ADMUX = ADMUX_THERM; + if (adc_step < 2) set_admux_voltage(); + else set_admux_therm(); #else - ADMUX = ADMUX_THERM; + set_admux_therm(); #endif #else #ifdef USE_LVP - ADMUX = ADMUX_VOLTAGE; + set_admux_voltage(); #endif #endif + + #ifdef TICK_DURING_STANDBY + // if we were asleep, go back to sleep + if (go_to_standby) ADC_off(); + #endif } #ifdef USE_BATTCHECK |
