⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 f_detecter.c

📁 F_Dtecter 单片机频率计 精确度1%以上
💻 C
字号:
/************************************************************
*频率计
*12M晶振,rs=P0^5,rw=P0^6,en=P0^7,Dx=P2
*Author:熊伟华
*2007
*************************************************************/
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int

uchar Dis0[]={" Welcome to the "},Dis1[]={"Frequency Detect"},Dis2[]={"The Frequency is"},Dis3[]={"0123456789Hz"},u;

sbit rs=P0^5;
sbit rw=P0^6;
sbit en=P0^7;

uchar temp[8];
uchar T0count=0;
uchar timecount=0;
bit flag;
unsigned long x;

void delay(uchar t);
void LCD_init();
uchar LCD_read_state();
void LCD_write_order(uchar temp0);
void LCD_read_date();
void LCD_write_date(uchar temp1);
void LCD_pos(uchar pos);

/************************************************************
*Function:延时处理
*parameter:
*Return:
*Modify:
*************************************************************/
void delay(uchar t)
{ uchar tb;
  while(t--)
   { for (tb=0;tb<127;tb++)
       {_nop_();
	    _nop_();
		_nop_();                                                                                                              
		_nop_();}
   }
}

/************************************************************
*Function:1602初始化
*parameter:
*Return:
*Modify:
*************************************************************/
void LCD_init()
{rs=0;
 _nop_();
 rw=0;
 _nop_();
 en=1;
 _nop_();
 LCD_write_order(0x38);
 delay(15);
 LCD_write_order(0x0c);
 delay(5);
 LCD_write_order(0x06);
 _nop_();
 en=0;
}
/************************************************************
*Function:读1602的状态
*parameter:
*Return:0为空闲,1为忙
*Modify:
*************************************************************/
uchar LCD_read_state(void)
{ uchar u;
  rs=0;
  _nop_();
  rw=1;
  _nop_();
  en=1;
  _nop_();
  u=P2;
  return(u);
  _nop_();
  en=0;
}
/************************************************************
*Function:向1602写命令字
*parameter:
*Return:
*Modify:
*************************************************************/
void LCD_write_order(uchar temp)
{ LCD_read_state();
  _nop_();
  if ((u&&0x80)==0)
   { rs=0;
     _nop_();
     rw=0;
     _nop_();
     en=1;
     _nop_();
     P2=temp;
   }
  _nop_();
  en=0;
}
/************************************************************
*Function:从1602读数据
*parameter:
*Return:
*Modify:
*************************************************************/
void LCD_read_date()
{ LCD_read_state();
  _nop_();
  if ((u&&0x80)==0)
   { rs=1;
     _nop_();
     rw=1;
     _nop_();
     en=1;
     _nop_();
   }
  _nop_();
  en=0;
}
/************************************************************
*Function:向1602写显示数据
*parameter:
*Return:
*Modify:
*************************************************************/
void LCD_write_date(uchar temp1)
{ LCD_read_state();
  _nop_();
  if ((u&&0x80)==0)
   { rs=1;
     _nop_();
     rw=0;
     _nop_();
     en=1;
     _nop_();
     P2=temp1;
   }
  _nop_();
  en=0;
}
/************************************************************
*Function:显示位置 00-0f为第一行,40-4f为第二行
*parameter:
*Return:
*Modify:
*************************************************************/
void LCD_pos(uchar pos)
{LCD_write_order(pos|0x80);}
/************************************************************
*Function:显示欢迎界面
*parameter:
*Return:
*Modify:
*************************************************************/
void LCD_Dis()
{ uchar i,temp0=0x00,temp1=0x40; 
  LCD_init();
  for (i=0;i<=15;i++)
   { LCD_pos(temp0);
     delay(1);
     LCD_write_date(Dis0[i]);
	 delay(1);
	 LCD_pos(temp1);
	 delay(1);
	 LCD_write_date(Dis1[i]);
	 temp0++;
	 temp1++;
   }
  delay(1000);
  delay(1000);
  delay(1000);
  delay(1000);
  temp0=0x00;
  LCD_write_order(0x01);
  for (i=0;i<=15;i++)
   { LCD_pos(temp0);
     delay(1);
     LCD_write_date(Dis2[i]);
	 temp0++;
   }
}
/************************************************************
*Function:定时器/计数器初始化
*parameter:
*Return:
*Modify:
*************************************************************/
void timer_init()
{ TMOD=0x15;
  TH0=0;
  TL0=0;
  TH1=(65536-4000)/256;
  TL1=(65536-4000)%256;
  TR1=1;
  TR0=1;
  ET0=1;
  ET1=1;
  EA=1;
}
/************************************************************
*Function:中断服务程序
*parameter:
*Return:
*Modify:
*************************************************************/
void t0(void) interrupt 1 using 0
{
  T0count++;
}
void t1(void) interrupt 3 using 0
{
  TH1=0x3c;
  TL1=0xb0;
  timecount++;
  if(timecount==60)
    {
      TR0=0;
      timecount=0;
      flag=1;
    }
}
/************************************************************
*Function:中断服务程序
*parameter:
*Return:
*Modify:
*************************************************************/
void counter ()
{
   while(1)
    {uchar i=0,pos=0x49;
     if(flag==1)
       {
         flag=0;
         x=T0count*65536+TH0*256+TL0;
         for(i=0;i<8;i++)
           {
             temp[i]=0;
           }
         i=0;
		 x=x/3;
         while(x/10)
           {
             temp[i]=x%10;
             x=x/10;
             i++;
           }
         temp[i]=x;
		 i=0;
         LCD_pos(0x4a);
         LCD_write_date(Dis3[10]);
		 LCD_pos(0x4b);
		 LCD_write_date(Dis3[11]);
		 for (i=0;i<=5;i++)
          { LCD_pos(pos);
            delay(1);
            LCD_write_date(Dis3[temp[i]]);
	        pos--;
          }
         timecount=0;
         T0count=0;
         TH0=0;
         TL0=0;
         TR0=1;
       }
	}
}
/************************************************************
*Function:主程序
*parameter:
*Return:
*Modify:
*************************************************************/
void main()
{LCD_Dis();
 timer_init();
 counter();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -