#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS=P2^6;
sbit RW=P2^5;
sbit E=P2^7;
sbit R=P1^0;
sbit C=P1^1;
sbit L=P1^2;
sbit A1=P1^3;
sbit A0=P1^4;
#define LCD_data P0
uchar code table1[10]={"R= R"};
uchar code table2[10]={"C= pF"};
uchar code table3[10]={"L= mH"};
uchar code f_table[88]={13,14,15,16,17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100};
uchar code f_correct[88]={9,10, 11, 12, 12, 12, 13, 14, 15, 16, 17, 18, 18, 18, 19, 20, 21, 21, 22, 23, 24, 25, 25, 26, 26, 27, 28, 29, 29, 30, 30, 31, 32, 33, 33, 34, 35, 35, 36, 37, 38, 38, 39, 40, 41, 41, 42, 42, 43, 44, 45, 45, 46, 47, 48, 49, 49, 50, 51, 51, 52, 53, 54, 54, 55, 55, 56, 57, 57, 58, 59, 60, 60, 62, 62, 63, 64, 64, 65, 66, 66, 67, 67, 68, 69, 70, 71, 71,};
uchar a6,a5,a4,a3,a2,a1;
uchar flag;
unsigned long cnt,cnt1;
uchar f_cnt;
/********************************/
void delay_us(); //18us
void delay_ms(uint);
void lcd_init();
void lcd_write_com(uchar com);
void lcd_write_dat(uchar dat);
void lcd_init();
void lcd_display(uchar add,uchar dat);
/***********************************/
void delay_us()
{
uchar x;
for(x=0;x<5;x++);
}
void delay_ms(uint z)
{
uint x,y;
for(x=0;x<z;x++)
for(y=0;y<123;y++);
}
void lcd_write_com(uchar com)
{
E=0;
RS=0;
RW=0;
delay_us();
LCD_data=com;
E=1; //高脉冲写入数据
delay_us();
E=0;
}
void lcd_write_dat(uchar dat)
{
E=0;
RS=1;
RW=0;
delay_us();
LCD_data=dat;
E=1; //高脉冲写入数据
delay_us();
E=0;
}
void lcd_init() //lcd初始化
{
delay_ms(15);
lcd_write_com(0x38);
delay_ms(10);
lcd_write_com(0x0c);
lcd_write_com(0x06);
lcd_write_com(0x01);
delay_ms(2);
}
void timer_init(void) //定时器初始化
{
TMOD=0X51;
PT0=1; /*中断优先*/
TH0=0x3c;
TL0=0xb0;
TH1=0;
TL1=0;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
EA=1;
}
void lcd_display(uchar add,uchar dat) //lcd显示(地址,数据)
{
lcd_write_com(add);
lcd_write_dat(dat);
delay_us();
}
void real_display(void)
{
if(!R)
{
// while(!R);
A1=A0=0;
lcd_display(0x80,table1[0]);
lcd_display(0x80+1,table1[1]);
lcd_display(0x80+8,table1[8]);
lcd_display(0x80+9,table1[9]);
}
else if(!C)
{
A0=0;A1=1;
lcd_display(0x80,table2[0]);
lcd_display(0x80+1,table2[1]);
lcd_display(0x80+8,table2[8]);
lcd_display(0x80+9,table2[9]);
}
else if(!L)
{
A0=1;A1=0;
lcd_display(0x80,table3[0]);
lcd_display(0x80+1,table3[1]);
lcd_display(0x80+8,table3[8]);
lcd_display(0x80+9,table3[9]);
}
if(a6)
lcd_display(0x80+2,0x30+a6);
else
lcd_display(0x80+2,' ');
if(a6||a5)
lcd_display(0x80+3,0x30+a5);
else
lcd_display(0x80+3,' ');
if(a6||a5||a4)
lcd_display(0x80+4,0x30+a4);
else
lcd_display(0x80+4,' ');
if(a6||a5||a4||a3)
lcd_display(0x80+5,0x30+a3);
else
lcd_display(0x80+5,' ');
if(a6||a5||a4||a3||a2)
lcd_display(0x80+6,0x30+a2);
else
lcd_display(0x80+6,' ');
lcd_display(0x80+7,0x30+a1);
}
void correct(void) //误差修正函数
{
uchar i,k;
unsigned long wucha;
if(cnt<100000) //100KHz以内的修正
{
if(cnt>980&&cnt<2100) cnt-=1;
if(cnt>=2100&&cnt<3900) cnt-=2;
if(cnt>=3900&&cnt<4800) cnt-=3;
if(cnt>=4800&&cnt<5700) cnt-=4;
if(cnt>=5700&&cnt<8000) cnt-=5;
if(cnt>=8000&&cnt<9100) cnt-=6;
if(cnt>=9100&&cnt<10900) cnt-=7;
if(cnt>=10900&&cnt<11900) cnt-=8;
if(cnt>=11900&&cnt<13000) cnt-=9;
if(cnt>=13000&&cnt<=100000)
{
k=cnt/1000;
for(i=0;i<88;i++)
{
if(k==f_table[i])
{
cnt-=f_correct[i];
}
}
}
}
if(cnt>100000)
{
wucha=(cnt/1000)*73065/100000;
cnt-=wucha;
}
}
void main()
{
timer_init();
lcd_init();
while(1)
{
if(flag==1)
{
real_display();
flag=0;
}
}
}
void timer0() interrupt 1
{
uchar timer0;
TH0=0x3c; //50ms
TL0=0xb0;
timer0++;
if(timer0==20)
{
TR1=0; //关闭的计数器
EA=0;
cnt=TL1+TH1*256+f_cnt*65536;
correct();
//cnt1=(1e+9)/(2*0.693 *cnt)-20000/2;
if(!R)
{
// while(!R);
cnt1=1000000/(0.2*0.693*cnt)-165;
}
else if(!C)
{
cnt1=1000000000/(0.693*3*510*cnt);
}
else if(!L)
{
cnt1=(1e+9)/(4*3.14*3.14*cnt*cnt*0.05);
}
timer0=0;
a6=cnt1%10000000/100000;
a5=cnt1%100000/10000;
a4=cnt1%10000/1000;
a3=cnt1%1000/100;
a2=cnt1%100/10;
a1=cnt1%10;
flag=1;
TH1=0;
TL1=0;
TH0=0x3c;
TL0=0xb0;
cnt=0;
f_cnt=0;
EA=1;
TR1=1; //打开计数器
}
}
void int1() interrupt 3
{
f_cnt++;
}