89c52单片机蜂鸣器程序编程

89c52单片机蜂鸣器程序编程,第1张

简易蜂鸣器裤神陪程序:

#include<reg52.h>

#define uchar unsigned char

#define uint unsigned int

sbit P36=P3^6//我的系统板蜂鸣器口定义

sbit k1=P1^4//按钮1

sbit k2=P1^5//按瞎嫌钮2

void Delay(uint num)//延时函数

{uchar i,j

for(i=0i<255i--)

{for(j=0j>0j++)}

}

main()

{

while(1)

{

if(k1==0)

{P36=0//蜂鸣器响胡蠢

Delay()

P36=1//蜂鸣器停

Dalay()

key_time()

}

}

}

#include <REG52.H>

#include <INTRINS.H>

//本例采用89C52, 晶振为11.0592MHZ

//关于如何编制音乐代码, 其实十分简单,各位可以看以下代码.

//频率常数即音乐术语中的音调,而节拍常数档团即音乐术语中的多少拍

//所以拿出谱子, 试探编吧!

sbit Beep = P1^5

unsigned char n=0 //n为节拍常数变量

unsigned char code music_tab[] ={

0x18, 0x30, 0x1C , 0x10, //格式为: 频率常数, 节拍常数, 频率常数, 节拍常数,

0x20, 0x40, 0x1C , 0x10,

0x18, 0x10, 0x20 , 0x10,

0x1C, 0x10, 0x18 , 0x40,

0x1C, 0x20, 0x20 , 0x20,

0x1C, 0x20, 0x18 , 0x20,

0x20, 0x80, 0xFF , 0x20,

0x30, 0x1C, 0x10 , 0x18,

0x20, 0x15, 0x20 , 0x1C,

0x20, 0x20, 0x20 , 0x26,

0x40, 0x20, 0x20 , 0x2B,

0x20, 0x26, 0x20 , 0x20,

0x20, 0x30, 0x80 , 0xFF,

0x20, 0x20, 0x1C , 0x10,

0x18, 0x10, 0x20 , 0x20,

0x26, 0x20, 0x2B , 0x20,

0x30, 0x20, 0x2B , 0x40,

0x20, 0x20, 0x1C , 0x10,

0x18, 0x10, 0x20 , 0x20,

0x26, 0x20, 0x2B , 0x20,

0x30, 0x20, 0x2B , 0x40,

0x20, 0x30, 0x1C , 0x10,

0x18, 0x20, 0x15 , 0x20,

0x1C, 0x20, 0x20 , 0x20,

0x26, 0x40, 0x20 , 0x20,

0x2B, 0x20, 0x26 , 0x20,

0x20, 0x20, 0x30 , 0x80,

0x20, 0x30, 0x1C , 0x10,

0x20, 0x10, 0x1C , 0x10,

0x20, 0x20, 0x26 , 0x20,

0x2B, 0x20, 0x30 , 0x20,

0x2B, 0x40, 0x20 , 0x15,

0x1F, 0x05, 0x20 , 0x10,

0x1C, 0x10, 0x20 , 0x20,

0x26, 0x20, 0x2B , 0x20,

0x30, 0x20, 0x2B , 0x40,

0x20, 0x30, 0x1C , 0x10,

0x18, 0x20, 0x15 , 0x20,

0x1C, 0x20, 0x20 , 0x20,

0x26, 0x40, 0x20 , 0x20,

0x2B, 0x20, 0x26 , 0x20,

0x20, 0x20, 0x30 , 0x30,

0x20, 0x30, 0x1C , 0x10,

0x18, 0x40, 0x1C , 0x20,

0x20, 0x20, 0x26 , 0x40,

0x13, 0x60, 0x18 , 0x20,

0x15, 0x40, 0x13 , 0x40,

0x18, 0x80, 0x00

}

void int0() interrupt 1 //采用中断0 控制节拍

{ TH0=0xd8

TL0=0xef

n--

}

void delay (unsigned char m) //控制频率延时兆蠢衫族腔

{

unsigned i=3*m

while(--i)

}

void delayms(unsigned char a) //豪秒延时子程序

{

while(--a) //采用while(--a) 不要采用while(a--)各位可编译一下看看汇编结果就知道了!

}

void main()

{ unsigned char p,m //m为频率常数变量

unsigned char i=0

TMOD&=0x0f

TMOD|=0x01

TH0=0xd8TL0=0xef

IE=0x82

play:

while(1)

{

a: p=music_tab[i]

if(p==0x00) { i=0, delayms(1000)goto play} //如果碰到结束符,延时1秒,回到开始再来一遍

else if(p==0xff) { i=i+1delayms(100),TR0=0goto a} //若碰到休止符,延时100ms,继续取下一音符

else {m=music_tab[i++], n=music_tab[i++]} //取频率常数 和 节拍常数

TR0=1//开定时器1

while(n!=0) Beep=~Beep,delay(m)//等待节拍完成, 通过P1口输出音频(可多声道哦!)

TR0=0//关定时器1

}

}

你参考这个吧

#include\r\n#include\r\n//本例采用89C52,晶振为11.0592MHZ\r\n//关于带橡碰如何编制音乐如陪代码,其实十分简单,各位可以看以下代码.\r\n//频率蠢谈常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍\r\n//所以拿出谱子,试探编吧!\r\nsbitBeep=P1^5\r\n\r\nunsignedcharn=0//n为节拍常数变量\r\nunsignedcharcodemusic_tab[]={\r\n0x18,0x30,0x1C,0x10,//格式为:频率常数,节拍常数,频率常数,节拍常数,\r\n0x20,0x40,0x1C,0x10,\r\n0x18,0x10,0x20,0x10,\r\n0x1C,0x10,0x18,0x40,\r\n0x1C,0x20,0x20,0x20,\r\n0x1C,0x20,0x18,0x20,\r\n0x20,0x80,0xFF,0x20,\r\n0x30,0x1C,0x10,0x18,\r\n0x20,0x15,0x20,0x1C,\r\n0x20,0x20,0x20,0x26,\r\n0x40,0x20,0x20,0x2B,\r\n0x20,0x26,0x20,0x20,\r\n0x20,0x30,0x80,0xFF,\r\n0x20,0x20,0x1C,0x10,\r\n0x18,0x10,0x20,0x20,\r\n0x26,0x20,0x2B,0x20,\r\n0x30,0x20,0x2B,0x40,\r\n0x20,0x20,0x1C,0x10,\r\n0x18,0x10,0x20,0x20,\r\n0x26,0x20,0x2B,0x20,\r\n0x30,0x20,0x2B,0x40,\r\n0x20,0x30,0x1C,0x10,\r\n0x18,0x20,0x15,0x20,\r\n0x1C,0x20,0x20,0x20,\r\n0x26,0x40,0x20,0x20,\r\n0x2B,0x20,0x26,0x20,\r\n0x20,0x20,0x30,0x80,\r\n0x20,0x30,0x1C,0x10,\r\n0x20,0x10,0x1C,0x10,\r\n0x20,0x20,0x26,0x20,\r\n0x2B,0x20,0x30,0x20,\r\n0x2B,0x40,0x20,0x15,\r\n0x1F,0x05,0x20,0x10,\r\n0x1C,0x10,0x20,0x20,\r\n0x26,0x20,0x2B,0x20,\r\n0x30,0x20,0x2B,0x40,\r\n0x20,0x30,0x1C,0x10,\r\n0x18,0x20,0x15,0x20,\r\n0x1C,0x20,0x20,0x20,\r\n0x26,0x40,0x20,0x20,\r\n0x2B,0x20,0x26,0x20,\r\n0x20,0x20,0x30,0x30,\r\n0x20,0x30,0x1C,0x10,\r\n0x18,0x40,0x1C,0x20,\r\n0x20,0x20,0x26,0x40,\r\n0x13,0x60,0x18,0x20,\r\n0x15,0x40,0x13,0x40,\r\n0x18,0x80,0x00\r\n}\r\n\r\nvoidint0()interrupt1//采用中断0控制节拍\r\n{TH0=0xd8\r\nTL0=0xef\r\nn--\r\n}\r\n\r\nvoiddelay(unsignedcharm)//控制频率延时\r\n{\r\nunsignedi=3*m\r\nwhile(--i)\r\n}\r\n\r\nvoiddelayms(unsignedchara)//豪秒延时子程序\r\n{\r\nwhile(--a)//采用while(--a)不要采用while(a--)各位可编译一下看看汇编结果就知道了!\r\n}\r\n\r\nvoidmain()\r\n{unsignedcharp,m//m为频率常数变量\r\nunsignedchari=0\r\nTMOD&=0x0f\r\nTMOD|=0x01\r\nTH0=0xd8TL0=0xef\r\nIE=0x82\r\nplay:\r\nwhile(1)\r\n{\r\na:p=music_tab[i]\r\nif(p==0x00){i=0,delayms(1000)gotoplay}//如果碰到结束符,延时1秒,回到开始再来一遍\r\nelseif(p==0xff){i=i+1delayms(100),TR0=0gotoa}//若碰到休止符,延时100ms,继续取下一音符\r\nelse{m=music_tab[i++],n=music_tab[i++]}//取频率常数和节拍常数\r\nTR0=1//开定时器1\r\nwhile(n!=0)Beep=~Beep,delay(m)//等待节拍完成,通过P1口输出音频(可多声道哦!)\r\nTR0=0//关定时器1\r\n}\r\n}\r\n\r\n你参考这个吧


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12545940.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存