怎么用单片机编写音乐程序

怎么用单片机编写音乐程序,第1张

//此程序在硬件上调试通过

//本程序的单片机晶振采用11.0592m

#include

<reg51.h>

sbit

speaker=p1^2

unsigned

char

timer0h,timer0l,time

//世上只有妈妈好数据表

code

unsigned

char

sszymmh[]={

6,2,3,

5,2,1,

3,2,2,

5,2,2,

1,3,2,

6,2,1,

5,2,1,

6,2,4,

3,2,2,

5,2,1,

6,2,1,

5,2,2,

3,2,2,

1,2,1,

6,1,1,

5,2,1,

3,2,1,

2,2,4,

2,2,3,

3,2,1,

5,2,2,

5,2,1,

6,2,1,

3,2,2,

2,2,2,

1,2,4,

5,2,3,

3,2,1,

2,2,1,

1,2,1,

6,1,1,

1,2,1,

5,1,6,

0,0,0

}

//

音阶频率

高八位

code

unsigned

char

freqh[]={

0xf2,0xf3,0xf5,0xf5,0xf6,0xf7,0xf8,

0xf9,0xf9,0xfa,0xfa,0xfb,0xfb,0xfc,0xfc,

//1,2,3,4,5,6,7,8,i

0xfc,0xfd,0xfd,0xfd,0xfd,0xfe,

0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xff,

}

//

音阶频率表

低八位

code

unsigned

char

freql[]={

0x42,0xc1,0x17,0xb6,0xd0,0xd1,0xb6,

0x21,0xe1,0x8c,0xd8,0x68,0xe9,0x5b,0x8f,

//1,2,3,4,5,6,7,8,i

0xee,0x44,

0x6b,0xb4,0xf4,0x2d,

0x47,0x77,0xa2,0xb6,0xda,0xfa,0x16,

}

void

delay(unsigned

char

t)

{

unsigned

char

t1

unsigned

long

t2

for(t1=0t1<tt1++)

{

for(t2=0t2<8000t2++)

{

}

}

tr0=0

}

void

t0int()

interrupt

1

{

tr0=0

speaker=!speaker

th0=timer0h

tl0=timer0l

tr0=1

}

void

song()

{

th0=timer0h

tl0=timer0l

tr0=1

delay(time)

}

void

main(void)

{

unsigned

char

k,i

tmod=1

//置ct0定时工作方式1

ea=1

et0=1//ie=0x82

//cpu开中断,ct0开中断

while(1)

{

i=0

while(i<100){

//音乐数组长度

,唱完从头再来

k=sszymmh[i]+7*sszymmh[i+1]-1

timer0h=freqh[k]

timer0l=freql[k]

time=sszymmh[i+2]

i=i+3

song()

}

}

}

//此程序在硬件上调试通过

//本程序的单片机晶振采用11.0592M

#include <reg51.h>

sbit speaker=P1^2

unsigned char timer0h,timer0l,time

//世上只有妈妈好数据表

code unsigned char sszymmh[]={6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1,

6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,

6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2,

5,2,1, 6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1,

2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0

}

// 音阶频率表 高八位

code unsigned char FREQH[]={

0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,

0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i

0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,

0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,

}

// 音阶频率表 低八位

code unsigned char FREQL[]={

0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,

0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i

0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,

0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,

}

void delay(unsigned char t)

{

unsigned char t1

unsigned long t2

for(t1=0t1<tt1++)

{

for(t2=0t2<8000t2++)

{

}

}

TR0=0

}

void t0int() interrupt 1

{

TR0=0

speaker=!speaker

TH0=timer0h

TL0=timer0l

TR0=1

}

void song()

{

TH0=timer0h

TL0=timer0l

TR0=1

delay(time)

}

void main(void)

{

unsigned char k,i

TMOD=1//置CT0定时工作方式1

EA=1

ET0=1//IE=0x82 //CPU开中断,CT0开中断

while(1)

{

i=0

while(i<100){ //音乐数组长度 ,唱完从头再来

k=sszymmh[i]+7*sszymmh[i+1]-1

timer0h=FREQH[k]

timer0l=FREQL[k]

time=sszymmh[i+2]

i=i+3

song()

}

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存