From e0cd617537512cf3da20e99133b2bc6219067020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sat, 2 Aug 2014 23:50:05 -0700 Subject: [PATCH] add functionnality: memory is erased when holding button for 5s --- pic/MDR/MDR.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/pic/MDR/MDR.c b/pic/MDR/MDR.c index 3f3898b..0b648e4 100644 --- a/pic/MDR/MDR.c +++ b/pic/MDR/MDR.c @@ -51,6 +51,7 @@ static uint8_t switches; /* save the last switch state */ #define START 208 /* starting timer 0 value to wait up to 12ms in start_timer*/ static uint8_t code[3] = {0xf1, 0x11, 0x11}; /* the received code (24 bits) */ static uint8_t new = 0; /* has a new code been detected (clear using button) */ +static uint8_t hold = 0; /* how long has the button been held, in 250ms steps */ /* configuration bits */ uint16_t __at(_CONFIG1) __CONFIG1 = _FCMEN_ON & /* enable fail-safe clock monitor */ @@ -80,7 +81,7 @@ void init (void) { LATA &= ~(RELAY1|RELAY2); /* switch relays off */ TRISB |= SWITCH1|SWITCH2; /* switches are inputs (with external pull-ups) */ TRISB &= ~(LED); /* LED is an output (used a sink) */ - led_off(); /* starting state */ + LATB |= LED; /* switch LED off */ /* configure switches input */ IOCIE = 1; /* enable interrupt on individual GPIO (typo in the lib) */ @@ -100,7 +101,7 @@ void init (void) { TMR0IF = 0; /* clear timer 0 interrupt */ /* use timer 2 to measure pulse durations (max 16.384ms) */ - TMR2ON = 1; /* stop timer 2 */ + TMR2ON = 0; /* stop timer 2 */ T2CKPS0 = 1; /* use prescaler of 64 */ T2CKPS1 = 1; /* use prescaler of 64 */ T2OUTPS0 = 0; /* use postscale of 1 */ @@ -111,7 +112,19 @@ void init (void) { TMR2IE = 1; /* enable timer 2 interrupt */ TMR2IF = 0; /* clear timer 2 interrupt */ - /* the MDR originally uses a PIC16C54A + /* use timer 4 to measure button hold (250ms per interrupt) */ + TMR4ON = 0; /* stop timer 4 */ + T4CKPS0 = 1; /* use prescaler of 64 */ + T4CKPS1 = 1; /* use prescaler of 64 */ + T4OUTPS0 = 1; /* use postscale of 16 */ + T4OUTPS1 = 1; /* use postscale of 16 */ + T4OUTPS2 = 1; /* use postscale of 16 */ + T4OUTPS3 = 1; /* use postscale of 16 */ + PR4 = 244; /* set interrupt on overflow on this value to get 250ms */ + TMR4IE = 1; /* enable timer 4 interrupt */ + TMR4IF = 0; /* clear timer 4 interrupt */ + + /* the MDR originally uses a PIC16C54A (PDIP for MDR/MDR2, SOIC for MDR-U) * this does does not provide hardware I2C function * they implement I2C in software (it's even a bit buggy) * it even does not used open collectors (with pull-ups), but drives the signals @@ -317,8 +330,11 @@ static void interrupt(void) __interrupt 0 if (PORTB&SWITCH1) { /* switch 1 released */ led_off(); /* reset LED */ new = 0; /* clear new code status */ + TMR4ON = 0; /* stop counting how long the button is held */ } else { /* switch 1 pressed */ led_on(); /* test LED */ + hold = 0; /* reset counter how long the button is held */ + TMR4ON = 1; /* start counting how long the button is held */ } } if (switches&SWITCH2) { /* switch 2 changed */ @@ -338,6 +354,23 @@ static void interrupt(void) __interrupt 0 TMR2 = 0xff; /* set timer 2 count to maximum */ TMR2IF = 0; /* clear timer 2 interrupt */ } + if (TMR4IF) { /* timer 4 overflow, 250ms passed during button press */ + hold++; /* increment 250ms counter */ + /* toggle LED */ + if (hold%2) { + led_off(); + } else { + led_on(); + } + /* pressed for 5s, clear memory */ + if (hold==20) { + led_on(); + clear_memory(); + led_off(); + TMR4ON = 0; + } + TMR4IF = 0; /* clear timer 4 interrupt */ + } } void main (void)