使用keil,利用stc89c52,设计使单片机的PX(x=0,1,2或3)端口作为输出口,分别与8个发光二极管相连,通过编写程序,循环将端口置“1”或清“0”,不断向发光二极管输送高低电平,实现流水灯的效果。某一端口接一按键作为外部中断,当按键按下时,所有发光二极管亮灭交替闪烁(或自己定义花样显示)。使用中断函数实现。利用<reg52.h>。
如何将PX(x=0,1,2或3)端口作为输出口并与8个发光二极管相连?
在STC89C52单片机上,可以通过P0、P1、P2、P3四个端口实现这个功能。可以使用<reg52.h>中的SFR(Special Function Register)来控制这些端口,例如:P0 = 0xFF; 代表将P0口的8个引脚设置为高电平,即8个LED全亮;P0 = 0x00; 代表将P0口的8个引脚设置为低电平,即8个LED全灭。如何循环将端口置“1”或清“0”,不断向发光二极管输送高低电平,实现流水灯的效果?
可以使用for循环不断改变P0、P1、P2、P3端口的状态,实现流水灯效果。例如:P0 = 0xFF; 表示P0口8个引脚全部亮起;delay(500); 表示停留500毫秒;P0 = 0x00; 表示P0口8个引脚全部熄灭;delay(500); 表示停留500毫秒;然后再将P0口8个引脚全部亮起,形成循环。
如何使用中断函数实现按键控制所有发光二极管亮灭交替闪烁(或自己定义花样显示)?
可以使用P3口的某一个引脚作为按键输入端口,然后编写中断函数实现对该引脚的中断响应。当该引脚检测到下降沿(即按键按下)时,触发中断,可以在中断函数中编写代码实现所有发光二极管的亮灭交替闪烁(或自己定义花样显示)。
#include <reg52.h>
// 定义 LED 灯连接的端口
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 key = P0^0;
void delay_ms(unsigned int ms) // 延时函数
{
unsigned int i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 114; j++);
}
void main()
{
unsigned char i = 0;
while(1)
{
// 使 LED 依次亮起
LED1 = 1; delay_ms(500);
LED2 = 1; delay_ms(500);
LED3 = 1; delay_ms(500);
LED4 = 1; delay_ms(500);
LED5 = 1; delay_ms(500);
LED6 = 1; delay_ms(500);
LED7 = 1; delay_ms(500);
LED8 = 1; delay_ms(500);
// 使 LED 依次熄灭
LED1 = 0; delay_ms(500);
LED2 = 0; delay_ms(500);
LED3 = 0; delay_ms(500);
LED4 = 0; delay_ms(500);
LED5 = 0; delay_ms(500);
LED6 = 0; delay_ms(500);
LED7 = 0; delay_ms(500);
LED8 = 0; delay_ms(500);
// 检测按键是否被按下,如果按下就开始交替闪烁
if(key == 0)
{
while(key == 0) delay_ms(10); // 消抖处理
while(1)
{
LED1 = i % 2; // i 为偶数时,LED1 亮;奇数时,LED1 熄灭
LED2 = (i+1) % 2; // i+1 为偶数时,LED2 亮;奇数时,LED2 熄灭
LED3 = (i+2) % 2; // ...
LED4 = (i+3) % 2;
LED5 = (i+4) % 2;
LED6 = (i+5) % 2;
LED7 = (i+6) % 2;
LED8 = (i+7) % 2;
delay_ms(500);
i++;
if(i >= 2) i = 0; // i 取值为 0 或 1,用于交替闪烁
}
}
}
}
注意:为了避免按键在按下的瞬间反复触发,需要对按键进行消抖处理。上面的代码中通过延时的方式进行
如何将PX(x=0,1,2或3)端口作为输出口并与8个发光二极管相连?
在STC89C52单片机上,可以通过P0、P1、P2、P3四个端口实现这个功能。可以使用<reg52.h>中的SFR(Special Function Register)来控制这些端口,例如:P0 = 0xFF; 代表将P0口的8个引脚设置为高电平,即8个LED全亮;P0 = 0x00; 代表将P0口的8个引脚设置为低电平,即8个LED全灭。如何循环将端口置“1”或清“0”,不断向发光二极管输送高低电平,实现流水灯的效果?
可以使用for循环不断改变P0、P1、P2、P3端口的状态,实现流水灯效果。例如:P0 = 0xFF; 表示P0口8个引脚全部亮起;delay(500); 表示停留500毫秒;P0 = 0x00; 表示P0口8个引脚全部熄灭;delay(500); 表示停留500毫秒;然后再将P0口8个引脚全部亮起,形成循环。
如何使用中断函数实现按键控制所有发光二极管亮灭交替闪烁(或自己定义花样显示)?
可以使用P3口的某一个引脚作为按键输入端口,然后编写中断函数实现对该引脚的中断响应。当该引脚检测到下降沿(即按键按下)时,触发中断,可以在中断函数中编写代码实现所有发光二极管的亮灭交替闪烁(或自己定义花样显示)。
#include <reg52.h>
// 定义 LED 灯连接的端口
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 key = P0^0;
void delay_ms(unsigned int ms) // 延时函数
{
unsigned int i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 114; j++);
}
void main()
{
unsigned char i = 0;
while(1)
{
// 使 LED 依次亮起
LED1 = 1; delay_ms(500);
LED2 = 1; delay_ms(500);
LED3 = 1; delay_ms(500);
LED4 = 1; delay_ms(500);
LED5 = 1; delay_ms(500);
LED6 = 1; delay_ms(500);
LED7 = 1; delay_ms(500);
LED8 = 1; delay_ms(500);
// 使 LED 依次熄灭
LED1 = 0; delay_ms(500);
LED2 = 0; delay_ms(500);
LED3 = 0; delay_ms(500);
LED4 = 0; delay_ms(500);
LED5 = 0; delay_ms(500);
LED6 = 0; delay_ms(500);
LED7 = 0; delay_ms(500);
LED8 = 0; delay_ms(500);
// 检测按键是否被按下,如果按下就开始交替闪烁
if(key == 0)
{
while(key == 0) delay_ms(10); // 消抖处理
while(1)
{
LED1 = i % 2; // i 为偶数时,LED1 亮;奇数时,LED1 熄灭
LED2 = (i+1) % 2; // i+1 为偶数时,LED2 亮;奇数时,LED2 熄灭
LED3 = (i+2) % 2; // ...
LED4 = (i+3) % 2;
LED5 = (i+4) % 2;
LED6 = (i+5) % 2;
LED7 = (i+6) % 2;
LED8 = (i+7) % 2;
delay_ms(500);
i++;
if(i >= 2) i = 0; // i 取值为 0 或 1,用于交替闪烁
}
}
}
}
注意:为了避免按键在按下的瞬间反复触发,需要对按键进行消抖处理。上面的代码中通过延时的方式进行
如何将PX(x=0,1,2或3)端口作为输出口并与8个发光二极管相连?
在STC89C52单片机上,可以通过P0、P1、P2、P3四个端口实现这个功能。可以使用<reg52.h>中的SFR(Special Function Register)来控制这些端口,例如:P0 = 0xFF; 代表将P0口的8个引脚设置为高电平,即8个LED全亮;P0 = 0x00; 代表将P0口的8个引脚设置为低电平,即8个LED全灭。如何循环将端口置“1”或清“0”,不断向发光二极管输送高低电平,实现流水灯的效果?
可以使用for循环不断改变P0、P1、P2、P3端口的状态,实现流水灯效果。例如:P0 = 0xFF; 表示P0口8个引脚全部亮起;delay(500); 表示停留500毫秒;P0 = 0x00; 表示P0口8个引脚全部熄灭;delay(500); 表示停留500毫秒;然后再将P0口8个引脚全部亮起,形成循环。
如何使用中断函数实现按键控制所有发光二极管亮灭交替闪烁(或自己定义花样显示)?
可以使用P3口的某一个引脚作为按键输入端口,然后编写中断函数实现对该引脚的中断响应。当该引脚检测到下降沿(即按键按下)时,触发中断,可以在中断函数中编写代码实现所有发光二极管的亮灭交替闪烁(或自己定义花样显示)。
#include <reg52.h>
// 定义 LED 灯连接的端口
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 key = P0^0;
void delay_ms(unsigned int ms) // 延时函数
{
unsigned int i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 114; j++);
}
void main()
{
unsigned char i = 0;
while(1)
{
// 使 LED 依次亮起
LED1 = 1; delay_ms(500);
LED2 = 1; delay_ms(500);
LED3 = 1; delay_ms(500);
LED4 = 1; delay_ms(500);
LED5 = 1; delay_ms(500);
LED6 = 1; delay_ms(500);
LED7 = 1; delay_ms(500);
LED8 = 1; delay_ms(500);
// 使 LED 依次熄灭
LED1 = 0; delay_ms(500);
LED2 = 0; delay_ms(500);
LED3 = 0; delay_ms(500);
LED4 = 0; delay_ms(500);
LED5 = 0; delay_ms(500);
LED6 = 0; delay_ms(500);
LED7 = 0; delay_ms(500);
LED8 = 0; delay_ms(500);
// 检测按键是否被按下,如果按下就开始交替闪烁
if(key == 0)
{
while(key == 0) delay_ms(10); // 消抖处理
while(1)
{
LED1 = i % 2; // i 为偶数时,LED1 亮;奇数时,LED1 熄灭
LED2 = (i+1) % 2; // i+1 为偶数时,LED2 亮;奇数时,LED2 熄灭
LED3 = (i+2) % 2; // ...
LED4 = (i+3) % 2;
LED5 = (i+4) % 2;
LED6 = (i+5) % 2;
LED7 = (i+6) % 2;
LED8 = (i+7) % 2;
delay_ms(500);
i++;
if(i >= 2) i = 0; // i 取值为 0 或 1,用于交替闪烁
}
}
}
}
注意:为了避免按键在按下的瞬间反复触发,需要对按键进行消抖处理。上面的代码中通过延时的方式进行
哈哈😃我不会的楼上gpt会,51没弄过,不熟不会。