#include "reg52.h"typedef unsigned char u8;typedef unsigned int u16;sbit feng = P2^5;int delay(int a) { while (a--);}int chr[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};void main() { u16 i = 0; while (1) { P0 = chr[i]; feng = 1; delay(50000); feng = 0; delay(500); i++; if (i >= 10) { i = 0; } }}让那个蜂鸣器一次响100毫秒间隔五百毫秒
时间: 2024-01-11 10:03:31 浏览: 12
好的,你可以修改 `delay` 函数,让它可以控制蜂鸣器响的时间。以下是修改后的代码:
```
#include "reg52.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit feng = P2^5;
void delay(int a) {
while (a--);
}
int chr[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
void display(u8 num) {
P0 = chr[num];
feng = 1;
delay(100);
feng = 0;
delay(500);
}
void main() {
u8 i = 0;
while (1) {
display(i);
i++;
if (i >= 10) {
i = 0;
}
}
}
```
在这个代码中,我们将 `delay` 函数的返回值类型改为 `void`,因为在这里我们不需要返回任何值。我们将蜂鸣器响的时间从原来的 50 毫秒改为 100 毫秒,间隔时间仍为 500 毫秒。这样就可以让蜂鸣器响一次 100 毫秒了。
相关问题
#include"reg52.h" typedef unsigned char u8; typedef unsigned int u16; sbit feng=P2^5; int delay(int a) { while(a--); } int chr[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f} void main() { while(1) { u16 i=0; p0=chr[i] if(i>9) i=0 feng=0; delay(100); feng=1; delay(500); { } } 蜂鸣器 feng = 1; delay(50000); feng = 0; 可以让静态数码管显示0-9同时蜂鸣器响起吗
可以,你可以将静态数码管的显示部分和蜂鸣器控制部分结合起来。以下是修改后的代码:
```
#include "reg52.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit feng = P2^5;
int delay(int a) {
while (a--);
}
int chr[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
void main() {
u16 i = 0;
while (1) {
P0 = chr[i];
feng = 1;
delay(50000);
feng = 0;
delay(500);
i++;
if (i >= 10) {
i = 0;
}
}
}
```
在这个代码中,我们在每次显示一个数字时,都让蜂鸣器响起一次。同时,我们使用了一个计数器 `i` 来循环显示 0-9 这些数字。当 `i` 大于等于 10 时,将其重置为 0,从头开始循环。
#include "reg52.h"typedef unsigned char u8;typedef unsigned int u16;sbit feng = P2^5;int delay(int a) { while (a--);}int chr[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};void main() { u16 i = 0; while (1) { P0 = chr[i]; feng = 1; delay(50000); feng = 0; delay(500); i++; if (i >= 10) { i = 0; } }}让静态数码管显示函数在主函数里
好的,你可以将静态数码管的显示部分封装成一个函数,然后在主函数里调用。以下是修改后的代码:
```
#include "reg52.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit feng = P2^5;
int delay(int a) {
while (a--);
}
int chr[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
void display(u8 num) {
P0 = chr[num];
feng = 1;
delay(50000);
feng = 0;
delay(500);
}
void main() {
u8 i = 0;
while (1) {
display(i);
i++;
if (i >= 10) {
i = 0;
}
}
}
```
在这个代码中,我们将静态数码管的显示部分封装成了一个名为 `display` 的函数。这个函数接受一个参数 `num`,用来指定要显示的数字。在主函数里,我们通过调用 `display` 函数来循环显示 0-9 这些数字。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)