一、电路图
点击下载:
二、代码
#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;
}
}