舍友仅仅打了一把游戏,我就学会了如何实现用中断控制数码管各位的显示

一、电路图

点击下载
在这里插入图片描述

二、代码

#include<reg51.h> // 引入头文件
unsigned int right,left,temp; // 定义个位,十位,中间转换变量,个位保存的变量
void main(){
    
    
	P2 = 0x00; // 初始化
	EA = 1;	// 总中断
	EX0 = 1;	// 外部中断0
	IT0 = 1;	// 触发方式	下降沿
	
	EX1 = 1;	// 外部中断1
	IT1 = 1;	// 触发方式 下降沿
	while(1);
}

// 外部中断0的服务子程序
void first() interrupt 0{
    
    
	left++;	// 十位++
	temp = left%10<<4|right%10; // 取十位的余数 左移 + 个位取余
	P2 = temp; 
}

// 外部中断0的服务子程序
void second() interrupt 2{
    
    
	if(right>9){
    
     // 大于九,十位++ 再加上个位
	left++;
	P2=left%10<<4|right%10;
	right=0;
	}else{
    
    
		right++;	// 个位++
		P2=left%10<<4|right%10;		
	}		
}

猜你喜欢

转载自blog.csdn.net/m0_49635911/article/details/122251951