/*******************************************************************
實驗名稱:音樂合成演示實驗
器件:ATmega16L
晶振:內(nèi)部 8M
接線方法:
1.PC0和PD7分別接揚(yáng)聲器的兩個輸入端子。
2.PA0-PA3分別接LED。
使用說明:
1.音樂數(shù)據(jù)存放于芯片的flash存貯器中。
2.PA口顯示不同的音階變化。
3.音樂數(shù)據(jù)的低四位選擇音調(diào)頻率,
高四位*5后,為音調(diào)持續(xù)時間。
********************************************************************/
#include
#include
#define DO63622//523
#define RE63831//587
#defineMI64017//659
#define FA64102//698
#defineSO64259//784
#define LA64398//880
#defineTI64522//988
#define DO_H64578//1046
#define RE_H64683//1174
#define MI_H64776//1318
#define FA_H64819//1397
#define SO_H64897//1568
#define LA_H64966//1760
#define TI_H65028//1976
#define DO_HH65057//2093
void delay_us(int time)
{//微秒級延時程序
do
{
time--;
}
while (time>1);
}
void delay_ms(unsigned int time)
{//毫秒級延時程序
while (time!=0)
{
delay_us(1000);
time--;
}
}
const unsigned char sound[]={ //樂曲數(shù)據(jù)表
/*************梁祝****************/
0x40,0x4c,0x4a,0x49,0xc8,0x49,0x47,0x46,
0xe5,0x4e,0x4d,0x4e,0x6c,0x2d,0x4e,
0x4d,0x2c,0x2d,0x2e,0x2d,0x6f,0x2d,
0x2c,0x2d,0x2c,0x29,0x2a,0x2b,
0x2a,0x29,0xc8,0x4c,0x4e,0x49,0x46,0x48,
0xc5,0x26,0x28,0xe5,0x83,0x65,0x26,
0x68,0x29,0x26,0x28,0x45,0x6c,0x2f,0x3d,
0x1c,0x2a,0x2c,0xe9,0x69,0x2a,0x47,0x46,
0x65,0x26,0x48,0x49,0x43,0x48,0x36,0x15,
0x26,0x18,0xe5,0x6a,0x2c,0x47,0x49,0x26,
0x28,0x85,0x33,0x15,0x43,0x25,0x26,0x27,
0x29,0xc6,0x25,0x26,0x68,0x29,0x4c,0x4a,
0x49,0x2a,0x29,0x49,0x26,0x25,0x83,0x88,
0x26,0x28,0x26,0x25,0x23,0x25,0x26,0x28,
0xc5,0x04,0x04,
/*************希望*****************/
0x49,0x4a,0x4a,0x6a,0x29,0x48,0x46,0x68,
0x29,0xc8,0x49,0x4a,0x4a,0x6a,0x2c,0x4a,
0x4a,0x49,0x4a,0xca,0x4c,0x4d,0x4d,0x6d,
0x2c,0x4a,0x4a,0x4c,0x4d,0xcc,0x49,0x4a,
0x4a,0x69,0x2a,0x4a,0x49,0x4a,0x46,
0x18,0xe6,0x29,0x29,0x29,0x28,0x46,
0x29,0x29,0x29,0x28,0x46,0x49,0x4a,
0x48,0x69,0x2a,0x4c,0x2d,0x2d,0x2d,
0x2c,0x4a,0x29,0x29,0x29,0x28,0x46,
0x46,0x45,0x46,0xe6,
0x00};
/* 音階表*/
const unsigned int TONETABLE[15]={DO,RE,MI,FA,SO,LA,TI,DO_H,
RE_H,MI_H,FA_H,SO_H,LA_H,TI_H,DO_HH};
/* 音階重裝常數(shù)*/
unsigned int toneconst;
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
TCNT1=toneconst;
PORTC^=1;
}
void music(void)
{
unsigned char temp=0,temp1=0;
unsigned int sound_add=0;
TCCR1A = 0x00;
TCCR1B =(1<
while(temp1!=0)
{
temp=temp1;
temp&=0x0f;
if(temp!=0)
{
TIMSK|=(1<
toneconst=TONETABLE[temp];
TCNT1=toneconst;
PORTA=toneconst;
}
temp=temp1;
temp>>=4;
temp&=0x0f;
delay_ms(temp*129);//根據(jù)SOUNDTABLE中數(shù)據(jù)的高四位*5后,為音調(diào)持續(xù)時間
TIMSK&=~(1<
temp1=sound[sound_add];
}
PORTC = 0xFF;
}
/* 主程序 */
void main(void)
{
DDRC=0x01;
DDRD=0x80;
DDRA=0x0f;
SREG=0x80;
while(1)
{
music();
}
}