标题: 单片机按键计数LED灯数码管显示实验程序 [打印本页]

作者: 雷51    时间: 2018-11-25 13:23
标题: 单片机按键计数LED灯数码管显示实验程序
电路原理图如下:


程序设计流程图:


实验总结:
实验中有遇到许多问题。定义LED显示字模时不能定义成unsigned char code类型;count不能定义成unsigned char类型等。程序中用到了外部中断,让我们把书本上的知识运用到了实践中,受益匪浅。


单片机源程序如下:
  1. #include<reg51.h> //包括一个51标准内核的头文件
  2. #include<intrins.h>
  3. sbit P3_2=P3^2; //输出端口定义
  4. sbit P3_3=P3^3;
  5. unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};  //LED灯显示字模
  6. char count; //定义符号变量
  7. void delay (unsigned int time)  //延时函数
  8. {unsigned int j=0;
  9. for(;time>0;time--)
  10.   for(j=0;j<125;j++);
  11. }
  12. void INT0_srv() interrupt 0 using 1  //外部中断0处理程序
  13.         {
  14.   
  15.     delay(10);
  16.         if(INT0==0){   //P3^3接口按键按下
  17.          count++;  //计数器增1
  18.          if(count==100)  //判断循环是否超限
  19.            count=0;
  20.            P1=table[count/10];  //显示十位数字
  21.            P2=table[count%10];  //求余,显示个位数字
  22.            while(P3_2==0);  //等待按键松开,防止连续计数
  23. }}
  24. void INT1_srv() interrupt 2 using 2 {  //外部中断1处理函数
  25.   

  26.     delay(10);
  27.         if(INT1==0){
  28.          count--;
  29.          if(count<0)
  30.            count=99;
  31.            P1=table[count/10];
  32.            P2=table[count%10];
  33.            while(P3_3==0);
  34. }}

  35. void main(){  
  36. count=0;
  37. EA=1;  //中断初始化
  38. EX0=1;
  39. EX1=1;
  40. while(1);  //无限循环体
  41. }
复制代码

完整的Word格式文档51黑下载地址:
实验二 按键计数LED灯数码管显示.docx (351.14 KB, 下载次数: 21)







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1