产生一个短延时,延时的时间正好是音符的频率
延时时间越短产生的频率就越高,发出的声音也越高
SONG_LONG[i]*20
是用于音符的持续时间。
就相当于长按跟短按一个按键一样
#include <reg51.h>sbit speaker=P1^2
unsigned char timer0h,timer0l,time
//生日歌
code unsigned char sszymmh[]={5,1,1, 5,1,1, 6,1,2, 5,1,2, 1,2,2, 7,1,4,
5,1,1, 5,1,1, 6,1,2, 5,1,2, 2,2,2, 1,2,4,
5,1,1, 5,1,1, 5,2,2, 3,2,2, 1,2,2, 7,1,2, 6,1,2,
4,2,1, 4,2,1, 3,2,2, 1,2,2, 2,2,2, 1,2,4}
// 音阶频率表 高八位
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<75){ //音乐数组长度 ,唱完从头再来
k=sszymmh[i]+7*sszymmh[i+1]-1
timer0h=FREQH[k]
timer0l=FREQL[k]
time=sszymmh[i+2]
i=i+3
song()
}
}
}
T0H EQU 30HT0L EQU 31H
ORG 0000H
LJMP MAIN
ORG 000BH
LJMP T0ISR
ORG 0030H
MAIN:
MOV SP,#5FH
MOV TMOD,#01H
SETB ET0
SETB EA
LOOP:
MOV DPTR,#FREQ
MOV A,P2 控制高中低音区
MOV R1,#0
ANL A,#03H
JB ACC.0,LOOP01
MOV R1,#14
SJMP LOOP0
LOOP01:
JB ACC.1,LOOP0
MOV R1,#28
LOOP0:
JB P0.0,LOOP1 控制音频1234567
MOV A,R1
MOVC A,@A+DPTR
MOV T0H,A
INC DPTR
MOV A,R1
MOVC A,@A+DPTR
MOV T0L,A
LJMP LOOP
LOOP1:
JB P0.1,LOOP2
MOV A,R1
ADD A,#2
MOVC A,@A+DPTR
MOV T0H,A
INC DPTR
MOV A,R1
ADD A,#2
MOVC A,@A+DPTR
MOV T0L,A
LJMP LOOP
LOOP2:
JB P0.2,LOOP3
MOV A,R1
ADD A,#4
MOVC A,@A+DPTR
MOV T0H,A
INC DPTR
MOV A,R1
ADD A,#4
MOVC A,@A+DPTR
MOV T0L,A
LJMP LOOP
LOOP3:
JB P0.3,LOOP4
MOV A,R1
ADD A,#6
MOVC A,@A+DPTR
MOV T0H,A
INC DPTR
MOV A,R1
ADD A,#6
MOVC A,@A+DPTR
MOV T0L,A
LJMP LOOP
LOOP4:
JB P0.4,LOOP5
MOV A,R1
ADD A,#8
MOVC A,@A+DPTR
MOV T0H,A
INC DPTR
MOV A,R1
ADD A,#8
MOVC A,@A+DPTR
MOV T0L,A
LJMP LOOP
LOOP5:
JB P0.5,LOOP6
MOV A,R1
ADD A,#10
MOVC A,@A+DPTR
MOV T0H,A
INC DPTR
MOV A,R1
ADD A,#10
MOVC A,@A+DPTR
MOV T0L,A
LJMP LOOP
LOOP6:
JB P0.6,LOOP7
MOV A,R1
ADD A,#12
MOVC A,@A+DPTR
MOV T0H,A
INC DPTR
MOV A,R1
ADD A,#12
MOVC A,@A+DPTR
MOV T0L,A
LJMP LOOP
LOOP7:
CLR TR0
LJMP LOOP
-----------------------------
T0ISR:
CLR TR0
MOV TH0,T0H
MOV TL0,T0L
SETB TR0
CPL P3.0
RETI
-----------------------------
FREQ:
DB 0F2H,03DH 低音1
DB 0F3H,0BDH 低音2
DB 0F5H,014H 低音3
DB 0F5H,0B1H 低音4
DB 0F6H,0D0H 低音5
DB 0F7H,0D1H 低音6
DB 0F8H,0B5H 低音7
DB 0F9H,01EH 中音1
DB 0F9H,0DEH 中音2
DB 0FAH,08AH 中音3
DB 0FAH,0D8H 中音4
DB 0FBH,068H 中音5
DB 0FBH,0E8H 中音6
DB 0FCH,05AH 中音7
DB 0FCH,08FH 高音1
DB 0FCH,0EFH 高音2
DB 0FDH,045H 高音3
DB 0FDH,06CH 高音4
DB 0FDH,0B4H 高音5
DB 0FDH,0F4H 高音6
DB 0FEH,029H 高音7
-----------------------------
END
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)