以下是一个使用 C 语言编写的 51 单片机中断程序示例,实现您描述的功能: ```c #include <reg51.h> // 定义灯的端口 sbit LED1 = P1^0; sbit LED2 = P1^1; sbit LED3 = P1^2; sbit LED4 = P1^3; sbit LED5 = P1^4; sbit LED6 = P1^5; sbit LED7 = P1^6; sbit LED8 = P1^7; // 定义按钮的端口 sbit Button1 = P3^2; // 从上往下按钮 sbit Button2 = P3^3; // 从下往上按钮 void main() { // 初始化端口 P1 = 0x00; P3 = 0xff; // 开启中断 EA = 1; // 总中断允许 IT0 = 1; // 外部中断 0 下降沿触发 IT1 = 1; // 外部中断 1 下降沿触发 while (1); } void int0() interrupt 0 { // 外部中断 0 服务函数(从上往下) static unsigned char ledState = 0x80; P1 = ledState; ledState >>= 1; if (ledState == 0x00) { ledState = 0x80; } } void int1() interrupt 2 { // 外部中断 1 服务函数(从下往上) static unsigned char ledState = 0x01; P1 = ledState; ledState <<= 1; if (ledState == 0x80) { ledState = 0x01; } } ``` 额外注意的逻辑点: 1. 确保单片机的时钟设置正确,以保证程序的运行时序。 2. 注意中断服务函数的执行时间,尽量简短,以免影响其他程序的正常运行。 3. 实际使用时,要根据硬件连接情况正确配置端口。 [2025-05-01 14:55:54 | AI写代码神器 | 504点数解答]