用51单片机蜂鸣器写一段小星星歌曲的代码
时间: 2023-09-21 20:02:51 浏览: 381
以下是用51单片机蜂鸣器写一段小星星歌曲的代码:
```
#include <reg52.h> // 引入单片机头文件
sbit beep = P1^5; // 定义蜂鸣器引脚
void delay(unsigned int i) // 延时函数
{
while(i--);
}
void main()
{
unsigned char k = 0; // 定义计数器
while(1)
{
switch(k)
{
case 0: beep = 0; delay(20000); break;
case 1: beep = 1; delay(10000); break;
case 2: beep = 0; delay(10000); break;
case 3: beep = 1; delay(10000); break;
case 4: beep = 0; delay(10000); break;
case 5: beep = 1; delay(10000); break;
case 6: beep = 0; delay(10000); break;
case 7: beep = 1; delay(20000); break;
case 8: beep = 0; delay(10000); break;
case 9: beep = 1; delay(10000); break;
case 10: beep = 0; delay(10000); break;
case 11: beep = 1; delay(10000); break;
case 12: beep = 0; delay(10000); break;
case 13: beep = 1; delay(10000); break;
case 14: beep = 0; delay(20000); break;
}
k++;
if(k == 15) // 播放完毕后重置计数器
{
k = 0;
}
}
}
```
希望对您有帮助!
阅读全文