电路是没问题的,蜂鸣器和LED彩灯单独是都可以工作,,,电路图挺简单的,就几个接口,看程序应该知道了,希望大家帮帮忙呗,谢谢!!!!
程序:#include<reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define LED0 P0
#define LED2 P2
uchar code water_led1[] = {0XFE,0XFD,0XFB,0XF7,0XEF,0XDF,0XBF,0X7F};//正向流动
uchar code water_led2[] = {0X7F,0XBF,0XDF,0XEF,0XF7,0XFB,0XFD,0XFE};//反向流动
sbit Buzz = P1^7; //声明绑定蜂鸣器
unsigned int code NoteFrequ[]=
{ 523,587,659,698,784,880,988, //中音对应的1-7
1047,1175,1319,1397,1568,1760,1976 //高音对应的1-7
};
unsigned int code NoteReload[]= //中音1-7和高音1-7对应的定时器重载值
{
65536 - (11059200/12) /(523*2),//中音1-7
65536 - (11059200/12) /(587*2),
65536 - (11059200/12) /(659*2),
65536 - (11059200/12) /(698*2),
65536 - (11059200/12) /(784*2),
65536 - (11059200/12) /(880*2),
65536 - (11059200/12) /(988*2),
65536 - (11059200/12) /(1047*2),//高音1-7
65536 - (11059200/12) /(1175*2),
65536 - (11059200/12) /(1319*2),
65536 - (11059200/12) /(1397*2),
65536 - (11059200/12) /(1568*2),
65536 - (11059200/12) /(1760*2),
65536 - (11059200/12) /(1970*2)
};
bit enable = 1; //发声使能标志
bit tmrflay = 0; //定时器 中断完成标志
unsigned char T0RH = 0xff; //T0重载值高字节
unsigned char T0RL = 0x00; //T0重载值低字节
void PlayTwotiger();
void main()
{
uint j = 0;
uint count = 0;
unsigned int i;
EA = 1;
TMOD =0x11; //模式1
TH0 = T0RH;
TL0 = T0RL;
ET0 = 1; //使能T0中断
TR0 = 1; //启动
TH1 = 0XF8;
TL1 = 0X43;
TR1 = 1;//启动T1
while(1)
{
if (TF1 == 1)//判断TF0是否溢出
{
TF1 = 0;//T1溢出后,清零
TH1 = 0XF8; //B8;
TL1 = 0X43; //ef;
count ++;
if (count == 100)//计数
{
count = 0;
if ( j < 8 && j>=0 ) LED0 = water_led1[j];
if ( j >= 8 && j < 16)
{
LED0 = 0XFF;
LED2 = water_led1[j - 8 ];
}
if ( j == 17 ) LED2 = 0XFF;
if ( j >= 18 && j < 26) LED2 = water_led2[j - 18 ];
if ( j >= 26 && j < 34)
{
LED2 = 0XFF;
LED0 = water_led2[j - 26 ];
}
if ( j >= 34 && j < 42 )
{
LED0 = 0XCC;
LED2 = 0XCC;
}
if ( j >= 42 && j < 50 )
{
LED0 = 0X33;
LED2 = 0X33;
}
if ( j >= 50 && j < 58 )
{
LED0 = water_led1[j - 50];
LED2 = water_led2[j - 50];
}
if ( j >= 58 && j < 66 )
{
LED0 = water_led2[j - 58];
LED2 = water_led1[j - 58];
}
if ( j >= 66 && j < 68)
{
LED0 = 0XFF;
LED2 = 0XFF;
}
if ( j >= 68 && j < 72)
{
LED0 = 0X00;
LED2 = 0X00;
}
if ( j >= 72 && j < 74)
{
LED0 = 0XFF;
LED2 = 0XFF;
}
if ( j >= 74 && j < 76)
{
LED0 = 0XFF;
LED2 = 0XFF;
}
if ( j >= 76 && j < 80)
{
LED0 = 0X00;
LED2 = 0X00;
}
if ( j >= 80 && j < 82)
{
LED0 = 0XFF;
LED2 = 0XFF;
}
j ++;
if ( j == 82) j = 0;
}
}
PlayTwoTiger();
for(i=0;i<40000;i++);
}
}
/**音乐函数**/
void PlayTwoTiger()
{
unsigned char beat; //节拍索引
unsigned char note; //节拍对应音符
unsigned int time=0; //节拍计时
unsigned int beattime=0; //总时间计时
unsigned int soundtime=0; //没拍发声时间
unsigned char code PlayTwoTigerNote[]= //音符表
{
1,2,3,1, 1,2,3,1, 3,4,5, 3,4,5,
5,6,5,4,3,1, 5,6,5,4,3,1, 2,1,4, 2,1,4 // 3,1,4, 3,1,4 // 1,5,1, 1,5,1
};
unsigned char code PlayTwoBeat[]= //节拍表,4表示一拍,1表示1/4拍,8表示两拍
{
4,4,4,4, 4,4,4,4, 4,4,8, 4,4,8,
3,1,3,1,4,4, 3,1,3,1,4,4, 4,4,8, 4,4,8,
};
for(beat=0; beat<sizeof(PlayTwoTigerNote);) //节拍索引循环变量
{
while(!tmrflay); //每次定时器中断完成 节拍处理
tmrflay = 0;
if(time == 0) //节拍播放完成重启
{
note = PlayTwoTigerNote[beat]-1;
T0RH = NoteReload[note]>>8;
T0RL = NoteReload[note]; //计算总时间,右移2位等于除4,移位代替除法加快速度
beattime = (PlayTwoBeat[beat]*NoteFrequ[note])
>>2; //计算发声时间,为总时间的0.75s
soundtime =beattime - (beattime>>2);
enable = 1; //开始发声
time++;
}
else //节拍播放未结束则继续处理
{
if(time >= beattime) //当前时间清零
{
time = 0; //准备重新启动
beat++;
}
else //累加时间
{
time++;
if(time == soundtime) //发声时间到达,关闭蜂鸣器
{
enable =0; //用以区分连续两个节拍
}
}
}
}
}
void InterRupt() interrupt 1 //中断服务
{
TH0 =T0RH;
TL0 =T0RL;
tmrflay = 1;
if(enable){
Buzz=~Buzz;
}else{
Buzz=1;
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>