?? intr1.c
字號:
#include <pic.h>
/*
* Interrupt demo for PIC; wait for button press on RB0/INT,
* turn on a relay on another port bit for a period of time.
* For simplicity here, literal constants are used, usually these
* should be calculated with compile-time arithmetic.
*/
static bit RELAY @ (unsigned)&PORTC*8+5; // use this bit to drive relay
static unsigned int relay_timer; // timer value for relay driver
void
main(void)
{
/* setup stuff */
RELAY = 1; // ensure relay is off before enabling output
TRISC = 0x0F; // Port B bits 7 and 6 are output
TRISB = 0xFF;
T0CS = 0; // Timer increments on instruction clock
T0IE = 1; // Enable interrupt on TMR0 overflow
INTEDG = 0; // falling edge trigger the interrupt
INTE = 1; // enable the external interrupt
GIE = 1; // Global interrupt enable
for(;;)
CLRWDT(); // Idly kick the dog
}
static void interrupt
isr(void) // Here be interrupt function - the name is
// unimportant.
{
if(T0IF) { // timer interrupt
TMR0 -= 250; // reload the timer - 250uS per interrupt
T0IF = 0; // clear the interrupt flag
if(relay_timer != 0) // is the relay timer running?
relay_timer--; // decrement it
if(relay_timer == 0) // if it has time out
RELAY = 1; // turn the relay off
PORTC ^= 0x10; // toggle a bit to say we're alive
}
if(INTF) { // did we see a button press?
RELAY = 0; // turn the relay on
relay_timer = 4000; // start the timer - 4000 ticks = 1 second
INTF = 0; // clear the interrupt
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -