简单的运用外部中断的仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg51.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code SMG[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
- 0x6f,0x00};//共阴段码,最后一位为数码管不显示
- uchar Display_Smg[4] = {0,0,0,0}; //计数值分解后的各待显示数位
- uchar Dis_Buffer[4]; //显示缓冲,用于存放待显示的数字的段码
- uint Count = 0;
- sbit Key_Clear = P3^6;
- void delay(uchar t)
- {
- uchar i;
- while(t--) for(i = 0;i < 120;i++);
- }
- void Display_Count()
- {
- static uchar Scan_Bit = 0xF7;
- static uchar Dsy_Idx = 0;
- uchar i;
- Display_Smg[3] = Count /1000;//取千位
- Display_Smg[2] = Count /100%10;//取百位
- Display_Smg[1] = Count % 100/10;
- Display_Smg[0] = Count % 10;
- for(i = 0; i < 4;i++)
- {
- Dis_Buffer[i] = SMG[Display_Smg[i]];
- }
- P2 = Scan_Bit;
- P0 = Dis_Buffer[Dsy_Idx];
- delay(1);
- Scan_Bit = _cror_(Scan_Bit,1);
- Dsy_Idx = (Dsy_Idx+1)%4;
- }
- void main()
- {
- EA = 1;
- EX0 = 1;
- EX1 = 1;
- IT0 = 1;
- IT1 = 1;
- while(1)
- {
- if(Count == 10000) Count = 0;
- Display_Count();
- }
- }
- void jishu() interrupt 0
- {
- Count++;
- }
- void clear0() interrupt 2
- {
- Count = 0;
- }
复制代码
所有资料51hei提供下载:
两个外部中断控制计数和清零.zip
(59.21 KB, 下载次数: 25)
|