《友谊地久天长》乐曲演奏电路设计.docx
友情地久天长乐曲演奏电路设计1 .试验任务音高与频率的对应关系见表1-1。表17音高频率比照表1234567低音262Hz294Hz330Hz349Hz392Hz440Hz494Hz中音532Hz578Hz659Hz698Hz784Hz880Hz988Hz高音1046Hz1175Hz1318Hz1397Hz1568Hz1760Hz1976Hz友情地久天长乐谱见图l-lo友情地久天长0 5 |1 1 3 | 2.1 2 3 |1 3 5 | 6. 6 | 53 3 1123 11._gs|1 6 iCZhH laZHle6. i | 53 1 | 212 3 | 1.16 5 | 1- 0 |图17友情地久天长乐谱注:N 一拍;N两拍;Nj_四拍;L八拍;N为音谱。此程序使用4MHz时钟,时钟不同则需要修改常数。总体框图如图2所示。clockinst4clock q13.0Jsppakclock qa13.Ospeakinst图1-2总体框图2 .模块及模块功能模块TONE见图l-2o该模块把握每一个音的长短,送初值给SPEAK模块,从而把握音图。/ / / / / xT* *T*T* *7*7*4、*7* xj、*7* *7* 4、*T*,卜 «、*7 *Tx *T* "、«、*J、*7*/、4、4、4、,工、,卜,卜,卜 、xT% xrx 4、4、*T*,、,卜,卜,卜卜 *7* xj、*7、/ /友情地久天长乐曲时钟:4MHz/ / / vl* slz six *1* *1 >1* *X* *1 / / I (*y* *|n *jx zjs zj* *v* *1* *r* *1 *f* *7* *7* *7* *7* *v* *j* *7* *(* *7* *7* *T* *1* *7* *7* *7* *7* *7* *7* *7* *7* *(* *7* *7* *1* *7* *7* *7* *1* *j* / / 音高与频率的对应关系/I|1|2|3|4|5|6|7/ 低音| 262Hz| 294Hz330Hz|349Hz| 392Hz| 440Hz494Hz|中音523Hz| 578Hz659Hz|698Hz| 784Hz| 880Hz988Hz|l 高音|1046Hz|1175Hz11318Hz1397Hz1568Hz11760Hz11976Hz|/假设时钟不同则需改参数module tone(clock,q);/I/O 口声明input clock;output13:0q;/I/O 存放器reg13:O q_r;内部存放器reg20:0 cntl;/375ms 计数器(一个节拍)endend6"d42:beginq_r <= M_1;if(cnt2 < 4"d3)cnt2<=cnt2 + 4,dl;elsebegincnt2 <= 4"d0;n <= 6"d43;endend6"d43:beginq_r <= L_6;if(cnt2 < 4"d2)cnt2 <= cnt2 + 4"dl;elsebegincnt2 <= 4"d0;n <= 6"d44;endend6"d44:beginq_r <= M_5;if(cnt2 < 4"dl)cnt2<=cnt2 + 4,dl;elsebegincnt2 <= 4"d0;n <= 6"d45; endend6"d45:beginq_r <= M_l;if(cnt2 < 4"d7)cnt2 <= cnt2 + 4"dl;elsebegincnt2 <= 4"d0;n <= 6"d0;endenddefaults <= 6"d0;endcaseendendendmodule模块SPEAK见图l-2o该模块的功能是依据TONE送来的初值倒计时,每次计到0时就使信号Q反转,由于初值不同,从而产生不同的音高。/KlxKlXKlXXI>i / fl /音调产生模块时钟:4MHz/ /%A* kt*X* *A*xL*/ / if /依据A的值倒计时,使信号Q反转,产生不同的音高。 module speak(clock,a,q);/I/O 口声明inputclock;input13:0 a;outputq;/I/O存放器regq_r;内部存放器regfl 3:01count;倒计数器regb;标志位assign q = q_r;always (posedge clock)beginif(b =l,b0)begincount <= a;b<= l"bl;endelsebeginif(count =14"d0)beginb<= l"b0;q_r <= q_r;endelsebegincount <= count - 14"dl;endendendendmodulereg3:0 cnt2;节拍计数器reg5:0 n;乐谱状态机乐谱参数公言山上表音高与频率对应关系计算得出。公式为:D=F/K其中D为参数值,F为时钟频率,K为音高频率如仲音1) parameterM_1 = 4000000/523 = 7648L_5= 1410204, L_6 = 14P9090, M=14"d7648, M_2 = 14”d6920, M_3 = 14”d6069, M_5 = 14"d5102, M_6 = 14P4545, HJ = 14"d3824;1低音5低音6中音1中音2中音3中音5中音6高音parameter TIME = 1500000;把握每一个音(节拍)的长短(375ms)assign q = q_r;always (posedge clock)beginif(cntl < TIME)把握每一个音(节拍)的长短(375ms)cntl <= cntl + 21"dl;elsebegincntl v=21"d0;case(n)6"d0:beginq_r <=L_5;此值为低音5的计数初值,送给SPEAK模块if(cnt2 < 4"d 1)两个节拍cnt2<=cnt2 + 4,dl;else begin cnt2 <= 4"d0; n<=6,dl;endend6"dl:beginq_r <= M_l;if(cnt2 < 4"d6)cnt2 <= cnt2 + 4”dl; else begincnt2 <= 4"d0;n <= 6"d2;endend6"d2:beginq_r <= M_3;if(cnt2 V 4"dl)cnt2 <= cnt2 + 4”d 1;else begincnt2 <= 4"d0; n <= 6"d3;6"d3:beginq_r <= M_2;if(cnt2 < 4"d3)cnt2 v=cnt2 + 4”dl; else begincnt2 <= 4"d0;n <= 6"d4;endend6"d4:beginq_r <= M_l;n <= 6"d5;end6"d5:beginq_r <= M_2;if(cnt2<4,dl)cnt2 <= cnt2 + 4”d 1; else begincnt2 <= 4"dO;n <= 6"d6;endend6"d6:beginq_r <= M_3;if(cnt2 < 4"dl)cnt2 <= cnt2 + 4”dl; else begincnt2 <= 4"d0;n <= 6"d7;endend6"d7:beginq_r <= M_1;if(cnt2 < 4"d4)cnt2 <= cnt2 + 4”dl; else begincnt2 <= 4"d0;n <= 6"d8;endend6”d8:beginq_r <= M_3;if(cnt2<4,dl)cnt2 <= cnt2 + 4”d 1; else beginendend6"d9:beginq_r二 M_5;if(cnt2 < 4"dl)cnt2 <= cnt2 + 4”dl; elsebegincnt2 <= 4"d0;nv=6"dl0;endend6"dl0:beginq_r <= M_6;if(cnt2 < 4”d9)cnt2 <= cnt2 + 4”dl; elsebegincnt2 <= 4"d0;n v=6"dll;endend6"dll:beginq_r <= M_5;if(cnt2 < 4"d3)cnt2 <= cnt2 + 4”dl; elsebegincnt2 <= 4"d0;n<= 6"dl2;endend6"dl2:beginq_r <= M_3;if(cnt2 < 4"d2)cnt2<=cnt2 + 4,dl; else begincnt2 <= 4"d0;nv=6"dl3;endend6"dl3:beginq_r <= M_l;if(cnt2 < 4"dl)cnt2 <= cnt2 + 4”dl; elsebegincnt2 <= 4"d0;n v=6"dl4;6"dl4:beginq_r <= M_2;if(cnt2 < 4"d3)cnt2 <= cnt2 + 4”dl;elsebegincnt2 <= 4"d0;nv=6"dl5;endend6"dl5:beginq_r <= M_l;n<= 6"dl6;end6"dl6:beginq_r二 M_2;if(cnt2 < 4"dl)cnt2 <= cnt2 + 4,dl;elsebegincnt2 <= 4"d0;nv=6"dl7;endend6"dl7:beginq_r <= M_3;if(cnt2 < 4"dl)cnt2 <= cnt2 + 4”d 1;elsebegincnt2 <= 4"d0;nv=6"dl8;endend6"dl8:beginq_r <= M_l;if(cnt2 < 4"d3)cnt2 <= cnt2 + 4”dl;elsebegincnt2 <= 4"d0;n<= 6"dl9;endend6"dl9:beginq_r二 L_6;if(cnt2 < 4"d2)cnt2 <= cnt2 + 4,dl;elsebeginendend6"d20:beginq_r <= M_5;if(cnt2 < 4"dl)cnt2<=cnt2 + 4,dl; else begincnt2 <= 4"d0;n <= 6"d21;endend6"d21:beginq_r <= M_l;if(cnt2 < 4"d7)cnt2 <= cnt2 + 4"dl; elsebegincnt2 <= 4"d0;n <= 6"d22;endend6"d22:beginq_r <= M_6;if(cnt2 < 4"dl)cnt2<=cnt2 + 4,dl;else begincnt2 <= 4"d0;n <= 6"d23;endend6"d23:beginq_r <= M_5;if(cnt2 < 4"d3)cnt2 <= cnt2 + 4”dl; elsebegincnt2 <= 4"d0;n <= 6"d24;endend6"d24:beginq_r <= M_3;if(cnt2 < 4"d2)cnt2 <= cnt2 + 4,dl;else begincnt2 <= 4"d0;n <= 6"d25;6"d25:beginq_r <= M_l;if(cnt2<4,dl)cnt2 <= cnt2 + 4”dl; else begincnt2 <= 4"dO;n <= 6"d26;endend6"d26:beginq_r <= M_2;if(cnt2 < 4"d3)cnt2 <= cnt2 + 4”dl; else begincnt2 <= 4"d0;n <= 6"d27;endend6"d27:beginq_r <= M_1;n <= 6"d28;end6"d28:beginq_r <= M_2;if(cnt2<4,dl)cnt2 <= cnt2 + 4”dl; elsebegincnt2 <= 4"d0;n <= 6"d29;endend6"d29:beginq_r <= M_6;if(cnt2<4"dl)cnt2 <= cnt2 + 4”d 1;else begincnt2 <= 4"d0;n <= 6"d30;endend6"d30:beginq_r <= M_5;if(cnt2 < 4"d3)cnt2 <= cnt2 + 4”dl;else beginendend6"d31:beginq_r <= M_3;if(cnt2 < 4"d2)cnt2<=cnt2 + 4,dl; else begincnt2 <= 4"d0;n <= 6"d32;endend6"d32:beginq_r <= M_5;if(cnt2 < 4"dl)cnt2 <= cnt2 + 4"dl; elsebegincnt2 <= 4"d0;n <= 6"d33;endend6"d33:beginq_r <= M_6;if(cnt2 < 4"d7)cnt2<=cnt2 + 4,dl;else begincnt2 <= 4"d0;n <= 6"d34;endend6"d34:beginq_r <= H_l;if(cnt2 < 4"dl)cnt2 <= cnt2 + 4”dl; elsebegincnt2 <= 4"d0;n <= 6"d35;endend6"d35:beginq_r <= M_5;if(cnt2 < 4"d3)cnt2 <= cnt2 + 4,dl;else begincnt2 <= 4"d0;n <= 6"d36;6"d36:beginq_r <= M_3;if(cnt2 < 4"d2)cnt2 <= cnt2 + 4”dl; else begincnt2 <= 4"dO;n <= 6"d37;endend6"d37:beginq_r <= M_l;if(cnt2 < 4"dl)cnt2 <= cnt2 + 4”dl; else begincnt2 <= 4"d0;n <= 6"d38;endend6"d38:beginq_r <= M_2;if(cnt2 < 4"d3)cnt2 <= cnt2 + 4”dl; elsebegincnt2 <= 4"d0;n <= 6"d39;endend6”d39:beginq_r <= M_l;n <= 6"d40;end6”d40:beginq_r <= M_2;if(cnt2<4,dl)cnt2 <= cnt2 + 4”d 1;else begincnt2 <= 4"d0;n<= 6"d41;endend6"d41:beginq_r <= M_3;if(cnt2 < 4"dl)cnt2 <= cnt2 + 4”dl;else begin