intr1.c

来自「在cypress晶片上實驗讀取ps2 keyboard按鍵後顯示在lcd上,同時」· C语言 代码 · 共 51 行

C
51
字号
#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 + =
减小字号Ctrl + -
显示快捷键?