在使用NE555模块时,NE555是不需要驱动程序的,NE555提供一个接口就是P3^4接口,当然这个接口不是默认连接的,需要将J3用跳线帽将P3^4和SIGNAL连接起来,我们可以看到P3^4是作为T0定时器、计数器的外部输入端口,那么NE555用于频率测量时候的思路就是将T0设在计数模式工作方式2自动重装下,用来接收SIGNAL给它的脉冲,然后T1设在定时模式下,定时1s,之后将1s中T0测得的脉冲数取出,通过数码管显示出来。
单片机测量信号频率,并显示在数码管中,频率数据显示用5位数码管,单位是HZ
//当显示长度不足5位时,未使用到的数码管熄灭,在最左边的1位数码管用F作为提示符
//频率就是在1s内产生的多少个脉冲信号。
//P34脚T0用于计数(外部信号计数,内部信号定时。)
//T1用于定时
//T0采用8位自动重装,初值设为255,只要来一个脉冲信号就溢出进入中断
//在中断服务函数里面变量加1
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
uint maichong=0;
uint maichong1=0;
uint count=0;
void hc138(uint z)
{
switch(z)
{
case 4:P2=P2&0x1f|0x80;break;
case 5:P2=P2&0x1f|0xa0;break;
case 6:P2=P2&0x1f|0xc0;break;
case 7:P2=P2&0x1f|0xe0;break;
case 0:P2=P2&0x1f|0x00;break;
}
}
void delaysmg(uint z)
{
while(z--);
}
void initsystem()
{
hc138(5);P0=0x00;
hc138(4);P0=0xff;
hc138(0);
}
void inittimer()
{
TMOD=0x16; //0001 0110 定时器1定时模式工作方式1,定时器0计数模式工作方式2
TH0=0xff;//1111 1111
TL0=0xff;
TH1=(65536-50000)/256; //50ms
TL1=(65536-50000)%266;
TR0=1;
TR1=1;
EA=1;
ET0=1;
ET1=1;
}
void t0() interrupt 1 //定时器0中断函数
{
maichong++;
}
void t1() interrupt 3//定时器1中断函数
{
TH1=(65536-50000)/256; //50ms
TL1=(65536-50000)%266;
count++;
if(count==20)
{
count=0;
maichong1=maichong;
maichong=0;
}
}
void selectsmg(uint we,uint du)
{
hc138(6);
P0=0x01<<we;//0000 0001
hc138(0);
hc138(7);
P0=du;
delay(500);
p0=0xff;
hc138(0);
}
void display()
{
selectsmg(0,0x8e);//数码管第一位显示F
delaysmg(100);
selectsmg(1,0xff);//数码管第二位显示-
delaysmg(100);
selectsmg(2,0xff);//数码管第三位显示-
delaysmg(100);
if(maichong1>9999) //23456
{
selectsmg(3,table[maichong1/10000]); //2
delaysmg(100);
}
if(maichong1>999)
{
selectsmg(4,table[maichong1/1000%10]);//3
delaysmg(100);
}
if(maichong1>99)
{
selectsmg(5,table[maichong1/100%10]);//4
delaysmg(100);
}
if(maichong1>9)
{
selectsmg(6,table[maichong1/10%10]);//5
delaysmg(100);
}
selectsmg(7,table[maichong1%10]);
delaysmg(100);
}
void main()
{
initsystem();
inittimer();
while(1)
{
display();
}
}