use interrupts for switches
This commit is contained in:
parent
ecff66736d
commit
43257b8ace
|
@ -46,6 +46,7 @@
|
||||||
#define sleep() __asm sleep __endasm
|
#define sleep() __asm sleep __endasm
|
||||||
|
|
||||||
/* variables */
|
/* variables */
|
||||||
|
static uint8_t switches; /* save the last switch state */
|
||||||
|
|
||||||
/* configuration bits */
|
/* configuration bits */
|
||||||
uint16_t __at(_CONFIG1) __CONFIG1 = _FCMEN_ON & /* enable fail-safe clock monitor */
|
uint16_t __at(_CONFIG1) __CONFIG1 = _FCMEN_ON & /* enable fail-safe clock monitor */
|
||||||
|
@ -68,11 +69,13 @@ void init (void) {
|
||||||
TRISB |= SWITCH1|SWITCH2; /* switches are inputs (with external pull-ups) */
|
TRISB |= SWITCH1|SWITCH2; /* switches are inputs (with external pull-ups) */
|
||||||
TRISB &= ~(LED|MEMORY_CLK|MEMORY_DATA); /* LED is I2C memory are outputs */
|
TRISB &= ~(LED|MEMORY_CLK|MEMORY_DATA); /* LED is I2C memory are outputs */
|
||||||
led_off(); /* starting state */
|
led_off(); /* starting state */
|
||||||
/*
|
|
||||||
IOC |= (SWITCH1|SWITCH2|SWITCH3|SWITCH4); // enable interrupt for the switches
|
|
||||||
GPIE = 1; // enable interrupt on GPIO
|
|
||||||
last_gpio = GPIO; // save current state
|
|
||||||
|
|
||||||
|
IOCIE = 1; /* enable interrupt on individual GPIO (typo in the lib) */
|
||||||
|
IOCBP |= (SWITCH1|SWITCH2); /* enable interrupt when switch is released */
|
||||||
|
IOCBN |= (SWITCH1|SWITCH2); /* enable interrupt when switch is pressed */
|
||||||
|
switches = PORTB; /* save current switch state */
|
||||||
|
|
||||||
|
/*
|
||||||
T1CON = _T1CKPS1 | _T1CKPS0; // set prescaler to 8
|
T1CON = _T1CKPS1 | _T1CKPS0; // set prescaler to 8
|
||||||
// 0 is set per default, but just be sure
|
// 0 is set per default, but just be sure
|
||||||
TMR1ON = 0; // stop timer 1
|
TMR1ON = 0; // stop timer 1
|
||||||
|
@ -81,16 +84,32 @@ void init (void) {
|
||||||
TMR1IF = 0; // clear interrupt
|
TMR1IF = 0; // clear interrupt
|
||||||
PIE1 = 1; // enable timer 1 interrupt
|
PIE1 = 1; // enable timer 1 interrupt
|
||||||
PEIE = 1; // enable timer interrupt
|
PEIE = 1; // enable timer interrupt
|
||||||
|
|
||||||
GIE = 1; // enable interrups
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
GIE = 1; /* golablly enable interrupts */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* funcion called on interrupts */
|
/* funcion called on interrupts */
|
||||||
/* interrupt 0 is only one on PIC16 */
|
/* interrupt 0 is only one on PIC16 */
|
||||||
static void interrupt(void) __interrupt 0
|
static void interrupt(void) __interrupt 0
|
||||||
{
|
{
|
||||||
|
if (IOCIF) { /* GPIO interrupt (typo in library?) */
|
||||||
|
if (IOCBF&(SWITCH1|SWITCH2)) { /* switch activity */
|
||||||
|
switches ^= PORTB; /* figure out which switch changed */
|
||||||
|
if (switches&SWITCH1) { /* switch 1 changed */
|
||||||
|
if (PORTB&SWITCH1) { /* switch 1 released */
|
||||||
|
led_off();
|
||||||
|
} else { /* switch 1 pressed */
|
||||||
|
led_on();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (switches&SWITCH2) { /* switch 2 changed */
|
||||||
|
}
|
||||||
|
switches = PORTB; /* save current switch state */
|
||||||
|
IOCBF &= ~(SWITCH1|SWITCH2); /* clear switch interrupts */
|
||||||
|
}
|
||||||
|
IOCIF = 0; /* clear GPIO interrupt */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void main (void)
|
void main (void)
|
||||||
|
@ -98,11 +117,6 @@ void main (void)
|
||||||
init(); /* configure micro-controller */
|
init(); /* configure micro-controller */
|
||||||
led_on();
|
led_on();
|
||||||
while (1) { /* a microcontroller runs forever */
|
while (1) { /* a microcontroller runs forever */
|
||||||
if (PORTB&SWITCH1) {
|
|
||||||
led_off();
|
|
||||||
} else {
|
|
||||||
led_on();
|
|
||||||
}
|
|
||||||
// sleep(); /* sleep to save power */
|
// sleep(); /* sleep to save power */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue