谢谢大家,我想做的只是要求有声音就亮的,没有声音就灭,比较简单。我用了下面这个电路,通过定时器T0检测P2^4口可以。P2.4口默认是高电平的,这样可以接受输入,我发现一说话,那排灯是可以亮灭的,但是有个疑问就是:我用万用表测了P2.4口的电压,说话和没说话有变化,但是看到数值都是高电平,从测量看没有变成低电平,有声音信号输入,但是灯光为什么可以变化,现在有疑惑。用的是这段程序:
#include "reg51.h"
typedef unsigned char u8;
typedef unsigned int u16;
#define led P1 //流水灯控制
sbit voice=P2^0; //声音输入 1101 1011
u8 code tab1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//上到下
u8 code tab2[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//下到上
u8 code tab3[]={0xe7,0xdb,0xbd,0x7e};//中间到两边
u8 code tab4[]={0x7e,0xbd,0xdb,0xe7};//两边到中间
u8 he[4]={0,0,0,0};
u8 num=0;
u8 counter=0;//定时计数
u8 counter1=0;//定时计数
bit flag=0;//切换闪烁
bit flag1=0;
void delay1m(u16 x)
{
u16 i,j;
for(i=0;i<x;i++) //连数x次,约xms
for(j=0;j<120;j++); //数120次,约1ms
}
void time0_time1_init()
{
TMOD=0x11;
TH1=(65535-50000)/256;
TL1=(65535-50000)%256;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
TR0=0;
ET0=1;
TR1=1;
ET1=1;
EA=1;
}
void main()
{
voice=1; //输入的I/O口要先置1,高电平;
time0_time1_init();
delay1m(2000);
while(1)
{
if(!flag1)
{
if(!voice)
{
num++;flag1=1;
he[0]=he[1]=he[2]=he[3]=0;
if(num>=5){num=0;}
TR0=1;
}
}
}
}
void time1_interrupt() interrupt 3
{
TH1=(65535-50000)/256;
TL1=(65535-50000)%256;
counter++;
if(counter>=6)
{
counter=0;
switch(num)
{
case 0:if(flag==0){led=0x00;flag=1;}else {led=0xff;flag=0;} break;//闪烁
case 1: led=tab1[he[0]];he[0]++; if(he[0]>=8) he[0]=0;break;//上到下
case 2: led=tab2[he[1]];he[1]++; if(he[1]>=8) he[1]=0;break;//下到上
case 3: led=tab3[he[2]];he[2]++; if(he[2]>=4) he[2]=0;break;//中间到两边
case 4: led=tab4[he[3]];he[3]++; if(he[3]>=4) he[3]=0;break;//两边到中间
default:break;
}
}
}
void time0_interrupt() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
counter1++;
if(counter1>=20)
{
counter1=0;
flag1=0;
TR0=0;
}
} |