mcu中利用软件延时函数编程使LED1周期亮和灭 (间隔为1秒),用外部中断1启动中断,下降沿方式启动 中断服务程序LED2亮3秒钟时延
时间: 2024-05-04 13:21:05 浏览: 13
以下是在MCU中利用软件延时函数编程实现LED1周期性亮和灭的代码:
```c
#include <reg51.h>
sbit LED1 = P1^0; // LED1连接到P1.0
void delay(unsigned int time) // 软件延时函数
{
unsigned int i, j;
for (i = 0; i < time; i++)
{
for (j = 0; j < 125; j++);
}
}
void main()
{
while (1)
{
LED1 = 1; // LED1亮
delay(200); // 延时等待1秒
LED1 = 0; // LED1灭
delay(200); // 延时等待1秒
}
}
```
以下是在MCU中利用外部中断1启动中断服务程序LED2亮3秒钟时延的代码:
```c
#include <reg51.h>
sbit LED2 = P1^1; // LED2连接到P1.1
void delay(unsigned int time) // 软件延时函数
{
unsigned int i, j;
for (i = 0; i < time; i++)
{
for (j = 0; j < 125; j++);
}
}
void ISR_LED2() interrupt 2 // 中断服务程序
{
LED2 = 1; // LED2亮
delay(600); // 延时等待3秒
LED2 = 0; // LED2灭
}
void main()
{
IT0 = 1; // 设置外部中断1为下降沿触发
EX0 = 1; // 使能外部中断1
EA = 1; // 使能总中断
while (1)
{
// 主程序不做任何操作
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)