普中开发板蜂鸣器《花海》演奏
有的板子的蜂鸣器IO口可能是P2^5
#include <REGX52.H>
void Timer0Init(void)
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器开始计时
ET0=1;
EA=1;
PT0=0;
}
void Delay(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
sbit Buzzer=P1^5;
#define SPEED 650
#define P 0
#define L1 1
#define L1_ 2
#define L2 3
#define L2_ 4
#define L3 5
#define L4 6
#define L4_ 7
#define L5 8
#define L5_ 9
#define L6 10
#define L6_ 11
#define L7 12
#define M1 13
#define M1_ 14
#define M2 15
#define M2_ 16
#define M3 17
#define M4 18
#define M4_ 19
#define M5 20
#define M5_ 21
#define M6 22
#define M6_ 23
#define M7 24
#define H1 25
#define H1_ 26
#define H2 27
#define H2_ 28
#define H3 29
#define H4 30
#define H4_ 31
#define H5 32
#define H5_ 33
#define H6 34
#define H6_ 35
#define H7 36
#define G1 37
#define G1_ 38
#define G2 39
#define G2_ 40
#define G3 41
#define G4 42
#define G4_ 43
#define G5 44
#define G5_ 45
#define G6 46
#define G6_ 47
#define G7 48
#define E1 49
#define E1_ 50
#define E2 51
#define E2_ 52
#define E3 53
#define E4 54
#define E4_ 55
#define E5 56
#define E5_ 57
#define E6 58
#define E6_ 59
#define E7 60
unsigned int FreqSelet,MusicSelet;
unsigned int code FreqTable[]=
{
0,61719,61939,62135,62331,62506,62679,62833,62985,63132,63263,63390,63512,
63628,63731,63835,63928,64021,64103,64185,64260,64331,64400,64463,64524,
64580,64633,64684,64732,64777,64820,64860,64898,64934,64968,65000,65030,
65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283,
65297,65310,65323,65335,65346,65357,65367,65377,65385,65394,65402,65409
};
unsigned int code Music[]=
{
P,4,P,4,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G1,13,G3,12,
0xFF
};
void main()
{
Timer0Init();
while(1)
{
if(Music[MusicSelet]!=0xFF)
{
FreqSelet=Music[MusicSelet];
MusicSelet++;
Delay(SPEED/4*Music[MusicSelet]);
MusicSelet++;
TR0=0;
Delay(5);
TR0=1;
}
else
{
TR0=0;
while(1);
}
}
}
void Timer0_Routine() interrupt 1
{
if(FreqTable[FreqSelet])
{
TL0 = FreqTable[FreqSelet]%256;
TH0 = FreqTable[FreqSelet]/256;
Buzzer=!Buzzer;
}
}