十年單片機開發方案公司深圳英銳恩分享音樂程序示例。
采用TMR1中斷作音符發聲,TMR0作音長節拍。
const byte music_15[]={0xfa,0x67,0xfb,0x03,0xfb,0x90,0xfc,0x0c,0xfc,0x44,
// 2 3 4 5 6
0xfc,0xac,0xfd,0x09,0xfd,0x34,0xfd,0x82,0xfd,0xc8,
// * * * * *
// 7 1 2 3 4 5
0xfe,0x06,0xfe,0x22,0xfe,0x56,0xfe,0x85,0xfe,0x98,0xfe,0xc1};
//梁祝
const byte M_MUSIC[79]=
{
0x23,0x42,0x50,0x72,0x80,0x50,0x70,0x41,0xb2,0xe0,0xc0,0xb0,0x90,0xb0,0x87,
0x81,0x80,0x90,0x61,0x51,0x42,0x50,0x71,0x81,0x21,0x71,0x50,0x40,0x50,0x70,
0x47,0x92,0xb0,0x61,0x81,0x50,0x70,0x41,0x43,0x20,0x40,0x21,0x40,0x50,0x60,
0x80,0x55,0x40,0x50,0x72,0x80,0xb1,0x91,0x81,0x90,0x80,0x71,0x50,0x40,0x23,
0x73,0x50,0x70,0x50,0x40,0x20,0x40,0x50,0x70,0x46,0x90,0xb0,0x80,0x90,0x80,
0x70,0x61,0x51,0x47
};
const byte M_MUSIC1[100]=
{
0xd0,0xc0,0xd0,0xf0,0xc0,0xd0,0xb0,0xa0,0x90,0x80,0x90,0xb0,0x82,0x70,
0x61,0x91,0x80,0x70,0x60,0x50,0x47,
0x91,0x90,0xb0,0x60,0x80,0x50,0x60,0x80,0x60,0x81,0x43,0xb1,0x91,0xd0,
0xf0,0xc0,0xd0,0xd1,0xb5,0xb2,0xc0,0xd1,0xf0,0xd0,0xc1,0xa1,0x91,0x81,
0x91,0x90,0xb0,0x60,0x80,0x50,0x60,0x80,0x60,0x85,0xb1,0xb0,0x90,0x80,
0x90,0xb0,0xa0,0x91,0x93,0x80,0x90,0xb1,0xb0,0x90,0x80,0x90,0xb0,0xa0,
0x91,0x93,0x80,0x90,0x91,0x90,0xb0,0x80,0x90,0xb0,0xa0,0x92,0xb0,0x81,
0x61,0x81,0x60,0x80,0x50,0x60,0x80,0x70,0x67
};
//繡金匾_序曲
const byte M_MUSIC2[51]=
{
0xd0,0xc0,0xb0,0x90,0xc1,0xb0,0x90,0xb0,0xc0,0xd1,0xd0,0xc1,0xb0,0x93,0x63,
0x61,0xc0,0xb0,0xc1,0xd1,0xb0,0xb0,0x90,0xb0,0xc3,0xd1,0xd0,0xc0,0xb1,0xc1,
0x91,0xd0,0xc0,0xc0,0xb0,0x91,0x61,0xc0,0xb0,0xc1,0xd1,0x91,0x81,0x63,0xc2,
0xd0,0xb0,0x90,0x80,0xb0,0x97
};
//********************** 中斷服務程序 ********************************************
//聲音產生.
//*******************************************************************************
void interrupt time_mu(void)
{
//TMR1做聲音
TMR1L=m_music_L; //重置計時器初值,以產生一定頻率
TMR1H=m_music_H;
TMR1IF=0;
if(!stop_f)
SOUND=!SOUND;
}
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
// 上電初始化
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
void init(void)
{
TRISA=0xff;
TRISC=0; //
PORTC=0;
T1CON=1; // 1/1 timer1 on
TMR1IF=0;
TMR1IE=1;
PEIE=1;
OPTION=0x88;
CMCON=0xff;
T0IF=0; //清除TMR0的中斷標志
INTF=0; //清RB0中斷
T0IE=0; //TMR0中斷允許
GIE=1; //總中斷允許
RFstate=0;
}
fSYS()
{
if(testbit(TMR0,7))
{
clrbit(TMR0,7);
recver();
ms_64++;
if(ms_64==0)
{
fre_time++;
if(fre_time==m_music_long_temp)
{
ONE_S_F=0;
fre_time=0;
stop_f=0;
}
}
}
}
//************************ 主程序 *************************//
void main()
{
init();
ONE_S_F=0;
while(1)
{
stop_f=0;
for(n=0;n<79;)
{
if(!ONE_S_F)
{
s_reg=M_MUSIC[n]&0x0f;
m_music_long=music_long[s_reg];
m_music_long_temp=(m_music_long<<1);
s_reg=(((M_MUSIC[n]&0xf0)>>4)<<1);
m_music_H=music_15[s_reg];
TMR1H=m_music_H;
s_reg++;
m_music_L=music_15[s_reg];
TMR1L=m_music_L;
ONE_S_F=1;
n++;
}
fSYS();
}
for(n=0;n<100;)
{
if(!ONE_S_F)
{
s_reg=M_MUSIC1[n]&0x0f;
m_music_long=music_long[s_reg];
m_music_long_temp=(m_music_long<<1);
s_reg=(((M_MUSIC1[n]&0xf0)>>4)<<1);
m_music_H=music_15[s_reg];
TMR1H=m_music_H;
s_reg++;
m_music_L=music_15[s_reg];
TMR1L=m_music_L;
ONE_S_F=1;
n++;
}
fSYS();
}
for(n=0;n<51;)
{
if(!ONE_S_F)
{
s_reg=M_MUSIC2[n]&0x0f;
m_music_long=music_long[s_reg];
m_music_long_temp=(m_music_long<<1);
s_reg=(((M_MUSIC2[n]&0xf0)>>4)<<1);
m_music_H=music_15[s_reg];
TMR1H=m_music_H;
s_reg++;
m_music_L=music_15[s_reg];
TMR1L=m_music_L;
ONE_S_F=1;
n++;
}
fSYS();
(文源網絡,侵刪)