【朱老師課程總結 侵刪】
第一部分、章節目錄
1.11.1.蜂鳴器的原理??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
1.11.2.讓蜂鳴器響起來
1.11.3.用定時器控制蜂鳴器音調
1.11.4.蜂鳴器發出滴滴聲
1.11.5.讓蜂鳴器唱歌1
1.11.6.讓蜂鳴器唱歌2
第二部分、章節介紹
1.11.1.蜂鳴器的原理
?? ?本節從工作原理、分類上講解了無源蜂鳴器和有源蜂鳴器2種的發聲原理和所需要的控制信號特性,為驅動蜂鳴器做理論積累。
1.11.2.讓蜂鳴器響起來
?? ?本節用最簡單的代碼讓蜂鳴器響起來,主要目的是驗證上節的理論。
1.11.3.用定時器控制蜂鳴器音調
?? ?本節用定時器定時間來控制蜂鳴器發出不同音調的聲音,主要讓大家提升編程功底的同時,理解定時時間、蜂鳴器音調等幾個因素之間的關聯性。
1.11.4.蜂鳴器發出滴滴聲
?? ?本節在上節基礎上,增加代碼實現蜂鳴器發出滴滴聲。
1.11.5.讓蜂鳴器唱歌1
?? ?本節講解蜂鳴器唱歌的原理,以及樂譜如何轉化成為音樂數據。
1.11.6.讓蜂鳴器唱歌2
?? ?本節對寫好的蜂鳴器唱歌程序進行分析理解,目的是讓大家能夠讀懂這段代碼。
?
第三部分、隨堂記錄
1.11.1.蜂鳴器的原理? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
1.11.1.1、蜂鳴器簡介
(1)分類:無源蜂鳴器和有源蜂鳴器:這里的"源"不是電源,是震蕩源
(2)外觀和內部結構
無源和有源外觀略有差別
(3)無源蜂鳴器發聲原理
- 蜂鳴器發聲原理是電流通過電磁線圈,使電磁線圈產生磁場來驅動振動膜發聲的,因此需要一定的電流才能驅動它
- 單片機IO引腳輸出的電流較小,單片機輸出的TTL電平基本上驅動不了蜂鳴器,因此需要增加一個電流放大的電路。三極管的作用為驅動,通過三極管放大驅動電流,從而可以讓蜂鳴器發出聲音。
- 電路圖如下,如果要驅動無源蜂鳴器需要在三極管的基極(B)上加載不同頻率的信號。對IO口進行電平翻轉信號,可以做出“多來米發索拉西”多種音調(初中物理:發生體的震動快慢,男女生音調、暖壺)的效果。
(4)電平翻轉控制信號,是高低電平相間的方波
(6)音調如何控制?音階與頻率又有確定的對應關系,因此就可以做出來“do re mi fa sol la si”的效果。音調受震動頻率控制,就等于控制信號的頻率。頻率越高音調越高,聽起來越刺耳。所以軟件可以控制!頻率是通過控制定時時間來決定的!
F = 1/T
(7)聲音大小(響度)如何控制?由硬件決定的,沒法寫代碼去控制聲音大小。
1.11.1.2、有源蜂鳴器
(1)內置震蕩電路后形成有源蜂鳴器,發聲原理和無源很像,但是只需要在三極管的基極(B)上給高電平就可以了。通過震蕩,內部自動形成方波。
(2)當然有源蜂鳴器也可以用頻率信號驅動!可以直接當無源用!
(3)無源蜂鳴器的缺陷
? ? ?不容易控制,必須控制好頻率!而有源蜂鳴器方便程序控制
(4)無源蜂鳴器缺陷
? ? 成本較高!
1.11.2.讓蜂鳴器響起來
1.11.2.1、接線確定。P0.0接到J8上面。
1.11.2.2、最簡單:使用delay讓蜂鳴器響起來,關鍵是計算聲音頻率(人耳可聽的聲音頻率范圍:20-20000Hz)
關鍵是調整延時函數!調節delay時間感受音調變化
#include<reg51.h>/******************接線*******************J7(buzzer)-P0.0
******************************************/sbit Buzzer = P0^0;//延時函數
//10kHz
void delay100us(void) //誤差 0us
{unsigned char a,b;for(b=1;b>0;b--)for(a=47;a>0;a--);
}
// 20Hz
//void delay50ms(void) //誤差 0us
//{
// unsigned char a,b;
// for(b=165;b>0;b--)
// for(a=150;a>0;a--);
//}void main(void)
{Buzzer = 1;while(1){Buzzer = !Buzzer;delay100us();}
}
1.11.3.用定時器控制蜂鳴器音調
1.11.3.1、上節驅動方法的問題
(1)不容易精準控制時間--每次都得新建一個新的delay(),而且別的單片機,可能不能用單片機小精靈!
(2)CPU控制蜂鳴器中不能做其他事
1.11.3.2、定時器控制蜂鳴器響
任務描述:定時器每隔一段時間響一次(10KHz),與此同時,靜態數碼管從0-F循環顯示!
(1)計算定時時間:10KHz => 1/10000s = 100us =>高電平+低電平一共100us,那高電平和低電平的時間都是50us。所以要定的時間就是50us
(2)外部晶振12MHz + 12T設置 => 內部時鐘頻率1MHz => 1us => 計數值為50
(3)初值為65535-50+1=65486=0xFFCE? ?則TH0 = 0xFF,TL0 = 0xCE;
#include<reg51.h>/******************接線*****************************本程序實現,在靜態數碼管顯示0-F過程中,蜂鳴器在響! J7(buzzer)-P0.0靜態數碼管-P1
**************************************************/sbit Buzzer = P0^0;// 獨立數碼管的段碼表
unsigned char val[16] =
{0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83,0xc6, 0xa1, 0x86, 0x8e
};//延時函數
void delay()
{unsigned char i = 200,j = 100;while(i--)while(j--);
}//定時器中斷處理程序
void timer0_buzser() interrupt 1 using 1
{TH0 = 0xFF;TL0 = 0xCE;Buzzer = !Buzzer;}void main(void)
{unsigned char i = 0;TMOD = 0x01; //使用16位定時器TH0 = 0xFF; //10KHzTL0 = 0xCE;ET0 = 1; EA = 1;TR0 = 1;Buzzer = 1;while (1){//主線任務for (i=0; i<16; i++){P1 = val[i];delay();}}
}
1.11.3.3、音調改變:現在想要一個特定頻率的音調,怎么辦呢?
假設想要得到 F(Hz)的頻率,對應的周期為T(s),需要計數次數為n,計數初值為N;
則T = 1/(F*2);
n = T*1000000=500000/F;
N = 65535-n+1;于是N = 65536-500000/F;TH0 = N/256;
TL0 = N%256;
//add
#define F_Hz 1000 //要定多少Hz,就寫多少
#define N (65536-50000/F_Hz) //設置初值 //modifyTH0 = N/256; TL0 = N%256;
1.11.4.蜂鳴器發出滴滴聲
首先,大家可能有個疑問,滴滴聲,和之前蜂鳴器的聲音有什么不同嗎?咱們先運行下效果,大家看看差別!
上節課的程序,都是蜂鳴器一直在吱吱的響;滴滴聲,就像水滴一樣,中間是有停頓的!如何做到?
在t1時間,我們可以定時讓蜂鳴器響,在t2時間,我們可以只進行計數count。
程序實現的思路就是
- 設置一個新的變量count,計數個數和蜂鳴器的頻率對應;可以宏定義一個CNT =?1000000/F_Hz
注意,count用char還能存下嗎? - 要區分是從有聲音到沒聲音,還是從沒聲音到有聲音!所以要有個標志位flag。
- 調整不同的頻率,聽效果!
#include<reg51.h>/******************接線*******************滴滴聲J7(buzzer)-P0.0靜態數碼管-P1
******************************************/#define F_Hz 1000 //要定多少Hz,就寫多少
#define N (65536-50000/F_Hz) //設置初值
#define CNT 1000000/F_Hzsbit Buzzer = P0^0;
unsigned int count = CNT; //計數次數
unsigned char flag = 0; // flag = 0表示有聲音,flag = 1表示沒聲音// 獨立數碼管的段碼表
unsigned char val[16] =
{0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83,0xc6, 0xa1, 0x86, 0x8e
};/*************延時函數*************/
void delay()
{unsigned char i = 200,j = 100;while(i--)while(j--);
}void timer0_buzser() interrupt 1 using 1
{TH0 = N/256; TL0 = N%256;if(count-- == 0){//說明開始翻轉了if(flag == 0){//從有聲音到無聲音flag = 1;}else{//從無聲音到有聲音flag = 0;Buzzer = !Buzzer;}count = CNT;}else{//不翻轉時if(flag == 0){Buzzer = !Buzzer; //響CNT-1次}else{//啥也不做}}}
void main(void)
{unsigned char i = 0;TMOD = 0x01; //使用16位定時器TH0 = N/256; TL0 = N%256;ET0 = 1; EA = 1;TR0 = 1;Buzzer = 1;//設置響和不響的周期時間count = CNT;flag = 0;while (1){//主線任務for (i=0; i<16; i++){P1 = val[i];delay();}}
}
還可以改,有聲音和沒聲音的長度不一樣!只要在蜂鳴器響和只計數這里改一下!比如我們設置,有聲音時間是無聲音的5倍!
這個地方是什么呢?沒錯就是PWM,有關PWM的介紹,可以看我的一篇文章!
單片機PWM輸出原理與實踐
?
1.11.5.讓蜂鳴器唱歌1
1.11.5.1、為什么蜂鳴器可以唱歌
(1)發聲音調可變(頻率)
(2)發聲長度可變(節拍)
1.11.5.2、體驗寫好的唱歌程序
/************************************************************************
[文件名] C51音樂程序(八月桂花)
[功能] 通過單片機演奏音樂 /**********************************************************************/
#include <REG51.H>
//#include <INTRINS.H>
//本例采用89C52, 晶振為11.0592MHZ
//關于如何編制音樂代碼, 其實十分簡單,各位可以看以下代碼.
//頻率常數即音樂術語中的音調,而節拍常數即音樂術語中的多少拍;
//所以拿出譜子, 試探編吧! sbit Beep = P0^0 ; // 要根據實際的接線來修改unsigned char n = 0; //n為節拍常數變量
unsigned char code music_tab[] =
{ 0x18, 0x30, 0x1C , 0x10, //格式為: 頻率常數, 節拍常數, 頻率常數, 節拍常數, 0x20, 0x40, 0x1C , 0x10, 0x18, 0x10, 0x20 , 0x10, 0x1C, 0x10, 0x18 , 0x40, 0x1C, 0x20, 0x20 , 0x20, 0x1C, 0x20, 0x18 , 0x20, 0x20, 0x80, 0xFF , 0x20, 0x30, 0x1C, 0x10 , 0x18, 0x20, 0x15, 0x20 , 0x1C, 0x20, 0x20, 0x20 , 0x26, 0x40, 0x20, 0x20 , 0x2B, 0x20, 0x26, 0x20 , 0x20, 0x20, 0x30, 0x80 , 0xFF, 0x20, 0x20, 0x1C , 0x10, 0x18, 0x10, 0x20 , 0x20, 0x26, 0x20, 0x2B , 0x20, 0x30, 0x20, 0x2B , 0x40, 0x20, 0x20, 0x1C , 0x10, 0x18, 0x10, 0x20 , 0x20, 0x26, 0x20, 0x2B , 0x20, 0x30, 0x20, 0x2B , 0x40, 0x20, 0x30, 0x1C , 0x10, 0x18, 0x20, 0x15 , 0x20, 0x1C, 0x20, 0x20 , 0x20, 0x26, 0x40, 0x20 , 0x20, 0x2B, 0x20, 0x26 , 0x20, 0x20, 0x20, 0x30 , 0x80, 0x20, 0x30, 0x1C , 0x10, 0x20, 0x10, 0x1C , 0x10, 0x20, 0x20, 0x26 , 0x20, 0x2B, 0x20, 0x30 , 0x20, 0x2B, 0x40, 0x20 , 0x15, 0x1F, 0x05, 0x20 , 0x10, 0x1C, 0x10, 0x20 , 0x20, 0x26, 0x20, 0x2B , 0x20, 0x30, 0x20, 0x2B , 0x40, 0x20, 0x30, 0x1C , 0x10, 0x18, 0x20, 0x15 , 0x20, 0x1C, 0x20, 0x20 , 0x20, 0x26, 0x40, 0x20 , 0x20, 0x2B, 0x20, 0x26 , 0x20, 0x20, 0x20, 0x30 , 0x30, 0x20, 0x30, 0x1C , 0x10, 0x18, 0x40, 0x1C , 0x20, 0x20, 0x20, 0x26 , 0x40, 0x13, 0x60, 0x18 , 0x20, 0x15, 0x40, 0x13 , 0x40, 0x18, 0x80, 0x00
}; // T0定時控制的是音樂的節拍(某一個音節持續的時間)而不管音調(頻率)// 音調是直接使用delay做出來的。
void int0() interrupt 1 //采用中斷0 控制節拍
{ TH0 = 0xd8; TL0 = 0xef; n--;
} void delay (unsigned char m) //控制頻率延時
{ unsigned i = 3 * m; while (--i);
} void delayms(unsigned char a) //豪秒延時子程序
{ while (--a); //采用while(--a) 不要采用while(a--); 各位可編譯一下看看匯編結果就知道了!
} void main()
{ unsigned char p, m; // m為頻率常數變量 unsigned char i = 0; TMOD &= 0x0f; TMOD |= 0x01; // timer0 工作在模式1,16位定時器下 TH0 = 0xd8;TL0 = 0xef; // 這個TH和TL的值合起來定了1個10ms左右的一個時間IE = 0x82;
play: while (1) {
a: p = music_tab[i]; if (p == 0x00) // 一遍播放完了,延時1s后自動開始下一遍 { i=0, delayms(1000); goto play;} //如果碰到結束符,延時1秒,回到開始再來一遍 else if (p == 0xff) { i = i + 1;delayms(100), TR0 = 0; goto a;} //若碰到休止符,延時100ms,繼續取下一音符 else {m = music_tab[i++], n = music_tab[i++];} // m取頻率常數 和 n取節拍常數 TR0 = 1; //開定時器1 while (n != 0){ Beep = ~Beep;delay(m); //等待節拍完成, 通過P1口輸出音頻(可多聲道哦!) }TR0 = 0; //關定時器1 }
}
(1)復制代碼過去(八月桂花)
(2)修改控制蜂鳴器的IO引腳定義
1.11.5.3、樂理知識入門(不想學的直接下課好了hhhh)轉自:(arduino)詳解蜂鳴器演奏音樂并配有LED閃爍實驗
從上面代碼我們可以看出,如果我們能夠控制好頻率(定時)和節拍(延時),那就有可能演奏出動聽的音樂。因此,我們首先需要搞清楚各音調的頻率,具體見下表:
我們知道了音調的頻率后,下一步就是控制音符的演奏時間。每個音符都會播放一定的時間,這樣才能構成一首優美的曲子,而不是生硬的一個調的把所有的音符一股腦的都播放出來。
音符節奏分為一拍、半拍、1/4拍、1/8拍,我們規定一拍音符的時間為1;半拍為0.5;1/4拍為0.25;1/8拍為0.125……,所以我們可以為每個音符賦予這樣的拍子播放出來,音樂就成了。
以歡樂頌為例:怎么看還可以參考這個怎么看樂譜簡譜
?
/***********************************************song:《歡樂頌》author: 馮慶巍引腳:P0.0接蜂鳴器**********************************************//*12Mhz晶振工作*/#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit voice=P0^0;
uchar code sound[]={0xff,0x26,0x40,0x26,0x40,0x24,0x40,0x20,0x40, //33450x20,0x40,0x24,0x40,0x26,0x40,0x2b,0x40, //54320x30,0x40,0x30,0x40,0x2b,0x40,0x26,0x40, //11230x26,0x40,0x26,0x20,0x2b,0x20,0x2b,0x40, 0x2b,0x40,//3.2_2-0x26,0x40,0x26,0x40,0x24,0x40,0x20,0x40, //33450x20,0x40,0x24,0x40,0x26,0x40,0x2b,0x40, //54320x30,0x40,0x30,0x40,0x2b,0x40,0x26,0x40, //11230x2b,0x40,0x2b,0x20,0x30,0x20,0x30,0x40,0x30,0x40,//2.1_1-0xff,0x40,0x2b,0x40,0x2b,0x40,0x26,0x40,0x30,0x40, //22310x2b,0x40,0x26,0x20,0x24,0x20,0x26,0x40,0x30,0x40,//23_4_310x2b,0x40,0x26,0x20,0x24,0x20,0x26,0x40,0x2b,0x40,//23_4_320x30,0x40,0x2b,0x40,0x40,0x40,0x26,0x40,//12.530x26,0x40,0x26,0x40,0x24,0x40,0x20,0x40,//33450x20,0x40,0x24,0x40,0x26,0x40,0x24,0x20,0x2b,0x20,//5434_2_0x30,0x40,0x30,0x40,0x2b,0x40,0x26,0x40,//11230x2b,0x40,0x2b,0x20,0x30,0x20,0x30,0x40,0x30,0x40,//2.1_1-0x00,};
uchar zdjs=0, jp;
del(yj);
void main(void){uint dpjs=0;uchar yj;TMOD=0x01,IE=0x82;TH0=0xd8, TL0=0xef;TR0=1;
while(1){ zdjs=0;dpjs++; yj=sound[dpjs]; dpjs++; jp=sound[dpjs];while(zdjs!=jp){ if(yj!=0xff){if(yj!=0){voice=!voice;del(yj);}else{dpjs=0; break;}}else{voice=0;del(jp);}}}}
time0() interrupt 1 using 1{TH0=0xd8, TL0=0xef;zdjs++;}
del(yj){uchar yj2=2;while(yj!=0){ while(yj2!=0){yj2--;}yj2=2;yj--; }}
/* 馮慶巍編制 */
/* 2020年11月19日22時54分43秒 */
1.11.6.讓蜂鳴器唱歌2
工具下載:樂譜編輯工具
1.11.6.1、我和我的祖國
《我和我的祖國》簡譜教學
/***********************************************song:《歡樂頌》author: 馮慶巍引腳:P0.0接蜂鳴器**********************************************//*12Mhz晶振工作*/#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit voice=P0^0;
uchar code sound[]={0xff,0x18,0x40,0x15,0x40,0x13,0x40,0x15,0x40,0x18,0x40,0x1c,0x40,//1+2+3+2+1+6 0x19,0x40,0x1c,0x40,0x26,0x40,0x20,0x40,0x20,0x20,0x20,0x40,0x20,0x20,//7635.5.0x18,0x40,0x15,0x40,0x13,0x40,0x15,0x40,0x18,0x40,0x1c,0x40,//1.2.3.2.1.60x19,0x40,0x20,0x40,0x20,0x20,0x26,0x20,0x1c,0x40,0x1c,0x20,0x1c,0x40,0x1c,0x20,//75.36.6.0x20,0x40,0x24,0x40,0x26,0x40,0x2b,0x40,0x2b,0x40,0x2b,0x40,//5432.0x33,0x40,0x39,0x40,0x40,0x40,0x26,0x40,0x26,0x40,0x26,0x40,//7-6-5-3.0x24,0x40,0x26,0x20,0x2b,0x40,0x30,0x20,//43_21_0x30,0x40,0x26,0x40,0x26,0x40,0x40,0x40,0x26,0x40,0x26,0x40,//1335-330x30,0x40,0x26,0x40,0x26,0x40,0x40,0x40,0x26,0x40,0x26,0x40,//1335-33 0x20,0x40,0x1c,0x40,0x20,0x40,0x24,0x40,0x26,0x40,0x2b,0x40,//565432 0x30,0x40,0x30,0x20,0x40,0x40,0x40,0x20,//1115-5-5-0x30,0x40,0x26,0x40,0x18,0x40,0x19,0x40,0x1c,0x40,0x26,0x40,//131+7630x20,0x40,0x20,0x20,0x20,0x40,0x20,0x20,//5555550x1c,0x40,0x19,0x40,0x1c,0x40,0x20,0x40,0x24,0x40,0x26,0x40,//6765430x2b,0x40,0x2b,0x40,0x2b,0x40,0x39,0x40,0x39,0x40,0x39,0x40,//222.6.6.60x33,0x40,0x39,0x40,0x40,0x40,0x20,0x40,0x30,0x40,0x2b,0x40,//.7.6.55120x26,0x40,0x26,0x40,0x26,0x40,0x26,0x40,0x26,0x40,0x26,0x40,//3333330x20,0x40,0x1c,0x40,0x20,0x40,0x24,0x40,0x26,0x40,0x2b,0x40,//5654320x30,0x40,0x30,0x40,0x30,0x40,0x40,0x40,0x40,0x40,0x40,0x40,//111.5.5.50x30,0x40,0x26,0x40,0x18,0x40,0x19,0x40,0x15,0x40,0x15,0x20,0x18,0x20,//131.72.-1._0x1c,0x40,0x1c,0x40,0x1c,0x40,0x1c,0x40,0x1c,0x40,0x1c,0x40,//6666660x18,0x40,0x19,0x40,0x1c,0x40,0x20,0x40,0x20,0x40,0x20,0x40,//.1765550x1c,0x40,0x20,0x40,0x24,0x40,0x26,0x40,0x26,0x40,0x26,0x40,//6543330x33,0x40,0x33,0x40,0x39,0x40,0x40,0x40,0x40,0x40,0x55,0x40,//.7.6_.52_0x30,0x40,0x30,0x40,0x30,0x40,0x30,0x40,0x30,0x40,0x30,0x40,//1111110x18,0x40,0x15,0x40,0x13,0x40,0x15,0x40,0x18,0x40,0x1c,0x40,//1+2+3+2+1+6 0x19,0x40,0x1c,0x40,0x26,0x40,0x20,0x40,0x20,0x20,0x20,0x40,0x20,0x20,//7635.5.0x18,0x40,0x15,0x40,0x13,0x40,0x15,0x40,0x18,0x40,0x1c,0x40,//1.2.3.2.1.60x19,0x40,0x20,0x40,0x20,0x20,0x26,0x20,0x1c,0x40,0x1c,0x20,0x1c,0x40,0x1c,0x20,//75.36.6.0x20,0x40,0x24,0x40,0x26,0x40,0x2b,0x40,0x2b,0x40,0x2b,0x40,//5432.0x33,0x40,0x39,0x40,0x40,0x40,0x26,0x40,0x26,0x40,0x26,0x40,//7-6-5-3.0x24,0x40,0x24,0x40,0x24,0x40,0x2b,0x40,0x2b,0x40,0x30,0x40,//4-21_0x30,0x40,0x30,0x40,0x30,0x40,0x30,0x40,0x30,0x40,0xff,0x40,//1.10_0x00,};
uchar zdjs=0, jp;
del(yj);
void main(void){uint dpjs=0;uchar yj;TMOD=0x01,IE=0x82;TH0=0xd8, TL0=0xef;TR0=1;
while(1){ zdjs=0;dpjs++; yj=sound[dpjs]; dpjs++; jp=sound[dpjs];while(zdjs!=jp){ if(yj!=0xff){if(yj!=0){voice=!voice;del(yj);}else{dpjs=0; break;}}else{voice=0;del(jp);}}}}
time0() interrupt 1 using 1{TH0=0xd8, TL0=0xef;zdjs++;}
del(yj){uchar yj2=2;while(yj!=0){ while(yj2!=0){yj2--;}yj2=2;yj--; }}
/* 馮慶巍編制 */
/* 2020年11月19日22時54分43秒 */
1.11.6.2、天空之城(部分)
?
/***********************************************song:《天空之城-久石讓》author: 馮慶巍引腳:P0.0接蜂鳴器**********************************************//*12Mhz晶振工作*/#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit voice=P0^0;
uchar code sound[]={0xff,0xff,0x40,0xff,0x40,0xff,0x40,0x1c,0x20,0x19,0x20, //0006_7_0x18,0x40,0x18,0x20,0x19,0x20,0x18,0x40,0x13,0x40,//.1-7_.1.30x19,0x40,0x19,0x40,0x19,0x40,0x26,0x40,//77730x1c,0x40,0x1c,0x20,0x20,0x20,0x1c,0x40,0x18,0x40,//6-5_6.10x20,0x40,0x20,0x40,0x20,0x40,0x26,0x20,0x26,0x20,//5553_3_0x24,0x40,0x24,0x20,0x26,0x20,0x24,0x20,0x18,0x40,0x18,0x20,//4-3_4_.1-0x26,0x40,0x26,0x40,0xff,0x20,0x18,0x20,0x18,0x20,0x18,0x20,//330_.1_.1_.1_0x19,0x40,0x19,0x20,0x20,0x20,0x20,0x40,0x19,0x40,//7-5_570x19,0x40,0x19,0x40,0x19,0x40,0x1c,0x20,0x19,0x20,//7776_7_0x18,0x40,0x18,0x20,0x19,0x20,0x18,0x40,0x13,0x40,//.1-7_.1.30x19,0x40,0x19,0x40,0x19,0x40,0x26,0x40,//77730x1c,0x40,0x1c,0x20,0x20,0x20,0x1c,0x40,0x18,0x40,//6.5_6.10x20,0x40,0x20,0x40,0x20,0x40,0x26,0x20,0x26,0x20,//5553_3_0x24,0x40,0x18,0x20,0x19,0x40,0x19,0x20,0x18,0x40,//4.1_7-.10x15,0x40,0x13,0x20,0x18,0x20,0x18,0x40,0x18,0x40,//.2.3_.1_.1.10x18,0x20,0x19,0x20,0x1c,0x40,0x19,0x40,0x1c,0x40,//.1_7_6760x1c,0x40,0x1c,0x40,0x1c,0x40,0x18,0x20,0x15,0x20,//666.1_.2_0x13,0x40,0x13,0x20,0x15,0x20,0x13,0x40,0x10,0x40,//.3-.2_.3.50x00, //0000};
uchar zdjs=0, jp;
del(yj);
void main(void){uint dpjs=0;uchar yj;TMOD=0x01,IE=0x82;TH0=0xd8, TL0=0xef;TR0=1;
while(1){ zdjs=0;dpjs++; yj=sound[dpjs]; dpjs++; jp=sound[dpjs];while(zdjs!=jp){ if(yj!=0xff){if(yj!=0){voice=!voice;del(yj);}else{dpjs=0; break;}}else{voice=0;del(jp);}}}}
time0() interrupt 1 using 1{TH0=0xd8, TL0=0xef;zdjs++;}
del(yj){uchar yj2=2;while(yj!=0){ while(yj2!=0){yj2--;}yj2=2;yj--; }}
/* 馮慶巍編制 */
/* 2020年11月20日20時03分43秒 */
1.11.6.3、黑人抬棺
/***********************************************song:《黑人抬棺BGM》author: 馮慶巍引腳:P0.0接蜂鳴器**********************************************//*12Mhz晶振工作*/#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit voice=P0^0;
uchar code sound[]={0xff,0x24,0x20,0x24,0x20,0x24,0x20,0x24,0x20, //44440x1c,0x20,0x1c,0x20,0x1c,0x20,0x1c,0x20, //66660x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, //55550x18,0x20,0x18,0x20,0x18,0x20,0x18,0x20, //1.1.1.1.0x15,0x20,0x15,0x20,0x15,0x20,0x15,0x20, //2.2.2.2.0x15,0x20,0x15,0x20,0x15,0x20,0x15,0x20, //2.2.2.2.0x20,0x20,0x24,0x20,0x26,0x20,0x30,0x20, //54310x2b,0x20,0xff,0x20,0x2b,0x20,0x1c,0x20, //20260x20,0x20,0xff,0x20,0x24,0x20,0xff,0x20, //50400x26,0x20,0xff,0x20,0x26,0x20,0x26,0x20, //30330x20,0x20,0xff,0x20,0x24,0x20,0x26,0x20, //50430x2b,0x20,0xff,0x20,0x2b,0x20,0x12,0x20, //2024.0x13,0x20,0x12,0x20,0x13,0x20,0x12,0x20, //3.4.3.4.0x2b,0x20,0xff,0x20,0x2b,0x20,0x12,0x20, //2024.0x13,0x20,0x12,0x20,0x13,0x20,0x12,0x20, //3.4.3.4.0x2b,0x20,0xff,0x20,0x2b,0x20,0x1c,0x20, //20260x20,0x20,0xff,0x20,0x24,0x20,0xff,0x20, //50400x26,0x20,0xff,0x20,0x26,0x20,0x26,0x20, //30330x20,0x20,0xff,0x20,0x24,0x20,0x26,0x20, //50430x2b,0x20,0xff,0x20,0x2b,0x20,0x12,0x20, //2024.0x13,0x20,0x12,0x20,0x13,0x20,0x12,0x20, //3.4.3.4.0x2b,0x20,0xff,0x20,0x2b,0x20,0x12,0x20, //2024.0x13,0x20,0x12,0x20,0x13,0x20,0x12,0x20, //3.4.3.4.0x48,0x20,0x48,0x20,0x48,0x20,0x48,0x20, //44440x1c,0x20,0x1c,0x20,0x1c,0x20,0x1c,0x20, //66660x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, //55550x18,0x20,0x18,0x20,0x18,0x20,0x18,0x20, //1.1.1.1.0x15,0x20,0x15,0x20,0x15,0x20,0x15,0x20, //2.2.2.2.0x15,0x20,0x15,0x20,0x15,0x20,0x15,0x20, //2.2.2.2.0x20,0x20,0x24,0x20,0x26,0x20,0x30,0x20, //54310x2b,0x20,0xff,0x20,0x2b,0x20,0x1c,0x20, //20260x20,0x20,0xff,0x20,0x24,0x20,0xff,0x20, //50400x26,0x20,0xff,0x20,0x26,0x20,0x26,0x20, //30330x20,0x20,0xff,0x20,0x24,0x20,0x26,0x20, //50430x2b,0x20,0xff,0x20,0x2b,0x20,0x12,0x20, //2024.0x13,0x20,0x12,0x20,0x13,0x20,0x12,0x20, //3.4.3.4.0x2b,0x20,0xff,0x20,0x2b,0x20,0x12,0x20, //2024.0x13,0x20,0x12,0x20,0x13,0x20,0x12,0x20, //3.4.3.4.0x2b,0x20,0xff,0x20,0x2b,0x20,0x1c,0x20, //20260x20,0x20,0xff,0x20,0x24,0x20,0xff,0x20, //50400x26,0x20,0xff,0x20,0x26,0x20,0x26,0x20, //30330x20,0x20,0xff,0x20,0x24,0x20,0x26,0x20, //50430x2b,0x20,0xff,0x20,0x2b,0x20,0x12,0x20, //2024.0x13,0x20,0x12,0x20,0x13,0x20,0x12,0x20, //3.4.3.4.0x2b,0x20,0xff,0x20,0x2b,0x20,0x12,0x20, //2024.0x13,0x20,0x12,0x20,0x13,0x20,0x12,0x20, //3.4.3.4.0x2b,0x20,0xff,0x20,0x2b,0x20,0x1c,0x20, //20260x20,0x20,0xff,0x20,0x24,0x20,0xff,0x20, //50400x26,0x20,0xff,0x20,0x26,0x20,0x26,0x20, //30330x20,0x20,0xff,0x20,0x24,0x20,0x26,0x20, //50430x2b,0x20,0xff,0x20,0x2b,0x20,0x12,0x20, //2024.0x13,0x20,0x12,0x20,0x13,0x20,0x12,0x20, //3.4.3.4.0x2b,0x20,0xff,0x20,0x2b,0x20,0x12,0x20, //2024.0x13,0x20,0x12,0x20,0x13,0x20,0x12,0x20, //3.4.3.4.0xff,0x20,0xff,0x20,0xff,0x20,0xff,0x20,0x00, //0000};
uchar zdjs=0, jp;
del(yj);
void main(void){uint dpjs=0;uchar yj;TMOD=0x01,IE=0x82;TH0=0xd8, TL0=0xef;TR0=1;
while(1){ zdjs=0;dpjs++; yj=sound[dpjs]; dpjs++; jp=sound[dpjs];while(zdjs!=jp){ if(yj!=0xff){if(yj!=0){voice=!voice;del(yj);}else{dpjs=0; break;}}else{voice=0;del(jp);}}}}
time0() interrupt 1 using 1{TH0=0xd8, TL0=0xef;zdjs++;}
del(yj){uchar yj2=2;while(yj!=0){ while(yj2!=0){yj2--;}yj2=2;yj--; }}
/* 馮慶巍編制 */
/* 2020年11月19日22時54分43秒 */
本節課結束!
總結
以上是生活随笔為你收集整理的【STC单片机学习】第十一课:蜂鸣器的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。