硬件是20MHz的时钟,乐谱是《青花瓷》的谱,我把20MHZ的时钟分成2MHZ和4HZ,用两个LED表示中音和低音的音符。乐谱我也不太熟悉,我把乐谱每个竖线间的音符用16个拍来表示的。乐谱附在程序后面。程序调试没有错误,硬件检测时,显示没有错误,但蜂鸣器的声音明显低,不清楚。应该是频率设置的不对,希望哪位大神能指点一下,万分感谢!
- module song(clk_20mhz,speaker,led1,led2);
- input clk_20mhz;
- output speaker;
- output [6:0] led1,led2; //中音和低音的led显示
- time clk_2mhz,clk_4hz;
- reg[12:0] divider,origin; //origin为初始值
- reg[7:0] counter;
- reg[2:0] med,low; //med为中音,low为低音
- reg[1:0] count;
- reg[6:0] led1,led2;
- reg speaker;
- integer a,b;
- wire carry;
- //20mhz transport 2Mhz
- always@(posedge clk_20mhz)
- begin
- if(a<5)begin a=a+1;end else begin a=0;clk_2mhz=!clk_2mhz;
- end
- end
- //2mhz transport 4hz
- always@(posedge clk_2mhz)
- begin
- if(b<250000)begin b=b+1;end
- else begin b=0;clk_4hz=!clk_4hz;end
- end
- //count
- assign carry=(divider==8191);
- always@(posedge clk_2mhz)
- begin
- if(carry) divider=origin;
- else divider=divider+1;
- end
- //4 ge jie pai
- always@(posedge carry)
- begin
- {speaker,count}={speaker,count}+1;
- end
- //yuepu
- always@(posedge clk_4hz)
- begin
- if(counter==495)counter=0;
- else counter=counter+1;
- case(counter)
- 0:{med,low}='b101000;
- 1:{med,low}='b101000;
- 2:{med,low}='b101000;
- 3:{med,low}='b101000;
- 4:{med,low}='b101000;
- 5:{med,low}='b101000;
- 6:{med,low}='b101000;
- 7:{med,low}='b101000;
- 8:{med,low}='b101000;
- 9:{med,low}='b101000;
- 10:{med,low}='b101000;
- 11:{med,low}='b101000;
- 12:{med,low}='b000000;
- 13:{med,low}='b010000;
- 14:{med,low}='b001000;
- 15:{med,low}='b000110;
- 16:{med,low}='b001000;
- 17:{med,low}='b001000;
- 18:{med,low}='b001000;
- 19:{med,low}='b000110;
- 20:{med,low}='b001000;
- 21:{med,low}='b001000;
- 22:{med,low}='b001000;
- 23:{med,low}='b000110;
- 24:{med,low}='b001000;
- 25:{med,low}='b000110;
- 26:{med,low}='b000101;
- 27:{med,low}='b000101;
- 28:{med,low}='b000000;
- 29:{med,low}='b010000;
- 30:{med,low}='b001000;
- 31:{med,low}='b000110;
- 32:{med,low}='b001000;
- 33:{med,low}='b001000;
- 34:{med,low}='b001000;
- 35:{med,low}='b000110;
- 36:{med,low}='b001000;
- 37:{med,low}='b001000;
- 38:{med,low}='b001000;
- 39:{med,low}='b011000;
- 40:{med,low}='b010000;
- 41:{med,low}='b001000;
- 42:{med,low}='b001000;
- 43:{med,low}='b001000;
- 44:{med,low}='b000000;
- 45:{med,low}='b000101;
- 46:{med,low}='b000110;
- 47:{med,low}='b011000;
- 48:{med,low}='b011000;
- 49:{med,low}='b011000;
- 50:{med,low}='b011000;
- 51:{med,low}='b010000;
- 52:{med,low}='b011000;
- 53:{med,low}='b011000;
- 54:{med,low}='b011000;
- 55:{med,low}='b010000;
- 56:{med,low}='b011000;
- 57:{med,low}='b101000;
- 58:{med,low}='b011000;
- 59:{med,low}='b011000;
- 60:{med,low}='b000000;
- 61:{med,low}='b011000;
- 62:{med,low}='b011000;
- 63:{med,low}='b011000;
- 64:{med,low}='b011000;
- 65:{med,low}='b010000;
- 66:{med,low}='b010000;
- 67:{med,low}='b010000;
- 68:{med,low}='b010000;
- 69:{med,low}='b010000;
- 70:{med,low}='b001000;
- 71:{med,low}='b010000;
- 72:{med,low}='b011000;
- 73:{med,low}='b010000;
- 74:{med,low}='b010000;
- 75:{med,low}='b010000;
- 76:{med,low}='b000000;
- 77:{med,low}='b010000;
- 78:{med,low}='b001000;
- 79:{med,low}='b000110;
- 80:{med,low}='b001000;
- 81:{med,low}='b001000;
- 82:{med,low}='b001000;
- 83:{med,low}='b000110;
- 84:{med,low}='b001000;
- 85:{med,low}='b001000;
- 86:{med,low}='b001000;
- 87:{med,low}='b000110;
- 88:{med,low}='b001000;
- 89:{med,low}='b000110;
- 90:{med,low}='b000101;
- 91:{med,low}='b000101;
- 92:{med,low}='b000000;
- 93:{med,low}='b000101;
- 94:{med,low}='b000110;
- 95:{med,low}='b011000;
- 96:{med,low}='b101000;
- 97:{med,low}='b101000;
- 98:{med,low}='b101000;
- 99:{med,low}='b011000;
- 100:{med,low}='b101000;
- 101:{med,low}='b101000;
- 102:{med,low}='b101000;
- 103:{med,low}='b011000;
- 104:{med,low}='b010000;
- 105:{med,low}='b001000;
- 106:{med,low}='b001000;
- 107:{med,low}='b001000;
- 108:{med,low}='b000000;
- 109:{med,low}='b010000;
- 110:{med,low}='b001000;
- 111:{med,low}='b010000;
- 112:{med,low}='b011000;
- 113:{med,low}='b010000;
- 114:{med,low}='b010000;
- 115:{med,low}='b001000;
- 116:{med,low}='b010000;
- 117:{med,low}='b010000;
- 118:{med,low}='b001000;
- 119:{med,low}='b000110;
- 120:{med,low}='b010000;
- 121:{med,low}='b001000;
- 122:{med,low}='b001000;
- 123:{med,low}='b000110;
- 124:{med,low}='b001000;
- 125:{med,low}='b001000;
- 126:{med,low}='b001000;
- 127:{med,low}='b001000;
- 128:{med,low}='b001000;
- 129:{med,low}='b001000;
- 130:{med,low}='b001000;
- 131:{med,low}='b001000;
- 132:{med,low}='b001000;
- 133:{med,low}='b001000;
- 134:{med,low}='b001000;
- 135:{med,low}='b001000;
- 136:{med,low}='b000000;
- 137:{med,low}='b000000;
- 138:{med,low}='b000000;
- 139:{med,low}='b000000;
- 140:{med,low}='b000000;
- 141:{med,low}='b101000;
- 142:{med,low}='b101000;
- 143:{med,low}='b011000;
- 144:{med,low}='b010000;
- 145:{med,low}='b011000;
- 146:{med,low}='b000110;
- 147:{med,low}='b000110;
- 148:{med,low}='b010000;
- 149:{med,low}='b011000;
- 150:{med,low}='b101000;
- 151:{med,low}='b011000;
- 152:{med,low}='b010000;
- 153:{med,low}='b010000;
- 154:{med,low}='b010000;
- 155:{med,low}='b010000;
- 156:{med,low}='b000000;
- 157:{med,low}='b101000;
- 158:{med,low}='b101000;
- 159:{med,low}='b011000;
- 160:{med,low}='b010000;
- 161:{med,low}='b011000;
- 162:{med,low}='b000101;
- 163:{med,low}='b000101;
- 164:{med,low}='b010000;
- 165:{med,low}='b011000;
- 166:{med,low}='b101000;
- 167:{med,low}='b010000;
- 168:{med,low}='b001000;
- 169:{med,low}='b001000;
- 170:{med,low}='b001000;
- 171:{med,low}='b001000;
- 172:{med,low}='b000000;
- 173:{med,low}='b001000;
- 174:{med,low}='b010000;
- 175:{med,low}='b011000;
- 176:{med,low}='b101000;
- 177:{med,low}='b110000;
- 178:{med,low}='b101000;
- 179:{med,low}='b011000;
- 180:{med,low}='b101000;
- 181:{med,low}='b011000;
- 182:{med,low}='b011000;
- 183:{med,low}='b010000;
- 184:{med,low}='b010000;
- 185:{med,low}='b010000;
- 186:{med,low}='b010000;
- 187:{med,low}='b010000;
- 188:{med,low}='b000000;
- 189:{med,low}='b001000;
- 190:{med,low}='b010000;
- 191:{med,low}='b001000;
- 192:{med,low}='b010000;
- 193:{med,low}='b010000;
- 194:{med,low}='b001000;
- 195:{med,low}='b010000;
- 196:{med,low}='b010000;
- 197:{med,low}='b011000;
- 198:{med,low}='b011000;
- 199:{med,low}='b101000;
- 200:{med,low}='b011000;
- 201:{med,low}='b011000;
- 202:{med,low}='b011000;
- 203:{med,low}='b011000;
- 204:{med,low}='b000000;
- 205:{med,low}='b101000;
- 206:{med,low}='b101000;
- 207:{med,low}='b011000;
- 208:{med,low}='b010000;
- 209:{med,low}='b011000;
- 210:{med,low}='b000110;
- 211:{med,low}='b000110;
- 212:{med,low}='b010000;
- 213:{med,low}='b011000;
- 214:{med,low}='b101000;
- 215:{med,low}='b011000;
- 216:{med,low}='b010000;
- 217:{med,low}='b010000;
- 218:{med,low}='b010000;
- 219:{med,low}='b010000;
- 220:{med,low}='b000000;
- 221:{med,low}='b101000;
- 222:{med,low}='b101000;
- 223:{med,low}='b011000;
- 224:{med,low}='b010000;
- 225:{med,low}='b011000;
- 226:{med,low}='b000101;
- 227:{med,low}='b000101;
- 228:{med,low}='b010000;
- 229:{med,low}='b011000;
- 230:{med,low}='b101000;
- 231:{med,low}='b010000;
- 232:{med,low}='b001000;
- 233:{med,low}='b001000;
- 234:{med,low}='b001000;
- 235:{med,low}='b001000;
- 236:{med,low}='b000000;
- 237:{med,low}='b001000;
- 238:{med,low}='b010000;
- 239:{med,low}='b011000;
- 240:{med,low}='b101000;
- 241:{med,low}='b110000;
- 242:{med,low}='b101000;
- 243:{med,low}='b011000;
- 244:{med,low}='b101000;
- 245:{med,low}='b011000;
- 246:{med,low}='b011000;
- 247:{med,low}='b010000;
- 248:{med,low}='b010000;
- 249:{med,low}='b010000;
- 250:{med,low}='b010000;
- 251:{med,low}='b000101;
- 252:{med,low}='b011000;
- 253:{med,low}='b011000;
- 254:{med,low}='b010000;
- 255:{med,low}='b010000;
- 256:{med,low}='b010000;
- 257:{med,low}='b010000;
- 258:{med,low}='b001000;
- 259:{med,low}='b001000;
- 260:{med,low}='b001000;
- 261:{med,low}='b001000;
- 262:{med,low}='b001000;
- 263:{med,low}='b001000;
- 264:{med,low}='b001000;
- 265:{med,low}='b001000;
- 266:{med,low}='b001000;
- 267:{med,low}='b001000;
- 268:{med,low}='b000000;
- 269:{med,low}='b000000;
- 270:{med,low}='b011000;
- 271:{med,low}='b101000;
- 272:{med,low}='b110000;
- 273:{med,low}='b110000;
- 274:{med,low}='b110000;
- 275:{med,low}='b110000;
- 276:{med,low}='b110000;
- 277:{med,low}='b110000;
- 278:{med,low}='b101000;
- 279:{med,low}='b011000;
- 280:{med,low}='b010000;
- 280:{med,low}='b010000;
- 281:{med,low}='b010000;
- 282:{med,low}='b010000;
- 283:{med,low}='b010000;
- 284:{med,low}='b010000;
- 285:{med,low}='b010000;
- 286:{med,low}='b001000;
- 287:{med,low}='b010000;
- 288:{med,low}='b101000;
- 289:{med,low}='b101000;
- 290:{med,low}='b101000;
- 291:{med,low}='b101000;
- 292:{med,low}='b101000;
- 293:{med,low}='b101000;
- 294:{med,low}='b101000;
- 295:{med,low}='b010000;
- 296:{med,low}='b001000;
- 297:{med,low}='b001000;
- 298:{med,low}='b001000;
- 299:{med,low}='b001000;
- 300:{med,low}='b001000;
- 301:{med,low}='b001000;
- 302:{med,low}='b001000;
- 303:{med,low}='b010000;
- 304:{med,low}='b011000;
- 305:{med,low}='b011000;
- 306:{med,low}='b010000;
- 307:{med,low}='b010000;
- 308:{med,low}='b001000;
- 309:{med,low}='b001000;
- 310:{med,low}='b000110;
- 311:{med,low}='b000110;
- 312:{med,low}='b001000;
- 313:{med,low}='b001000;
- 314:{med,low}='b010000;
- 315:{med,low}='b010000;
- 316:{med,low}='b011000;
- 317:{med,low}='b011000;
- 318:{med,low}='b110000;
- 319:{med,low}='b110000;
- 320:{med,low}='b101000;
- 321:{med,low}='b101000;
- 322:{med,low}='b101000;
- 323:{med,low}='b101000;
- 324:{med,low}='b101000;
- 325:{med,low}='b101000;
- 326:{med,low}='b101000;
- 327:{med,low}='b101000;
- 328:{med,low}='b101000;
- 329:{med,low}='b101000;
- 330:{med,low}='b101000;
- 331:{med,low}='b101000;
- 332:{med,low}='b000000;
- 333:{med,low}='b010000;
- 334:{med,low}='b001000;
- 335:{med,low}='b000110;
- 336:{med,low}='b010000;
- 337:{med,low}='b010000;
- 338:{med,low}='b001000;
- 339:{med,low}='b001000;
- 340:{med,low}='b001000;
- 341:{med,low}='b001000;
- 342:{med,low}='b001000;
- 343:{med,low}='b001000;
- 344:{med,low}='b001000;
- 345:{med,low}='b001000;
- 346:{med,low}='b001000;
- 347:{med,low}='b001000;
- 348:{med,low}='b000000;
- 349:{med,low}='b101000;
- 350:{med,low}='b101000;
- 351:{med,low}='b011000;
- 352:{med,low}='b010000;
- 353:{med,low}='b011000;
- 354:{med,low}='b000110;
- 355:{med,low}='b000110;
- 356:{med,low}='b010000;
- 357:{med,low}='b011000;
- 358:{med,low}='b101000;
- 359:{med,low}='b011000;
- 360:{med,low}='b010000;
- 361:{med,low}='b010000;
- 362:{med,low}='b010000;
- 363:{med,low}='b010000;
- 364:{med,low}='b000000;
- 365:{med,low}='b101000;
- 366:{med,low}='b101000;
- 367:{med,low}='b011000;
- 368:{med,low}='b010000;
- 369:{med,low}='b011000;
- 370:{med,low}='b000101;
- 371:{med,low}='b000101;
- 372:{med,low}='b010000;
- 373:{med,low}='b011000;
- 374:{med,low}='b101000;
- 375:{med,low}='b010000;
- 376:{med,low}='b001000;
- 377:{med,low}='b001000;
- 378:{med,low}='b001000;
- 379:{med,low}='b001000;
- 380:{med,low}='b000000;
- 381:{med,low}='b001000;
- 382:{med,low}='b010000;
- 383:{med,low}='b011000;
- 384:{med,low}='b101000;
- 385:{med,low}='b110000;
- 386:{med,low}='b101000;
- 387:{med,low}='b100000;
- 388:{med,low}='b101000;
- 389:{med,low}='b011000;
- 390:{med,low}='b011000;
- 391:{med,low}='b010000;
- 392:{med,low}='b010000;
- 393:{med,low}='b010000;
- 394:{med,low}='b010000;
- 395:{med,low}='b010000;
- 396:{med,low}='b000000;
- 397:{med,low}='b001000;
- 398:{med,low}='b010000;
- 399:{med,low}='b001000;
- 400:{med,low}='b001000;
- 401:{med,low}='b010000;
- 402:{med,low}='b001000;
- 403:{med,low}='b010000;
- 404:{med,low}='b010000;
- 405:{med,low}='b011000;
- 406:{med,low}='b011000;
- 407:{med,low}='b011000;
- 408:{med,low}='b101000;
- 409:{med,low}='b011000;
- 410:{med,low}='b011000;
- 411:{med,low}='b011000;
- 412:{med,low}='b000000;
- 413:{med,low}='b101000;
- 414:{med,low}='b101000;
- 415:{med,low}='b011000;
- 416:{med,low}='b010000;
- 417:{med,low}='b011000;
- 418:{med,low}='b000110;
- 419:{med,low}='b000110;
- 420:{med,low}='b010000;
- 421:{med,low}='b011000;
- 422:{med,low}='b101000;
- 423:{med,low}='b011000;
- 424:{med,low}='b010000;
- 425:{med,low}='b010000;
- 426:{med,low}='b010000;
- 427:{med,low}='b010000;
- 428:{med,low}='b000000;
- 429:{med,low}='b101000;
- 430:{med,low}='b101000;
- 431:{med,low}='b011000;
- 432:{med,low}='b010000;
- 433:{med,low}='b011000;
- 434:{med,low}='b000101;
- 435:{med,low}='b000101;
- 436:{med,low}='b010000;
- 437:{med,low}='b011000;
- 438:{med,low}='b101000;
- 439:{med,low}='b010000;
- 440:{med,low}='b001000;
- 441:{med,low}='b001000;
- 442:{med,low}='b001000;
- 443:{med,low}='b001000;
- 444:{med,low}='b000000;
- 445:{med,low}='b001000;
- 446:{med,low}='b010000;
- 447:{med,low}='b011000;
- 448:{med,low}='b101000;
- 449:{med,low}='b110000;
- 450:{med,low}='b101000;
- 451:{med,low}='b100000;
- 452:{med,low}='b101000;
- 453:{med,low}='b011000;
- 454:{med,low}='b011000;
- 455:{med,low}='b010000;
- 456:{med,low}='b010000;
- 457:{med,low}='b010000;
- 458:{med,low}='b010000;
- 459:{med,low}='b000101;
- 460:{med,low}='b011000;
- 461:{med,low}='b011000;
- 462:{med,low}='b010000;
- 463:{med,low}='b010000;
- 464:{med,low}='b010000;
- 465:{med,low}='b001000;
- 466:{med,low}='b001000;
- 467:{med,low}='b001000;
- 468:{med,low}='b001000;
- 469:{med,low}='b001000;
- 470:{med,low}='b001000;
- 471:{med,low}='b001000;
- 472:{med,low}='b001000;
- 473:{med,low}='b001000;
- 474:{med,low}='b001000;
- 475:{med,low}='b001000;
- 476:{med,low}='b001000;
- 477:{med,low}='b001000;
- 478:{med,low}='b001000;
- 479:{med,low}='b001000;
- 480:{med,low}='b000000;
- 481:{med,low}='b000000;
- 482:{med,low}='b000000;
- 483:{med,low}='b000000;
- 484:{med,low}='b000000;
- 485:{med,low}='b000000;
- 486:{med,low}='b000000;
- 487:{med,low}='b000000;
- 488:{med,low}='b000000;
- 489:{med,low}='b000000;
- 490:{med,low}='b000000;
- 491:{med,low}='b000000;
- 492:{med,low}='b000000;
- 493:{med,low}='b000000;
- 494:{med,low}='b000000;
- 495:{med,low}='b000000;
- endcase
- case({med,low})
- 'b000101:origin=3089;//diyin5
- 'b000110:origin=3646;//diyin6
- 'b001000:origin=4369;
- 'b010000:origin=4786;
- 'b011000:origin=5157;
- 'b100000:origin=5328;
- 'b101000:origin=5640;
- 'b110000:origin=5918;
- 'b000000:origin=8191;
- endcase
- end
- always@(med or low)
- begin
- case(med)
- 3'd0: led1=7'b0111111;
- 3'd1: led1=7'b0000110;
- 3'd2: led1=7'b1011011;
- 3'd3: led1=7'b1001111;
- 3'd4: led1=7'b1100110;
- 3'd5: led1=7'b1101101;
- 3'd6: led1=7'b1111101;
- 3'd7: led1=7'b0000111;
- default:led1=7'bx;
- endcase
- case(low)
- 3'd0: led2=7'b0111111;
- 3'd1: led2=7'b0000110;
- 3'd2: led2=7'b1011011;
- 3'd3: led2=7'b1001111;
- 3'd4: led2=7'b1100110;
- 3'd5: led2=7'b1101101;
- 3'd6: led2=7'b1111101;
- 3'd7: led2=7'b0000111;
- default:led2=7'bx;
- endcase
- end
- endmodule
复制代码
|