本文目錄一覽:
單片機蜂鳴器c語言《蘭花草》音樂代碼
程序名: MCS51U實驗板配套程序-歌曲:蘭花草
;電路介紹:蜂鳴器接P2.0;實驗前要連接J12
;***********************************************************************
OUT BIT P2.0
ORG 0
AJMP START
ORG 0BH
AJMP TIM0
START: MOV TMOD,#1
MOV IE,#10000010B
START0: MOV 30H,#0
NEXT: MOV A,30H
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV R2,A
JZ END0
ANL A,#0FH
MOV R5,A
MOV A,R2
SWAP A
ANL A,#0FH
JNZ SING
CLR TR0
AJMP D1
SING: DEC A
MOV 22H,A
RL A
MOV DPTR,#TABLE1
MOVC A,@A+DPTR
MOV TH0,A
MOV 21H,A
MOV A,22H
RL A
INC A
MOVC A,@A+DPTR
MOV TL0,A
MOV 20H,A
SETB TR0
D1: CALL DELAY
INC 30H
AJMP NEXT
END0: CLR TR0
AJMP START0
TIM0: PUSH ACC
PUSH PSW
MOV TL0,20H
MOV TH0,21H
CPL OUT
POP PSW
POP ACC
RETI
DELAY: MOV R7,#2
D2: MOV R4,#125
D3: MOV R3,#248
DJNZ R3,$
DJNZ R4,D3
DJNZ R7,D2
DJNZ R5,DELAY
RET
;====================================
TABLE1: DW 64021,64103,64260,64400
DW 64524,64580,64684,64777
DW 64820,64898,64968,65030
DW 64934
TABLE: ;1
DB 42H,82H,82H,82H,84H,02H,72H
DB 62H,72H,62H,52H,48H
DB 0B2H,0B2H,0B2H,0B2H,0B4H,02H,0A2H
;2
DB 12H,0A2H,0D2H,92H,88H
DB 82H,0B2H,0B2H,0A2H,84H,02H,72H
DB 62H,72H,62H,52H,44H,02H,12H
;3
DB 12H,62H,62H,52H,44H,02H,82H
DB 72H,62H,52H,32H,48H
DB 00H
END
如何用C語言編寫音樂歌曲?
很簡單的。
計算機發聲原理:
聲音有三個特性:響度、音調、音色。
響度,這個非常好理解。就是我們感覺到的聲音的大小。具體點說就是由「振幅」決定,振幅越大響度越大。一般計量響度的單位是分貝,dB。
音調,這個就是聲音的高低,由「頻率」決定,頻率越高音調越高。頻率單位是赫茲,符號Hz。
音色,在生活當中,我們會發現各種物品發出的聲音的特點是不一樣的,如二胡和笛子。聲音的特性就是音色。而決定聲音的音色是由於物體本身的材料、結構。
對於我們的計算機而言。發聲的設置都是固定了的。要麼要計算機上的那個喇叭,那麼是外接的音響等。
現在這裡對計算機上的那個喇叭用Turbo C 2.0進行編程,使之發出動聽的音樂《梁祝》。
第一步:定義《梁祝》的每一個音符的頻率和時間,將定義好的頻率和時間寫入文件或者保存在數組裡面。如果僅僅是寫一兩個程序自己玩玩的話,那直接保存在數組裡面就可以了。如果你想自己寫個播放器的話,那你先自己定義一種音樂格式文件(類似.mp3、.wav這種文件,這裡暫時把這個文件定為.mymusic),將《梁祝》每個音符的頻率、時間寫入這個自定義的音樂格式文件liangzhu.mymusic。
第二步:完成了liangzhu.mymusic文件的定義後,我們要做的事情就是讀文件,控制喇叭發聲。這個具體可以參照現有的資料。這裡需要事先寫幾個函數,讀文件的函數,暫停函數,頻率對照函數等等。
第三步,將上面的程序鏈接起來,就OK了。
將上面的步驟優化下,一個DIY的音樂播放器就成功了。
我上面說的只是控制了聲音的頻率和時間,其實還可以控制響度。對於一些特殊的設備,控制音色也不是不可能。
這種程序我在大一的時候寫過。現在想起來不難的。
主要是對C語言是否熟練,當然,你想要實現一個播放器,那你途中會遇到很多困難的。
祝你好運!
急求高手啊,怎麼在c語言程序中寫一段音樂啊!!!!
編譯能通過———- 編譯時把注釋去掉
#includestdio.h
#includestdlib.h
#includestring.h
struct song{ //定義一個歌曲結構
int id; //歌曲號
char title[20]; //歌曲名
char singer[20]; //歌手名
};
typedef struct song SONG; //把結構命名為 SONG
int main( void )
{
SONG songlist[100]; //定義一個存儲歌曲信息的列表,長100
int i=0,j;
int c;
clrscr();
while (i=100) {
printf( “please enter the song’s id, title and singer:\n” );
//輸入歌曲號 然後空格 輸入歌曲名 然後空格 輸入歌手名 然後回車
scanf( “%d%s%s”, songlist[i].id, songlist[i].title, songlist[i].singer);
i++;
//每次輸入完一條信息後,把列表中的歌曲信息全部列印出來看一下
printf(“the songlist is below:\n\n”);
for( j=0; j=i; j++){
printf( “%d %s %s\n”, songlist[j].id, songlist[j].title, songlist[j].singer );
}
printf(“\n”);
}
getch();
return 0;
}
或者是
#include “dos.h”
#include “stdio.h”
#define time 10000 /*預定義節拍長度 time(一拍)*/
#define time0 5000 /*預定義半節拍長度 time0*/
#define time1 15000 /*預定義1.5節拍長度 time1*/
#define time2 20000 /*預定義2節拍長度 time2*/
#define time3 30000 /*預定義3節拍長度 time3*/
#define _a 262 /*預定義低音音符1~7*/
#define _b 294
#define _c 330
#define _d 349
#define _e 392
#define _f 440
#define _g 494
#define a 523/*預定義中音音符1~7*/
#define b 587
#define c 659
#define d 698
#define e 784
#define f 880
#define g 988
#define a_ 1047/*預定義高音音符1~7*/
#define b_ 1175
#define c_ 1319
#define d_ 1397
#define e_ 2568
#define f_ 1760
#define g_ 1976
main()
{
int i=0,j;
unsigned milliseconds;
int music[1000]={
f,time,
f,time,
g,time0,
f,time,
f,time,
g,time0,
f,time,
g,time,
a_,time,
g,time,
f,time,
g,time0,
f,time0,
d,time,
c,time,
a,time,
c,time,
d,time,
c,time,
c,time0,
a,time0,
_g,time,
f,time,
g,time,
a_,time,
g,time,
f,time,
g,time0,
f,time0,
d,time1,
c,time,
a,time,
c,time,
d,time,
c,time,
c,time0,
a,time0,
g,time1,
f,time,
f,time,
g,time1,
f,time,
f,time,
g,time1,
c,time,
d,time,
g,time0,
f,time0,
c,time,
d,time,
g,time0,
f,time0,
d,time0,
d,time,
c,time3
}
;
while(music[i]!=’\0′)
{
if(music[i]=494) /*判斷不是低音*/
{
milliseconds=music[i+1];
for(j=1;j8;j++)
{
switch(j)
{
case 1: sound(a);
delay(milliseconds);break;
case 2: sound(b);
delay(milliseconds);break;
case 3: sound(c);
delay(milliseconds);break;
case 4: sound(d);
delay(milliseconds);break;
case 5: sound(e);
delay(milliseconds);break;
case 6: sound(f);delay(milliseconds);break;
case 7: sound(g);
delay(milliseconds);break;
}
nosound();
}
}
if(music[i]494music[i]988) /*判斷不是中音*/
{
milliseconds=music[i+1];
for(j=1;j8;j++)
{
switch(j)
{
case 1: sound(_a);
delay(milliseconds);break;
case 2: sound(_b);
delay(milliseconds);break;
case 3: sound(_c);
delay(milliseconds);break;
case 4: sound(_d);
delay(milliseconds);break;
case 5: sound(_e);
delay(milliseconds);break;
case 6: sound(_f);
delay(milliseconds);break;
case 7: sound(_g);
delay(milliseconds);break;
}
nosound();
}
}
if(music[i]988) /*判斷不是高音*/
{
milliseconds=music[i+1];
for(j=1;j8;j++)
{
switch(j)
{
case 1: sound(a_);
delay(milliseconds);break;
case 2: sound(b_);
delay(milliseconds);break;
case 3: sound(c_);
delay(milliseconds);break;
case 4: sound(d_);
delay(milliseconds);break;
case 5: sound(e_);
delay(milliseconds);break;
case 6: sound(f_);
delay(milliseconds);break;
case 7: sound(g_);
delay(milliseconds);break;
}
nosound();
}
}
nosound();
i=i+2;
}
}
如何用c語言編寫歌曲
我們知道,音樂是音高和音長的有序組合,設計微機音樂最重要的就是如何定義音高和音長,以及如何讓揚聲器發出指定的音符。下面給出音符與頻率的關係表。C語言提供的三個函數sound( )、nosound( )和clock( )可以很方便地解決上述的問題。sound( )函數可以用指定頻率打開PC機揚聲器直到用nosound( )函數來關閉它; clock( )函數正好用來控制發聲時間,而且它不受PC機主頻高低的影響。下面這段程序可使微機發出c調1的聲音。
音符與頻率關係表
音符 c d e f g a b
1 2 3 4 5 6 7
頻率 262 294 330 349 392 440 494
音符 c d e f g a b
1 2 3 4 5 6 7
頻率 523 587 659 698 784 880 988
音符 c d e f g a b
1 2 3 4 5 6 7
頻率 1047 1175 1319 1397 2568 1760 1976
#includestdio.h
#includedos.h
void pause(int);
void sound1(int,int);
void main(void)
{
int i,freq,speed=5;
int time=4*speed;
char *qm=”iddgwwwQQgfff dddfghhhggg ddgwwwqqgfff\
ddffhjqqqqq wpggjhgddgqq hhqwwqjjjggg\
ddgwwwqqqgfff ddffhjqqqqqq”;/*定義歌曲*/
while (*qm++ !=’\0′){
i=1;
switch(*qm){
case ‘k’:
time=1*speed; i=0;
break;
case ‘i’:
time=6*speed; i=0;
break;
case ‘o’:
time=10*speed; i=0;
break;
case ‘p’:
pause(time); i=0;
break;
case ‘a’:
freq=523;
break;
case ‘s’:
freq=587;
break;
case ‘d’:
freq=659;
break;
case ‘f’:
freq=698;
break;
case ‘g’:
freq=784;
break;
case ‘h’:
freq=880;
break;
case ‘j’:
freq=988;
break;
case ‘z’:
freq=262;
break;
case ‘X’:
freq=294;
break;
case ‘c’:
freq=330;
break;
case ‘v’:
freq=349;
break;
case ‘b’:
freq=392;
break;
case ‘n’:
freq=440;
break;
case ‘m’:
freq=494;
break;
case ‘q’:
freq=1047;
break;
case ‘w’:
freq=1175;
break;
case ‘e’:
freq=1319;
break;
case ‘r’:
freq=1397;
break;
case ‘t’:
freq=2568;
break;
case ‘y’:
freq=1760;
break;
case ‘u’:
freq=1976;
break;
default:
i=0;
break;
}
if(i)
sound1(freq,time);
}
}
void sound1(int freq,int time) /*freq為頻率,time為持續時間*/
{
union{
long divisor;
unsigned char c[2];
}count;
unsigned char ch;
count.divisor=1193280/freq; /* 1193280 是系統時鐘速率*/
outp(67,182);
outp(66,count.c[0]);
outp(66,count.c[1]);
ch=inp(97);
outp(97,ch|3);
pause(time);
outp(97,ch);
}
void pause(int time)
{
int t1,t2;
union REGS in,out;
in.h.ah=0X2c;
int86(0X21,in,out); /* 取當前時間*/
t1=t2=100*out.h.dh+out.h.dl; /*out.h.dh 為秒值,out.h.dl 為1/100 秒值*/
while(t2-t1time)
{
int86(0X21,in,out);
t2=100*out.h.dh+out.h.dl;
if (t2t1)t2+=6000; /* 增加一分鐘*/
}
}
原創文章,作者:IEOA,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/139396.html