【Proteus仿真】51单片机外部中断使用
- 🔖Proteus仿真基础实验-定时器使用。
- 🌿Proteus
8.12
平台 - 🌿本实验代码基于VSM Studio,采用SDCC编译器。
- 🎬仿真演示:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fa69079ef07f971de44baaadb140ab57.gif#pic_center)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0a3543cc027ba42700a88a3e1ee5c52a.gif#pic_center)
📝示例一代码
#include <mcs51reg.h>
#define LED (P2_0)
__bit INT_FLAG = 0;
void delayms(unsigned int ms)
{
unsigned int x;
unsigned int y;
for (y=ms; y>0; y--)
{
for (x=227; x>0; x--)
;
}
}
void led_flash()
{
LED = !LED;
delayms(200);
LED = !LED;
delayms(200);
LED = !LED;
delayms(200);
LED = !LED;
}
void int0_init()
{
IT0 = 1;
EX0 = 1;
EA = 1;
}
void int0_isr() __interrupt 0 __using 1
{
INT_FLAG = 1;
}
void main(void)
{
LED = 1;
int0_init();
while (1)
{
if(INT_FLAG)
{
led_flash();
INT_FLAG = 0;
}
}
}
📚示例一仿真资源
链接: https:
提取码: 4av2
📝示例二代码
- ⚡ 开启外部中断0和外部中断2,由于led闪烁采用了阻塞式延时,所以led和led1不能同时闪烁。
#include <mcs51reg.h>
#define LED (P2_0)
#define LED1 (P2_1)
__bit INT0_FLAG = 0;
__bit INT1_FLAG = 0;
void delayms(unsigned int ms)
{
unsigned int x;
unsigned int y;
for (y=ms; y>0; y--)
{
for (x=227; x>0; x--)
;
}
}
void led_flash()
{
LED = !LED;
delayms(200);
LED = !LED;
delayms(200);
LED = !LED;
delayms(200);
LED = !LED;
}
void led1_flash()
{
LED1 = !LED1;
delayms(200);
LED1 = !LED1;
delayms(200);
LED1 = !LED1;
delayms(200);
LED1 = !LED1;
}
void int0_init()
{
IT0 = 1;
EX0 = 1;
IT1 = 1;
EX1 = 1;
EA = 1;
}
void int0_isr(void) __interrupt 0 __using 1
{
INT0_FLAG = 1;
}
void exint1(void) __interrupt 2 __using 3
{
INT1_FLAG = 1;
}
void main(void)
{
LED = 1;
LED1 = 1;
int0_init();
while (1)
{
if(INT0_FLAG)
{
led_flash();
INT0_FLAG = 0;
}
if(INT1_FLAG)
{
led1_flash();
INT1_FLAG = 0;
}
}
}
📚示例二资源
链接: https:
提取码: 32pv