使用keil共15个LDED灯,若手电筒的亮度需要改为全亮、66%亮、33%亮、全灭,需要如何编程???
时间: 2023-08-24 18:07:23 浏览: 182
对于使用Keil开发环境的15个LDED灯,可以按照以下步骤进行编程,实现手电筒的亮度控制:
1. 首先,需要定义15个LED的端口,可以使用宏定义或者直接定义GPIO端口。
2. 在主函数中,使用无限循环语句,不断接收用户输入的亮度控制指令。
3. 根据用户输入的指令,使用if语句或者switch语句来控制LED灯的亮度。
4. 如果用户输入全亮指令,则将15个LED灯全部点亮;如果用户输入66%亮指令,则将15个LED灯的亮度调整为66%;如果用户输入33%亮指令,则将15个LED灯的亮度调整为33%;如果用户输入全灭指令,则将15个LED灯全部熄灭。
5. 在每次改变LED灯亮度之后,需要加入适当的延时,以便用户可以观察到亮度变化。
下面是一个简单的代码示例,可以根据需要进行修改和优化:
```c
#include <reg52.h> //包含STC15系列单片机的头文件
#define LED1 P2_0 //定义15个LED灯的端口
#define LED2 P2_1
#define LED3 P2_2
#define LED4 P2_3
#define LED5 P2_4
#define LED6 P2_5
#define LED7 P2_6
#define LED8 P2_7
#define LED9 P3_0
#define LED10 P3_1
#define LED11 P3_2
#define LED12 P3_3
#define LED13 P3_4
#define LED14 P3_5
#define LED15 P3_6
void delay(int time) //延时函数
{
int i, j;
for (i = time; i > 0; i--)
for (j = 110; j > 0; j--);
}
void main()
{
while (1) //无限循环
{
int brightness; //定义亮度变量
scanf("%d", &brightness); //从键盘读入亮度指令
switch (brightness) //根据亮度指令控制LED灯的亮度
{
case 1: //全亮
LED1 = LED2 = LED3 = LED4 = LED5 = LED6 = LED7 = LED8 = LED9 = LED10 = LED11 = LED12 = LED13 = LED14 = LED15 = 0;
break;
case 2: //66%亮
LED1 = LED2 = LED3 = LED4 = LED5 = LED6 = LED7 = LED8 = LED9 = LED10 = LED11 = LED12 = LED13 = LED14 = LED15 = 0;
delay(500);
LED1 = LED2 = LED3 = LED4 = LED5 = LED6 = LED7 = LED8 = LED9 = LED10 = LED11 = LED12 = LED13 = LED14 = 1;
delay(500);
break;
case 3: //33%亮
LED1 = LED2 = LED3 = LED4 = LED5 = LED6 = LED7 = LED8 = LED9 = LED10 = LED11 = LED12 = LED13 = LED14 = LED15 = 0;
delay(500);
LED1 = LED2 = LED3 = LED4 = LED5 = LED6 = LED7 = LED8 = LED9 = LED10 = LED11 = LED12 = LED13 = LED14 = 1;
delay(500);
LED1 = LED2 = LED3 = LED4 = LED5 = LED6 = LED7 = LED8 = LED9 = LED10 = LED11 = LED12 = LED13 = LED14 = 0;
delay(500);
break;
case 4: //全灭
LED1 = LED2 = LED3 = LED4 = LED5 = LED6 = LED7 = LED8 = LED9 = LED10 = LED11 = LED12 = LED13 = LED14 = LED15 = 1;
break;
default: //输入错误指令
printf("Invalid input!");
break;
}
}
}
```
阅读全文
相关推荐













