51单片机蜂鸣器奏乐实验汇编代码

合集下载

单片机实验报告蜂鸣器

单片机实验报告蜂鸣器

一、实验目的1. 熟悉51单片机的基本结构和工作原理。

2. 掌握51单片机的I/O口编程方法。

3. 学习蜂鸣器的驱动原理和应用。

4. 通过实验,提高动手实践能力和问题解决能力。

二、实验原理蜂鸣器是一种将电信号转换为声音信号的器件,常用于产生按键音、报警音等提示信号。

根据驱动方式,蜂鸣器可分为有源蜂鸣器和无源蜂鸣器。

1. 有源蜂鸣器:内部自带振荡源,将正负极接上直流电压即可持续发声,频率固定。

2. 无源蜂鸣器:内部不带振荡源,需要控制器提供振荡脉冲才能发声,调整提供振荡脉冲的频率,可发出不同频率的声音。

在本次实验中,我们使用的是无源蜂鸣器。

51单片机通过控制P1.5端口的电平,产生周期性的方波信号,驱动蜂鸣器发声。

三、实验器材1. 51单片机实验板2. 蜂鸣器3. 连接线4. 电路焊接工具5. 编程软件(如Keil)四、实验步骤1. 电路连接:- 将蜂鸣器的正极连接到51单片机的P1.5端口。

- 将蜂鸣器的负极接地。

2. 程序编写:- 使用Keil软件编写程序,实现以下功能:1. 初始化P1.5端口为输出模式。

2. 通过循环,不断改变P1.5端口的电平,产生方波信号。

3. 调整方波信号的频率,控制蜂鸣器的音调。

3. 程序下载:- 将程序下载到51单片机中。

4. 实验观察:- 启动程序后,观察蜂鸣器是否发声,以及音调是否与程序设置一致。

五、实验结果与分析1. 实验结果:- 成功驱动蜂鸣器发声,音调与程序设置一致。

2. 结果分析:- 通过实验,我们掌握了51单片机的I/O口编程方法,以及蜂鸣器的驱动原理。

- 在程序编写过程中,我们学习了方波信号的生成方法,以及如何调整方波信号的频率。

六、实验总结本次实验成功地实现了51单片机控制蜂鸣器发声的功能,达到了预期的实验目的。

通过本次实验,我们提高了以下能力:1. 对51单片机的基本结构和工作原理有了更深入的了解。

2. 掌握了51单片机的I/O口编程方法。

3. 学习了蜂鸣器的驱动原理和应用。

51单片机设计电子琴报告含代码

51单片机设计电子琴报告含代码

电子琴设计报告一、实验目的1.更深刻的了解、学习8051单片机的发声原理,利用定时器可以发出不同频率的脉冲,不同频率的脉冲经喇叭驱动电路放大滤波后,就会发出不同的音调。

2.其次,定时器按设置的定时参数产生中断,这一次中断发出脉冲低电平,下一次反转发出脉冲高电平,由于定时参数不同,所以发出不同频率的脉冲。

3.进一步熟悉定时器的编程方法和定时初值的计算,进一步熟悉键盘扫描电路的工作原理和编程方法,了解单片机芯片的接口技术。

二、实验要求1.能够通过键盘演奏音符。

2.能够保存演奏的音乐,并实现回放。

3.有音调调整功能(如:C调,G调)。

4.自由发挥其他功能。

5.要求有单片机硬件系统框图,电路原理图,软件流程图。

三、实验基本原理简易电子琴有主控、蜂鸣器、键盘输入、电源四部分组成。

主控部分以AT89S52 为核心,用C 语言编程,充分运用AT89S52 的8k字节闪烁可编程可擦除只读存储器及其丰富的I/O 口,实现了对键盘数据的采集,和对蜂鸣器声音的控制;键盘输入部分采用4×4的键盘键盘输入,可以实现多个音调;供电部分可对整个电路进行供电。

经测试,整机基本实现预计功能,可以实现键盘演奏音符、调整音调、保存并回放的功能。

四、实验设计分析根据实验所要求实现的功能设计实现该项实验设计的软件电路及硬件电路。

五、实验要求实现A.电路设计1. 整体设计计划利用AT89S52 单片机的功能结合C 语言编程,实现电子琴播放音符等的简单功能,然后结合AT89S52 单片机的控制功能,利用蜂鸣器将输入表达出来,结合程序编制过程中,对各个I/O 的利用设置了键盘的扫描读入,结合电子琴需要多键位的现实,加入了4×4 键盘输入,达到了预期的效果。

2.分块设计1.控制模块AT89S52单片机是美国ATMEL公司生产的低功耗,高性能CMOS 8位单片机,片内含有8kb的可系统编程的Flash只读程序存储器,器件采用ATMEL公司的高密度、非易失性存储技术生产,兼容标准8051指令系统及引脚。

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); //播放}}。

51单片机蜂鸣器播放单音节音乐

51单片机蜂鸣器播放单音节音乐
// 3_ 2_ 3 1
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;

单片机蜂鸣器发声代码

单片机蜂鸣器发声代码

单片机蜂鸣器发声代码蜂鸣器是一种常见的电子元件,可以发出短促的蜂鸣声用于提醒或警示。

在单片机应用中,蜂鸣器可以用来作为声音提示的一种方式。

下面介绍一段单片机蜂鸣器的发声代码。

1. 引脚连接在使用单片机控制蜂鸣器发声之前,需要将蜂鸣器与单片机的引脚连接。

通常情况下,蜂鸣器有两个引脚,其中一个为正极,一个为负极。

将蜂鸣器的正极连接到单片机的一个IO口上,将负极连接到单片机的GND上。

2. 代码实现单片机发出频率为f的声音,需要将IO口以1/f的频率高低电平切换。

以下是一个基本的单片机蜂鸣器发声代码:#include 'reg51.h' //单片机头文件#define BEEP P1_5 //定义蜂鸣器连接的IO口void Delay(unsigned int Time) //延时函数{unsigned int i,j;for(i=Time;i>0;i--)for(j=110;j>0;j--);}void Beep(unsigned int Time) //发出一次蜂鸣声{BEEP = 0; //使IO口输出低电平Delay(Time); //延时BEEP = 1; //使IO口输出高电平Delay(Time); //延时}void main(void){while(1){Beep(100); //发出100ms的蜂鸣声}}在上述代码中,我们首先定义了蜂鸣器连接的IO口为P1_5,然后定义了一个延时函数Delay和一个发出蜂鸣声函数Beep。

Delay函数用于延时,Beep函数用于发出一次蜂鸣声。

在main函数中,我们使用一个while循环不停地发出蜂鸣声。

在Beep函数中,我们首先使IO口输出低电平,然后延时一段时间,再使IO口输出高电平,再延时一段时间,从而在IO口上形成一个频率为1/(2*Time)的方波信号,从而发出蜂鸣声。

3. 修改参数如果我们想要修改蜂鸣器发出的声音的频率和时长,只需要在Beep函数中修改Time的值即可。

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:

#51单片机实现的音乐闹钟程序代码

#51单片机实现的音乐闹钟程序代码

功能描述:产品可以显示时间和日期,时间格式为 hh mm ss 日期格式为 yy.mm.dd时间和日期轮流显示。

时间显示5S 日期显示3S。

可以设置5个闹铃,闹铃音乐可以设置两种:毛驴和童年。

三个按键对时间和闹铃进行设置,六个LED进行显示。

计时采用DS1307。

继电时间不丢失,设置过的闹铃也不丢失。

闹铃音乐由单片机的两个定时器去产生频率实现。

部分程序如下://*************************************************//************************************************//***********************************************//程序名:DS1307 时钟程序//功能描述:用六个八段LED 轮流显示时间// 和日期。

有6个闹钟。

上电时从DS1307中读出// 当前时间、日期、闹钟。

//////////#include <reg52.h>#include <intrins.h>#define uchar unsigned char#define uint unsigned int#define LED P2#define LEDBit P0#define nop _nop_(>#define LightCount 40#define LightMax 80sbit SCL=P3^1。

sbit SDA=P3^0。

sbit ModeKey=P1^0。

sbit UpKey=P1^1。

sbit DownKey=P1^2。

sbit Speak=P3^6。

code ucharLCD_NUM[10]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09}。

//0x25,//uchar Clock[]={0x88,0x88,0x88}。

51单片机蜂鸣器代码理解

51单片机蜂鸣器代码理解

51单片机蜂鸣器代码理解1.引言1.1 概述概述:蜂鸣器是一种广泛应用于电子设备中的声音输出装置,它通过控制某个频率的电信号使蜂鸣器发出特定的声音。

而51单片机,则是一种常见的单片机芯片,具有广泛的应用领域。

本文将主要探讨51单片机蜂鸣器的代码理解和应用。

通过对其基本原理的概述以及相关代码的解析,希望读者能够深入理解51单片机蜂鸣器的工作原理和实现方式。

在第二部分中,我们将介绍单片机蜂鸣器的基本原理。

包括如何通过单片机控制蜂鸣器的电信号频率和时长,从而实现不同的声音效果。

接着,在第二点中,我们将详细解析51单片机蜂鸣器的代码。

通过对代码的分析,读者可以了解到如何使用51单片机的引脚功能和定时器功能来控制蜂鸣器。

最后,在结论部分,我们将对所述内容进行总结,并展望51单片机蜂鸣器在未来的应用前景。

蜂鸣器作为一种重要的声音输出装置,具有广泛的应用前景,可以应用于报警系统、提醒装置等领域。

通过本文的阅读,读者将能够全面了解51单片机蜂鸣器的工作原理和代码实现方式,为相关领域的应用开发提供参考和指导。

让我们开始探索吧!1.2 文章结构文章结构的部分主要介绍了本文的组织和分类方式,以帮助读者更好地理解文章的内容和思路。

本文按照以下结构进行组织:1. 引言部分:介绍了文章的概述、结构和目的。

通过引言部分,读者可以初步了解到本文的内容和主题,并对文章的结构和目的有一个整体的认识。

2. 正文部分:主要分为两个小节,分别是"单片机蜂鸣器的基本原理"和"51单片机蜂鸣器代码解析"。

2.1 单片机蜂鸣器的基本原理:该部分将详细介绍单片机蜂鸣器的基本工作原理,包括蜂鸣器的构成和工作原理,以及单片机如何控制蜂鸣器发出指定的声音。

2.2 51单片机蜂鸣器代码解析:该部分将对51单片机蜂鸣器的代码进行解析,包括如何初始化引脚、设置定时器和中断等相关代码。

通过对代码的逐行解析和说明,读者可以更加深入地理解代码的功能和实现原理。

单片机课程设计报告(利用蜂鸣器播放音乐)汇编

单片机课程设计报告(利用蜂鸣器播放音乐)汇编

课程设计:电子设计题目名称:音乐流水灯姓名:戴锦超学号:08123447班级:信科12-3班完成时间:2014年10月23日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.蜂鸣器发声原理本实验由于采用有源蜂鸣器,只需将引脚端口P1^4清零,蜂鸣器即可发声;P1^4置位,蜂鸣器停止发声。

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); //播放}}。

基于c51单片机编写简单蜂鸣器音乐程序的方法

基于c51单片机编写简单蜂鸣器音乐程序的方法
/*7*/case 0x22:j=s/494;break;case 0x23:j=s/998;break;case 0x24:j=s/1967;break;
/*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里面的数组来达到连续播放各种不同音调的音的目的,这样听起来就像一首曲子了。
以下是我按以上方法编写的一个小程序:

51单片机常见汇编程序实验代码

51单片机常见汇编程序实验代码

51单片机常见汇编程序实验代码1. 将片外8000H-80FFH单元写入数据AB 32. 将片内RAM20H单元中数据在数码管上显示出来 (3)3. 将片内ARM30H-40H单元清零 54. 将六位数显示在数码管上 55. 8255并口芯片的应用:交通灯控制系统的设计 (6)6. 将交通灯点亮 (7)7. AD转换实验 (8)8. DA转换实验 (10)9. 定时器的应用 ·· 1110. 开关控制LED的亮灭及速度 1211. 计数器实验 (12)12. 串并转换实验 · 1413. 直流电机速度检测1514. 8255PB外接8个开关,编程将开关状态显示在数码管上(串并转换动态扫描方式)17 15. P3.4接开关K,编程将开关拨动次数,通过串并转换的方式进行显示1816. 比较片内RAM30H、31H两个单元值的大小,将较大的数显示在数码管上1917. 单片机P1口接8个开关,编程将开关状态显示在数码管上(串并转换动态扫描方式) (21)18. 将片内50H单元的值显示在数码管上2219. 开关K1接P1.0,K2接P1.1,编程实现当按下K1时在数码管上显示50H单元的值,按下K2在数码管上显示51H单元的值231. 将片外8000H-80FFH单元写入数据ABORG 0000H ;程序从0000H开始执行AJMP MAIN ;跳转到主程序ORG 0030H ;以免覆盖中断MAIN: MOV SP,#60H; 避免堆栈和工作寄存器区冲突MOV DPTR,#8000HMOV R0,#0LOOP: MOV A,#0ABHMOVX @DPTR,AINC DPTRINC R0CJNE R0,#0,LOOP ; 判断AJMP $; 等待END ;调试-视图-M存储器(输入X:8000H)2. 将片内RAM20H单元中数据在数码管上显示出来ORG 0000H ;程序从0000H开始执行AJMP MAIN ;跳转到主程序ORG 0030H ;以免覆盖中断MAIN: MOV SP,#60H ;避免堆栈和工作寄存器区冲突MOV DPTR,#0E100H ;指向命令口MOV A,#03H ;PA、PB口输出MOVX @DPTR,A ;所有并口显示程序先进行8155初始化MOV 20H,#34HMOV A,20HACALL CHAILOOP: MOV R0,#10H ;第一个显示数的送R0MOV R1,#2 ;显示2个数MOV R2,#1 ;从倒数第一个数码管开始显示ACALL DISPLAYLJMP LOOP ;判断CHAI: MOV B,#10HDIV ABMOV 10H,BMOV 11H,ARETDISPLAY: MOV A,@R0MOV 0FH,#8ACALL P164ACALL PBITACALL DELAYMOV A,#0MOVX @DPTR,AINC R0DJNZ R1,DISPLAYRETP164: MOV DPTR,#TABMOVC A,@A+DPTRP164_1: RRC AMOV R3,AMOV ACC.0,CANL A,#0FDHMOV DPTR,#0E102HMOVX @DPTR,AORL A,#0FEHMOVX @DPTR,AMOV A,R3DJNZ 0FH,P164_1MOV 0FH,#8RETPBIT: MOV DPTR,#0E101HMOV A,R2MOVX @DPTR,ARL AMOV R2,ARETDELAY: MOV R6,#4DEL: MOV R7,#250DJNZ R7,$DJNZ R6,DELRETTAB: DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EHEND3. 将片内ARM30H-40H单元清零ORG 0000H ;程序从0000H开始执行AJMP MAIN ;跳转到主程序ORG 0030H ;以免覆盖中断MAIN: MOV SP,#60H ;避免堆栈和工作寄存器区冲突MOV R0,#30HMOV A,#0LOOP: MOV @R0,AINC R0CJNE R0,#41H,LOOP ;判断AJMP $ ;等待END ;D:30H4. 将六位数显示在数码管上ORG 0000H ;程序从0000H开始执行AJMP MAIN ;跳转到主程序ORG 0030H ;以免覆盖中断MAIN: MOV SP,#60H ;避免堆栈和工作寄存器区冲突MOV DPTR,#0E100H ;指向命令口MOV A,#03H ;PA、PB口输出MOVX @DPTR,A ;所有并口显示程序先进行8155初始化LOOP: MOV R0,#10H ;第一个显示数的送R0MOV 10H,#0HMOV 11H,#3HMOV 12H,#1HMOV 13H,#2HMOV 14H,#1HMOV 15H,#1HMOV R1,#6 ;显示6个数MOV R2,#1 ;从倒数第一个数码管开始显示ACALL DISPLAYLJMP LOOPDISPLAY: MOV A,@R0MOV 0FH,#8ACALL P164ACALL PBITACALL DELAYMOV A,#0MOVX @DPTR,AINC R0DJNZ R1,DISPLAYRETP164: MOV DPTR,#TABMOVC A,@A+DPTRP164_1: RRC AMOV R3,AMOV ACC.0,CANL A,#0FDHMOV DPTR,#0E102HMOVX @DPTR,AORL A,#0FEHMOVX @DPTR,AMOV A,R3DJNZ 0FH,P164_1MOV 0FH,#8RETPBIT: MOV DPTR,#0E101HMOV A,R2MOVX @DPTR,ARL AMOV R2,ARETDELAY: MOV R6,#4DEL: MOV R7,#250DJNZ R7,$DJNZ R6,DELRETTAB: DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0HDB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EHEND5. 8255并口芯片的应用:交通灯控制系统的设计ORG 0000HAJMP STARTORG 0030HSTART: MOV DPTR,#0AC03HMOV A,#80HMOVX @DPTR,A //8155初始化(所有并口显示中都要先8155初始化)MOV R5,#0F0HACALL DENGMOV R1,#20ACALL DELAYMOV R0,#1LOOP01: MOV R5,#5AHACALL DENGMOV R1,#100ACALL DELAYDJNZ R0,LOOP01MOV R0,#3 // LED_2闪烁次数LOOP02: MOV R5, #0FAHACALL DENGMOV R1,#10ACALL DELAYMOV R5,#50HACALL DENGMOV R1,#10ACALL DELAYDJNZ R0,LOOP02MOV R5,#0A5HACALL DENGMOV R1,#100ACALL DELAYMOV R0,#3LOOP03: MOV R5, #0F5HACALL DENGMOV R1,#10ACALL DELAYMOV R5,#0A0HACALL DENGMOV R1,#10ACALL DELAYDJNZ R0,LOOP03AJMP LOOP01DENG: MOV DPTR,#0AC00H //8155 PA口输出MOV A,R5MOVX @DPTR,A //8155 PA口送出显示参数RET //100ms延时子程序DELAY: MOV R7,#200DEL: MOV R6,#229DJNZ R6,$DJNZ R7,DELDJNZ R1,DELAYRETEND6. 将交通灯点亮ORG 0000HAJMP STARTORG 0030HSTART: MOV DPTR,#0AC03HMOV A,#80HMOV DPTR,#0AC00HMOV A,#05AHMOVX @DPTR,AAJMP $END7. AD转换实验ORG 0000AJMP STARTORG 000BHAJMP INT_0ORG 0030H START: MOV SP,#60HMOV TMOD,#01HMOV TH0,#4BHMOV TL0,#0FDHMOV R4,#2SETB EASETB ET0SETB TR0MOV DPTR,#0E100HMOV A,#3MOVX @DPTR,AMOV 22H,#9MOV 23H,#0MOV 24H,#8MOV 25H,#0 LOOP: MOV R0,#20HMOV R1,#6MOV R2,#1ACALL DISPLAYSJMP LOOPINT_0: MOV TH0,#4BHMOV TL0,#0FDHDJNZ R4,EXITPUSH ACCPUSH DPHPUSH DPLMOV DPTR,#0A000HMOVX A,@DPTRMOV B,#10HDIV ABMOV 21H,AMOVX @DPTR,AMOV R4,#2POP DPLPOP DPHPOP ACCEXIT: RETIDISPLAY: MOV A,@R0MOV 0FH,#8ACALL P164ACALL PBITACALL DELAYMOV A,#0MOVX @DPTR,AINC R0DJNZ R1,DISPLAYRETP164: MOV DPTR,#TABMOVC A,@A+DPTRP164_1: RRC AMOV R3,AMOV ACC.0,CANL A,#0FDHMOV DPTR,#0E102HMOVX @DPTR,AORL A,#0FEHMOVX @DPTR,AMOV A,R3DJNZ 0FH,P164_1MOV 0FH,#8RETPBIT: MOV DPTR,#0E101HMOV A,R2MOVX @DPTR,ARL AMOV R2,ARETDELAY: MOV R6,#4DEL: MOV R7,#250DJNZ R7,$DJNZ R6,DELRETTAB: DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EHEND8. DA转换实验ORG 0000SJMP STARTORG 0030HSTART: MOV SP,#60HMOV A,#0LOOP1: MOV DPTR,#0B000HMOVX @DPTR,AACALL XSACALL DELAYINC ACJNE A,#0FFH,LOOP1LOOP2: MOV DPTR,#0B000HMOVX @DPTR,AACALL XSACALL DELAYDEC ACJNE A,#0,LOOP2SJMP LOOP1XS: PUSH ACCMOV DPTR,#TABMOV B,#51DIV ABMOV R1,AXCH A,BMOV B,#5DIV ABMOVC A,@A+DPTRMOV R4,#8ACALL S164MOV A,R1MOVC A,@A+DPTRMOV R4,#8ACALL S164MOV A,#0CLR CMOV R4,#16ACALL S164POP ACCRETS164: RRC AMOV P1.0,CCLR P1.1SETB P1.1DJNZ R4,S164RETDELAY: MOV R7,#200DEl: MOV R6,#229DJNZ R6,$DJNZ R7,DELRETTAB: DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EHEND9. 定时器的应用ORG 0000HAJMP STARTORG 00BHAJMP INT_0ORG 0030HSTART: MOV TMOD,#01HMOV TH0,#4BHMOV TL0,#0FDHMOV R0,#20MOV R1,#0SETB ET0SETB TR0SETB EAAJMP $INT_0: MOV TH0,#4BHMOV TL0,#0FDHDJNZ R0,EXITMOV R0,#20MOV A,R1MOV DPTR,#TABMOVC A,@A+DPTRMOV P1,AINC R1CJNE R1,#8,EXITMOV R1,#0EXIT: RETITAB: DB 0FFH,0FAH,0F5H,0AFH,05FH,0AAH,55H,0H,0FFH END10. 开关控制LED的亮灭及速度ORG 0000H ;程序从0000H开始执行AJMP START ;跳转到主程序ORG 0030H ;以免覆盖中断START: MOV R4,#1 ;立即数传送到累加器LOOP: CLR P3.3 ;进位标志位清零LOOP1: JNB P3.5,LOOP ;直接寻址位为0则转移JB P3.4,LOOP2 ;直接寻址位为1则转移JNB P3.4,$INC R4 ;寄存器增1LOOP2: CPL P3.3 ;累加器求反MOV A,R4 ;寄存器内容传送到累加器AMOV R5,A ;累加器内容传送到累加器ACALL DELAY ;调用延时函数AJMP LOOP1 ;循环DELAY: MOV R6,#200DEL: MOV R7,#229DJNZ R7,$DJNZ R6,DEL ;寄存器减1,不为0则转移DJNZ R5,DELAYRET ;子程序返回END11. 计数器实验ORG 0000SJMP STARTORG 001BHLJMP INT_1START: MOV SP,#60HMOV R5,#20MOV TMOD,#15HMOV TH1,#4BHMOV TL1,#0FDHMOV TH0,#0MOV TL0,#0SETB ET1SETB EASETB TR1SETB TR0MOV DPTR,#0E100HMOV A,#3MOVX @DPTR,ALOOP: MOV R0,#50H;MOV R1,#4LCALL DISPLAYSJMP LOOPINT_1: MOV TH1,#4BHMOV TL1,#0FDHDJNZ R5,CONMOV R5,#20PUSH ACCPUSH BMOV A,TL0MOV B,#10HDIV ABMOV 50H,BMOV 51H,AMOV A,TH0MOV B,#10HDIV ABMOV 52H,BMOV 53H,AMOV TH0,#0MOV TL0,#0POP BPOP ACCCON: RETIDISPLAY: MOV A,@R0MOV 0FH,#8ACALL P164ACALL PBITACALL DELAYMOV A,#0MOVX @DPTR,AINC R0DJNZ R1,DISPLAYRETP164: MOV DPTR,#TABMOVC A,@A+DPTR P164_1: RRC AMOV R3,AMOV ACC.0,CANL A,#0FDHMOV DPTR,#0E102HMOVX @DPTR,AORL A,#0FEHMOVX @DPTR,ADJNZ 0FH,P164_1MOV 0FH,#8RETPBIT: MOV DPTR,#0E101HMOV A,R2MOVX @DPTR,ARL AMOV R2,ARETDELAY: MOV R6,#4DEL: MOV R7,#250DJNZ R7,$DJNZ R6,DELRETTAB: DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EHEND12. 串并转换实验ORG 0000SJMP STARTORG 0030HSTART: MOV SP,#60HMOV R1,#0MOV DPTR,#TABLOOP: MOV A,#0CLR CMOV R4,#24ACALL S164MOV A,R1MOVC A,@A+DPTRMOV R4,#8ACALL S164INC R1MOV R5,#10ACALL DELAYCJNE R1,#10,EXITMOV R1,#0EXIT: SJMP LOOPS164: RRC AMOV P1.0,CCLR P1.1SETB P1.1DJNZ R4,S164RETDELAY: MOV R6,#200DEL: MOV R7,#230DJNZ R7,$DJNZ R6,DELDJNZ R5,DELAYRETTAB: DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H, 0FEH,0F6H END13. 直流电机速度检测ORG 0000HAJMP MAINORG 001BHAJMP INT_1ORG 0030HMAIN: MOV SP,#60HMOV TMOD,#15HMOV TH1,#4BHMOV TL1,#0FDHMOV R0,#20MOV TH0,#0MOV TL0,#0SETB ET1SETB EASETB TR0SETB TR1MOV R2,#00HUP: MOV A,R2MOV DPTR,#0A000H //注意片选接YC2,非YC3MOVX @DPTR,AMOV R5,#1ACALL DELAY ;100msINC R2CJNE R2,#0FFH,UPDOWN:MOV A,R2MOV DPTR,#0A000HMOVX @DPTR,AMOV R5,#1ACALL DELAY ;100msDEC R2CJNE R2,#00H,DOWNAJMP UPINT_1: MOV TH1,#4BHMOV TL1,#0FDHDJNZ R0,EXITMOV R0,#20PUSH ACCPUSH DPHPUSH DPLMOV A,TL0ACALL XSMOV TL0,#0MOV A,TH0ACALL XSMOV TH0,#0POP DPLPOP DPHPOP ACCEXIT: RETIXS: MOV DPTR,#TAB;MOV A,R2MOV B,#10 ;显示10进制数DIV ABXCH A,B;MOV DPTR,#TABMOVC A,@A+DPTRACALL FSXCH A,B;MOV DPTR,#TABMOVC A,@A+DPTRACALL FSRETFS: MOV R1,#8YW:RRC AMOV P1.0,CCLR P1.1SETB P1.1DJNZ R1,YWRETDELAY:MOV R6,#200DEL:MOV R7,#230DJNZ R7,$DJNZ R6,DELDJNZ R5,DELAYRETTAB:DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0HDB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EHEND14. 8255PB外接8个开关,编程将开关状态显示在数码管上(串并转换动态扫描方式)ORG 0000AJMP STARTORG 0030HSTART: MOV DPTR,#0AC03HMOV A, #82HMOVX @DPTR, AMOV DPTR,#0E100HMOV A,#3MOVX @DPTR,ALOOP: MOV DPTR,#0AC01HMOVX A,@DPTRMOV B,#10HDIV ABMOV 10H,BMOV 11H,AMOV R0,#10HMOV R1,#2MOV R2,#1ACALL DISPLAYSJMP LOOPDISPLAY:MOV A,@R0MOV 0FH,#8ACALL P164ACALL PBITACALL DELAYMOV A,#0MOVX @DPTR,AINC R0DJNZ R1,DISPLAYRETP164: MOV DPTR,#TABMOVC A,@A+DPTRP164_1: RRC AMOV R3,AMOV ACC.0,CANL A,#0FDHMOV DPTR,#0E102HMOVX @DPTR,AORL A,#0FEHMOVX @DPTR,ADJNZ 0FH,P164_1MOV 0FH,#8RETPBIT: MOV DPTR,#0E101HMOV A,R2MOVX @DPTR,ARL AMOV R2,ARETDELAY: MOV R6,#4DEL: MOV R7,#250DJNZ R7,$DJNZ R6,DELRETTAB:DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0HDB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EHEND15. P3.4接开关K,编程将开关拨动次数,通过串并转换的方式进行显示ORG 0000SJMP STARTORG 0030HSTART: MOV SP,#60HMOV R5,#0LOOP: JB P3.4,$ACALL DELAYJB P3.4,LOOPINC R5ACALL XSLOOP1: JNB P3.4,$ACALL DELAYJNB P3.4,LOOP1INC R5ACALL XSSJMP LOOPDELAY: MOV R7,#200DEL: MOV R6,#229DJNZ R6,$DJNZ R7,DELRETXS: MOV DPTR,#TABMOV A,R5MOV B,#10HMOV R1,AMOV A,BMOVC A,@A+DPTRMOV R4,#8ACALL S164MOV A,R1MOVC A,@A+DPTRMOV R4,#8ACALL S164MOV A,#0CLR CMOV R4,#16ACALL S164RETS164: RRC AMOV P1.0,CCLR P1.1SETB P1.1DJNZ R4,S164RETTAB: DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0HDB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EHEND16. 比较片内RAM30H、31H两个单元值的大小,将较大的数显示在数码管上ORG 0000SJMP STARTORG 0030HSTART: MOV SP,#60HMOV DPTR,#0E100HMOV A,#3MOVX @DPTR,AMOV 30H,#06H ;随便赋两个值,显示较大的MOV 31H,#11HMOV A,30HSUBB A,31HJC SMALLMOV B,#10HMOV A,30HDIV ABMOV 30H,BMOV 31H,ABIG: MOV R0,#30HMOV R1,#2MOV R2,#1ACALL DISPLAYSJMP BIG SMALL: MOV B,#10HMOV A,31HDIV ABMOV 30H,BMOV 31H,ASMALL1:MOV R0,#30HMOV R1,#2MOV R2,#1ACALL DISPLAYSJMP SMALL1 DISPLAY:MOV A,@R0MOV 0FH,#8ACALL P164ACALL PBITACALL DELAYMOV A,#0MOVX @DPTR,AINC R0DJNZ R1,DISPLAYRETP164: MOV DPTR,#TABMOVC A,@A+DPTR P164_1: RRC AMOV R3,AMOV ACC.0,CANL A,#0FDHMOV DPTR,#0E102HMOVX @DPTR,AORL A,#0FEHMOVX @DPTR,AMOV A,R3DJNZ 0FH,P164_1MOV 0FH,#8RETPBIT: MOV DPTR,#0E101HMOV A,R2MOVX @DPTR,ARL AMOV R2,ARETDELAY: MOV R6,#4DEL: MOV R7,#250DJNZ R7,$DJNZ R6,DELRETTAB:DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0HDB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EHEND17. 单片机P1口接8个开关,编程将开关状态显示在数码管上(串并转换动态扫描方式)ORG 0000HSJMP STARTORG 0030HSTART: MOV SP,#60HMOV DPTR,#0E100HMOV A,#3MOVX @DPTR,ALOOP: MOV A,P1MOV B,#10HDIV ABMOV 20H,BMOV 21H,AMOV R0,#20HMOV R1,#2MOV R2,#1ACALL DISPLAYAJMP LOOPDISPLAY:MOV A,@R0MOV 0FH,#8ACALL P164ACALL PBITACALL DELAYMOV A,#0MOVX @DPTR,AINC R0DJNZ R1,DISPLAYRETP164: MOV DPTR,#TABMOVC A,@A+DPTRP164_1: RRC AMOV R3,AMOV ACC.0,CANL A,#0FDHMOV DPTR,#0E102HMOVX @DPTR,AORL A,#0FEHMOVX @DPTR,AMOV A,R3DJNZ 0FH,P164_1MOV 0FH,#8RETPBIT: MOV DPTR,#0E101HMOV A,R2MOVX @DPTR,ARL AMOV R2,ARETDELAY: MOV R6,#4DEL: MOV R7,#250DJNZ R7,$DJNZ R6,DELRETTAB:DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EHEND18. 将片内50H单元的值显示在数码管上ORG 0000HSJMP STARTORG 0030HSTART: MOV SP,#60HMOV DPTR,#0E100HMOV A,#3MOVX @DPTR,ALOOP: MOV A,P1MOV B,#10HDIV ABMOV 20H,BMOV 21H,AMOV R0,#20HMOV R1,#2MOV R2,#1ACALL DISPLAYAJMP LOOPDISPLAY:MOV A,@R0MOV 0FH,#8ACALL P164ACALL PBITACALL DELAYMOV A,#0MOVX @DPTR,AINC R0DJNZ R1,DISPLAYRETP164: MOV DPTR,#TABMOVC A,@A+DPTRP164_1: RRC AMOV R3,AMOV ACC.0,CANL A,#0FDHMOV DPTR,#0E102HMOVX @DPTR,AORL A,#0FEHMOVX @DPTR,AMOV A,R3DJNZ 0FH,P164_1MOV 0FH,#8RETPBIT: MOV DPTR,#0E101HMOV A,R2MOVX @DPTR,ARL AMOV R2,ARETDELAY: MOV R6,#4DEL: MOV R7,#250DJNZ R7,$DJNZ R6,DELRETTAB:DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0HDB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EHEND19. 开关K1接P1.0,K2接P1.1,编程实现当按下K1时在数码管上显示50H单元的值,按下K2在数码管上显示51H单元的值ORG 0000SJMP STARTORG 0030HSTART: MOV SP,#60HMOV DPTR,#0E100HMOV A,#3MOVX @DPTR,AMOV 50H,#18H ;50H,51H 随便赋两个值MOV 51H,#22HLOOP:JB P1.0,D50JB P1.1,D51SJMP LOOPD51: MOV R0,#10HMOV R1,#2MOV R2,#1MOV A,51HMOV B,#10HDIV ABMOV 10H,BMOV 11H,AACALL DISPLAYSJMP LOOPD50: MOV R0,#10HMOV R1,#2MOV R2,#1MOV A,50HMOV B,#10HDIV ABMOV 10H,BMOV 11H,AACALL DISPLAYSJMP LOOPDISPLAY:MOV A,@R0MOV 0FH,#8ACALL P164ACALL PBITACALL DELAYMOV A,#0MOVX @DPTR,AINC R0DJNZ R1,DISPLAYRETP164:MOV DPTR,#TABMOVC A,@A+DPTRP164_1:RRC AMOV R3,AMOV ACC.0,CANL A,#0FDHMOV DPTR,#0E102HMOVX @DPTR,AORL A,#0FEHMOVX @DPTR,AMOV A,R3DJNZ 0FH,P164_1MOV 0FH,#8RETPBIT:MOV DPTR,#0E101HMOV A,R2MOVX @DPTR,ARL AMOV R2,ARETDELAY:MOV R6,#4DEL:MOV R7,#250DJNZ R7,$DJNZ R6,DELRETTAB:DB 0FCH,60H,0DAH,0F2H,66H,0B6H,0BEH,0E0H DB 0FEH,0F6H,0EEH,3EH,9CH,7AH,9EH,8EHEND。

(原创)51单片机C语言程序设计--速学教程实例(进阶篇)之蜂鸣器奏乐

(原创)51单片机C语言程序设计--速学教程实例(进阶篇)之蜂鸣器奏乐

/******************************************************************//*名称:蜂鸣器奏乐演示程序*//*作者:苏涛*//*时间:2011-01-18*//*单位:安徽建筑工业学院电子与信息工程学院*//*07级电子信息工程专业二班*//******************************************************************/#include<reg52.h>//头文件调用,写程序时都要加上#include"SoundPlay.h"/******************************************************************//*名称:延时函数*//*功能:C语言中常用的for循环延时方法*//*延时长短可以通过变量初值来改变*//******************************************************************/void Delay1ms(uint count){uint i,j;for(i=0;i<count;i++)for(j=0;j<120;j++);}/*****************************Music***************************************************/ //挥着翅膀的女孩unsigned char code Music_Girl[]={0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x15,0x03,0x16,0x03,0x17,0x03,0x17,0x03,0x17,0x03,0x18,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x18,0x02,0x18,0x03,0x17,0x03,0x15,0x02,0x18,0x03,0x17,0x03,0x18,0x02,0x10,0x03,0x15,0x03,0x16,0x02,0x15,0x03,0x16,0x03,0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x1A,0x03,0x1B,0x03,0x1F,0x03,0x1F,0x03,0x17,0x03,0x18,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,0x1F,0x03,0x1F,0x02,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,0x20,0x03,0x20,0x02,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x15,0x03,0x15,0x03,0x17,0x03,0x16,0x66,0x17,0x04,0x18,0x04,0x18,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x00,0x1A,0x03,0x1A,0x03,0x1A,0x03,0x1B,0x03,0x1B,0x03,0x1A,0x03,0x19,0x03,0x19,0x02,0x17,0x03,0x15,0x17,0x15,0x03,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x04,0x18,0x0E,0x18,0x03,0x17,0x04,0x18,0x0E,0x18,0x66,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,0x20,0x03,0x20,0x02,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x04,0x1B,0x0E,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x15,0x03,0x15,0x03,0x17,0x03,0x16,0x66,0x17,0x04,0x18,0x04,0x18,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x00,0x18,0x02,0x18,0x03,0x1A,0x03,0x19,0x0D,0x15,0x03,0x15,0x02,0x18,0x66,0x16,0x02,0x17,0x02,0x15,0x00,0x00,0x00}; //同一首歌unsigned char code Music_Same[]={0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x66,0x18,0x03,0x17,0x02,0x15,0x02,0x16,0x01,0x15,0x02,0x10,0x02,0x15,0x00,0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x15,0x02,0x18,0x66,0x17,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x16,0x00,0x17,0x01,0x19,0x02,0x1B,0x02,0x1B,0x70,0x1A,0x03,0x1A,0x01,0x19,0x02,0x19,0x03,0x1A,0x03,0x1B,0x02,0x1A,0x0D,0x19,0x03,0x17,0x00,0x18,0x66,0x18,0x03,0x19,0x02,0x1A,0x02,0x19,0x0C,0x18,0x0D,0x17,0x03,0x16,0x01,0x11,0x02,0x11,0x03,0x10,0x03,0x0F,0x0C,0x10,0x02,0x15,0x00,0x1F,0x01,0x1A,0x01,0x18,0x66,0x19,0x03,0x1A,0x01,0x1B,0x02,0x1B,0x03,0x1B,0x03,0x1B,0x0C,0x1A,0x0D,0x19,0x03,0x17,0x00,0x1F,0x01,0x1A,0x01,0x18,0x66,0x19,0x03,0x1A,0x01,0x10,0x02,0x10,0x03,0x10,0x03,0x1A,0x0C,0x18,0x0D,0x17,0x03,0x16,0x00,0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x70,0x18,0x03,0x17,0x02,0x15,0x03,0x15,0x03,0x16,0x66,0x16,0x03,0x16,0x02,0x16,0x03,0x15,0x03,0x10,0x02,0x10,0x01,0x11,0x01,0x11,0x66,0x10,0x03,0x0F,0x0C,0x1A,0x02,0x19,0x02,0x16,0x03,0x16,0x03,0x18,0x66,0x18,0x03,0x18,0x02,0x17,0x03,0x16,0x03,0x19,0x00,0x00,0x00};//两只蝴蝶unsigned char code Music_Two[]={0x17,0x03,0x16,0x03,0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0E,0x15,0x04,0x0F,0x01,0x17,0x03,0x16,0x03,0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x15,0x03,0x16,0x01,0x17,0x03,0x16,0x03,0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0E,0x15,0x04,0x0F,0x01,0x17,0x03,0x19,0x03,0x19,0x01,0x19,0x03,0x1A,0x03,0x19,0x03,0x17,0x01,0x16,0x03,0x16,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0D,0x15,0x00,0x19,0x03,0x19,0x03,0x1A,0x03,0x1F,0x03,0x1B,0x03,0x1B,0x03,0x1A,0x03,0x17,0x0D,0x16,0x03,0x16,0x03,0x16,0x0D,0x17,0x01,0x17,0x03,0x17,0x03,0x19,0x03,0x1A,0x02,0x1A,0x02,0x10,0x03,0x17,0x0D,0x16,0x03,0x16,0x01,0x17,0x03,0x19,0x03,0x19,0x03,0x17,0x03,0x19,0x02,0x1F,0x02,0x1B,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x17,0x02,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x1A,0x03,0x19,0x03,0x17,0x03,0x16,0x03,0x17,0x0D,0x16,0x03,0x17,0x03,0x19,0x01,0x19,0x03,0x19,0x03,0x1A,0x03,0x1F,0x03,0x1B,0x03,0x1B,0x03,0x1A,0x03,0x17,0x0D,0x16,0x03,0x16,0x03,0x16,0x03,0x17,0x01,0x17,0x03,0x17,0x03,0x19,0x03,0x1A,0x02,0x1A,0x02,0x10,0x03,0x17,0x0D,0x16,0x03,0x16,0x01,0x17,0x03,0x19,0x03,0x19,0x03,0x17,0x03,0x19,0x03,0x1F,0x02,0x1B,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x17,0x02,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x17,0x16,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x1A,0x03,0x19,0x03,0x17,0x03,0x16,0x03,0x0F,0x02,0x10,0x03,0x15,0x00,0x00,0x00};/*************************************************************************************/ void main(){InitialSound();while(1){Play(Music_Girl,0,3,360);Delay1ms(500);Play(Music_Same,0,3,360);Delay1ms(500);Play(Music_Two,0,3,360);Delay1ms(500);}}/******************************************************************//*头文件:SOUNDPLAY.H*//*说明:此头文件来源于网络*//******************************************************************//*说明**************************************************************曲谱存贮格式unsigned char code MusicName{音高,音长,音高,音长....,0,0};末尾:0,0表示结束(Important)音高由三位数字组成:个位是表示1~7这七个音符十位是表示音符所在的音区:1-低音,2-中音,3-高音;百位表示这个音符是否要升半音:0-不升,1-升半音。

单片机-利用蜂鸣器演奏音乐

单片机-利用蜂鸣器演奏音乐

实验三-利用蜂鸣器演奏音乐一、实验目的1.了解BlueSkyC51单片机实验板中蜂鸣器的硬件电路2.学会利用蜂鸣器实现音乐的演奏3.掌握蜂鸣器实现音乐演奏的编程二、实验硬件设计及电路1. BlueSkyC51单片机实验板2.单片机最小系统3.蜂鸣器电路连接三极管主要是做驱动用的。

因为单片机的IO口驱动能力不够让蜂鸣器发出声音,所以我们通过三极管放大驱动电流,从而可以让蜂鸣器发出声音,你要是输出高电平,三极管导通,集电极电流通过蜂鸣器让蜂鸣器发出声音,当输出低电平时,三极管截止,没有电流流过蜂鸣器,所以就不会发出声音。

三、实验原理1.音调及节拍用一个口,输出方波,这个方波输入进蜂鸣器就会产生声音,通过控制方波的频率、时间,就能产生简单的音乐。

一般说来,单片机演奏音乐基本都是单音频率,因此单片机奏乐只需控制音调和节拍。

(1)音调的确定音调是由频率来确定的。

通过单片机的定时器定时中断,将单片机上对应蜂鸣器的I/O 口来回取反,从而让蜂鸣器发出不同频率的声音。

只需将定时器给以不同的定时值就可实现。

通过延时,即可发出所需要的频率。

(2)节拍的确定一拍的时长大约为400—500ms,每个音符的时长通过节拍来计算。

详细见程序代码。

2.软件设计相关(1)头文件#include<reg51.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned longsbit beep=P1^4; //蜂鸣器与P1.4口连接(2)歌曲曲谱uchar code diaodata[30]={ //音调代码0x10,0x0e,0x0c,0x10,0x0e,0x0c,0x10,0x0e,0x10,0x0c,0x10,0x15,0x13,0x10,0x13,0x15,0x15,0x13,0x15,0x13,0x10,0x0e,0x0c,0x0e,0x0e,0x10,0x0e,0x0c,0x0e,0x00};uchar code jiedata[30]={ //音长代码0x04,0x04,0x08,0x06,0x02,0x08,0x04,0x04,0x04,0x04,0x02,0x06,0x08,0x02,0x02,0x04,0x04,0x04,0x04,0x08,0x02,0x02,0x04,0x04,0x02,0x02,0x04,0x04,0x0c,0x00};(3)音阶表uchar code freq[36*2]={ //音阶码表0xf7,0xd8, //440hz , 1 //00xf8,0x50, //466hz , 1# //10xf8,0xbc, //494hz , 2 //20xf9,0x26, //524hz , 2# //30xf9,0x85, //554hz , 3 //40xf9,0xe5, //588hz , 4 //50xfa,0x3d, //622hz , 4# //60xfa,0x92, //660hz , 5 //70xfa,0xdd, //698hz , 5# //80xfb,0x29, //740hz , 6 //90xfb,0x70, //784hz , 6# //100xfb,0xb0, //830hz , 7 //110xfb,0xef, //880hz , 1 //120xfc,0x29, //932hz , 1# //130xfc,0x62, //988hz , 2 //140xfc,0x95, //1046hz, 2# //150xfc,0xc7, //1108hz, 3 //160xfc,0xf5, //1174hz, 4 //170xfd,0x20, //1244hz, 4# //180xfd,0x4c, //1318hz, 5 //190xfd,0x72, //1396hz, 5# //200xfd,0x97, //1480hz, 6 //210xfd,0xbb, //1568hz, 6# //220xfd,0xdc, //1662hz, 7 //230xfd,0xfb, //1769hz, `1 //240xfe,0x18, //1864hz, `1# //250xfe,0x34, //1976hz, `2 //260xfe,0x4e, //2092hz, `2# //270xfe,0x67, //2218hz, `3 //280xfe,0x7d, //2350hz, `4 //290xfe,0x94, //2488hz, `4# //300xfe,0xa8, //2639hz, `5 //310xfe,0xbc, //2794hz, `5# //320xfe,0xcf, //2960hz, `6 //330xfe,0xe0, //3136hz, `6# //340xfe,0xf1, //3322hz, `7 //35};(4) 音符发生程序(中断服务程序)void timer0() interrupt 1 //用于产生音符的T0中断服务程序{TH0=th0_f;TL0=tl0_f;beep=~beep; //取反beep引脚,发声}三、实验过程及问题1.Keil开发过程(1)新建“Project”,选择单片机为“AT89C52”(2)新建c文件“music.c”,并添加到工程中(3)设置生成“HEX”文件,编译(4)将生成的“HEX”文件烧录到单片机,检验(5)不断修改完善2.编译实验相关问题(1)实际发音颤音重解决方法为修改蜂鸣器的驱动频率.(2)实际节奏过快或者过慢调整延时四、C51程序代码(部分来源于网络)#include<reg51.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned longsbit beep=P1^4; //蜂鸣器与P1.4口连接uchar th0_f; //中断装载T0高8位uchar tl0_f; //T0低8位uchar code freq[36*2]={ //音阶码表0xf7,0xd8, //440hz , 1 //00xf8,0x50, //466hz , 1# //10xf8,0xbc, //494hz , 2 //20xf9,0x26, //524hz , 2# //30xf9,0x85, //554hz , 3 //40xf9,0xe5, //588hz , 4 //50xfa,0x3d, //622hz , 4# //60xfa,0x92, //660hz , 5 //70xfa,0xdd, //698hz , 5# //80xfb,0x29, //740hz , 6 //90xfb,0x70, //784hz , 6# //100xfb,0xb0, //830hz , 7 //110xfb,0xef, //880hz , 1 //120xfc,0x29, //932hz , 1# //130xfc,0x62, //988hz , 2 //140xfc,0x95, //1046hz, 2# //150xfc,0xc7, //1108hz, 3 //160xfc,0xf5, //1174hz, 4 //170xfd,0x20, //1244hz, 4# //180xfd,0x4c, //1318hz, 5 //190xfd,0x72, //1396hz, 5# //200xfd,0x97, //1480hz, 6 //210xfd,0xbb, //1568hz, 6# //220xfd,0xdc, //1662hz, 7 //230xfd,0xfb, //1769hz, `1 //240xfe,0x18, //1864hz, `1# //250xfe,0x34, //1976hz, `2 //260xfe,0x4e, //2092hz, `2# //270xfe,0x67, //2218hz, `3 //280xfe,0x7d, //2350hz, `4 //290xfe,0x94, //2488hz, `4# //300xfe,0xa8, //2639hz, `5 //310xfe,0xbc, //2794hz, `5# //320xfe,0xcf, //2960hz, `6 //330xfe,0xe0, //3136hz, `6# //340xfe,0xf1, //3322hz, `7 //35};uchar code diaodata[30]={ //音调代码0x10,0x0e,0x0c,0x10,0x0e,0x0c,0x10,0x0e,0x10,0x0c,0x10,0x15,0x13,0x10,0x13,0x15,0x15,0x13,0x15,0x13,0x10,0x0e,0x0c,0x0e,0x0e,0x10,0x0e,0x0c,0x0e,0x00};uchar code jiedata[30]={ //音长代码0x04,0x04,0x08,0x06,0x02,0x08,0x04,0x04,0x04,0x04,0x02,0x06,0x08,0x02,0x02,0x04,0x04,0x04,0x04,0x08,0x02,0x02,0x04,0x04,0x02,0x02,0x04,0x04,0x0c,0x00};void timer0() interrupt 1 //用于产生音符的T0中断服务程序{TH0=th0_f;TL0=tl0_f;beep=~beep; //取反beep引脚,发声}void main(void){uchar i,j,k=0;uint n;TMOD=0X01; //T0方式1TR0=0; //关闭T0(不发声)ET0=1; //允许T0中断EA=1; //允许总中断while(1){TR0=1; //开T0for(i=0;diaodata[i]!=0;i++){th0_f=freq[diaodata[i]*2];tl0_f=freq[diaodata[i]*2+1];for(j=0;j<20000;n++); //(jiedata*20000)TR0=0;for(n=0;n<256;n++); //音符之间的短暂延时TR0=1;}TR0=0;}}(注:可编辑下载,若有不当之处,请指正,谢谢!)。

51单片机通过按键控制蜂鸣器发生详解

51单片机通过按键控制蜂鸣器发生详解

单片机开发报告院系:电子工程学院专业:自动化班级:自动化1401学号: 2姓名:赵越指导老师:刘星光2018年 01 月04 日.系统任务按键控制蜂鸣器发声二.电路原理图三.程序设计内容“叮咚”电子门铃实验程序:常有的家用电子门铃在有客人来访时候,假如按压门铃按钮时,室内会发出“叮咚”声音,本实验程序模拟电子门铃的发音,当我们按压实验板上的K1按钮时候,蜂鸣器发出“叮咚”音乐声,是一个比较适用的程序。

使用无源蜂鸣器输出7个基本音阶声音是由物体振动所产生的。

不过因为物体的资料以及振幅、频次不同,而产生不一样的声音。

声音的响度是由振幅决定的,而音调则是由频次决定的,那么我们只要要控制物体振动的频次,就能够发出固定的腔调。

五.汇编程序ORG0000HAJMPSTARTORG 000BH INCM OV 20H ;中止服务TH0,#0D8H,中止计数器加1MOVTL0,#0F0H 12M晶振,形成10毫秒中止RETIORG001BHLJMPINTT1;跳转到T1中止服务程序START: MOV DPTR,#00H;初始化程序MOV A,#00HOBUF1 EQU30HOBUF2 EQU31HOBUF3 EQU32HOBUF4 EQU33HFLAGB BIT00HSTOPB BIT01HMOV SP,#50HMOV TH0,#0D8HMOV TL0,#0F0HMOV TMOD,#21HMOV TH1,#09HMOV TL1,#09HMOV IE,#8AHAJMPLOOPLOOP:JNB P3.2,MUSIC0JNB P3.1,MAINAJMPLOOPMAIN:JBP3.1,MAIN;检测按钮LCALLJBYS10MP3.1,MAIN;延时去颤动SETB MOV MOV MOV MOV CLR CLRJNBTR1OBUF1,#00HOBUF2,#00HOBUF3,#00HOBUF4,#00HFLAGBSTOPBSTOPB,$;按钮有效AJMP START ;发出“叮咚”完成,返回从头检测按钮YS10M: ;10ms延时子程序MOVR6,#20 D1:MOVDJNZDJNZ R7,#100 R7,$R6,D1RETTING:AJMPSTARTINTT1:;准时器T1中止服务程序INCMOVCJNEMOVINCMOVCJNEMOVJBCPLAJMPOBUF3 ;中止服务程序中发出一声“叮咚”响声A,OBUF3A,#100,NEXTOBUF3,#00HOBUF4 A,OBUF4A,#20,NEXT OBUF4,#00H FLAGB,PGSTP FLAGB NEXTPGSTP:SETBCLRSTOPBTR1LJMPINT0RET NEXT:JBINCMOVCJNEMOVCPLLJMP FLAGB,SOU2 OBUF2A,OBUF2A,#04H,INT0RET OBUF2,#00HINT0RETSOU2: INCMOVCJNEMOVCPL OBUF1A,OBUF1A,#05H,INT0RET OBUF1,#00HINT0RET:RETIMUSIC0:JBLCALLJBp3.2,MUSIC0YS10Mp3.2,MUSIC0NOPMOV DPTR,#DAT表头地点送DPTRM OV20H,#0 0H;中止计数器清MOVMAIN2: JNBB,#00H ;表序号清P3.3,TINGCLRAMOVCA,@A+DPTR;查表代替码JZ END0 ;是00H,则结束CJNEA,#0FFH,MUSIC5LJMPMUSIC3MUSIC5:NOPMOV R6,AINC DPTRMOV A,BMOVCA,@A+DPTR;取节拍代码送R7MOV R7,ASETBTR0 MUSIC2: NOP CPL MOV A,R6MOV R3,A;启动计数LCALLDELMOVA,R7CJNEA,20H,MUSIC2;中止计数器(20H)=R7否?MOV 20H,#00HINC DPTRINCBLJMPMAIN2;不等,则持续循环;等于,则取下一代码MUSIC3:;休止100毫秒NOPCLRTR0MOV R2,#0DHMUSIC4:NOPMOV R3,#0FFHLCALLDELDJNZR2,MUSIC4INC DPTRLJMPMAIN2END0:NOPMOV R2,#0FFH;歌曲结束,延时MUSIC6:MOV R3,#00HLCALLDELDJNZR2,MUSIC6CLR TR0LJMPLOOPDEL:NOPDEL3:MOV R4,#03HDEL4:NOPDJNZR4,DEL4NOPDJNZR3,DEL3RETDENG1: MOV R3,#64HDJNZR3,$AJMPMAINDAT:DB 30h,30h,26h,26h,20h,20h,1ch,1ch,1ah,1ah,18h,18h,00hEND六、程序下载及调试步骤:1.点击translate按钮预编译2.点击build 按钮编译3.点击rebuild 按钮编译全部目标4.翻开普中烧录软件5.点击程序下载四.程序流程图开始初始化设置中止程序按键判断Yes中止变电平循环体中止51单片机通过按键控制蜂鸣器发生详解结束判断NoYes结束21 / 2121。

51单片机控制喇叭(蜂鸣器)简单发声,频率可调

51单片机控制喇叭(蜂鸣器)简单发声,频率可调

/*-----------------------------------------------------------------------------------------------------------------------------------*//*--*************************功能:【喇叭发出声音,频率可调】*********************--*//*--*************************芯片:【STC12C5A60S2】******************************--*//*--*************************说明:【频率500Hz,单次发声时长1s】******************--*//*--*************************控制:【每按下P1.0一次,喇叭发声1s,长按一直发声】**--*//*------------------------------------------------------------------------------------------------------------------------------------*//*头文件*/#include <reg52.h>#include <intrins.h>sbitControl_Key = P1^0; //定义控制独立按键P1.0,按下发声。

sbitSpeaker_port = P2^7; //定义喇叭控制端口unsigned intnum = 8; //num = (4 * 1000) / Frequency(500Hz) : 决定喇叭的响应周期,从而决定不同的频率unsigned int counter0 = 0; //T0控制频率unsigned int counter1 = 0; //T1控制时长unsigned char Juge_Start; //判断发声1s是否开始。

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