謝謝大家,我想做的只是要求有聲音就亮的,沒有聲音就滅,比較簡單。我用了下面這個電路,通過定時器T0檢測P2^4口可以。P2.4口默認是高電平的,這樣可以接受輸入,我發(fā)現(xiàn)一說話,那排燈是可以亮滅的,但是有個疑問就是:我用萬用表測了P2.4口的電壓,說話和沒說話有變化,但是看到數(shù)值都是高電平,從測量看沒有變成低電平,有聲音信號輸入,但是燈光為什么可以變化,現(xiàn)在有疑惑。用的是這段程序:
#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;//定時計數(shù)
u8 counter1=0;//定時計數(shù)
bit flag=0;//切換閃爍
bit flag1=0;
void delay1m(u16 x)
{
u16 i,j;
for(i=0;i<x;i++) //連數(shù)x次,約xms
for(j=0;j<120;j++); //數(shù)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;
}
} |