電路原理圖如下:
51hei.gif (43.89 KB, 下載次數: 0)
下載附件
2024-11-20 23:00 上傳
單片機源程序如下:
#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");
}
}
}
|