STC15单片机播放音乐(凉凉)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include "intrins.h"
unsigned int frequency=39; //控制音调
unsigned char duty=20;//占空比*100 -控制声音响度
void Delay190ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 8;
j = 252;
k = 248;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay()
{
Delay190ms();
}
void Delay4()
{
Delay();
Delay();
Delay();
Delay();
}
void Delay8()
{
Delay();
Delay();
}
void Delay16()
{
Delay();
}
unsigned int code yinfu[]={262,294,330,349,392,440,494,//低音频率
523,587,659,698,784,880,988,//中音频率
1047,1175,1319,1397,1568,1760,1967//高音频率
};
//设高八位表示音符,低八位表示节拍。
//音符:高四位为低0中1高2;低四位为音符1234567
//节拍:4为1/4节拍,8为1/8节拍,16为1/16节拍
//<<凉凉>>
unsigned int code qupu[]={0x1604,0x1604,0x1608,0x1608,0x1508,0x1608,0x1304,0x1304,0x1304,0x1508,0x1208,0x1304,0x1304,0x1304,0x1304,0x1304,0x1304,0x1304,0x1304,0x1604,0x1604,0x1608,0x1608,0x1508,0x1608,0x1304,0x1304,0x1304,0x1508,0x1208,0x1304,0x1308,0x1210,0x1110,0x0604,0x0604,0x0604,0x0604,0x0604,0x0604,0x1704,0x1704,0x1504,0x1504,0x1204,0x1204,0x0704,0x0704,0x1304,0x1304,0x1304,0x1304,0x1304,0x1304,0x1204,0x1204,0x1704,0x1704,0x2104,0x2104,0x2204,0x2204,0x2204,0x1708,0x2110,0x1710,0x1604,0x1604,0x1604,0x1604,0x1604,0x1604,0x1604,0x1604,0x1108,0x1108,0x1108,0x0708,0x1104,0x0708,0x1108,0x1108,0x1108,0x1108,0x0708,0x1104,0x1208,0x0708,0x0708,0x0708,0x0708,0x0608,0x0704,0x0608,0x0708,0x0708,0x0708,0x0708,0x0608,0x0704,0x0608,0x0508,0x0604,0x0604,0x0604,0x0604,0x0104,0x0104,0x0608,0x0508,0x0308,0x0308,0x0510,0x0210,0x0308,0x0304,0x0304,0x0304,0x1108,0x1108,0x1108,0x0708,0x1104,0x0708,0x1108,0x1108,0x1108,0x1108,0x0708,0x1104,0x1208,0x0708,0x0708,0x0708,0x0708,0x0608,0x0704,0x0608,0x0708,0x0708,0x0708,0x0708,0x0608,0x0708,0x1108,0x0708,0x0508,0x0604,0x0604,0x0604,0x0604,0x0604,0x0604,0x0604,0x1308,0x1208,0x1208,0x1308,0x1304,0x1304,0x1304,0x0608,0x1308,0x1308,0x1208,0x1308,0x1508,0x1308,0x1208,0x1304,0x0604,0x0604,0x0604,0x0708,0x0708,0x0708,0x1108,0x0708,0x0304,0x0508,0x0608,0x0508,0x0504,0x0504,0x0504,0x0608,0x1308,0x1308,0x1208,0x1308,0x1208,0x1308,0x1508,0x1508,0x1304,0x1308,0x1208,0x1208,0x1308,0x1508,0x1508,0x1604,0x1608,0x1604,0x1608,0x1508,0x1508,0x1608,0x1604,0x1604,0x1604};
void main()
{
int i=0;
int length=0;
CMOD|=0x04;
CCAPM0=0x42;
CL=0;
CH=0;
CCAP0L= 256-256*duty/100;;//占空比*100=(255-x)/256;
CCAP0H= 256-256*duty/100;;//=>x=256-256*duty/100;
PCA_PWM0=0;
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式 16位自动重载
TL0 = (65535-11059200/frequency/256)%256; //设置定时初值
TH0 = (65535-11059200/frequency/256)/256; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
CR=1;//开启P11口输出PWM
P1M1=0;//P1
口强推挽输出
P1M0=1;//P1口强推挽输出
while(1)
{
length=sizeof(qupu)/2;
for(i=0;i
frequency=yinfu[(qupu[i]>>12)*7+(qupu[i]>>8)%16-1];
TL0 = (65535-11059200/frequency/256)%256; //设置定时初值
TH0 = (65535-11059200/frequency/256)/256; //设置定时初值
switch(qupu[i]%256)
{
case 4:Delay4();
break;
case 8:Delay8();
break;
case 16:Delay16();
default :break;
}
}
}
}