📄 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)&PORTB*8+7; // use this bit to drive relaystatic unsigned int relay_timer; // timer value for relay drivervoidmain(void){ /* setup stuff */ RELAY = 1; // ensure relay is off before enabling output TRISB = 0x3F; // Port B bits 7 and 6 are output 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 interruptisr(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 PORTB ^= 0x40; // 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 + -