将点亮流水灯的时间间隔0.2s调整为点亮流水灯时间间隔为1s,要如何修改程序?
时间: 2024-05-06 13:20:20 浏览: 7
假设原来的程序如下:
```c
#include <reg51.h>
void main() {
while(1) {
P1 = 0x01;
delay(200);
P1 = 0x02;
delay(200);
P1 = 0x04;
delay(200);
P1 = 0x08;
delay(200);
P1 = 0x10;
delay(200);
P1 = 0x20;
delay(200);
P1 = 0x40;
delay(200);
P1 = 0x80;
delay(200);
}
}
void delay(unsigned int time) {
unsigned int i, j;
for(i = time; i > 0; i--)
for(j = 110; j > 0; j--);
}
```
可以将delay函数中的for循环中的j的次数调整为1秒钟内计数器的次数,即将j的值调整为1100。修改后的程序如下:
```c
#include <reg51.h>
void main() {
while(1) {
P1 = 0x01;
delay(1000);
P1 = 0x02;
delay(1000);
P1 = 0x04;
delay(1000);
P1 = 0x08;
delay(1000);
P1 = 0x10;
delay(1000);
P1 = 0x20;
delay(1000);
P1 = 0x40;
delay(1000);
P1 = 0x80;
delay(1000);
}
}
void delay(unsigned int time) {
unsigned int i, j;
for(i = time; i > 0; i--)
for(j = 1100; j > 0; j--);
}
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)