电路原理图如下:
程序设计流程图:
实验总结:
实验中有遇到许多问题。定义LED显示字模时不能定义成unsigned char code类型;count不能定义成unsigned char类型等。程序中用到了外部中断,让我们把书本上的知识运用到了实践中,受益匪浅。
单片机源程序如下:
- #include<reg51.h> //包括一个51标准内核的头文件
- #include<intrins.h>
- sbit P3_2=P3^2; //输出端口定义
- sbit P3_3=P3^3;
- unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //LED灯显示字模
- char count; //定义符号变量
- void delay (unsigned int time) //延时函数
- {unsigned int j=0;
- for(;time>0;time--)
- for(j=0;j<125;j++);
- }
- void INT0_srv() interrupt 0 using 1 //外部中断0处理程序
- {
-
- delay(10);
- if(INT0==0){ //P3^3接口按键按下
- count++; //计数器增1
- if(count==100) //判断循环是否超限
- count=0;
- P1=table[count/10]; //显示十位数字
- P2=table[count%10]; //求余,显示个位数字
- while(P3_2==0); //等待按键松开,防止连续计数
- }}
- void INT1_srv() interrupt 2 using 2 { //外部中断1处理函数
-
-
- delay(10);
- if(INT1==0){
- count--;
- if(count<0)
- count=99;
- P1=table[count/10];
- P2=table[count%10];
- while(P3_3==0);
- }}
-
- void main(){
- count=0;
- EA=1; //中断初始化
- EX0=1;
- EX1=1;
- while(1); //无限循环体
- }
复制代码
完整的Word格式文档51黑下载地址:
实验二 按键计数LED灯数码管显示.docx
(351.14 KB, 下载次数: 21)
|