[文件名] C51音乐程序(八月桂花)
[功能]通过单片机演奏音乐
注意:通过了74HC14控制ULN2003 驱动芯片驱动蜂鸣器 *
/**********************************************************************/
#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
}
}
1、Beep蜂鸣
# include <dos.h># include <windows.h>
#include <iostream>
using namespace std
main(){
Beep(494, 200)
Beep(523, 300)
Beep(578, 400)
Beep(659, 500)
Beep(698, 600)
Beep(784, 700)
Beep(880, 800)
Beep(932, 900)
Beep(1046, 1000)
Beep(1175, 1100)
Beep(1381, 1200)
Beep(1480, 1300)
Beep(1661, 1400)
cout<<"all?right!"<<endl
cin.get()
return 0
}
附《祝你生日快乐》
#include <stdio.h>#include <windows.h>
typedef struct _sound
{
int n_frequence
int n_duration
char text[16]
}sound
#define LOW_1 262
#define LOW_2 294
#define LOW_3 330
#define LOW_4 349
#define LOW_5 392
#define LOW_6 440
#define LOW_7 494
#define MID_1 523
#define MID_2 578
#define MID_3 659
#define MID_4 698
#define MID_5 784
#define MID_6 880
#define MID_7 988
#define HIGH_1 1046
#define HIGH_2 1175
#define HIGH_3 1318
#define HIGH_4 1397
#define HIGH_5 1568
#define HIGH_6 1760
#define HIGH_7 1976
#define BEAT_120(x) (int)(x * 500)
#define BEAT_60(x) (int)(x * 1000)
void play_sound(sound s[], int n_len)
{
int i = 0
for (i = 0 i < n_len i++)
{
if (strlen(s[i].text) != 0)
{
printf("%s", s[i].text)
}
Beep(s[i].n_frequence, s[i].n_duration)
}
}
main()
{
sound s_happy_birthday_to_you[] = {
{MID_5, BEAT_120(0.5) + BEAT_120(0.25), "祝"},
{MID_5, BEAT_120(0.25), "你"},
{MID_6, BEAT_120(1), "生"},
{MID_5, BEAT_120(1), "日"},
{HIGH_1, BEAT_120(1), "快"},
{MID_7, BEAT_120(2), "乐\n"},
{MID_5, BEAT_120(0.5) + BEAT_120(0.25), "祝"},
{MID_5, BEAT_120(0.25), "你"},
{MID_6, BEAT_120(1), "生"},
{MID_5, BEAT_120(1), "日"},
{HIGH_2, BEAT_120(1), "快"},
{HIGH_1, BEAT_120(2), "乐\n"},
{MID_5, BEAT_120(0.5) + BEAT_120(0.25), "祝"},
{MID_5, BEAT_120(0.25), "你"},
{HIGH_5, BEAT_120(1), "生"},
{HIGH_3, BEAT_120(1), "日"},
{HIGH_1, BEAT_120(1), "快"},
{MID_7, BEAT_120(1), "乐\n"},
{MID_6, BEAT_120(1)},
{HIGH_4, BEAT_120(0.5) + BEAT_120(0.25), "祝"},
{HIGH_4, BEAT_120(0.25), "你"},
{HIGH_3, BEAT_120(1), "生"},
{HIGH_1, BEAT_120(1), "日"},
{HIGH_2, BEAT_120(1), "快"},
{HIGH_1, BEAT_120(2), "乐\n"}
}
play_sound(s_happy_birthday_to_you, sizeof(s_happy_birthday_to_you) / sizeof(sound))
}
2、PlaySound播放
#include <windows.h>#include <stdlib.h>
int main(int argc, char* argv[])
{
PlaySound("C:\\WINDOWS\\Media\\Windows XP 启动.wav", NULL, SND_FILENAME | SND_ASYNC)
system("pause")
return 0
}
Integer abc =nullif(1!=2)
abc =abc
Integer num = null
num = 1==2 ? 0 : num
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)