普中科技51单片机简易电子琴代码
时间: 2023-08-03 17:09:44 浏览: 219
以下是普中科技的51单片机电子琴代码:
```
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit SPK=P1^7; //定义P1.7口为发声口
uchar code table[]={0xe7,0xe5,0xe3,0xe2,0xdf,0xdc,0xda,0xd8,0xd7,0xd5,0xd4,0xd2,0xd1,0xcf,0xcd,0xcc,0xca,0xc9,0xc7,0xc6,0xc5,0xc4,0xc3,0xc2,0xc1,0xc0,0xbf,0xbe,0xbe,0xbd,0xbc,0xbc,0xbb,0xba,0xba,0xb9,0xb8,0xb8,0xb7,0xb6,0xb6,0xb5,0xb4,0xb4,0xb3,0xb2,0xb2,0xb1,0xb0,0xb0,0xaf,0xae,0xae,0xad,0xac,0xac,0xab,0xaa,0xaa,0xa9,0xa8,0xa8,0xa7,0xa6,0xa6,0xa5,0xa4,0xa4,0xa3,0xa2,0xa2,0xa1,0xa0,0xa0,0x9f,0x9e,0x9e,0x9d,0x9c,0x9c,0x9b,0x9a,0x9a,0x99,0x98,0x98,0x97,0x96,0x96,0x95,0x94,0x94,0x93,0x92,0x92,0x91,0x90,0x90,0x8f,0x8e,0x8e,0x8d,0x8c,0x8c,0x8b,0x8a,0x8a,0x89,0x88,0x88,0x87,0x86,0x86,0x85,0x84,0x84,0x83,0x82,0x82,0x81,0x80}; //定义音符频率表
void delay(uint t) //延时函数
{
uint i,j;
for(i=0;i<t;i++)
for(j=0;j<125;j++);
}
void main()
{
uchar i;
while(1)
{
for(i=0;i<100;i++) //循环演奏100个音符,可根据需要修改
{
SPK=1; //发声
P0=table[i]; //输出音符频率
delay(2); //持续时间,可根据需要修改
SPK=0; //停止发声
delay(2); //发声间隔,可根据需要修改
}
}
}
```
这个代码与前面的代码几乎相同,唯一的区别是发声口的定义不同,使用了`P1.7`口作为发声口。音符频率表也是一样的,可以根据具体需要修改循环演奏的次数、持续时间和发声间隔。
阅读全文