好,十楼的程序已经给大家详细讲了一下了。当然,这程序我已经测试过了的,肯定不会出什么问题的。各位现在已经可以用蜂鸣器播放极简单的音乐了,拿出去给别人炫耀一下应该不成大问题了
不过如果就只能到此为止的话那这东西还是废柴一个,所以接着开发吧!这一回编出的程序可以依靠T0+T2真正地播放你想要的音乐了,激动吗?
#include "STC15.h"
#include "typedefine.h"
uint16 volatile index = 0;
uint8 code MusicNote[5] = {37,38,0,39,40}; //记载具体要播放的音,你可以自由更改
//同样下一个数组也可以更改
int16 code MusicBeat[5] = {2000,2000,2000,2000,2000}; //这五个音每个都播放2000毫秒即2秒,不难理解吧
volatile BeatTemp = 0;
uint16 code NoteCycle[89] = //钢琴88个键每个音的重载值都被我算好放在这里了
{ //注:这里的重载值跟之前程序的稍有不同:上次的可以直接放到TH0和TL0里面用,
//而这里的是记录每两次电平翻转之间的间隔纳秒数,最终载入TH0和TL0的值需要计算一下,这一块我过一会再讲
1 ,18182,17161,16198,15289,
14431,13621,12856,12135,11454,10811,10204, 9631, 9091, 8581, 8099, 7645,
7215, 6810, 6428, 6067, 5727, 5405, 5102, 4816, 4545, 4290, 4050, 3822,
3608, 3405, 3214, 3034, 2863, 2703, 2551, 2408, 2273, 2145, 2025, 1911,
1804, 1703, 1607, 1517, 1432, 1351, 1276, 1204, 1136, 1073, 1012, 956,
902, 851, 804, 758, 716, 676, 638, 602, 568, 536, 506, 478,
451, 426, 402, 379, 358, 338, 319, 301, 284, 268, 253, 239,
225, 213, 201, 190, 179, 169, 159, 150, 142, 134, 127, 119
};
#define ChangeNote() TR0 = 0;\
TL0 = (uint16)0xFFFF-(uint16)NoteCycle[(MusicNote[index])];\
TH0 = ((uint16)0xFFFF-(uint16)NoteCycle[(MusicNote[index])])>>8;\
BeatTemp = MusicBeat[index];\
TR0 = 1;
void main(void)
{
//P3M0 = 0xFF;
//P3M1 = 0x00;
ChangeNote();
AUXR &= 0x7F;
TMOD &= 0xF0;
AUXR |= 0x04;
T2L = 0x40;
T2H = 0xA2;
IE2 |= 0x04;
EA = 1;
AUXR |= 0x10;
TR0 = 1;
INT_CLKO = 0x01;
while (1);
}
void BeatInterrupt(void) interrupt 12
{
BeatTemp --;
if(BeatTemp == 0)
{
TR0 = 0;
index++;
TL0 = (uint16)0xFFFF-(uint16)NoteCycle[(MusicNote[index])];
TH0 = ((uint16)0xFFFF-(uint16)NoteCycle[(MusicNote[index])])>>8;
TR0 = 1;
BeatTemp = MusicBeat[index];
}
return;
}