NE555测频

注意:

  1. 测频前要把P34和NAL用短路帽短接
  2. 因为P34口对应定时器0脉冲输入口,所以使用定时器0作为计数器,计数接收脉冲
  3. 改变RB3改变频率
  4. 原理图下方:
    NE555测频_第1张图片

测频部分代码:

计数器部分

void Timer0Init()
{
		TMOD = 0x04;  //使用定时器0作为计数器
		TL0 = 0;      //设置初值都为0
		TH0 = 0;
		TF0 = 0;
		TR0 = 1;
}

计算频率部分

if(flag == 1)        //设置一个500ms一次的标志位
{
		flag = 0;
		TR0 = 0;       			//关闭定时器
		Fre = TH0*256+TL0;      //计算500ms内的脉冲次数
		Fre = Fre*2;            //结果乘2就是1S内的频率
		TL0 = 0;                //初值再次清零
		TH0 = 0;
		TR0 = 1;                //启动计数器
}

全部代码:

main.c

******************************************************************************
* 文件名:NE555测频
* 描  述:
* 作  者:思索与猫
* 日  期: 19/3/28
* 备  注: 
*         
******************************************************************************
#include
typedef unsigned char uchar;
typedef unsigned int uint;

uchar code duan[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};
uchar disbuf[8];
uint Fre;
bit flag = 1;
void CloseFucker();
void Display();
void ShowNumber();
void Timer1Init();		//[email protected]
void Timer0Init();

void main()
{
		CloseFucker();
		Timer0Init();
		Timer1Init();
		while(1)
		{
				if(flag == 1)
				{
						flag = 0;
						TR0 = 0;
						Fre = TH0*256+TL0;
						Fre = Fre*2;
						TL0 = 0;
						TH0 = 0;
						TR0 = 1;
				}
				ShowNumber();
		}
}

void T1_time() interrupt 3
{
		static uint count = 0;
		Display();
		if(++count == 250)
		{
				count = 0;
				flag = 1;
		}
}

void Timer0Init()
{
		TMOD = 0x04;    
		TL0 = 0;
		TH0 = 0;
		TF0 = 0;
		TR0 = 1;
}

void Timer1Init()		//[email protected]
{
		AUXR |= 0x40;		
		TMOD = 0x0F;		
		TL1 = 0x40;		
		TH1 = 0xA2;		
		TF1 = 0;		
		TR1 = 1;	
		ET1 = 1;
		EA = 1;
}

void ShowNumber()
{
		disbuf[0] = 10;
		disbuf[1] = 10;
		disbuf[2] = 10;
		disbuf[3] = Fre/10000;
		disbuf[4] = Fre/1000%10;
		disbuf[5] = Fre/100%10;
		disbuf[6] = Fre/10%10;
		disbuf[7] = Fre%10;
}

void Display()
{
		static uchar index = 0;
	
		P2 = P2&0x1f|0xe0;
		P0 = 0xff;
		P2 = P2&0x1f;
	
		P2 = P2&0x1f|0xc0;
		P0 = 1<

你可能感兴趣的:(蓝桥杯单片机,蓝桥杯单片机)