标题: 单片机INT0及INT1中断计数仿真加源代码 [打印本页]

作者: longllls    时间: 2017-12-5 09:49
标题: 单片机INT0及INT1中断计数仿真加源代码
这是我今天做的单片机实验,附件里面包含INT0及INT1中断计数源代码,proteus仿真工程文件
说明:每次按下第1个计数键时,第1组计数值累加并显示在右边3只数码管上, 每次按下第2个计数键时,第2组计数值累加并显示在左边3只数码管上,后两个按键分别清零。

仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源代码:
  1. #include<reg51.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. sbit K3=P3^4;        //2个清零键
  5. sbit K4=P3^5;
  6. //数码管段码与位码
  7. uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
  8. uchar code DSY_Scan_Bits[]={0x20,0x10,0x08,0x04,0x02,0x01};
  9. //2组计数的显示缓冲,前3位一组,后3位一组
  10. uchar data Buffer_Counts[]={0,0,0,0,0,0};
  11. uint Count_A,Count_B=0;
  12. //延时
  13. void DelayMS(uint x)
  14. {
  15.         uchar t;
  16.         while(x--) for(t=0;t<120;t++);
  17. }
  18. //数据显示
  19. void Show_Counts()
  20. {
  21.         uchar i;
  22.         Buffer_Counts[2]=Count_A/100;
  23.         Buffer_Counts[1]=Count_A%100/10;
  24.         Buffer_Counts[0]=Count_A%10;
  25.         if(        Buffer_Counts[2]==0)
  26.         {
  27.                 Buffer_Counts[2]=0x0a;
  28.                 if(        Buffer_Counts[1]==0)
  29.                         Buffer_Counts[1]=0x0a;
  30.         }
  31.         Buffer_Counts[5]=Count_B/100;
  32.         Buffer_Counts[4]=Count_B%100/10;
  33.         Buffer_Counts[3]=Count_B%10;
  34.         if(        Buffer_Counts[5]==0)
  35.         {
  36.                 Buffer_Counts[5]=0x0a;
  37.                 if(        Buffer_Counts[4]==0)
  38.                         Buffer_Counts[4]=0x0a;
  39.         }
  40.         for(i=0;i<6;i++)
  41.         {
  42.                 P2=DSY_Scan_Bits[i];
  43.                 P1=DSY_CODE[Buffer_Counts[i]];
  44.                 DelayMS(1);
  45.         }
  46. }
  47. //主程序
  48. void main()
  49. {
  50.         IE=0x85;
  51.         PX0=1;        //中断优先
  52.         IT0=1;
  53.         IT1=1;
  54.         while(1)
  55.         {
  56.                 if(K3==0) Count_A=0;
  57.                 if(K4==0) Count_B=0;
  58.                 Show_Counts();
  59.         }
  60. }
  61. //INT0中断函数
  62. void EX_INT0() interrupt 0
  63. {
  64.         Count_A++;         
  65. }
  66. //INT1中断函数
  67. void EX_INT1() interrupt 2
  68. {
  69.         Count_B++;         
  70. }
复制代码

全部资料51hei下载地址:
INT0及INT1中断计数.rar (37.94 KB, 下载次数: 29)







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