单片机入门(利用中断控制流水灯的走向)--适合初学者

电路图
点击下载
(下载时可能会提醒不安全,其实没事,本博主是放在自己服务器上面)
单片机入门(利用中断控制流水灯的走向)--适合初学者_第1张图片
代码

#include
// 延时函数
void delay(unsigned time){
	unsigned i , j;
	for(i = 0 ;i< time;i++)
		for(j = 0;j < time ;j++);
}

// 主函数
void main(){
	// 总中断
	EA = 1;
	// 设置下降沿触发,还是上升沿触发,1的话就是上升沿触发
	IT0 = 1;
	// 外部中断0
	EX0 = 1;
	// 设置下降沿触发,还是上升沿触发,1的话就是上升沿触发
	IT1 = 1;
	// 外部中断1
	EX1 = 1;
	while(1);
}

// 外部中断0服务子程序
void direction() interrupt 0{
	int temp,i;
		temp = 0xfe;
		for(i = 0;i<8;i++){
			P2 = temp;
			temp = ( temp << 1 ) | 0x01;
			delay(200);
		}
		P2 = 0xff;
}

// 外部中断1服务子程序
void direction1() interrupt 2{
	int i ;
	int array[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
	for( i = 0 ; i < 8;i++){
		P2 = array[i];
		delay(200);
	}
	P2 = 0xff;
}

你可能感兴趣的:(单片机,单片机,嵌入式硬件,c语言)