AVR单片机模拟电子琴.docx
- 文档编号:8995925
- 上传时间:2023-02-02
- 格式:DOCX
- 页数:22
- 大小:18.70KB
AVR单片机模拟电子琴.docx
《AVR单片机模拟电子琴.docx》由会员分享,可在线阅读,更多相关《AVR单片机模拟电子琴.docx(22页珍藏版)》请在冰豆网上搜索。
AVR单片机模拟电子琴
附一:
#include"config.h"
//决定是否播放,全局变量
/////////uint16*p;//歌曲选择指针
uint8list=0;//歌曲选择变量
uint8input=1;
uint8pause=1;
uint8state=0,stop=1,play=1,key;
uint8MUSIC_DATA[50][2];
uint8i=0,length=50,key_buffer;//i歌曲播放指针
//671235671
flashuint16Mode_Data[16]={440,494,523,587,659,698,784,
880,998,1046,1156,1318,1396,1568,0};//存放声音的频率
/***********************************
*音乐产生函数*
*功能:
输出频率为x的方波,延时yMS*
*范围:
x:
100-20000Hz,0:
不发声*
*y:
0-65536MS*
***********************************/
externvoidsound(uint16i)
{
//uint16x;
//x=music_data[i][0];//取音乐的频率数据
if(i!
=0)
{
//x=;//1M时钟数据计算
OCR1A=500000/i;//比较值赋值
TCCR1A=0x40;//允许比较反转输出
}
else
{
TCCR1A=0x00;//频率为0,休止符,不发声
}
}
/************************************
*延时函数:
1M时钟时为MS*
************************************/
externvoiddelay_ms(uint16i)
{
uint8a;
for(;i;i--)
{
for(a=220;a;a--)//倍率原来220
{;}}
}
flashuint16kaqiusha[][2]=
{
{440,600},{494,200},{523,600},{443,200},{523,400},{494,200},
{440,200},{494,400},{330,400},{494,600},{523,200},{578,600},
{494,200},{578,400},{523,200},{494,200},{440,800},{659,400},
{880,400},{784,400},{880,200},{784,200},{698,400},{659,200},
{578,200},{659,400},{440,400},{0,200},{698,400},{578,200},
{659,600},{523,200},{494,200},{330,200},{523,200},{494,200},
{440,800},{659,400},{880,400},{784,400},{880,200},{784,200},
{698,400},{659,200},{578,200},{659,400},{440,400},{0,200},
{698,400},{578,200},{659,600},{523,200},{494,200},{330,200},
{523,200},{494,200},{440,800},{0,0}
};
voidkaqiusha_sing(void)
{
sound(kaqiusha[i][0]);//输出音符
//PORTA=seg7_data(i%16);
delay_ms(kaqiusha[i][1]);//节拍延时
TCCR1A=0x00;//奏完一个音符停止
PORTD|=1<<5;
if(!
kaqiusha[++i][1])//准备一下个音符
{
i=0;
play=0;
pause=1;
input=0;
if((++list)>5)
{list=0;};
}
}
flashuint16moscow_night[][2]=
{
{440,400},{523,400},{659,400},{523,400},
{587,800},{523,400},{494,400},{659,800},
{587,800},{440,1600},{523,400},{659,400},
{784,400},{784,400},{880,800},{784,400},
{698,400},{659,1600},{740,800},{831,800},
{988,400},{880,400},{659,1200},{494,800},{440,400},
{659,400},{587,400},{698,1600},{784,400},
{698,400},{659,800},{587,400},{523,400},{659,800},
{587,800},{440,1600},{740,800},{831,800},
{988,400},{880,400},{659,1200},{494,800},{440,400},
{659,400},{587,400},{698,1600},{784,400},
{698,400},{659,800},{587,400},{523,400},{659,800},
{587,800},{440,3200},{0,0}
};
voidmoscow_night_sing(void)
{
sound(moscow_night[i][0]);//输出音符
//PORTA=seg7_data(i%16);
delay_ms(moscow_night[i][1]);//节拍延时
TCCR1A=0x00;//奏完一个音符停止
PORTD|=1<<5;
if(!
moscow_night[++i][1])//准备一下个音符
{
i=0;
play=0;
pause=1;
input=0;
if((++list)>5)
{list=0;};
}
}
flashuint16super_mario[][2]=
{
{659,200},{659,200},{0,200},{659,200},
{0,200},{523,200},{659,200},{0,200},
{784,400},{0,400},{392,400},{0,400},
{523,200},{0,400},{392,400},{0,400},
{330,200},{0,400},{440,200},{0,200},
{494,200},{0,200},{466,200},{440,200},{0,200},
{392,266},{659,266},{784,266},{880,200},
{0,200},{698,200},{784,200},{0,200},
{659,200},{0,200},{523,200},{587,200},
{494,200},{0,400},{523,200},{0,400},{392,400},{0,400},
{330,200},{0,400},{440,200},{0,200},
{494,200},{0,200},{466,200},{440,200},{0,200},
{392,266},{659,266},{784,266},{880,200},
{0,200},{698,200},{784,200},{0,200},
{659,200},{0,200},{523,200},{587,200},
{494,200},{0,800},{784,200},{740,200},
{698,200},{622,200},{0,200},{659,200},{0,200},
{415,200},{440,200},{523,200},{0,200},
{440,200},{523,200},{587,200},{0,400},
{784,200},{740,200},{698,200},{622,200},{0,200},
{659,200},{0,200},{1047,200},{0,200},{1047,200},{1047,200},
{0,800},{784,200},{740,200},
{698,200},{622,200},{0,200},{659,200},{0,200},
{415,200},{440,200},{523,200},{0,200},
{440,200},{523,200},{587,200},{0,200},{622,200},{0,400},{587,200},
{0,400},{523,200},{0,600},{392,200},{392,200},{0,200},{262,200},{0,400},
{784,200},{740,200},
{698,200},{622,200},{0,200},{659,200},{0,200},
{415,200},{440,200},{523,200},{0,200},
{440,200},{523,200},{587,200},{0,400},
{784,200},{740,200},{698,200},{622,200},{0,200},
{659,200},{0,200},{1047,200},{0,200},{1047,200},{1047,200},
{0,800},{784,200},{740,200},
{698,200},{622,200},{0,200},{659,200},{0,200},
{415,200},{440,200},{523,200},{0,200},
{440,200},{523,200},{587,200},{0,200},{622,200},{0,400},{587,200},
{0,400},{523,200},{0,600},{392,200},{392,200},{0,200},{262,200},{0,400},{0,0}
};//supermario
voidsuper_mario_sing(void)
{
sound(super_mario[i][0]);//输出音符
//PORTA=seg7_data(i%16);
delay_ms(super_mario[i][1]);//节拍延时
TCCR1A=0x00;//奏完一个音符停止
PORTD|=1<<5;
if(!
super_mario[++i][1])//准备一下个音符
{
i=0;
play=0;
pause=1;
input=0;
if((++list)>5)
{list=0;};
}
}
flashuint16butterfly_lovers[][2]=
{
{247,800},{294,600},{330,200},{392,600},
{440,200},{330,200},{392,200},{294,200},
{0,200},{587,600},{784,200},{659,200},
{587,200},{494,200},{587,200},{440,1400},
{0,200},{440,400},{440,200},{494,200},
{370,400},{330,400},{294,600},{330,200},{392,400},
{440,400},{247,400},{392,400},{330,200},
{294,200},{330,200},{392,200},{294,1400},{0,200},
{494,600},{587,200},{370,400},{440,400},
{330,200},{392,200},{294,1200},{247,300},{294,100},
{247,400},{294,300},{330,100},{370,200},
{440,200},{330,1200},{294,200},{330,200},{392,600},
{440,200},{587,400},{494,400},{440,400},{494,200},
{440,200},{392,400},{330,200},{294,200},
{247,400},{0,400},{392,400},{0,400},{330,300},
{392,100},{330,200},{294,200},{247,200},{294,200},
{330,200},{392,200},{294,1400},
{0,0}
};///梁祝歌曲
voidbutterfly_lovers_sing(void)
{
sound(butterfly_lovers[i][0]);//输出音符
//PORTA=seg7_data(i%16);
delay_ms(butterfly_lovers[i][1]);//节拍延时
TCCR1A=0x00;//奏完一个音符停止
PORTD|=1<<5;
if(!
butterfly_lovers[++i][1])//准备一下个音符
{
i=0;
play=0;
pause=1;
input=0;
if((++list)>5)
{list=0;};
}
}
flashuint16Sdu_Song[][2]=
{
{698,300},{698,100},{698,200},{698,200},
{698,200},{698,200},{659,200},{587,200},
{587,200},{523,1000},{349,400},{440,400},
{523,400},{523,200},{466,400},{440,200},
{466,400},{440,400},{392,800},{392,300},
{392,100},{392,200},{392,200},{392,200},{370,200},
{392,200},{440,200},{466,200},{523,200},
{587,200},{659,200},{698,200},{784,600},{880,600},
{784,200},{784,200},{523,600},{698,1600},{0,0}
};//songofSDU
voidSdu_Song_sing(void)
{
sound(butterfly_lovers[i][0]);//输出音符
//PORTA=seg7_data(i%16);
delay_ms(Sdu_Song[i][1]);//节拍延时
TCCR1A=0x00;//奏完一个音符停止
PORTD|=1<<5;
if(!
Sdu_Song[++i][1])//准备一下个音符
{
i=0;
play=0;
pause=1;
input=0;
if((++list)>5)
{list=0;};
}
}
flashuint16music_data[][2]=
{
{440,400},{440,400},{659,400},{659,400},{740,400},{740,400},{659,800},
{587,400},{587,400},{554,400},{554,400},{494,400},{494,400},{440,800},
{659,400},{659,400},{587,400},{587,400},{554,400},{554,400},{497,800},
{659,400},{659,400},{587,400},{587,400},{554,400},{554,400},{497,800},
{440,400},{440,400},{659,400},{659,400},{740,400},{740,400},{659,800},
{587,400},{587,400},{554,400},{554,400},{494,400},{494,400},{440,800},
{0,0}
};//////abcdefg
voidmusic_data_sing(void)
{
sound(music_data[i][0]);//输出音符
//PORTA=seg7_data(i%16);
delay_ms(music_data[i][1]);//节拍延时
TCCR1A=0x00;//奏完一个音符停止
PORTD|=1<<5;
if(!
music_data[++i][1])//准备一下个音符
{
i=0;
play=0;
pause=1;
input=0;
if((++list)>5)
{list=0;};
}
}
/************************************
*端口初始化函数*
************************************/
voidport_init(void)
{
DDRD|=0xa0;//D5口配置为输出,驱动蜂鸣器
PORTD|=0x0f;
DDRB=0xf0;
PORTB=0X0F;
DDRA=0xff;
Seg7_Led_Init();
MCUCR=0x00;
GICR=0x40;
SEI();
}
/*********************************
name:
list_deal();
function:
dealwithmanysongsofthemachine
**********************************/
externvoidlist_deal(void)
{
switch(list)
{
case0:
butterfly_lovers_sing();break;
case1:
moscow_night_sing();break;
case2:
Sdu_Song_sing();break;
case3:
super_mario_sing();break;
case4:
kaqiusha_sing();break;
case5:
music_data_sing();break;
}
}
/*********************************
name:
key_deal();
function:
dealwithstatesofthemachine
**********************************/
externvoidkey_deal(void)
{
if(state==0)//播放状态
{
while(play&&pause)
{
list_deal();
/*sound(music_data[i][0]);//输出音符
delay_ms(music_data[i][1]);//节拍延时
TCCR1A=0x00;//奏完一个音符停止
PORTD|=1<<5;
if(!
music_data[i++][1])
{
i=0;
}
*///调试用
}//播放选择的音乐
}//播放mode
else
{
while(play&&pause&&(~input))
{
sound(Mode_Data[MUSIC_DATA[i][0]]);//输出音符
PORTA=seg7_data(i%16);
delay_ms(400);//节拍延时
TCCR1A=0x00;//奏完一个音符停止
PORTD|=1<<5;
if(!
MUSIC_DATA[++i][1])//准备一下个音符
{
i=0;
play=0;
pause=1;
input=0;
}
}//播放选择的音乐
}//输入mode
}
/**************************************
handlekeyin;
PROBLEMS:
**************************************/
#pragmainterrupt_handlerint0_isr:
2
voidint0_isr(void)
{
TCCR1A=0x00;//关计时器减少干扰,只有再打开才会响
GICR&=~0x40;//关中断
key=key_scan();//externalinteruptonINT0
if(key)
{
if(!
state)//播放状态
{
Seg7_Bit0_En();
switch(key)
{
casePAUSE:
{pause=0;break;};
casePLAY:
{play=1;pause=1;break;};
caseSTOP:
{play=0;i=0;break;};
caseRST:
{if(~play){state=1;input=1;break;};i=0,play=0,pause=1;break;};
caseADVANCE:
if(play==1)
{if((i+2)>length)break;elsei+=2;break;};
if(!
play)
{if(++list>5)list=0;break;};
caseBACK:
if(play==1)
{if(i<2){i=0;break;}elsei-=2;break;};
if(!
play)
{if(--list<0)list=5;break;};
default:
break;
};
Seg7_Data_Port=seg7_data(i);
}
elseif(state)
{
if((key<=8)&&input)
{
add(key);
MUSIC_DATA[i++][0]=key;
MUSIC_DATA[i-1][1]=400;
sound(Mode_Data[key]);
delay_ms(200);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- AVR 单片机 模拟 电子琴