/*******************************************************************
实验名称:音乐合成演示实验
器件:ATmega16L
晶振:内部 8M
接线方法:
1.PC0和PD7分别接扬声器的两个输入端子。
2.PA0-PA3分别接LED。
使用说明:
1.音乐数据存放于芯片的flash存贮器中。
2.PA口显示不同的音阶变化。
3.音乐数据的低四位选择音调频率,
高四位*5后,为音调持续时间。
********************************************************************/
#include
#include
#define DO 63622 //523
#define RE 63831 //587
#define MI 64017 //659
#define FA 64102 //698
#define SO 64259 //784
#define LA 64398 //880
#define TI 64522 //988
#define DO_H 64578 //1046
#define RE_H 64683 //1174
#define MI_H 64776 //1318
#define FA_H 64819 //1397
#define SO_H 64897 //1568
#define LA_H 64966 //1760
#define TI_H 65028 //1976
#define DO_HH 65057 //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[]={ //乐曲数据表
/*************梁祝****************/
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};
/* 音阶重装常数 */
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);//根据SOUNDTABLE中数据的高四位*5后,为音调持续时间
TIMSK&=~(1<
temp1=sound[sound_add];
}
PORTC = 0xFF;
}
/* 主程序 */
void main(void)
{
DDRC=0x01;
DDRD=0x80;
DDRA=0x0f;
SREG=0x80;
while(1)
{
music();
}
}
上一篇:ATmega16L串口通信程序
下一篇:IAR for AVR delay函数
推荐阅读最新更新时间:2024-03-16 15:21