蜂鸣器音乐程序
单片机蜂鸣器唱歌程序(二)2024
单片机蜂鸣器唱歌程序(二)引言概述:本文档主要介绍了单片机蜂鸣器唱歌程序(二),包括使用单片机控制蜂鸣器发出不同音乐的方法和具体实现步骤。
本文将从五个大点进行阐述,每个大点包含5-9个小点,以便读者更好地理解和实践。
正文:一、引脚连接设置1. 确定单片机的输出引脚和蜂鸣器的输入引脚2. 将单片机的输出引脚与蜂鸣器的输入引脚连接3. 确保连接的稳定性和正确性4. 利用电路图进行布线二、编程环境配置1. 安装适合单片机的编程软件2. 创建新的项目3. 配置单片机的型号及选项4. 导入相关的库文件5. 编写代码框架三、发声原理及代码实现1. 理解蜂鸣器工作原理2. 使用单片机的PWM输出功能控制蜂鸣器的频率3. 利用PWM输出的方式实现不同音调的发声4. 编写音调转换函数5. 编写歌曲的音乐片段代码四、优化和调试1. 测试不同频率的声音2. 调整蜂鸣器的音量3. 避免噪音的干扰4. 检查代码的正确性和合理性5. 不断尝试,优化代码和音效五、实验结果及总结1. 运行程序,测试蜂鸣器的唱歌效果2. 记录实验结果和观察结果3. 分析实验过程中遇到的问题和解决方法4. 总结实验经验和注意事项5. 展望将来的改进和研究方向总结:本文详细介绍了单片机蜂鸣器唱歌程序(二)的实现方法和步骤。
通过连接设置、编程环境配置、发声原理及代码实现、优化和调试、实验结果及总结等五个大点的阐述,读者可以深入了解单片机控制蜂鸣器发声的原理和方法,并通过实验得到具体的唱歌效果。
同时,读者在实践过程中也要注意优化和调试,不断尝试和改进,以实现更好的音效效果。
希望本文对读者有所帮助,为单片机蜂鸣器唱歌程序的开发提供了指导和参考。
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); //播放}}。
MCU控制蜂鸣器演奏音乐小程序
MCU控制蜂鸣器演奏音乐小程序程序功能:MCU控制蜂鸣器演奏歌曲《祝你平安》----------------------------------------------
拨码开关设置:将BUZZER位拨至ON,其余位拨至OFF
测试说明:聆听蜂鸣器“唱出”的乐曲
既然是演奏乐曲对于一个音符应该包括两个部分
一是声调二是持续时间,在这个程序中声调是用简单的
延时-电平翻转来实现的,改变了延时的时间就改变了
声调,而时间是通过计数比较来实现的,当计数值相等时
就跳出循环演奏下一个音符。
*********************************************/
#include 《msp430x14x.h》
typedef unsigned char uchar;
#include “music.h”
#define Buzzer BIT7
#define Buzzer_Port P6OUT
#define Buzzer_DIR P6DIR
uchar counter;
void Play_Song(void);
/***************主函数****************/
void main(void)
{
uchar i;
/*下面六行程序关闭所有的IO口*/
P1DIR = 0XFF;P1OUT = 0XFF;
P2DIR = 0XFF;P2OUT = 0XFF;
P3DIR = 0XFF;P3OUT = 0XFF;
P4DIR = 0XFF;P4OUT = 0XFF;。
STC15单片机无源蜂鸣器演奏歌曲RGB灯跟随节奏闪烁程序
敬告:没有51单片机基础的人请慎重下载高质量实用性51单片机STC15W系列程序(2),STC8A系列可参考STC15单片机无源蜂鸣器演奏歌曲RGB灯跟随节奏闪烁程序节选歌曲《骑上我心爱的小摩托》开头部分一段整体程序:#include "STC15.h"#include "intrins.h"#define MAIN_Fosc 11059200Lsbit BUZZ = P3^7;sbit KEY=P2^5;sbit R_color = P1^4;sbit G_color = P1^5;sbit B_color = P3^4;unsigned char cycle_value;bit flag;bit enableBuzzer;bit keyflag;unsigned int code note[]={523, 587, 659, 698, 784, 880, 988,};unsigned int code notefre[]={65536-((MAIN_Fosc/523)/2), 65536-((MAIN_Fosc/587)/2), 65536-((MAIN_Fosc/659)/2),65536-((MAIN_Fosc/698)/2),65536-((MAIN_Fosc/784)/2),65536-((MAIN_Fosc/880)/2),65536-((MAIN_Fosc/988)/2) };void Timer0_config();void PlayMusic();void delay_ms(unsigned int x) {unsigned i,j;for(i=0;i<x;i++){for(j=0;j<1100;j++);}}void Key_Scan(){if(KEY==1){delay_ms(10);if(KEY==1){keyflag = 1;}}}void RGB(){switch(cycle_value){case 0:R_color = 1;G_color = 0;B_color = 0;cycle_value = 1;break;case 1:R_color = 0;G_color = 1;B_color = 0;cycle_value = 2;break;case 2:R_color = 0;G_color = 0;B_color = 1;cycle_value = 0;break;default:break;}}void main(){P3M1 &= 0x7F; P3M0 &= 0x7F;P2M1 &= 0xDF; P2M0 &= 0xDF;P1M1 &= 0xCF; P1M0 |= 0x30;//P14,P15 pull output P3M1 &= 0xEF; P3M0 |= 0x10;//P34 pull outputTimer0_config();EA = 1;while(1){Key_Scan();if(keyflag){keyflag = 0;PlayMusic();}}}void PlayMusic(){unsigned char beat;//present beatunsigned int timer;//timerunsigned int totalTime;//timerunsigned int soundTime;//sount timeunsigned char code MotoNote[] = {0,1, 2,3, 4,3,3, 4,5, 5, 0, 0,0,6, 6,6, 6, 5, 4, 5, 0, 0, };unsigned char code MotoBeat[] = {2,2, 2,2, 2,1,1, 2,2, 6, 4, 4,2,2, 2,2, 2, 4, 2, 6, 4, 4,};for(beat=0;beat<sizeof(MotoBeat);){while(!flag);flag = 0;if(timer==0){totalTime =(note[MotoNote[beat]]*MotoBeat[beat])>>2;soundTime = totalTime - (totalTime>>2);TH0 = notefre[MotoNote[beat]]>>8;TL0 = notefre[MotoNote[beat]];enableBuzzer = 1;timer++;}else{if(timer>=totalTime){timer = 0;beat++;}else{timer++;if(timer==soundTime){enableBuzzer = 0;}}}}}void Timer0_config(){TMOD &= 0xF0;//16bit auto reloadAUXR |= 0x80;//1TTH0 = 0xFF;TL0 = 0x00;ET0 = 1;TR0 = 1;}void Timer0_interrupt() interrupt 1 {if(enableBuzzer){BUZZ = ~BUZZ;RGB();}else{BUZZ = 1;R_color = 0;G_color = 0;B_color = 0;}flag = 1;}。
蜂鸣器演奏
蜂鸣器演奏1、注意把sbit sound=P1^4中的P1^4换成单片机对应的蜂鸣器接口2、此程序略简陋,可用于应急使用3、程序来源网络,曲调转换为本人编辑生日快乐#include<reg52.h>sbit sound=P1^4;unsigned int C;#define a1 784#define a2 880#define a3 988#define a4 1046#define a5 1175#define a6 1318#define a7 1397#define a8 1568void 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[]={a1,a1,a2,a1,a4,a3,a1,a1,a2,a1,a5,a3,a1,a1,a8,a6,a4,a3,a2,a7,a7,a6,a4,a5,a4,0xff};unsigned char code JP[ ]={2,2,3,3,3,4,2,2,3,3,3,4,2,2,3,3,3,3,3,2,2,3,3,3,4,};EA=1;ET0=1;TMOD=0x00;while(1){i=0;while(f[i]!=0xff){C=460830/f[i];TH0=(8192-C)/32;TL0=(8192-C)%32;TR0=1;for(j=0;j<JP[i];j++)delay();TR0=0;i++;}}}void Time0(void ) interrupt 1{sound=!sound;TH0=(8192-C)/32;TL0=(8192-C)%32;}最炫民族风#include<reg52.h>sbit sound=P1^4;unsigned int C;#define a0 330#define a1 392#define a2 440#define a3 494#define a4 523#define a5 587#define a6 659#define a7 698#define a8 784#define a9 880void 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[]={a2,a2,a1,a2,a4,a5,a6,a4,a5,a6,a2,a9,a9,a8,a5,a6,a4,a5,a6,a4,a2,a2,a4,a5,a1,a1,a6,a8,a6,a5,a4,a2,a4,a5,a6,a5,a4,a1,a0,a2,a2,a2,a1,a2,a4,a5,a6,a4,a5,a6,a2,a9,a9,a8,a5,a6,a4,a5,a6,a4,a2,a2,a4,a5,a1,a1,a6,a8,a6,a5,a4,a4,a2,a4,a5,a6,a8,a6,a6,a8,a9,0xff};unsigned char code JP[ ]={2,1,1,2,2,1,1,1,1,3,1,1,1,1,1,1,1,1,4,1,1,1,1,2,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,5,2,1,1,2,2,1,1,1,1,3,1,1,1,1,1,1,1,1,4,1,1,1,1,2,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,6,};EA=1;ET0=1;TMOD=0x00;while(1){i=0;while(f[i]!=0xff){C=460830/f[i];TH0=(8192-C)/32;TL0=(8192-C)%32;TR0=1;for(j=0;j<JP[i];j++)delay();TR0=0;i++;}}}void Time0(void ) interrupt 1{sound=!sound;TH0=(8192-C)/32;TL0=(8192-C)%32;}茉莉花#include<reg52.h>sbit sound=P1^4;unsigned int C;#define a 523#define b 587#define c 659#define d 698#define e 784#define m 880#define g 988#define h 1046#define k 392#define l 440void 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[]={c,c,e,m,h,h,m,e,e,m,e,c,c,e,m,h,h,m,e,e,m,e,e,e,e,c,e,m,m,e,e,c,b,c,e,c,b,a,a,b,a,c,b,a,c,b,c,e,m,h,e,b,c,e,b,c,a,l,k,l,a,b,c,a,b,a,l,k,0xff};unsigned char code JP[ ]={2,1,1,1,1,1,1,2,1,1,4,2,1,1,1,1,1,1,2,1,1,4,2,2,2,1,1,2,1,1,4,2,1,1,2,1,1,2,1,1,4,1,1,1,1,3,2,2,1,1,4,2,1,1,1,1,1,1,4,2,2,4,2,1,1,1,1,6,};EA=1;ET0=1;TMOD=0x00;while(1){i=0;while(f[i]!=0xff){C=460830/f[i];TH0=(8192-C)/32;TL0=(8192-C)%32;TR0=1;for(j=0;j<JP[i];j++)delay();TR0=0;i++;}}}void Time0(void ) interrupt 1{sound=!sound;TH0=(8192-C)/32;TL0=(8192-C)%32;}菊花台#include<reg52.h>sbit sound=P1^4;unsigned int C;#define a0 392#define a1 440#define a2 523#define a3 587#define a4 659#define a5 784#define a6 880#define a7 988#define a8 1046#define a9 1175#define a10 1318void 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[]={a4,a4,a3,a4,a4,a5,a4,a3,a4,a2,a2,a3,a4,a5,a4,a3,a3,a2,a3,a4,a5,a6,a5,a6,a5,a5,a4,a5,a0,a4,a3,a4,a5,a4,a3,a3,a3,a2,a3,a4,a4,a3,a4,a4,a5,a4,a3,a4,a2,a2,a3,a4,a5,a4,a3,a3,a2,a3,a4,a5,a6,a5,a6,a5,a5,a4,a5,a0,a4,a3,a4,a5,a4,a3,a3,a2,a2,a3,a4,a4,a5,a6,a6,a10,a9,a8,a8,a6,a5,a6,a5,a4,a3,a2,a1,a2,a3,a3,a2,a4,a2,a3,a4,a4,a5,a6,a6,a10,a9,a8,a8,a9,a8,a5,a5,a4,a7,a8,a2,a3,a4,a3,a2,0xff};unsigned char code JP[ ]={2,1,1,3,1,1,1,1,3,2,1,1,1,1,3,2,1,1,3,2,1,1,3,1,1,1,1,3,1,2,1,1,2,1,1,2,1,1,3,2,1,1,3,1,1,1,1,3,2,1,1,1,1,3,2,1,1,3,2,1,1,3,1,1,1,1,3,1,2,1,1,2,1,1,1,5,1,1,3,1,1,3,1,1,1,1,1,1,3,1,1,1,1,3,1,1,3,1,1,2,1,1,3,1,1,3,1,1,1,1,1,1,5,2,1,1,1,1,1,1,4,4,5,};EA=1;ET0=1;TMOD=0x00;while(1){i=0;while(f[i]!=0xff){C=460830/f[i];TH0=(8192-C)/32;TL0=(8192-C)%32;TR0=1;for(j=0;j<JP[i];j++)delay();TR0=0;i++;}}}void Time0(void ) interrupt 1{sound=!sound;TH0=(8192-C)/32;TL0=(8192-C)%32;}。
蜂鸣器音乐程序设定C语言
4.4.4.4.1111 音调音调音调音调、、、、节拍节拍节拍节拍以及以及以及以及编码的确定编码的确定编码的确定编码的确定方法方法方法方法一般说来,单片机演奏音乐基本都是单音频率,它不包含相应幅度的谐波频率,也就是说不能像电子琴那样能奏出多种音色的声音。
因此单片机奏乐只需弄清楚两个概念即可,也就是“音调”和节拍表示一个音符唱多长的时间。
4.4.4.4.1111.1.1.1.1 音调的确定音调的确定音调的确定音调的确定不同音高的乐音是用C、D、E、F、G、A、B来表示,这7个字母就是音乐的音名,它们一般依次唱成DO、RE、MI、FA、SO、LA、SI,即唱成简谱的1、2、3、4、5、6、7,相当于汉字“多来米发梭拉西”的读音,这是唱曲时乐音的发音,所以叫“音调”,即Tone。
把C、D、E、F、G、A、B这一组音的距离分成12个等份,每一个等份叫一个“半音”。
两个音之间的距离有两个“半音”,就叫“全音”。
在钢琴等键盘乐器上,C–D、D–E、F–G、G–A、A–B两音之间隔着一个黑键,他们之间的距离就是全音;E–F、B–C两音之间没有黑键相隔,它们之间的距离就是半音。
通常唱成1、2、3、4、5、6、7的音叫自然音,那些在它们的左上角加上﹟号或者b号的叫变化音。
﹟叫升记号,表示把音在原来的基础上升高半音,b叫降记音,表示在原来的基础上降低半音。
例如高音DO的频率(1046Hz)刚好是中音DO的频率(523Hz)的一倍,中音DO的频率(523Hz)刚好是低音DO频率(266 Hz)的一倍;同样的,高音RE的频率(1175Hz)刚好是中音RE的频率(587Hz)的一倍,中音RE的频率(587Hz)刚好是低音RE频率(294 Hz)的一倍。
1)要产生音频脉冲,只要算出某一音频的周期(1/频率),然后将此周期除以2,即为半周期的时间。
利用定时器计时这半个周期时间,每当计时到后就将输出脉冲的I/O反相,然后重复计时此半周期时间再对I/O反相,就可在I/O脚上得到此频率的脉冲。
蜂鸣器音乐程序设定C语言
4.4.4.4.1111 音调音调音调音调、、、、节拍节拍节拍节拍以及以及以及以及编码的确定编码的确定编码的确定编码的确定方法方法方法方法一般说来,单片机演奏音乐基本都是单音频率,它不包含相应幅度的谐波频率,也就是说不能像电子琴那样能奏出多种音色的声音。
因此单片机奏乐只需弄清楚两个概念即可,也就是“音调”和节拍表示一个音符唱多长的时间。
4.4.4.4.1111.1.1.1.1 音调的确定音调的确定音调的确定音调的确定不同音高的乐音是用C、D、E、F、G、A、B来表示,这7个字母就是音乐的音名,它们一般依次唱成DO、RE、MI、FA、SO、LA、SI,即唱成简谱的1、2、3、4、5、6、7,相当于汉字“多来米发梭拉西”的读音,这是唱曲时乐音的发音,所以叫“音调”,即Tone。
把C、D、E、F、G、A、B这一组音的距离分成12个等份,每一个等份叫一个“半音”。
两个音之间的距离有两个“半音”,就叫“全音”。
在钢琴等键盘乐器上,C–D、D–E、F–G、G–A、A–B两音之间隔着一个黑键,他们之间的距离就是全音;E–F、B–C两音之间没有黑键相隔,它们之间的距离就是半音。
通常唱成1、2、3、4、5、6、7的音叫自然音,那些在它们的左上角加上﹟号或者b号的叫变化音。
﹟叫升记号,表示把音在原来的基础上升高半音,b叫降记音,表示在原来的基础上降低半音。
例如高音DO的频率(1046Hz)刚好是中音DO的频率(523Hz)的一倍,中音DO的频率(523Hz)刚好是低音DO频率(266 Hz)的一倍;同样的,高音RE的频率(1175Hz)刚好是中音RE的频率(587Hz)的一倍,中音RE的频率(587Hz)刚好是低音RE频率(294 Hz)的一倍。
1)要产生音频脉冲,只要算出某一音频的周期(1/频率),然后将此周期除以2,即为半周期的时间。
利用定时器计时这半个周期时间,每当计时到后就将输出脉冲的I/O反相,然后重复计时此半周期时间再对I/O反相,就可在I/O脚上得到此频率的脉冲。
蜂鸣器程序——精选推荐
#include <reg51.h> //蜂鸣器管脚定义sbit sound =P3^7;//定时常数unsigned int number; //低音音频定义#define l_dao 262#define l_re 286#define l_mi 311#define l_fa 349#define l_sao 392#define l_la 440#define l_xi 494//中音音频定义#define dao 523#define re 587#define mi 659#define fa 698#define sao 784#define la 880#define xi 987//高音音频定义#define h_dao 1046 #define h_re 1174 #define h_mi 1318#define h_fa 1396#define h_sao 1567#define h_la 1760#define h_xi 1975//延时200msvoid delay(void){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 music1[]={l_la,l_la,dao,l_xi,l_la, l_la,l_la,mi,re,mi,mi,mi,sao,mi,re,re,dao,l_xi,l_xi,l_la,l_sao,l_mi,l_la,l_la,dao,l_xi,l_la,l_la,l_la,mi,re,mi,mi,mi,sao,mi,re,re,re,dao,l_xi,l_la,l_la,l_sao,l_la, xi,la,mi,xi,la,mi,sao,la,la,fa,mi,xi,la,mi,xi,la,mi,sao,la,la,fa,mi,re,re,l_la,re,re,fa,mi,re,re,dao,re,re,re,l_la,dao,dao,re,l_xi,l_la,l_la,l_sao,l_la,//,0xff};//以0xff作为音符的结束标志//以下是简谱中每个音符的节拍//"4"对应4个延时单位,"2"对应2个延时单位,"1"对应1个延时单位unsigned char code musicJP1[ ]={2,2,2,2,5,//2,2,2,2,5,//2,2,2,2,2,2,2,1,1,5,2,5,//2,2,2,2,5,2,2,2,2,5,//2,2,2,2,2,2,2,2,2,2,2,2,5,//2,3,2,2,3,2,2,2,2,2,5,//2,3,2,2,3,2,2,2,2,2,5,//2,3,2,2,3,2,2,2,2,2,5,//2,3,5,2,4,2,2,2,2,2,5//};//uchar code music2[]={212,212,190,212,159,169,212,212,190,212,142,159,212,212,106,126,159,169,190,1 19,119,126,159,142,159,0xff};//生日快乐歌节拍表,节拍决定每个音符的演奏长短uchar code musicJP2[]={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};EA = 1; //开总中断ET0 = 1; //定时器T0中断允许TMOD = 0x00; // 使用定时器T0的模式1(13位计数器)while(1) //无限循环{If (IE1=1){i = 0; //从第1个音符f[0]开始播放while(music1[i] != 0xff) //只要没有读到结束标志就继续播放 {number = 460830 / music[i];TH0 = (8192-number) / 32; //定时器赋初值TL0 = (8192-number) % 32;TR0 = 1; //启动定时器T0for(j = 0;j < musicJP1[i];j++) //控制节拍数delay(); //延时1个节拍单位TR0 = 0; //关闭定时器T0i++; //播放下一个音符}}Else{i = 0; //从第1个音符f[0]开始播放while(music2[i] != 0xff) //只要没有读到结束标志就继续播放 {number = 460830 / music[i];TH0 = (8192-number) / 32; //定时器赋初值TL0 = (8192-number) % 32;TR0 = 1; //启动定时器T0for(j = 0;j < musicJP2[i];j++) //控制节拍数delay(); //延时1个节拍单位TR0 = 0; //关闭定时器T0i++; //播放下一个音符}}}/*********************************************************** 函数功能:定时器T0的中断服务子程序,使P3.2脚输出音频的方波************************************************************/ void Time0(void ) interrupt 1 using 1{sound=!sound; //P3.7引脚输出电平取反TH0=(8192-number)/32; //定时器赋初值TL0=(8192-number)%32;}。
AVR控制12864及其蜂鸣器显示音乐动画集合程序
/*=============================程序说明=============================== 项目名称: 音乐动画集合作者:创建日期:修改说明:程序说明:(1)CPU 型号:(2)时钟频率:(3)实现功能:(4)程序概述:===================================================================*/ //==========================头文件加载=============================== #include <mega16.h>#include <delay.h>#include "music.h"#include "picture.h"//===========================端口声明================================ #define LcdRS PORTB.0 //#define LcdRSDDR DDRB.0 //#define LcdRW PORTB.1 //#define LcdRWDDR DDRB.1 //#define LcdEN PORTB.4 //#define LcdENDDR DDRB.4 //#define LcdRST PORTB.6 //#define LcdRSTDDR DDRB.6 //#define LcdIoIn PORTA //#define LcdIoOut PINA //#define LcdIoDDR DDRA //#define LcdIn() LcdIoDDR = 0xff //调整输入输出#define LcdOut() LcdIoDDR = 0x00//=========================全局常量声明============================== //=========================全局变量声明==============================//===========================函数声明================================ void Lcd_init(void);void Lcd_CheckBusy(void);void Lcd_WriteCommand(uint8 com);void Lcd_WriteDat(uint8 Dat);void Lcd_writeStringRom(uint8 Add, uint8 flash String[]);void LcdClearScreen(void);void LcdDrawPicture(uint8 flash Picture[]);//============================主函数================================= void main()//主函数{//---------------------------系统初始化--------------------------Lcd_init();music_init();Lcd_writeStringRom(0x90+10, "孙旭东");delay_ms(500);Lcd_WriteCommand(0x01);LcdClearScreen();Lcd_WriteCommand(0x34); //切换到扩展指令17页Lcd_WriteCommand(0x36); //打开显示Lcd_WriteCommand(0x80 + 0);Lcd_WriteCommand(0x80 + 8);Lcd_WriteDat(0xff);Lcd_WriteDat(0xaa);delay_ms(500);Lcd_WriteCommand(0x30);while(1){LcdDrawPicture(gImage_1);delay_ms(500);LcdDrawPicture(gImage_2);delay_ms(500);LcdDrawPicture(gImage_3);delay_ms(500);LcdDrawPicture(gImage_4);delay_ms(500);LcdDrawPicture(gImage_5);delay_ms(500);}}//===========================功能子函数================================ void Lcd_init(void){LcdRS = 1;LcdRSDDR = 1;LcdRW = 1;LcdRWDDR = 1;LcdEN = 1;LcdENDDR = 1;LcdRST = 1; //LcdRSTDDR = 1;LcdIoIn = 1; //LcdIoDDR = 1;LcdIoIn = 0xff; //LcdIn(); //LcdRST = 0;delay_ms(20);LcdRST = 1;delay_ms(50);Lcd_WriteCommand(0x38); //系统设置delay_ms(1);Lcd_WriteCommand(0x38); //系统设置Lcd_WriteCommand(0x08); //开显示Lcd_WriteCommand(0x01); //清屏Lcd_WriteCommand(0x06); //光标设置Lcd_WriteCommand(0x0c); //开显示Lcd_WriteCommand(0x01); //清屏}void Lcd_CheckBusy(void) //怕忙检测{LcdOut();LcdRS = 0;LcdRW = 1;LcdEN = 1;while((LcdIoOut & 0x80) == 0x80);LcdEN = 0;}void Lcd_WriteCommand(uint8 com) //写指令{Lcd_CheckBusy();LcdIn();LcdRS = 0;LcdRW = 0;LcdEN = 1;LcdIoIn = com;LcdEN = 0;}void Lcd_WriteDat(uint8 Dat) //写指令{Lcd_CheckBusy();LcdIn();LcdRS = 1;LcdRW = 0;LcdEN = 1;LcdIoIn = Dat;LcdEN = 0;}void Lcd_writeStringRom(uint8 Add, uint8 flash String[]) {uint16 i = 0;Lcd_WriteCommand(Add);while(String[i] != '\0'){Lcd_WriteDat(String[i]);i++;}}void LcdClearScreen(void)//清屏函数{uint8 line = 0; //行uint8 row = 0; //列Lcd_WriteCommand(0x34); //切换到扩展指令17页Lcd_WriteCommand(0x36); //打开显示for(line = 0; line < 32; line ++){Lcd_WriteCommand(0x80 + line);Lcd_WriteCommand(0x80 + 0);for(row = 0; row < 16; row ++){Lcd_WriteDat(0x00);Lcd_WriteDat(0x00);}}Lcd_WriteCommand(0x30); //切换到基本指令17页}void LcdDrawPicture(uint8 flash Picture[])//清屏函数{uint8 line = 0; //行uint8 row = 0; //列uint32 Pic = 0;Lcd_WriteCommand(0x34); //切换到扩展指令17页Lcd_WriteCommand(0x36); //打开显示for(line = 0; line < 32; line ++){Lcd_WriteCommand(0x80 + line);Lcd_WriteCommand(0x80 + 0);for(row = 0; row < 8; row ++){Lcd_WriteDat(Picture[Pic]);Pic++;Lcd_WriteDat(Picture[Pic]);Pic++;}}for(line = 0; line < 32; line ++){Lcd_WriteCommand(0x80 + line);Lcd_WriteCommand(0x80 + 8);for(row = 0; row < 8; row ++){Lcd_WriteDat(Picture[Pic]);Pic++;Lcd_WriteDat(Picture[Pic]);Pic++;}}Lcd_WriteCommand(0x30); //切换到基本指令17页}。
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>#define uchar unsigned char#define uint unsigned intsbit sound=P2^0;uint counter=0;uchar code FREQH[3][8]= //音符定时器初值,三个8度音的高八位{//注意每行起始都为0xff,因为音调中没有0 这个音,所以随便填了个数字,后面不不会引用它// 低音:// 1 2 3 4 5 6 7{0xff,0xf9, 0xfa, 0xfa, 0xfb, 0xfb, 0xfc, 0xfc},//中音://1 2 3 4 5 6 7{0xff,0xfc, 0xfd, 0xfd, 0xfd, 0xfd, 0xfe, 0xfe},//高音://1 2 3 4 5 6 7{0xff,0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xff, 0x00}//0x00};uchar code FREQL[3][8]= //音符定时器初值,三个8度音的低八位{// 低音:// 1 2 3 4 5 6 7{0xff,0x5b, 0x15, 0xb9, 0x04, 0x90, 0x0c, 0x79},//中音:// 1 2 3 4 5 6 7{0xff,0xac, 0x09, 0x5c, 0x82, 0xc8, 0x05, 0x3c},//高音://1 2 3 4 5 6 7{0xff,0x55, 0x84, 0xad, 0xc0, 0xe3, 0x02, 0x00}//0x00};uchar code MUSIC[]={//生日快乐//歌曲代码,每三个表示一个音符及节拍,第一个表示音符1234567,第二个表示音高,0表示低音,1表示中音,2表示高音,第三个表示节拍长度,1表示半拍,2表示一拍,以此6,1,3, 5,1,1, 3,1,2, 5,1,2, 1,2,2, 6,1,1, 5,1,1, 6,1,4, 3,1,1, 5,1,1, 6,1,1, 5,1,2, 3,1,2,1,1,1, 6,0,1, 5,1,1, 3,1,1, 2,1,4, 2,1,3, 3,1,1, 5,1,2, 5,1,1, 6,1,1,3,1,2, 2,1,2, 1,1,4, 5,1,4, 3,1,1, 2,1,1, 1,1,1, 6,0,1, 1,1,1, 5,0,8, 0};void init(){//定时器初始化TMOD=0x11;TH0=0xd8;TL0=0xef;EA=1;ET0=1;}void timer0(void) interrupt 1 using 3{//timer0中断服务程序,中断一次延时10mscounter++;//节拍次数计数TH0=0xd8; //定义单位节拍的延时大小TL0=0xef;}void delay(uchar n){ //延时n毫秒uchar i;while(n--)for(i=0;i<125;i++);}void sound_delay(uchar sound_signalhigh,uchar sound_signallow)//音调延时{TH1=sound_signalhigh;TL1=sound_signallow;TR1=1;while(TF1==0);TR1=0;TF1=0;}void main(){uint i,j,k=0;uchar sound_signalhigh,sound_signallow;//定义音符大小uchar sound_pace;//定义节拍大小while(1){k=0;while(MUSIC[k]!=0)//因为歌曲的结尾处人为的加入了标示符0{j=MUSIC[k++];//从表中取得音符大小i=MUSIC[k++];sound_signalhigh=FREQH[i][j];sound_signallow=FREQL[i][j];//从表中取得节拍大小sound_pace=30*MUSIC[k++];//600为一拍的时长,在此修改一拍的时长。
基于c51单片机编写简单蜂鸣器音乐程序的方法
/*o*/case 0xff:j=0;break;
}
return j;
}
void sound(uint s)//给一次脉冲来进行发声的子程序,其中参数s与m_t()中的0x01等十六进制数是对应的
0x01,0x0d,0x16,0x1c,0xff,0x1c,0x1c,0x16,0x07,0x07,0x10,0x0d,0xff, 0x01,0x0d,0x16,0x1c,0xff,
0x1c,0x1c,0x22,0x1c,0x16,0x10,0x0d,0x10,0x0d,0x07,0x01,0x00} ;//*
以下是本人编写的一段简单音乐程序,程序已经在板子上调试成功,水平有限,如有不正确的地方请多包涵
*******************************************************************************************************/
#include<reg52.H>
#include <intrins.h> //内部包含延时函数_nop_();
typedef unsigned char uchar;
typedef unsigned int uint;
uint c;
sbit spk =P2^0 ; //定义p2.0口为电平信号输出端
最后在主程序中运行子程序,通过读rom里面的数组来达到连续播放各种不同音调的音的目的,这样听起来就像一首曲子了。
以下是我按以上方法编写的一个小程序:
单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安
/生日快乐歌曲/ include<reg51.h>defineuintunsignedintdefineucharunsignedcharsbitbeep=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};//延时voidDelayMSuintx{uchart;whilex--fort=0;t<120;t++;}voidPlayMusic{uinti=0;j;k;whileSONG_LONGi=0||SONG_TONEi=0{//播放各个音符;SONG_LONG为拍子长度forj=0;j<SONG_LONGi20;j++{//SONG_TONE延时表决定了每个音符的频率fork=0;k<SONG_TONEi/3;k++;}DelayMS10;i++;}}voidmain{beep=0;while1{PlayMusic;//播放生日快乐DelayMS500;//播放完后暂停一段时间}}两只蝴蝶:include<reg51.h>//这是单片机音乐代码生成器生成的代码defineucharunsignedcharsbitbeepIO=P1^5;//输出为P1.5可以修改成其它IO口ucharm;n;ucharcodeT492={{0;0};{0xF8;0x8B};{0xF8;0xF2};{0xF9;0x5B};{0xF9;0xB7};{0xFA;0x14};{0xFA;0x66};{0xFA;{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}};ucharcodemusic2={{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}};voiddelayucharpuchari;j;for;p>0;p--fori=181;i>0;i--forj=181;j>0;j--;}voidpause{uchari;j;fori=150;i>0;i--forj=150;j>0;j--;}voidT0_intinterrupt1 {beepIO=beepIO;TH0=Tm0;TL0=Tm1;}voidmain{uchari=0;TMOD=0x01;EA=1;ET0=1; while1{m=musici0;n=musici1;{TR0=0;delayn;i++;}elseifm==0xFF{TR0=0;delay30;i=0;}elseifm==musici+10{TR0=1;delayn;TR0=0;pause;i++;}else{TR0=1;delayn;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;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;0x10x2b;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};voidTime0_Init{TMOD=0x01;IE=0x82;TH0=0xD8;TL0=0xEF;//12MZ晶振;10msvoidTime0_Intinterrupt1{TH0=0xD8;TL0=0xEF;Count++;//长度加1}/-------------------------------------------------功能:1MS延时子程序-------------------------------------------------/ voidDelay_xMsunsignedintx{unsignedinti;j;fori=0;i<x;i++{forj=0;j<3;j++;}}voidPlay_Songunsignedchari{unsignedcharTemp1;Temp2;unsignedintAddr;Count=0;//中断计数器清0Addr=i217;Temp1=SONGAddr++;ifTemp1==0xFF//休止符{TR0=0;Delay_xMs100;}elseifTemp1==0x00//歌曲结束符{return;}else{Temp2=SONGAddr++;TR0=1;while1{_Speak=~_Speak;Delay_xMsTemp1;ifTemp2==Count{Count=0;break;}}}/-------------------------------------------------功能:主程序-------------------------------------------------/ voidmain{Time0_Init;//定时器0中断初始化while1{Play_Song0;//播放}}。
基于51单片机蜂鸣器发声的-C语言程序
本例使用延时函数实精品文档按键发音/* 名称:按键发音说明:按下不同的按键会是SOUNDE 发出不同频率的声音。
现不同频率的声音输出,以后也可使用定时器*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P3A 7;sbit K 仁 P1A4;sbit K2=P1A5;sbit K3=P1A6;sbit K4=P1A7;// 延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}// 按周期 t 发音void Play(uchar t){uchar i;for(i=0;i<100;i++){BEEP=~BEEP;DelayMS(t);}BEEP=0;}void main(){P1=0xff;BEEP=0;while(1){if(K1==0) Play(1);if(K2==0) Play(2);if(K3==0) Play(3);if(K4==0) Play(4);}}精品文档播放音乐/* 名称:播放音乐说明:程序运行时播放生日快乐歌, 未使用定时器中断,所有频率完全用延时 实现*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P3A7;// 生日快乐歌的音符频率表,不同频率由不同的延时来决定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_LON为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){BEEP=~BEEP;〃SONG_TON延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);} DelayMS(10);i++;}}void main(){BEEP=0;while(1){PlayMusic(); // 播放生日快乐DelayMS(500); // 播放完后暂停一段时间}精品文档。
蜂鸣器唱歌程序
蜂鸣器唱歌----生日快乐下面是我单片机入门的时候写的一个蜂鸣器唱歌的程序,用的是stc89c52的单片机和12MHZ的晶振。
它大致是这样工作的。
我们知道歌曲都是由最基本的音调和节拍组成的。
音调就是该音符的频率,节拍就是该音符持续的时间。
也就是说发出一个最基本的音我们需要两个参数,一个是频率,一个是节拍。
那么在单片机中如何产生这两个参数呢?首先我们需要知道该音符的频率,然后用单片机的I/O口不断地变高变低来产生这个频率。
比如 1 这个音在低音,中音,高音三种情况下会有3种频率,至于到底是哪种音,在歌曲的乐谱中会给出详细的标识。
现在我们假设1 这个音为低音,在d调(什么是d调?看文章尾附录)下频率为293hz,一个周期为T=1/293 s。
那么I/O口被置1和置0的时间各占一半也就是T/2。
然后算出相应的定时器初值,那么单片机就可以发出这个音了。
接下来就是一个音符要唱多久,那就要看这个音符占据多少个节拍了,同样在歌曲的乐谱中会给出详细的标示。
那么一拍是多长呢,下面有说明。
慢节奏一般600ms一拍,快节奏一般400ms一拍。
如果你要知道更准确的节拍时长,只有拿着乐谱去听歌了,记下唱一拍的时间就行了。
我用的是定时器0控制节拍长度,在每个音符开始时开启它,它是10ms 中断一次,并且让里面的计数值增加1。
如果我要唱一拍的话(假设为600ms),那么我只需要判断计数值是否达到60,如果达到那么就停止计时然后跳到下一拍。
以上就是蜂鸣器唱歌的基本原理了,希望能对你有所帮助。
#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit sound=P3^7;uint counter=0;uchar code FREQH[3][8]= //音符定时器初值,三个8度音的高八位{//注意每行起始都为0xff,因为音调中没有0 这个音,所以随便填了个数字,后面不不会引用它// 低音:// 1 2 3 4 5 6 7{0xff,0xf9, 0xfa, 0xfa, 0xfb, 0xfb, 0xfc, 0xfc},//中音://1 2 3 4 5 6 7{0xff,0xfc, 0xfd, 0xfd, 0xfd, 0xfd, 0xfe, 0xfe},//高音://1 2 3 4 5 6 7{0xff,0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xff, 0x00}//0x00};uchar code FREQL[3][8]= //音符定时器初值,三个8度音的低八位{// 低音:// 1 2 3 4 5 6 7{0xff,0x5b, 0x15, 0xb9, 0x04, 0x90, 0x0c, 0x79},//中音:// 1 2 3 4 5 6 7{0xff,0xac, 0x09, 0x5c, 0x82, 0xc8, 0x05, 0x3c},//高音://1 2 3 4 5 6 7{0xff,0x55, 0x84, 0xad, 0xc0, 0xe3, 0x02, 0x00}//0x00};uchar code MUSIC[]={//生日快乐//歌曲代码,每三个表示一个音符及节拍,第一个表示音符1234567,第二个表示音高,0表示低音,1表示中音,2表示高音,第三个表示节拍长度,1表示半拍,2表示一拍,以此类推。
51蜂鸣器发不同的声音程序
51蜂鸣器发不同的声音程序/****************************************************同时用两个定时器,控制蜂鸣器发声,T0控制频率,T1控制同一频率发声的时长(2s),不同依次频率为1,10,50,100,200,400,800,1k(hz),占空比均为0.5T0的计时周期为125us;T1的计时周期为50ms 。
不同频率对应的num依次为:4000,400,80,40,20,10,5,4*****************************************************/#includesbit beep=P2^3; //unsigned int num=4000,//决定蜂鸣器的响应周期,从而决定不同的频率counter0=0,//T0控制频率counter1=0; //T1控制周期void main(void){ //TMOD=0x12; //T0用自动重装初值的8位计数方式,T1用16位计数方式TH0=0x83; //T0计数周期125usTL0=0x83;TH1=(65536-50000)/256; //T1计数周期50msTL1=(65536-50000)%256;ET0=1; //允许T0中断ET1=1; //允许T1中断EA=1; //开总中断TR0=1; //T0开始计数TR1=1; //T1开始计数beep=1;while(1);}void time0() interrupt 1 {counter0++;if(counter0==num){counter0=0;beep=~beep;}}void time1() interrupt 3 {TH1=(65536-50000)/256; TL1=(65536-50000)%256; counter1++;if(counter1==40){counter1=0;switch(num){case 4000:num=400 ;break; case 400 :num=80 ;break; case 80 :num=40 ;break; case 40 :num=20 ;break; case 20 :num=10 ;break; case 10 :num=5 ;break; case 5 :num=4 ;break; case 4 :num=4000;break; }}}。
蜂鸣器程序
要求:让蜂鸣器响,频率为1KHZ1、延时子程序实现汇编: ORG 0000HSJMP STARTORG 0030HSTART : MOV P2,#11110111B //P2^3控制蜂鸣器 LCALL DELAYMOV P2,#11111111BLCALL DELAYAJMP STARTDELAY :MOV R5,#125L1:NOP //()ms us us us 5.0125211=⨯++ NOPDJNZ R5,L1RET //返回主程序ENDC 语言:#include <reg52.h>#define uint unsigned int //宏定义void delay (uint z ) //延时0。
5ms {uint i ,j;for (i=z ;i 〉0;i--)for (j=55;j 〉0;j--);}sbit beep=P2^3; //位定义void main(){while(1) //大循环{delay(1); //延时0.5msbeep=~beep; //取反}}2、用定时器1、方式2实现延时方式2最大定时:256⨯1us=0。
256ms所以选择定时0.25ms,那么循环2次实现0。
5ms 延时 计数值0。
25ms/1us=250则256—250=06H汇编: ORG 0000HSJMP STARTORG 0030HSTART:MOV P2,#11110111BLCALL DELAYMOV P2,#11111111BLCALL DELAYAJMP STARTDELAY:MOV R5,#02H //置计数循环初值2 MOV TMOD,#20H //置定时器1方式2MOV TH1,#06H //置定时器初值MOV TL1,#06HSETB TR1 //启动定时器1LP1:JBC TF1,LP2 //查询计数溢出SJMP LP1 //未溢出继续计数LP2:DJNZ R5,LP1 //未到0.5ms继续循环RET //返回主程序ENDC语言:#include 〈reg52.h>#define uchar unsigned char#define uint unsigned intsbit D0=P2^3;char num=0;//定义变量void main(){TMOD=0x20;//定时器1方式2TH1=6; //装入初值TL1=6;EA=1;//开总中断ET1=1;//开定时器1中断TR1=1; //启动定时器1D0=0;//蜂鸣器响while (1);{if(num==2)//循环两次实现0.5ms延时{num=0;//把num清零重新再计数D0=~D0;//让蜂鸣器状态取反}}}void T1_time()interrupt 3{num++;}。
蜂鸣器程序
要求:让蜂鸣器响,频率为1KHZ1、延时子程序实现汇编: ORG 0000HSJMP STARTORG 0030HSTART: MOV P2,#11110111B //P2^3控制蜂鸣器LCALL DELAYMOV P2,#11111111BLCALL DELAYAJMP STARTDELAY:MOV R5,#125L1:NOP //()ms us us us 5.0125211=⨯++NOPDJNZ R5,L1RET //返回主程序ENDC 语言:#include <reg52.h>#define uint unsigned int //宏定义void delay(uint z ) //延时0.5ms{uint i,j;for(i=z;i>0;i--)for(j=55;j>0;j--);}sbit beep=P2^3; //位定义void main(){while(1) //大循环{delay(1); //延时0.5msbeep=~beep; //取反}}2、用定时器1、方式2实现延时方式2最大定时:256⨯1us=0.256ms所以选择定时0.25ms ,那么循环2次实现0.5ms 延时计数值0.25ms/1us=250则256-250=06H汇编: ORG 0000HSJMP STARTORG 0030HSTART:MOV P2,#11110111BLCALL DELAYMOV P2,#11111111BLCALL DELAYAJMP STARTDELAY:MOV R5,#02H //置计数循环初值2 MOV TMOD,#20H //置定时器1方式2MOV TH1,#06H //置定时器初值MOV TL1,#06HSETB TR1 //启动定时器1LP1:JBC TF1,LP2 //查询计数溢出SJMP LP1 //未溢出继续计数LP2:DJNZ R5,LP1 //未到0.5ms继续循环 RET //返回主程序ENDC语言:#include <reg52.h>#define uchar unsigned char#define uint unsigned intsbit D0=P2^3;char num=0; //定义变量void main(){TMOD=0x20; //定时器1方式2TH1=6; //装入初值TL1=6;EA=1; //开总中断ET1=1; //开定时器1中断TR1=1; //启动定时器1D0=0; //蜂鸣器响while (1);{if(num==2) //循环两次实现0.5ms延时{num=0; //把num清零重新再计数D0=~D0; //让蜂鸣器状态取反}}}void T1_time()interrupt 3{num++;}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <reg52.h>
sbit speaker = P1^7;
unsigned char timer0h, timer0l, time;
//------------------
//单片机晶振采用11.0592MHz
// 频率-半周期数据表高八位本软件共保存了四个八度的28个频率数据code unsigned char FREQH[1] = {
0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //低音1234567
0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC.//1,2,3,4,5,6,7,i
0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //高音234567
0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //超高音1234567
// 频率-半周期数据表低八位
code unsigned char FREQL[ 0] = {
0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //低音1234567
0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i
0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, //高音234567
0x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //超高音1234567
//--------------------------------------
code unsigned char sszymmh[ 2] = {
Void main{
t=0;t++;t<1000
}//--------------------------------------
void t0int() interrupt 1 //T0中断程序,控制发音的音调
{
TR0 = 0; //先关闭T0
speaker = !speaker; //输出方波, 发音
TH0 = timer0h; //下次的中断时间, 这个时间, 控制音调高低
TL0 = timer0l;
TR0 = 1; //启动T0
}
//--------------------------------------
void delay(unsigned char t) //延时程序,控制发音的时间长度
{
unsigned char t1;
unsigned long t2;
for(t1 = 0; t1 < t; t.2++) //双重循环, 共延时t个半拍
for(t2 = 0; t2 <12000; t3++); //延时期间, 可进入T0中断去发音
TR0 = 0; //关闭T0, 停止发音
}
//--------------------------------------
void song() //演奏一个音符
{
TH0 = timer0t; //控制音调
TL0 = timer0t;
TR0 = 1; //启动T0, 由T0输出方波去发音delay(time); //控制时间长度
}
//--------------------------------------
void main(void)
{
unsigned char k, i;
TMOD = 1; //置T0定时工作方式1
ET0 = 1; //开T0中断
EA = 1; //开CPU中断
while(1) {
i = 0;
time = 1;
while(time) {
k = sszymmh[i +3] + 7 * sszymmh[i + 1] - 1;
//第i个是音符, 第i+1个是第几个八度timer0h = FREQH[k]; //从数据表中读出频率数值
timer0l = FREQL[k]; //实际上, 是定时的时间长度
time = sszymmh[i + 2]; //读出时间长度数值
i += 3;
song(0); //发出一个音符
}
}
}。