找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1461|回复: 5
打印 上一主题 下一主题
收起左侧

使用AT89C4051单片机+CD4069的简易LC测试仪源程序电路原理图

[复制链接]
跳转到指定楼层
楼主
电路原理图如下:


单片机源程序如下:
#include <reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
/**********1602************/
sbit RS=P1^7;
sbit EN=P1^6;
sbit LCD_D4=P1^3;
sbit LCD_D5=P1^2;
sbit LCD_D6=P1^1;
sbit LCD_D7=P1^0;
sbit L_C=P3^0;
//---------------------------
bit Lc=1;
uchar cnt=0;
float counter=0;
float F1=0;
float F2=0;
float C1=0;
float L1=0;
ulong temp1=0;
ulong temp2=0;
unsigned char Data[8];
unsigned char Table[]="0123456789";
/********以下是函数声明********/
void LCD_en_write(void);//液晶使能
void LCD_by(uchar abc);//写字节
void LCD_set_xy(uchar x, uchar y);//写地址
void LCD_write_str(uchar X,uchar Y,uchar *s);//写字符串
void LCD_init(void);//液晶初始化
void del_ms(uint n);//ms延时函数
void delay_5us(void);//5us延时函数
void delay_nus(uint n);//N us延时函数
void T0_T1_ini(void);//定时器初始化
void process_8(unsigned long i,unsigned char *p) ;
void display_8C(unsigned char x, unsigned char y, unsigned char *p);
ulong calc_Cx(float cl);//计算被测电容Cx
float calc_Lx(float cl);//计算被测电感Lx
void display_8L(unsigned char x, unsigned char y, uchar *p);
//-----------------------------------------
void process_8(unsigned long i,uchar *p)
{
  p[0]=i/10000000%10;
  p[1]=i/1000000%10;
  p[2]=i/100000%10;
  p[3]=i/10000%10;
  p[4]=i/1000%10;
  p[5]=i/100%10;
  p[6]=i/10%10;
  p[7]=i%10;
}
//-------------------------------------------------------
void display_8C(unsigned char x, unsigned char y, uchar *p)
{
  unsigned char i;
  LCD_set_xy( x, y );
  RS=1;            
  for(i=0;i<8;i++)
  {
    LCD_by(Table[p[ i]]);
  }
}
//--------------------------------------------------------
void display_8L(unsigned char x, unsigned char y, uchar *p)
{
  unsigned char i;
  LCD_set_xy( x, y );
  RS=1;         
  for(i=0;i<8;i++)
  {
    if(i==6)
    {
      LCD_write_str(10,1,".");
    }
      LCD_by(Table[p[ i]]);
  }
}
//---------------------------------------------
void LCD_en_write(void)
{
     delay_5us();
     EN=1;        
     delay_5us();
     EN=0;        
}
//---------------------------------------
void LCD_by(uchar abc)
{
    delay_nus(500);
    if(((abc<<0)&0x80)==0)     
     LCD_D7=0;            
     else LCD_D7=1;         
    if(((abc<<1)&0x80)==0)     
     LCD_D6=0;              
     else LCD_D6=1;        
    if(((abc<<2)&0x80)==0)     
     LCD_D5=0;              
     else LCD_D5=1;        
    if(((abc<<3)&0x80)==0)     
     LCD_D4=0;              
     else LCD_D4=1;        
    LCD_en_write();

    if(((abc<<4)&0x80)==0)     
     LCD_D7=0;              
     else LCD_D7=1;         
    if(((abc<<5)&0x80)==0)     
     LCD_D6=0;              
     else LCD_D6=1;         
    if(((abc<<6)&0x80)==0)     
     LCD_D5=0;              
     else LCD_D5=1;         
    if(((abc<<7)&0x80)==0)     
     LCD_D4=0;              
     else LCD_D4=1;         
    LCD_en_write();
}
//----------------------------------------------
void LCD_set_xy( uchar x, uchar y )
  {
    uchar address;
    if (y == 0) address = 0x80 + x;
    else
    address = 0xc0 + x;
    RS=0;           
    LCD_by(address);
  }
//---------------------------------------------
void LCD_write_str(uchar X,uchar Y,uchar *s)
  {
    LCD_set_xy(X,Y);
    RS=1;
    while(*s)
    {
       LCD_by(*s);
       s++;
    }
  }
//------------------------------------
void LCD_init(void)     
{
    RS=0;            
    del_ms(500);

    LCD_by(0x30);
    del_ms(60);
    LCD_by(0x30);
    del_ms(10);
    LCD_by(0x30);
    del_ms(10);
    LCD_by(0x02);
    del_ms(10);
    LCD_by(0x28);
    del_ms(10);
    LCD_by(0x08);
    del_ms(10);
    LCD_by(0x01);
    del_ms(10);
    LCD_by(0x06);
    del_ms(10);
    LCD_by(0x0C);
    del_ms(100);
}
//----------------------------------------------
void delay_nus(uint n)//N us延时函数
  {
   uint i=0;
   for (i=0;i<n;i++){;}
  }
//------------------------------
void delay_5us(void)//@12.000MHz//5us延时函数
{
   unsigned char data i;

   _nop_();
   _nop_();
   i = 12;
   while (--i);
}

//------------------------------
void del_ms(uint n)//ms延时函数
{
   uchar j;
   while(n--)
  {for(j=0;j<125;j++);}
}
//------------------------------
void T0_T1_ini(void)
{
   TMOD=0x15;//T1方式1定时,T0方式1计数
   TH1=(65536-50000)/256;//50ms
   TL1=(65536-50000)%256;
   TH0=0x00;
   TL0=0x00;
   TR0=1;
   TR1=1;
   EA=1;
   ET1=1;
}
//-------------------------------
void timer1(void) interrupt 3 using 2//测频率
{
   TH1=(65536-50000)/256;//50ms
   TL1=(65536-50000)%256;
   TF1=0;
   cnt++;
   if(cnt==2)//0.1秒闸门
   {
     cnt=0;
     temp1=TL0;
     temp2=TH0;
     TR0=0;
     TL0=0;
     TH0=0;
     TR0=1;
     counter=((temp2*256)+temp1)*10;
     if(Lc==1)//先测出频率值F1
     {
       Lc=0;
       F1=counter;
      }
    }
}
//--------------------------------
ulong calc_Cx(float cx)//计算C
{
  float x;
  float temp1;
  
  temp1=(float)F1/(float)F2;
  x=(float)(((temp1*temp1)-1)*cx);        
  return x;
}
//--------------------------------
float calc_Lx(float lx)//计算L
{
  float x;
  float temp1;
  
  temp1=(float)F1/(float)F2;
  x=(float)(((temp1*temp1)-1)*lx);
  x=x*100;        
  return x;
}
//--------------------------------
void main(void)
{
  float L2,C2;
        
  del_ms(1000);//等待震荡稳定
  T0_T1_ini();
  LCD_init();
  del_ms(100);
  if(L_C==0)//档位错误
  {
    while(1)
    {
      LCD_write_str(4,0,"Error !!!");
    }
}  

  while(1)
  {
    F2=counter;//测频率值F2
               
    LCD_write_str(0,0,"fq=");
    process_8(counter,Data);
    display_8C(4,0,Data);
    LCD_write_str(14,0,"Hz");
               
     if(L_C==0)//测电感
     {
        L1=76.95;//uH
        L2=calc_Lx(L1);
        LCD_write_str(0,1,"Lx=");
        process_8(L2,Data);
        display_8L(4,1,Data);
        LCD_write_str(14,1,"uH");
      }
     if(L_C==1)//测电容
     {
        LCD_write_str(12,1," ");
        C1=2408;//pF
        C2=calc_Cx(C1);
        LCD_write_str(0,1,"Cx=");
        process_8(C2,Data);
        display_8C(4,1,Data);
        LCD_write_str(14,1,"pF");
      }   
   }
}

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏6 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:1136941 发表于 2024-11-22 11:19 | 只看该作者
电路图中的电容C8容量应该是2200PF
回复

使用道具 举报

板凳
ID:1136941 发表于 2024-11-22 11:21 | 只看该作者
电路图中的电容C8改为2200PF
回复

使用道具 举报

地板
ID:71955 发表于 2024-11-24 16:38 来自触屏版 | 只看该作者
问一下使用cd4069对比lm393哪一个频率稳,我用lm393做的频率很不稳导致数字乱跳
回复

使用道具 举报

5#
ID:1136941 发表于 2024-11-24 17:37 | 只看该作者
用CD4069的震荡频率在10HZ位跳动,测试值还比较稳定。C8用的是2200PF涤纶电容,其它的是独石电容。
回复

使用道具 举报

6#
ID:1136941 发表于 2024-11-25 16:49 | 只看该作者
精度还是可以满足一般应用的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表