单片机蜂鸣器奏乐实验大全代码

合集下载

51单片机蜂鸣器播放音乐代码

51单片机蜂鸣器播放音乐代码

/*生日快乐歌曲*/#include <>#define uint unsigned int#define uchar unsigned charsbit beep = P1^5;uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159, 212,212,106,126,159,169,190,119,119,126,159,142,159,0};uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0};//延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}void PlayMusic(){uint i=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){ //播放各个音符,SONG_LONG 为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}void main(){beep=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}}两只蝴蝶:#include <> // 这是单片机音乐代码生成器生成的代码#define uchar unsigned charsbit beepIO=P1^5; // 输出为可以修改成其它 IO 口uchar m,n;uchar code T[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0 xB9},{0xFB,0x03},{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0 x5B},{0xFD,0x81},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0 xAD},{0xFE,0xC0},{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0 x56},{0xFF,0x60},{0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};uchar code music[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8}, {21,4},{23,4},{21,4},{19,4},{16,4},{19,4},{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8}, {21,4},{23,4},{21,4},{19,4},{16,4},{19,4},{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8}, {21,4},{23,4},{21,4},{19,4},{16,4},{19,4},{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4}, {23,4},{21,4},{19,4},{16,4},{16,2},{19,2},{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{ 0,4},{23,4},{23,4},{26,4},{28,8},{28,12},{16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{2 3,8},{0,4},{28,4},{28,4},{30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4 },{26,16},{0xFF,0xFF}};void delay(uchar p){uchar i,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}void pause(){uchar i,j;for(i=150;i>0;i--)for(j=150;j>0;j--);}void T0_int() interrupt 1{beepIO=!beepIO;TH0=T[m][0]; TL0=T[m][1];}void main()uchar i=0;TMOD=0x01; EA=1; ET0=1;while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}else if(m==0xFF){TR0=0;delay(30);i=0;}else if(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else{TR0=1;delay(n);i++;}}}祝你平安:#include ""unsigned char Count;sbit _Speak =P1^5 ;unsigned char code SONG[] ={ //祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20 ,0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20 ,0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20 ,0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20 ,0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10 ,0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20 ,0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20 ,0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20 ,0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10 ,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80 ,0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20 ,0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x200x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10 ,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00};void Time0_Init(){TMOD = 0x01;IE = 0x82;TH0 = 0xD8;TL0 = 0xEF; //12MZ晶振,10ms}void Time0_Int() interrupt 1{TH0 = 0xD8;TL0 = 0xEF;Count++; //长度加1}/*-------------------------------------------------功能:1MS延时子程序-------------------------------------------------*/void Delay_xMs(unsigned int x){unsigned int i,j;for( i =0;i < x;i++ ){for( j =0;j<3;j++ );}}void Play_Song(unsigned char i){unsigned char Temp1,Temp2;unsigned int Addr;Count = 0; //中断计数器清0Addr = i * 217;while(1){Temp1 = SONG[Addr++];if ( Temp1 == 0xFF ) //休止符{TR0 = 0;Delay_xMs(100);}else if ( Temp1 == 0x00 ) //歌曲结束符{return;}else{Temp2 = SONG[Addr++];TR0 = 1;while(1){_Speak = ~_Speak;Delay_xMs(Temp1);if ( Temp2 == Count ){Count = 0;break;}}}}}/*-------------------------------------------------功能:主程序-------------------------------------------------*/ void main(){Time0_Init(); //定时器0中断初始化while(1){Play_Song(0); //播放}}。

51单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)精编版

51单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)精编版

/*生日快乐歌曲*/#include <reg51.h>#define uint unsigned int#define uchar unsigned charsbit beep = P1^5;uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159, 212,212,106,126,159,169,190,119,119,126,159,142,159,0};uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0};//延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}void PlayMusic(){uint i=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){ //播放各个音符,SONG_LONG 为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}void main(){beep=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}}两只蝴蝶:#include <reg51.h> // 这是单片机音乐代码生成器生成的代码#define uchar unsigned charsbit beepIO=P1^5; // 输出为P1.5 可以修改成其它IO 口uchar m,n;uchar code T[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{0xFB,0x03 },{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{0xFD,0x8 1},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{0xFE,0xC0 },{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{0xFF,0x60},{ 0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};uchar code music[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16 ,4},{16,2},{19,2},{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{0,4},{23,4},{23,4},{26,4} ,{28,8},{28,12},{16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{23,8},{0,4},{28,4},{28,4},{ 30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4},{26,16},{0xFF,0xFF}};void delay(uchar p){uchar i,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}void pause(){uchar i,j;for(i=150;i>0;i--)for(j=150;j>0;j--);}void T0_int() interrupt 1beepIO=!beepIO;TH0=T[m][0]; TL0=T[m][1];}void main(){uchar i=0;TMOD=0x01; EA=1; ET0=1;while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}else if(m==0xFF){TR0=0;delay(30);i=0;}else if(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else{TR0=1;delay(n);i++;}}}祝你平安:#include "reg52.h"unsigned char Count;sbit _Speak =P1^5 ;unsigned char code SONG[] ={ //祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20, 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20, 0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10, 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20, 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20, 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20, 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10, 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80, 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20, 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20, 0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10, 0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00};void Time0_Init(){TMOD = 0x01;IE = 0x82;TH0 = 0xD8;TL0 = 0xEF; //12MZ晶振,10ms}void Time0_Int() interrupt 1{TH0 = 0xD8;TL0 = 0xEF;Count++; //长度加1}/*-------------------------------------------------功能:1MS延时子程序-------------------------------------------------*/ void Delay_xMs(unsigned int x){unsigned int i,j;for( i =0;i < x;i++ ){for( j =0;j<3;j++ );}}void Play_Song(unsigned char i){unsigned char Temp1,Temp2;unsigned int Addr;Count = 0; //中断计数器清0Addr = i * 217;while(1){Temp1 = SONG[Addr++];if ( Temp1 == 0xFF ) //休止符{TR0 = 0;Delay_xMs(100);}else if ( Temp1 == 0x00 ) //歌曲结束符{return;}else{Temp2 = SONG[Addr++];TR0 = 1;while(1){_Speak = ~_Speak;Delay_xMs(Temp1);if ( Temp2 == Count ){Count = 0;break;}}}}}/*------------------------------------------------- 功能:主程序-------------------------------------------------*/ void main(){Time0_Init(); //定时器0中断初始化while(1){Play_Song(0); //播放}}。

(单片机)

(单片机)

实验四一、实验题目:当K1键按下后,首先使蜂鸣器响一声,然后使LED1-LED8完成3种闪亮的花样(自己定义),每一种花样循环3次,然后周而复始。

二、keil代码:/*当K1键按下后,首先使蜂鸣器响一声,然后使LED1- LED8完成3种闪亮的花样(自己定义),每一种花样循环3次,然后周而复始。

*/#include<reg51.h>sbit P2_0=P2^0;//接蜂鸣器sbit P2_7=P2^7;sbit P1_0=P1^0;sbit P1_1=P1^1;sbit P1_2=P1^2;sbit P1_3=P1^3;sbit P1_4=P1^4;sbit P1_5=P1^5;sbit P1_6=P1^6;sbit P1_7=P1^7;void DELAY(int time)//延时{while(time--){}}void BUZ_ON(){if(P2_7==0){P2_0=1;}else{ P2_0=0;}}void F1(void){int i;char data_group_mide[5]={0x00,0x18,0x24,0x42,0x81};//向两边延伸for(i=0;i<5;i++){P1=data_group_mide[i];DELAY(20000);}P1=0x00;}void F2(void){int i;char data_group_left[8]={0xFF,0x7F,0x3F,0x0F,0x07,0x03,0x01,0x00};//向左延伸for(i=0;i<8;i++){P1=data_group_left[i];DELAY(20000);P1=0x00;}void F3(void){int i;char date_group_right[8]={0x00,0x01,0x03,0x07,0x0f,0x3f,0x7f,0xff};//向右延伸for(i=0;i<8;i++){P1=date_group_right[i];DELAY(20000);}P1=0x00;}void main(){unsigned int i; //每种花样循环三次P2_0=0;P2_7=1;BUZ_ON();P1=0x00;while(P2_7==0){for(i=0;i<3;i++)//花样1 {F1();}for(i=0;i<3;i++)//花样2 {F2();}for(i=0;i<3;i++)//花样3 {F3();}}}三、protues电路图:四、实验截图:五、实验小结:通过本次实验,我们熟悉了protues的编译环境,对以后的单片机学习有很大帮助。

51单片机用蜂鸣器演奏卡农

51单片机用蜂鸣器演奏卡农
DB 12H,20H,13H,20H
DB 15H,20H,12H,20H
DB 12H,20H,13H,20H
DB 18H,20H,19H,20H
DB 1CH,20H,20H,20H
DB 24H,20H,26H,20H
DB 2BH,20H,24H,20H
DB 26H,20H,2BH,20H
MOV IE,#82H ;EA,ET0
MUSIC0:
NOP
MOV DPTR,#DAT ;表头地址送DPTR
MOV 20H,#00H ;中断计数器清0
MOV B,#00H ;表序号清0
MUSIC1:
NOP
CLR A
DB 10H,10H,13H,20H
DB 18H,10H,15H,10H
DB 13H,20H,15H,10H
DB 18H,10H,15H,10H
DB 19H,10H,18H,10H
DB 15H,10H,13H,10H
DB 15H,10H,18H,10H
DB 19H,10H,18H,20H
DB 20H,10H,1CH,10H
DB 19H,10H,18H,10H
DB 15H,10H,13H,10H
DB 12H,10H,13H,20H
DB 18H,10H,15H,10H
DB 13H,20H,26H,10H
DB 24H,10H,20H,10H
DB 1CH,10H,20H,10H
MOV A,B
MOVC A,@A+DPTR ;取节拍代码送R7
MOV R7,A
SETB TR0 ;启动计数
MUSIC2:

蜂鸣器唱两只老虎单片机程序

蜂鸣器唱两只老虎单片机程序

#include<reg52.h> //包含52单片机寄存器定义的头文件sbit sound=P3^7; //将sound位定义为P3.7unsigned int C; //储存定时器的定时常数//以下是C调中音的音频宏定义#define dao 523 //将"dao"宏定义为中音"1"的频率523Hz#define re 587 //将"re"宏定义为中音"2"的频率587Hz#define mi 659 //将"mi"宏定义为中音"3"的频率659Hz#define fa 698 //将"fa"宏定义为中音"4"的频率698Hz#define sao 784 //将"sao"宏定义为中音"5"的频率784Hz#define la 880 //将"la"宏定义为中音"6"的频率880Hz#define xi 987 //将"xi"宏定义为中音"7"的频率523Hz/*******************************************函数功能:1个延时单位,延时200ms******************************************/void delay(){unsigned char i,j;for(i=0;i<250;i++)for(j=0;j<250;j++);}/*******************************************函数功能:主函数******************************************/void main(void){unsigned char i,j;//以下是《两只老虎》歌曲unsigned int code f[]={dao,re,mi,dao, //每行对应一小节音符dao,re,mi,dao,mi,fa,sao,mi,fa,sao,sao,la,sao,fa,mi,dao,sao,la,sao,fa,mi,dao,dao,sao,dao,dao,sao,dao,0xff}; //以0xff作为音符的结束标志//以下是简谱中每个音符的节拍//"4"对应4个延时单位,"2"对应2个延时单位,"1"对应1个延时单位unsigned char code JP[ ]={2,2,2,2,2,2,2,2,2,2,3,2,2,3,1,2,2,1,2,2,1,2,2,1,2,2,2,2,2,2,2,2,};EA=1; //开总中断ET0=1; //定时器T0中断允许TMOD=0x00; // 使用定时器T0的模式1(13位计数器)while(1) //无限循环{i=0; //从第1个音符f[0]开始播放while(f[i]!=0xff) //只要没有读到结束标志就继续播放{C=460830/f[i];TH0=(8192-C)/32; //可证明这是13位计数器TH0高8位的赋初值方法TL0=(8192-C)%32; //可证明这是13位计数器TL0低5位的赋初值方法TR0=1; //启动定时器T0for(j=0;j<JP[i];j++) //控制节拍数delay(); //延时1个节拍单位TR0=0; //关闭定时器T0i++; //播放下一个音符}}}/***********************************************************函数功能:定时器T0的中断服务子程序,使P3.7引脚输出音频的方波************************************************************/void Time0(void ) interrupt 1 using 1{sound=!sound; //将P3.7引脚输出电平取反,形成方波TH0=(8192-C)/32; //可证明这是13位计数器TH0高8位的赋初值方法TL0=(8192-C)%32; //可证明这是13位计数器TL0低5位的赋初值方法}。

单片机控制蜂鸣器鸣奏音乐

单片机控制蜂鸣器鸣奏音乐

单片机控制蜂鸣器鸣奏音乐——中北大学:马政贵首先介绍蜂鸣器的发声原理。

我们都知道,音调和音调的时长是音符的主要特征,通过产生不同的音调和音调的时长可以奏出不同的音符来。

然后一个个音符串联在一起就可以产生美妙的音乐来了。

音调主要由声音的频率决定,通过单片机给蜂鸣器不同的音频脉冲来产生不同的音调。

要产生音频脉冲,只要算出某一音频的周期(周期=1/频率),然后将此周期除以2即为半周期的时间。

利用单片机的定时器工作在计数模式MODE1下,设定TH0和TL0的值以产生这半个周期,每当计时到达时就将输出脉冲的I/O(即接蜂鸣器的那个管脚)反相,然后重复计时此半个周期再对I/O反相,就可以在I/O引脚上得到此频率的脉冲。

如果没有必要进行精确的计时,可以用for循环空语句来粗略计时即可(本文选用此法)。

当单片机使用11.0592Mhz的晶振时,for(i=0;i<115;i++);这个空循环延时约为1ms;当晶振选用12Mhz时,可使用for(i=0;i<125;i++);这个空循环来延时1ms。

在这个空循环外头再进行一次循环就可以实现延时若干ms。

如:知道如何产生不同的频率的音调和进行音调的延时的之后,我们便可以编写程序来让单片机控制蜂鸣器来鸣奏音乐了。

下面附上不同音调所对应的频率表:下文介绍了让单片机控制蜂鸣器进行鸣奏音乐的两种方法。

第一种方法是基础的方法,就是顺序地让蜂鸣器挨个地演奏每个音符。

方法一:(此歌曲是《莫斯哥郊外的晚上》的乐曲)#include <reg52.h>sbit fmq=P3^6;void delay(unsigned int a){unsigned char b;while(a--){for(b=0;b<115;b++) ;}}void yanzou(unsigned char i,unsigned int pai){unsigned char y;unsigned int j;for(j=0;j<pai;j++){fmq=0;for(y=0;y<i;y++) ;fmq=1;for(y=0;y<i;y++) ;}}void main(){while(1){yanzou(65,220); //1/2拍中音6yanzou(55,262); //1/2拍高音1yanzou(44,330); //1/2拍高音3yanzou(55,262); //1/2拍高音1yanzou(49,587); //1拍高音2yanzou(55,262); //1/2拍高音1yanzou(58,247); //1/2拍中音7yanzou(44,660); //1拍高音3yanzou(49,587); //1拍高音2yanzou(65,880); //2拍中音6yanzou(55,262); //1/2拍高音1yanzou(44,330); //1/2拍高音3yanzou(37,392); //1/2拍高音5yanzou(37,392); //1/2拍高音5yanzou(65,440); //1拍中音6yanzou(37,392); //1/2拍高音5yanzou(41,349); //1/2拍高音4yanzou(44,2640); //4拍高音3yanzou(41,698); //1拍高音4yanzou(37,784); //1拍高音5yanzou(29,492); //1/2拍高音7yanzou(33,440); //1/2拍高音6yanzou(44,660); //1拍高音3delay(250);yanzou(58,494); //1拍中音7yanzou(65,220); //1/2拍中音6yanzou(44,330); //1/2拍高音3yanzou(49,293); //1/2拍高音2yanzou(41,698); //1拍高音4yanzou(41,698); //1拍高音4yanzou(37,392); //1/2拍高音5yanzou(41,349); //1/2拍高音4yanzou(44,660); //1拍高音3yanzou(49,293); //1/2拍高音2yanzou(55,262); //1/2拍高音1yanzou(44,660); //1拍高音3yanzou(49,587); //1拍高音2yanzou(65,880); //2拍中音6yanzou(65,880); //2拍中音6yanzou(41,698); //1拍高音4yanzou(37,784); //1拍高音5yanzou(29,492); //1/2拍高音7yanzou(33,440); //1/2拍高音6yanzou(44,660); //1拍高音3delay(250);yanzou(58,494); //1拍中音7yanzou(65,220); //1/2拍中音6yanzou(44,330); //1/2拍高音3yanzou(49,293); //1/2拍高音2yanzou(41,698); //1拍高音4yanzou(41,698); //1拍高音4yanzou(37,392); //1/2拍高音5yanzou(41,349); //1/2拍高音4yanzou(44,660); //1拍高音3yanzou(49,293); //1/2拍高音2yanzou(55,262); //1/2拍高音1yanzou(44,660); //1拍高音3yanzou(49,587); //1拍高音2yanzou(65,880); //2拍中音6yanzou(65,880); //2拍中音6delay(5000);}}观察方法一的主函数可以发现,每个语句的形式和实现的功能都是一样的,于是可以想到用数组来进行代码的简化。

51单片机蜂鸣器播放音乐代码

51单片机蜂鸣器播放音乐代码

/*生日快乐歌曲*/#include <>#define uint unsigned int#define uchar unsigned charsbit beep = P1^5;uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159, 212,212,106,126,159,169,190,119,119,126,159,142,159,0};uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0};//延时!void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}void PlayMusic(){uint i=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){ //播放各个音符,SONG_LONG 为拍子长度%for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}~void main(){beep=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}}两只蝴蝶:%#include <> // 这是单片机音乐代码生成器生成的代码#define uchar unsigned charsbit beepIO=P1^5; // 输出为可以修改成其它IO 口uchar m,n;uchar code T[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{0xFB,0x03 },{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{0xFD,0x8 1},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{0xFE,0xC0 },{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{0xFF,0x60},{ 0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};~uchar code music[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16 ,4},{16,2},{19,2},{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{0,4},{23,4},{23,4},{26,4} ,{28,8},{28,12},{16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{23,8},{0,4},{28,4},{28,4},{ 30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4},{26,16},{0xFF,0xFF}};void delay(uchar p){.uchar i,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}void pause(){uchar i,j;for(i=150;i>0;i--)for(j=150;j>0;j--);/}void T0_int() interrupt 1{beepIO=!beepIO;TH0=T[m][0]; TL0=T[m][1];}void main(){uchar i=0;TMOD=0x01; EA=1; ET0=1;、while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}else if(m==0xFF){TR0=0;delay(30);i=0;}else if(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else.{TR0=1;delay(n);i++;}}}祝你平安:#include ""unsigned char Count;sbit _Speak =P1^5 ;unsigned char code SONG[] ={ //祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20, 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20, %0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10, 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20, 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20, 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20, 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10, 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80, 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20, 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,¥0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00}; void Time0_Init(){TMOD = 0x01;IE = 0x82;TH0 = 0xD8;TL0 = 0xEF; //12MZ晶振,10ms}void Time0_Int() interrupt 1({TH0 = 0xD8;TL0 = 0xEF;Count++; //长度加1}/*-------------------------------------------------功能:1MS延时子程序-------------------------------------------------*/void Delay_xMs(unsigned int x){)unsigned int i,j;for( i =0;i < x;i++ ){for( j =0;j<3;j++ );}}void Play_Song(unsigned char i){unsigned char Temp1,Temp2;unsigned int Addr;!Count = 0; //中断计数器清0Addr = i * 217;while(1){Temp1 = SONG[Addr++];if ( Temp1 == 0xFF ) //休止符{TR0 = 0;Delay_xMs(100);}:else if ( Temp1 == 0x00 ) //歌曲结束符{return;}else{Temp2 = SONG[Addr++];TR0 = 1;while(1){._Speak = ~_Speak;Delay_xMs(Temp1);if ( Temp2 == Count ){Count = 0;break;}}}}}/*-------------------------------------------------功能:主程序-------------------------------------------------*/ void main(){Time0_Init(); //定时器0中断初始化while(1){Play_Song(0); //播放}}。

单片机蜂鸣器发声代码

单片机蜂鸣器发声代码

单片机蜂鸣器发声代码
单片机蜂鸣器是单片机开发中经常使用的一种输出设备,在很多作品中都发挥着很重要的作用。

本文将介绍如何通过单片机控制蜂鸣器发出不同频率的声音。

一、硬件设计
我们需要使用一个蜂鸣器和一块单片机开发板,比如STC89C52。

蜂鸣器有正负两个针脚,需要将正极接到控制单片机的GPIO端口上,负极接地即可。

1. 预处理指令
首先需要在头文件中定义单片机的型号和所要使用的GPIO端口。

```
#include <STC89C5xRC.H> //使用STC89C52
#define buzz P2 //定义蜂鸣器控制口
```
2. 主函数
接下来就是核心部分,主函数中需要实现的就是通过改变GPIO口的电平来控制蜂鸣器发出不同频率的声音。

对于控制蜂鸣器发出持久的“滴滴声”,可以采用下面的程序:
程序中先将GPIO口输出低电平,等待一段时间后再输出高电平,蜂鸣器发出持续的“滴滴声”。

如果想控制蜂鸣器发出不同频率的声音,可以修改上述程序中的DelayMs()函数来设置不同的延时时间。

因为蜂鸣器的振动频率与输入信号的高低电平时间比例有关,所以延时时间变化会使输出的声音频率发生变化。

比如,如果想让蜂鸣器发出音调为“咳咳声”,可以改变延时时间来实现。

如此,蜂鸣器就发出了“咳咳声”。

以上就是单片机蜂鸣器发声的基本方法,不同的延时时间可以产生不同的声音效果,可以根据实际需要进行调整。

单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安

单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安

单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安/*生日快乐歌曲*/#include<reg51.h>#defineuintunsignedint#defineucharunsignedcharsbitbeep=P1^5;ucharcodeSONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159,212,212,106,126,159,169,190,119,119,126,159,142,159,0};ucharcodeSONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0};//延时voidDelayMS(uintx){uchart;while(x--)for(t=0;t<120;t++);}voidPlayMusic(){uinti=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){//播放各个音符,SONG_LONG为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}voidmain(){beep=0;while(1){PlayMusic();//播放生日快乐DelayMS(500);//播放完后暂停一段时间}}两只蝴蝶:#include<reg51.h>//这是单片机音乐代码生成器生成的代码#defineucharunsignedcharsbitbeepIO=P1^5;//输出为P1.5可以修改成其它IO口ucharcodeT[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{ 0xFB,0x03},{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{ 0xFD,0x81},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{ 0xFE,0xC0},{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{ 0xFF,0x60},{0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};ucharcodemusic[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4} ,{23,4},{21,4},{19,4},{16,4},{19,4},{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8},{21,4} ,{23,4},{21,4},{19,4},{16,4},{19,4},{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4} ,{23,4},{21,4},{19,4},{16,4},{19,4},{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4},{23,4} ,{21,4},{19,4},{16,4},{16,2},{19,2},{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{0,4},{ 23,4},{23,4},{26,4},{28,8},{28,12},{16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{23,8},{ 0,4},{28,4},{28,4},{30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4},{26,16}, {0xFF,0xFF}};voiddelay(ucharp){uchari,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}voidpause(){uchari,j;for(i=150;i>0;i--)for(j=150;j>0;j--);}voidT0_int()interrupt1{beepIO=!beepIO;TH0=T[m][0];TL0=T[m][1];}voidmain(){uchari=0;while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}elseif(m==0xFF){TR0=0;delay(30);i=0;}elseif(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else{TR0=1;delay(n);i++;}}}祝你平安:#include"reg52.h"unsignedcharCount;sbit_Speak=P1^5;unsignedcharcodeSONG[]={//祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20, 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20, 0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10, 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20, 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20, 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20, 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10, 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80, 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20, 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20, 0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10, 0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00};voidTime0_Init(){TMOD=0x01;IE=0x82;TH0=0xD8;TL0=0xEF;//12MZ晶振,10ms}voidTime0_Int()interrupt1{TH0=0xD8;TL0=0xEF;Count++;//长度加1}功能:1MS延时子程序-------------------------------------------------*/ voidDelay_xMs(unsignedintx){unsignedinti,j;for(i=0;i<x;i++){for(j=0;j<3;j++);}}voidPlay_Song(unsignedchari){unsignedcharTemp1,Temp2;unsignedintAddr;Count=0;//中断计数器清0Addr=i*217;while(1){Temp1=SONG[Addr++];if(Temp1==0xFF)//休止符{TR0=0;Delay_xMs(100);}elseif(Temp1==0x00)//歌曲结束符{return;}else{Temp2=SONG[Addr++];TR0=1;while(1){_Speak=~_Speak;Delay_xMs(Temp1);if(Temp2==Count){Count=0;break;}}}}}功能:主程序-------------------------------------------------*/ voidmain(){Time0_Init();//定时器0中断初始化while(1){Play_Song(0);//播放}}。

蜂鸣器和弦音实现基于瑞萨单片机实现电路+代码

蜂鸣器和弦音实现基于瑞萨单片机实现电路+代码

蜂鸣器和弦电路以下是基于瑞萨单片机实现代码:/****************************************************************************** **文件:Beep.c功能:蜂鸣器和弦音,定义三种音,关机,开机,单音Author:DB.YAN,*****************Time:2013.10.15Recent Modify:2013.10.16******************************************************************************//*=======================以下是音调定义======================*/#define Tone_C6 1#define Tone_CS6 2#define Tone_D6 3#define Tone_DS6 4#define Tone_E6 5#define Tone_F6 6#define Tone_FS6 7#define Tone_G6 8#define Tone_GS6 9#define Tone_A6 10#define Tone_AS6 11#define Tone_B6 12#define Tone_C7 13#define Tone_CS7 14#define Tone_D7 15#define Tone_DS7 16#define Tone_E7 17#define Tone_F7 18#define Tone_FS7 19#define Tone_G7 20#define Tone_GS7 21#define Tone_A7 22#define Tone_AS7 23#define Tone_B7 24#define END 0//音频结束位#define SYS_FREQ 8 // MHZ 系统时钟#define TIMER_CNT_BASE 1000000*SYS_FREQ/2 //定时器计数基数#define BUZZER_IO P3_bit.no1 //BEEP IO#define BUZZER_OUT PM3_bit.no1=0 //Mode OUT#define BUZZER_PULL PU3_bit.no1=1 //Pull up#define PWR_CTRL P13_bit.no0 //蜂鸣器供电控制脚#define BUZZERIO_INIT {BUZZER_OUT;BUZZER_PULL;}/*=========================音调频率表=======================*/ const uint Tune_Tbl[24]={//C6,CS6, D6 ,DS6 ,E6 F6 ,FS6 ,G6 ,GS6 ,A6 ,AS6 ,B61047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976,//C7,CS7, D7 ,DS7 ,E7 F7 ,FS7 ,G7 ,GS7 ,A7 ,AS7 ,B72093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951,};/*======================关机和弦1,2,3...====================*/ const uchar Sound_ShutDown[10]={Tone_C7,2,14,Tone_D7,2,6,Tone_E7,6,60,END};/*======================开机和弦3,2,1...====================*/const uchar Sound_PowerOn[10]={Tone_E7,2,6,Tone_D7,2,14,Tone_C7,6,60,END/*======================单音和弦2...=======================*/ const uchar Sound_Single[10]={Tone_D7,12,60,END,END,END,END,END,END,END,};#define S_SHUTDOWN 1//关机音#define S_POWERON 2//开机音#define S_SINGLE 3//单音const uchar (*pSoundSel)[10];//选曲指针struct BE{uint CountFR; //定时器计数值,通过改变计数值改变音频频率uchar PWR_time; //开启时间,单位为10mSuchar FREQ_time; //音频供给时间,单位为10mSuchar index; //}Beep;volatile union { uchar flag4; __BITS8 flag4_bit; };#define Beep_EN flag4_bit.no0#define Beep_Finish flag4_bit.no1 //和弦结束标志/*======================================================= 函数名:Beep_On(uchar SoundSel)传入参数:SoundSel功能:选择声音种类调用:外部调用=========================================================*/ __callt void Beep_On(uchar SoundSel){Beep_EN=1;Beep.index=0;Beep_Finish=0;switch(SoundSel){case S_SHUTDOWN:pSoundSel=&Sound_ShutDown;break;case S_POWERON:pSoundSel=&Sound_PowerOn;break;case S_SINGLE:pSoundSel=&Sound_Single;break;default:break;}}/*=======================================================函数名:Beep_set()功能:蜂鸣器参数设置调用:Beep_control()=========================================================*/__callt void Beep_set(){if ((*pSoundSel)[Beep.index]!=END){Beep.CountFR=(uint)(TIMER_CNT_BASE/Tune_Tbl[(*pSoundSel)[Beep.index]-1]);Beep.PWR_time=(*pSoundSel)[Beep.index+1];Beep.FREQ_time=(*pSoundSel)[Beep.index+2];Beep.index=Beep.index+3;}else {Beep.index=0; PWR_CTRL=1;Beep_EN=0;Beep_Finish=1;}return;}/*=======================================================函数名:Beep_control()功能:蜂鸣器控制调用:每10ms调用一次=========================================================*/__callt void Beep_control()//每10mS调用一次{if (Beep_EN){if (Beep.PWR_time){PWR_CTRL=0;//供电Beep.PWR_time--;}else{PWR_CTRL=1;//掉电,依靠电容放电实现和弦拖音}if (Beep.FREQ_time){Beep.FREQ_time--;CR000=Beep.CountFR;TOC00=0x03;//TMC00=0X0C;//启动操作}else{TOC00=0x02;//TMC00=0X00;//启动操作Beep_set();PWR_CTRL=0;//发声完毕,打开电源给电容充电}}else{BUZZER_IO=0;TOC00=0x02;}return;}。

蜂鸣器演奏祝你平安——51单片机

蜂鸣器演奏祝你平安——51单片机

蜂鸣器演奏祝你平安——51单片机的Proteus实验实验原理51单片机的一个I/O口控制speaker发声,演唱祝你平安歌曲。

主要器件以及电路图单片机——AT89C51,蜂鸣器——speaker。

汇编程序源码晶振12M,已在Proteus下测试,好像有点失真。

程序是从网上照的,感觉不是很对劲,删除了一部分之后还是没有变化。

部分注释是我后加上去的。

;------------------------------------; 蜂鸣器演奏--祝你平安; 功能:蜂鸣器-蜂鸣器奏乐-祝你平安;------------------------------------SPK bit P2.7 ;以后SPK就是speaker了ORG 0000HLJMP STARTORG 000BHINC 20H ;T0中断服务,中断计数器加1MOV TH0,#0D8HMOV TL0,#0EFH ;12M晶振,形成10毫秒中断RETISTART:MOV TH0,#0D8HMOV TL0,#0EFHMOV TMOD,#01HMOV IE,#82H ;10000010B——开T0中断MUSIC0:NOPMOV DPTR,#DAT ;表头地址送DPTRMOV 20H,#00H ;中断计数器清0MUSIC1:NOPCLR AMOVC A,@A+DPTR ;查表取代码JZ END0 ;是00H,则结束CJNE A,#0FFH,MUSIC5 ;只要A不等于FF,一直执行MUSIC5LJMP MUSIC3 ;当A到FF的时候,执行MUSIC3 MUSIC5:NOPMOV R6,A ;代码送R6INC DPTRMOVC A,@A+DPTR ;取节拍代码送R7MOV R7,ASETB TR0 ;启动计数MUSIC2:NOPCPL SPKMOV A,R6 ;按R6(代码)延时MOV R3,A ;LCALL DEL ;利用代码(R6)来调用延时CJNE A,20H,MUSIC2 ;中断计数器(20H)=R7否?不等,则继续循环 MOV 20H,#00H ;等于,则取下一代码INC DPTRLJMP MUSIC1MUSIC3:NOPCLR TR0 ;休止100毫秒MOV R2,#0DHMUSIC4:NOPMOV R3,#0FFHLCALL DELDJNZ R2,MUSIC4INC DPTRLJMP MUSIC1END0:NOPMOV R2,#64H ;歌曲结束,延时1秒后继续MUSIC6:MOV R3,#00HLCALL DELDJNZ R2,MUSIC6LJMP MUSIC0DEL: ;DEL子程序利用R3来延时一定时间 NOPDEL3:MOV R4,#02HDEL4:NOPDJNZ R4,DEL4NOPDJNZ R3,DEL3RETNOPDAT: ;祝你平安db 26h,20h,20h,20h,20h,20h,26h,10h,20h,10h,20h,80h,26h,20h,30h,20h db 30h,20h,39h,10h,30h,10h,30h,80h,26h,20h,20h,20h,20h,20h,1ch,20h db 20h,80h,2bh,20h,26h,20h,20h,20h,2bh,10h,26h,10h,2bh,80h,26h,20h db 30h,20h,30h,20h,39h,10h,26h,10h,26h,60h,40h,10h,39h,10h,26h,20h db 30h,20h,30h,20h,39h,10h,26h,10h,26h,80h,26h,20h,2bh,10h,2bh,10h db 2bh,20h,30h,10h,39h,10h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,20h db 20h,10h,20h,10h,2bh,10h,26h,30h,30h,80h,18h,20h,18h,20h,26h,20h db 20h,20h,20h,40h,26h,20h,2bh,20h,30h,20h,30h,20h,1ch,20h,20h,20h db 20h,80h,1ch,20h,1ch,20h,1ch,20h,30h,20h,30h,60h,39h,10h,30h,10h db 20h,20h,2bh,10h,26h,10h,2bh,10h,26h,10h,26h,10h,2bh,10h,2bh,80h db 18h,20h,18h,20h,26h,20h,20h,20h,20h,60h,26h,10h,2bh,20h,30h,20h db 30h,20h,1ch,20h,20h,20h,20h,80h,26h,20h,30h,10h,30h,10h,30h,20h db 39h,20h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,10h,40h,10h,20h,10h db 20h,10h,2bh,10h,26h,30h,30h,80h,00HEND ;程序结束。

利用蜂鸣器和单片机演奏简单的音乐电路设计

利用蜂鸣器和单片机演奏简单的音乐电路设计

利用蜂鸣器和单片机演奏简单的音乐电路设计利用蜂鸣器和单片机演奏简单的音乐实验原理图实验程序/* =========================================================== *//* ----------------------------------------------------------- *//* 曲谱存贮格式 uchar code 数组名{音高,音长,音高,音长....} *//* 音高由三位数字组成: *//* 个位是表示 1~7 这七个音符. *//* 十位是表示音符所在的音区,1-低音,2-中音,3-高音; *//* 百位表示这个音符是否要升半音 0(不写)-不升,1-升半音。

*//* 音长最多由三位数字组成: *//* 个位表示音符的时值,其对应关系是: *//* 数值(n) 0 1 2 3 4 5 6 *//* --------------------------------------------- *//* 几分音符 1 2 4 8 16 32 64 *//* 即:音符=2^n ,这样做的目的是为了节省曲谱的存贮空间。

*//* 十位表示音符的演奏效果(0-2),0-普通,1-连音,2-顿音。

*//* 百位是符点位,0(不写)-无符点,1-有符点。

*//* ----------------------------------------------------------- */ /* 调用演奏子程序的方法为: *//* play(乐曲数组名,调号,升降八度,演奏速度,开始指针,结束指针) */ /* 调号(0-11)是指乐曲升多少个半音演奏;升降八度(1-3)是指在演奏 */ /* 在哪个八度演奏: 1-降八度,2-不升不降,3-升八度.开始指针(0- ) */ /* 是从哪个音符开始演奏,结束指针是演奏到哪个音符为止. *//* ----------------------------------------------------------- */ //本程序用T0 来产生音调,用T1 产生音长#include <reg51.h>#define uchar unsigned char#define yx 4/5 /* 定义普通音符演奏的长度分率 */#define plen 2 /* 定义晶振的时钟周期(us) */#define uchar unsigned char#define uint unsigned intsbit speaker=P3^5;/* ------------------下面是曲谱 ------------------------------ */uchar code sound[100]={25,2,23,3,25,3,31,1,26,2,31,3,26,3,25,1,25,2,21,3,22,3,23,2,22,3,21,3,22, 0,25,2,23,3,25,3,31,102,27,3,26,2,31,2,25,1,25,2,22,3,23,3,24,102,17,3, 21,0};uchar tc0,tc1,sc0,sc1; /* 音长和音符两个计数器初值暂存 */void play(sound,dh,sj,speed,point1,point2)uchar code sound[]; /* 接受乐曲数组的地址 */uchar speed,sj,dh; /* 速度、八度、调号 */uint point1,point2; /* 乐曲开始、结束指针 */{uint code fftab[12]={262,277,294,311,330,349,369,392,415,440,466,494}; /* 频率表*/uchar code stab[7]={0,2,4,5,7,9,11}; /* 1~7 在频率表中的位置 */uchar code ltab[7]={1,2,4,8,16,32,64};uchar tl,ts,sl,sm,sh,slen,xg,ii,fd;uint point,hz,tc,sc,len,len0,len1,len2,len4,i,ftab[12]; speaker=1;for(i=0;i<12;i++) /* 根据调号及升降八度来计算音符频率 */ {ii=i+dh;if(ii>11){ii=ii-12;ftab[i]=fftab[ii]*2;}elseftab[i]=fftab[ii];if(sj==1) ftab[i]>>=2;if(sj==3) ftab[i]<<=2;}point=point1;ts=sound[point];tl=sound[point+1]; /* 读出第一个音符和它时时值 */tc=65535-10000/plen; /* 算出10ms 的初装值 */tc0=tc%256; /* 计算TL1 应装入的初值 */tc1=tc/256; /* 计算TH1 应装入的初值 */len0=12000/speed; /* 算出1 分音符的长度(几个10ms) */ len4=len0/4; /* 算出4 分音符的长度 */len4=len4-len4*yx; /* 普通音最长间隔标准 */TMOD=0x11;TH1=tc1; TL1=tc0;ET0=1; EA=1;TR0=0; TR1=1;while(point<=point2){sl=ts%10; /* 计算出音符 */sh=ts/100; /* 计算出是否升半 */sm=ts/10%10; /* 计算出高低音 */hz=ftab[stab[sl-1]+sh]; /* 查出对应音符的频率 */if(sl!=0){if (sm==1) hz>>=2; /* 若是低音 */if (sm==3) hz<<=2; /* 若是高音 */sc=(50000/hz)*10/plen; /* 计算脉冲个数 */sc=65536-sc; /* 计算计数器初值 */sc0=sc%256; /* 算出TL0 应装初值 */sc1=sc/256; /* 算出TH0 应装初值 */TH0=sc1; /* 装入初值 */TL0=sc0+12; /* 加12 是对中断延时的补偿 */}slen=ltab[tl%10]; /* 算出是几分音符 */xg=tl/10%10; /* 算出音符类型(0 普通1 连音2 顿音) */fd=tl/100;len=len0/slen; /* 算出连音音符演奏的长度(多少个10ms)*/ if (fd==1) len=len+len/2;if(xg!=1)if(xg==0) /* 算出普通音符的演奏长度 */if (slen<=4)len1=len-len4;elselen1=len*yx;elselen1=len/2; /* 算出顿音的演奏长度 */elselen1=len;if(sl==0) len1=0;len2=len-len1; /* 算出不发音的长度 */if (sl!=0)TR0=1;for(i=len1;i>0;i--) /* 发规定长度的音 */{while(TF1==0);TH1=tc1; TL1=tc0;TF1=0;}}if(len2!=0){TR0=0; speaker=1;for(i=len2;i>0;i--) /* 音符间的间隔 */{while(TF1==0);TH1=tc1; TL1=tc0;TF1=0;}}point+=2; /* 音符指针下移 */ts=sound[point]; tl=sound[point+1]; /* 读出下一个音符和它时时值 */}void yin() interrupt 1 /* 音符发生程序(中断服务程序)*/ {speaker=~speaker;TH0=sc1; TL0=sc0;}//==============================================main(){while(1){play(sound,0,2,60,0,57);play(sound,0,1,60,0,57);play(sound,0,3,60,0,57);play(sound,0,2,40,0,57);play(sound,5,2,60,0,57);play(sound,0,2,80,0,57);}}。

基于单片机的蜂鸣器代码

基于单片机的蜂鸣器代码

#include <STC89C52.H>unsigned char Count;sbit bee_Speak = P3^7; //喇叭控制脚sbit key1 = P1^5;sbit key2 = P1^7;unsigned char kk=0;unsigned int k;bit flag;unsigned char code SONG[] ={ //天空之城0x1c,0x30,0x19,0x30,0x18,0x40,0x19,0x30,0x18,0x30,0x13,0x30,0x19,0x800x26,0x30,0x26,0x30,0x1c,0x40,0x20,0x30,0x1c,0x30,0x18,0x30,0x20,0x60,0x26,0x30,0x26,0x30,0x24,0x40,0x26,0x30,0x24,0x30,0x18,0x40,0x26,0x60,0x18,0x20,0x18,0x20,0x18,0x20,0x19,0x40,0x24,0x30,0x24,0x30,0x19,0x30,0x19,0x60,0x1c,0x30,0x19,0x30,0x18,0x40,0x19,0x30,0x18,0x30,0x13,0x30,0x19,0x60,0x26,0x30,0x26,0x30,0x1c,0x40,0x20,0x30,0x1c,0x30,0x18,0x30,0x20,0x80,0x26,0x30,0x24,0x30,0x24,0x30,0x18,0x30,0x19,0x20,0x19,0x30,0x18,0x30,0x15,0x20,0x15,0x20,0x13,0x20,0x18,0x40,0x18,0x30,0x19,0x30,0x1c,0x20,0x1c,0x20,0x19,0x30,0x20,0x30,0x1c,0x60,0x18,0x30,0x15,0x30,0x13,0x40,0x15,0x30,0x13,0x30,0x10,0x30,0x15,0x60,0x20,0x30,0x20,0x30,0x18,0x30,0x19,0x30,0x18,0x30,0x13,0x30,0x13,0x60,0x1c,0x20,0x19,0x20,0x18,0x30,0x19,0x30,0x15,0x20,0x15,0x20,0x18,0x40,0x20,0x30,0x20,0x30,0x12,0x30,0x13,0x30,0x15,0x30,0x18,0x30,0x13,0xc0,0x13,0x80,0x13,0x30,0x0e,0x80,0x10,0x30,0x10,0x30,0x13,0x20,0x15,0x20,0x18,0x40,0x18,0x30,0x15,0x30,0x18,0x20,0x15,0x20,0x15,0x30,0x10,0x30,0x13,0x60,0x13,0x30,0x0e,0x60,0x10,0x60,0x13,0x20,0x15,0x20,0x18,0x40,0x18,0x30,0x15,0x30,0x15,0x20,0x18,0x20,0x15,0x30,0x19,0x30,0x1c,0x60,0x1c,0x30,0x19,0x30,0x1c,0xc0,0x00,//祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x2 0,0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x2 0,0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x2 0,0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x2 0,0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x2 0,0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x2 0,0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x2 0,0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x1 0,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x8 0,0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x2 0,0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x2 0,0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x1 0,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,//路边的野花不要采0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40,0x1 C,0x20,0x20,0x20,0x1C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x2 0,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x2 0,0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x3 0,0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x3 0,0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x2 6,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1C,0x10,0x2 0,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15,0x1 F,0x05,0x20,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x2 0,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2 B,0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1C,0x10,0x18,0x40,0x1C,0x20,0x2 0,0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00,};void Time0_Init(){TMOD = 0x01;IE = 0x82;TH0 = 0xDC;TL0 = 0x00;}void Time0_Int() interrupt 1{TH0 = 0xDC;TL0 = 0x00;Count++; //长度加1}/*-------------------------------------------------功能:1MS延时子程序-------------------------------------------------*/void Delay_xMs(unsigned int x){unsigned int i,j;for( i =0;i < x;i++ ){ for( j =0;j<2;j++ );}}/*-------------------------------------------------功能:歌曲播放子程序i为播放哪一段曲目-------------------------------------------------*/void Play_Song(unsigned char i){unsigned char Temp1,Temp2;unsigned int Addr;Count = 0; //中断计数器清0Addr = i * 217;while(1){if(!key1){Delay_xMs(5);if(!key1){ kk++;if(kk>2)kk=0;}while(!key1);break;}else if(!key2){Delay_xMs(5);if(!key2){flag=~flag;}while(!key2);break;}if(flag){/* k=Addr;k++;if(k>=((i+1) * 217)){flag=0;Addr = i * 217;} */Temp1 = SONG[Addr++];if (Temp1 == 0xFF)//休止符{TR0 = 0;Delay_xMs(100);}else if (Temp1 == 0x00)//歌曲结束符{return;flag=0;}else{Temp2 = SONG[Addr++];TR0 = 1;while(key1){bee_Speak = ~bee_Speak;Delay_xMs(Temp1);if(Temp2 ==Count){Count = 0;break;}}}}}}/*------------------------------------------------- 功能:按键切换歌曲 -------------------------------------------------*/ /*unsigned charkey1_sacn(void)unsigned char k;if(key1==0) //菜单键按下{Delay_xMs(5);//防抖if(key1==0){k++;if(k>2) k=0;//k=0,1,2;}while(!key1); //等待按键弹起}return k;} *//*-------------------------------------------------功能:主程序-------------------------------------------------*/voidmain(){Time0_Init(); //定时器0中断初始化while(1){P2=kk;Play_Song(kk); //播放//0改作1可播放第二首歌曲}}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档