单片机蜂鸣器奏乐实验大全代码
蜂鸣器歌唱原理以及代码
3.3 蜂鸣器播放歌曲原理一般说来,单片机演奏音乐基本都是单音频率,它不包含相应幅度的谐波频率。
因此单片机奏乐只需弄清楚两个概念即可,也就是“音调”和“节拍”。
音调表示一个音符唱多高的频率,节拍表示一个音符唱多长的时间。
1)音调的确定音调就是我们常说的音高。
它是由频率来确定的!我们可以查出各个音符所对应的相应的频率,那么现在就需要我们来用51来发出相应频率的声音!我们常采用的方法就是通过单片机的定时器定时中断,将单片机上对应蜂鸣器的I/O口来回取反,或者说来回清零,置位,从而让蜂鸣器发出声音,为了让单片机发出不同频率的声音,我们只需将定时器予置不同的定时值就可实现。
2)节拍的确定一般说来,如果乐曲没有特殊说明,一拍的时长大约为400—500ms 。
3.3 蜂鸣器播放歌曲程序#include <reg52.h>sbit speaker = P1^5; //定义蜂鸣器端口unsigned char timer0h, timer0l, time;//--------------------------------------//单片机晶振采用11.0592MHz// 频率-半周期数据表高八位本软件共保存了四个八度的28个频率数据code unsigned char FREQH[] = {0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //低音12345670xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i 0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //高音 2345670xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //超高音 1234567// 频率-半周期数据表低八位code unsigned char FREQL[] = {0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //低音12345670x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i 0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, //高音 2345670x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //超高音 1234567//--------------------------------------//世上只有妈妈好数据表要想演奏不同的乐曲, 只需要修改这个数据表code unsigned char sszymmh[] = {6, 2, 3, 5, 2, 1, 3, 2, 2, 5, 2, 2, 1, 3, 2, 6, 2, 1, 5, 2, 1, //一个音符有三个数字。
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单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)精编版
/*生日快乐歌曲*/#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单片机蜂鸣器播放单音节音乐
0xFD,0x23,0x7F, 0xFC,0xAC,0x7F, 0xFD,0x23,0xFF, 0xFC,0x44,0xFF,
// 1_ 3_ 2 .5
0xFC,0x44,0x7F, 0xFD,0x23,0x7F, 0xFC,0xAC,0xFF, 0xFA,0x68,0xFF,
{
for (i =0; i < 363; i++)
{;}
}
}
*名称:Init()
*功能:设置计数器0工作方式,16位计数,溢出中断方式
**********************************************************************************/
void Init()
{
TMOD = 0x01; //定时器0处于计时方式,16位
*名称:Count1(void) interrupt 1
*功能:设置计时器0溢出中断,每中断一次改变P2_3引脚电平
*********************************************************************************/
void Count1(void) interrupt 1
void main()
{
uchar time;
Init();
TH0 = high;
TL0 = low;
while (1)
{
if (music[ptr] != 0xFF && music[ptr] != 0x00)//判断是否是正常音符
{
TR0 = 0;
P2_3 = 1;
51单片机用蜂鸣器演奏卡农
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:
单片机蜂鸣器音乐
单片机蜂鸣器音乐单片机在我们的生活中无处不在,它被广泛地应用在各种电子产品中,为我们的生活带来了便利。
今天,我要向大家介绍的是一种基于单片机的蜂鸣器音乐播放器。
一、硬件部分1、单片机:我们选用的是AT89C51单片机,它具有低功耗、高性能的特点,非常适合用于音乐播放器。
2、蜂鸣器:蜂鸣器是用来发出声音的,我们将其连接在单片机的输出口上。
3、存储芯片:为了能够播放存储在芯片中的音乐,我们需要将音乐以某种格式存储在芯片中。
常用的存储芯片有EEPROM和Flash芯片。
4、按键:为了能够选择播放不同的音乐,我们需要添加一个按键。
二、软件部分1、音乐编码:我们需要将音乐转换成二进制编码,这样才能被单片机读取并播放。
常用的音乐编码格式有MIDI、WAV等。
2、音乐播放:当按下按键时,单片机读取存储芯片中的音乐数据,并通过蜂鸣器播放。
3、音乐选择:通过按键可以选择不同的音乐进行播放。
4、音量控制:我们可以通过编程来控制蜂鸣器的音量大小。
三、调试与测试1、硬件调试:检查连接是否正确,确保没有短路或断路的情况。
2、软件调试:将程序下载到单片机中进行调试,确保能够正常播放音乐。
3、综合测试:将所有硬件和软件都连接起来进行测试,确保能够正常工作。
四、总结与展望通过本次实验,我们成功地制作了一个基于单片机的蜂鸣器音乐播放器。
它具有简单、实用的特点,可以用来播放存储在芯片中的音乐。
未来,我们可以进一步扩展其功能,例如添加更多的按键来选择不同的音乐、添加显示屏来显示歌曲名称等。
我们也可以将其应用到其他领域,例如智能家居、智能安防等。
单片机蜂鸣器唱歌程序在许多应用中,单片机蜂鸣器经常被用来发出声音或音乐。
下面是一个使用单片机蜂鸣器唱歌的程序示例。
我们需要确定单片机和蜂鸣器的连接方式。
通常,单片机具有一个内置的蜂鸣器输出引脚,可以将蜂鸣器连接到这个引脚上。
在以下的示例中,我们将假设单片机具有一个内置蜂鸣器输出引脚,并将其连接到P1.0端口上。
单片机控制蜂鸣器鸣奏音乐
单片机控制蜂鸣器鸣奏音乐——中北大学:马政贵首先介绍蜂鸣器的发声原理。
我们都知道,音调和音调的时长是音符的主要特征,通过产生不同的音调和音调的时长可以奏出不同的音符来。
然后一个个音符串联在一起就可以产生美妙的音乐来了。
音调主要由声音的频率决定,通过单片机给蜂鸣器不同的音频脉冲来产生不同的音调。
要产生音频脉冲,只要算出某一音频的周期(周期=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单片机蜂鸣器播放音乐代码
/*生日快乐歌曲*/#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); //播放}}。
蜂鸣器唱两只老虎单片机程序
#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位的赋初值方法}。
单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安
单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安/*生日快乐歌曲*/#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);//播放}}。
单片机课程设计报告(利用蜂鸣器播放音乐)
课程设计:嵌入式系统应用题目名称:利用蜂鸣器实现音乐播放功能姓名:学号:班级:完成时间:1设计的任务设计内容:动手焊接一个51单片机设计目标:利用单片机上的蜂鸣器实现音乐播放功能2 设计的过程2.1 基本结构1.STC89C52RC在本次的试验中采用了STC89C52RC单片机.STC89C52RC单片机是宏晶科技推出的新一代高速/低功耗/超强抗干扰的单片机.指令代码完全兼容传统8051单片机.12时钟/机器周期.工作电压:5.5V~3.3V(5V单片机)/3.8V~2.0V(3V单片机).工作频率范围:0~40MHz.相当于普通8051的0~80MHz.实际工作频率可达48MHz.用户应用程序空间为8K字节。
(STC89C52RC引脚图)STC89C52RC单片机的工作模式:(1)典型功耗<0.1μA,可由外部中断唤醒.中断返回后.继续执行原程序(2)空闲模式:典型功耗2mA(3)正常工作模式:典型功耗4Ma~7mA(4)唤醒.适用于水表、气表等电池供电系统及便携设备2.蜂鸣器及其工作原理:蜂鸣器按其结构分主要分为压电式蜂鸣器和电磁式蜂鸣器两种类型。
电磁式蜂鸣器由振荡器、电磁线圈、磁铁、振动膜片及外壳等组成。
接通电源后.振荡器产生的音频信号电流通过电磁线圈.使电磁线圈产生磁场.振动膜片在电磁线圈和磁铁的相互作用下.周期性地振动发声。
本实验采用的是电磁式蜂鸣器。
蜂鸣器按其是否带有信号源又分为有源和无源两种类型。
有源蜂鸣器只需要在其供电端加上额定直流电压.其内部的震荡器就可以产生固定频率的信号.驱动蜂鸣器发出声音。
无源蜂鸣器可以理解成与喇叭一样.需要在其供电端上加上高低不断变化的电信号才可以驱动发出声音。
本实验采用的是有源蜂鸣器。
(蜂鸣器与单片机连接电路图)2.2 软件设计过程1.蜂鸣器发声原理本实验由于采用有源蜂鸣器.只需将引脚端口P3^4清零.蜂鸣器即可发声;P3^4置位.蜂鸣器停止发声。
蜂鸣器演奏祝你平安——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);}}。
单片机汇编程序代码(闪烁灯、流水灯、蜂鸣器、点阵)
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
;TP0: DB 00H,3CH,42H,42H,42H,42H,3CH,00H ;圆形
;TP2: DB 07Fh,0BFh,0DFh,0EFh,0F7h,0FBh,0FDh,0FEh
END
MOV 20H,#00H ;中断计数器清0
MOV B,#00H ;表序号清0
MUSIC1:
NOP
CLR A
MOVC A,@A+DPTR ;查表取代码
JZ END0 ;是00H,则结束
CJNE A,#0FFH,MUSIC5
LJMP MUSIC3
MUSIC5:
NOP
MOV R6,A
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
一、闪烁灯
ORG 0000H
AJMP MAIN
ORG 0030H
蜂鸣器和弦音实现基于瑞萨单片机实现电路+代码
蜂鸣器和弦电路以下是基于瑞萨单片机实现代码:/****************************************************************************** **文件: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;}。