⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 portb.c

📁 picc_18v 8.35pl35 PIC18系列单片机开发C编译器
💻 C
字号:
#include <pic18.h>
#include "portb.h"

unsigned int COUNTER0;
unsigned int COUNTER1;
unsigned int COUNTER2;
unsigned int COUNTER3;
unsigned int COUNTER4;
unsigned int COUNTER5;
unsigned int COUNTER6;
unsigned int COUNTER7;
unsigned int COUNTER8;

void interrupt ISR(void);
void init(void);

void main(void)
{
	
	init();
	
	while(1)
	{
		INTEDG0=RA5;	// rising/falling edge trigger set by device on RA5
		INTEDG1=RA5;
		INTEDG2=RA5;
		INTEDG3=RA5;
		if(COUNTER0>0)LED0=((--COUNTER0)>>8)&1;	// if an LED 16-Bit counter is active,
		if(COUNTER1>0)LED1=((--COUNTER1)>>8)&1;	// the LED will display the status of
		if(COUNTER2>0)LED2=((--COUNTER2)>>8)&1;	// bit 8 of the counter variable.
		if(COUNTER3>0)LED3=((--COUNTER3)>>8)&1;	// This will give the LED a blinking
		if(COUNTER4>0)LED4=((--COUNTER4)>>8)&1;	// characteristic.
		if(COUNTER5>0)LED5=((--COUNTER5)>>8)&1;
		if(COUNTER6>0)LED6=((--COUNTER6)>>8)&1;
		if(COUNTER7>0)LED7=((--COUNTER7)>>8)&1;
		if(COUNTER8>0)LED8=((--COUNTER8)>>8)&1;
	}
}

void interrupt ISR(void)
{
	static unsigned char last_portb;
	unsigned char change;
	
	if((INT0IE)&&(INT0IF))
	{
		COUNTER0=RELOAD;	// reload the LED counter if interrupt detected
		INT0IF=0;
	}
	if((INT1IE)&&(INT1IF))
	{
		COUNTER1=RELOAD;
		INT1IF=0;
	}
	if((INT2IE)&&(INT2IF))
	{
		COUNTER2=RELOAD;
		INT2IF=0;
	}
	if((INT3IE)&&(INT3IF))
	{
		COUNTER3=RELOAD;
		INT3IF=0;
	}
	
	if((RBIF)&&(RBIE))
	{
		if((PORTB)>(last_portb))
		{
			change=(PORTB^last_portb);	// general interrupt on change, need to
			switch(change)			// work out which pin was responsible for
			{				// this interrupt.
				case 0x10:
					COUNTER4=RELOAD;
					break;
				case 0x20:
					COUNTER5=RELOAD;
					break;
				case 0x40:
					COUNTER6=RELOAD;
					break;
				default:
					COUNTER7=RELOAD;
					break;
			}
		}
		else
			COUNTER8=RELOAD;
		RBIF=0;
	}
	last_portb=PORTB;
}

void init(void)
{
	TRISB=0;
	PORTB=0;
	TRISB=0xFF;
	TRISC=0x00;
	PORTC=0;
	TRISA=0x3F;

	RBPU=0;
	
	GIE=1;
	IPEN=0;

	INT0IE=1;
	INT1IE=1;
	INT2IE=1;
	INT3IE=1;
	RBIE=1;
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -