Skip to content

Commit bfcd829

Browse files
committed
Take a sample from LM35 only every few seconds
1 parent 7b0897d commit bfcd829

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

include/lang.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static __code const char *__code _str_state[] = {"TIME", "DATE", "TEMPERATURE",
3535

3636
#define STR_FMT_AUTO " AUTO<%s>"
3737
#define STR_FMT_INIT "p18clock firmware/%hu.%hu.%hu"
38-
#define STR_FMT_TEMP " %02u C"
38+
#define STR_FMT_TEMP " %02u'C"
3939
#define STR_FMT_MDAY "day"
4040
#define STR_FMT_MONTH "m "
4141
#define STR_FMT_YEAR "y"
@@ -56,7 +56,7 @@ static __code const char *__code _str_state[] = {"HORA", "FECHA", "TEMPERATURA",
5656

5757
#define STR_FMT_AUTO " AUTO<%s>"
5858
#define STR_FMT_INIT "p18clock firmware/%hu.%hu.%hu"
59-
#define STR_FMT_TEMP " %02u C"
59+
#define STR_FMT_TEMP " %02u'C"
6060
#define STR_FMT_MDAY "dia"
6161
#define STR_FMT_MONTH "m "
6262
#define STR_FMT_YEAR "a"

src/main.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ SIGHANDLERNAKED(_tmr0_handler)
349349
}
350350
// clang-format on
351351

352+
/// Interval (in seconds) that separates two LM35 samples.
353+
#define TEMPERATURE_INTERVAL 4
354+
/// Last LM35 measurement, in Celsius degrees.
355+
static int _temperature = 0;
356+
352357
/* PWM 2.44khz@50 (Fosc=8mhz) */
353358
#define CCP1_PR2 0xcb
354359
#define CCP1_R 0x066
@@ -391,6 +396,8 @@ void uc_init(void) {
391396

392397
INTCON2bits.INTEDG0 = 0; // INT0 triggered on falling edge
393398
INTCONbits.INT0IE = 1;
399+
400+
_temperature = lm35_get();
394401
}
395402

396403
/// Return the day of the week for the given date, i.e. 0 - Sunday, 1 - Monday,
@@ -581,7 +588,7 @@ void S_temp(char arg, __data char *input) /* __wparam */
581588
(void)arg;
582589
if (input == (__data char *)NULL) {
583590
LEDMTX_HOME();
584-
printf(STR_FMT_TEMP, lm35_get());
591+
printf(STR_FMT_TEMP, _temperature);
585592
} else if (*input == B_MODE) {
586593
_state = STATE_ALARM;
587594

@@ -811,6 +818,7 @@ static state_func_t __code _state_fn[] = {
811818
void main(void) {
812819
static char c;
813820
static unsigned char rcounter = 0xff;
821+
static unsigned char temp_measurement_sched_sec = 0;
814822

815823
/* initialisation */
816824
uc_init();
@@ -824,11 +832,16 @@ void main(void) {
824832

825833
/* main loop */
826834
while (1) {
835+
if (_time.sec >= temp_measurement_sched_sec) {
836+
_temperature = lm35_get();
837+
temp_measurement_sched_sec =
838+
(temp_measurement_sched_sec + TEMPERATURE_INTERVAL) % 60;
839+
}
840+
827841
if (++rcounter == 0) {
828842
rcounter = 0x51; // Should overflow in roughly 0.5s
829843
_state_fn[_state](MESSAGE_CLASS(c), NULL);
830844
}
831-
832845
c = rbuf_get(_mbuf);
833846
if (c != -1) {
834847
switch (c) {

0 commit comments

Comments
 (0)