|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#include"stc15.h"
#include "math.h"
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
//定义变量
//sbit frequence_flag= P3^5 ; // 脉冲接收信号T
//#define DataPort P2 //LCD1602数据端口
sbit LcdRs=P4^5; //LCD1602命令端口
sbit LcdRw=P4^4; //LCD1602命令端口
sbit LcdEn=P4^2; //LCD1602命令端口
uchar dis_tab1[]={"Welcome "}; //第一行显示数组
uchar dis_tab2[]={" "}; //第二行显示数组
sbit R1_KEY=P1^0;
sbit R2_KEY=P1^1;
sbit C1_KEY=P1^2;
sbit C2_KEY=P1^3;
sbit L_KEY=P1^4;
sbit auto_R_KEY=P3^6;
sbit auto_C_KEY=P3^3;
sbit AD_KEY=P3^7;
sbit A0=P5^4;
sbit A1=P5^5;
sbit EN1=P4^1;
sbit EN2=P3^4 ;
sbit R_relay=P3^0;
sbit C_relay=P3^1;
sbit L_relay=P3^2;
uint frequence=0,n,ad_conv;
float meaturement, meaturement_1=0.0,R_up_limit=19000,C_up_limit=90,R_low_limit=19,C_low_limit=0.09;
uint i,j,R_flag=0,C_flag=0;
uchar adc_dat_hi,adc_dat_low,status;
float sum=0,addata=0;
//函数声明
void delay(uint z);
void write_date(uchar date);
void write_com(uchar date);
void init_1602(void);
void LCD1602_CleanScans(void);
void LCD1602_SetPos(uchar x,uchar y);
void LCD1602_DisplayString(uchar *s);
void decimal_process(float date);
void integer_process(uint date);
void timer_init(void);
uchar key_scan(void);
void R1_CD4052(void);
void R2_CD4052(void);
void C1_CD4052(void);
void C2_CD4052(void);
void L_CD4052(void);
//延时
void delay(uint z)
{
uint a,b;
for(a=0; a<z; a++)
for(b=0; b<120; b++);
}
//发送单个字符
void write_date(uchar date)
{ LcdEn=0;
LcdRs=1;
LcdRw=0;
P2=date;
LcdEn=1;
delay(5);
LcdEn=0;
}
//发送命令
void write_com(uchar date)
{ LcdEn=0;
LcdRs=0;
LcdRw=0;
P2=date;
LcdEn=1;
delay(5);
LcdEn=0;
}
//初始化:dis_tab1和dis_tab2是初始化的内容
void init_1602()
{
unsigned char i=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01); //清屏
write_com(0x80); //第一行坐标
for (i=0;i<16;i++)
{
write_date(dis_tab1);
}
write_com(0x80+0x40); //第二行坐标
for (i=0;i<16;i++)
{
write_date(dis_tab2);
}
}
//清屏
void LCD1602_CleanScans()
{
write_com(0x01);
}
//void key_scan()
//{
//
// write_com(0x80+0x40);
// write_date(' ');
// write_date('r');
// write_date('u');
// write_date('n');
//
//}
//设定显示位置
void LCD1602_SetPos(uchar x,uchar y)
{
uchar pos;
if(x == 0)
x = 0x80;
else if(x == 1)
x = 0x80+0x40;
pos = x + y;
write_com(pos);
}
//显示字符串 LCD1602_DisplayString("1234")
void LCD1602_DisplayString(uchar *s)
{
uchar i = 0;
while(*s != 0)
{
write_date(*s);
s++;
delay(5);
}
}
//5位整数3位小数十进制转字符
void decimal_process(float date)
{
if((date/10000>1)||(date/10000==1))
dis_tab2[2]=((int)date)%100000/10000+0x30;
else
dis_tab2[2]=' ';
if((date/1000>1)||(date/1000==1))
dis_tab2[3]=((int)date)%10000/1000+0x30;
else
dis_tab2[3]=' ';
if((date/100>1)||(date/100==1))
dis_tab2[4]=((int)date)%1000/100+0x30;
else
dis_tab2[4]=' ';
if((date/10>1)||(date/10==1))
dis_tab2[5]=((int)date)%100/10+0x30;
else
dis_tab2[5]=' ';
if((date>1)||(date==1))
dis_tab2[6]=((int)date)%10+0x30;
else
dis_tab2[6]=0x30;
dis_tab2[7]='.';
if((date>0.1)||(date==0.1))
dis_tab2[8]=((long int)(date*10))%10+0x30;
else
dis_tab2[8]=0x30;
if((date>0.01)||(date==0.01))
dis_tab2[9]=((long int)(date*100))%10+0x30;
else
dis_tab2[9]=0x30;
if((date>0.001)||(date==0.001))
dis_tab2[10]=((long int)(date*1000))%10+0x30;
else
dis_tab2[10]=0x30;
}
//5位整数10进制转字符
void integer_process(uint date)
{
// if(data1/1000000>0)
// dis_tab2[0]=data1/1000000+0x30;
// else
// dis_tab2[0]=' ';
// if(data1/100000>0)
// dis_tab2[1]=data1%1000000/100000+0x30;
// else
// dis_tab2[1]=' ';
if(date/10000>0)
dis_tab1[2]=date%100000/10000+0x30;
else
dis_tab1[2]=' ';
if(date/1000>0)
dis_tab1[3]=date%10000/1000+0x30;
else
dis_tab1[3]=' ';
if(date/100>0)
dis_tab1[4]=date%1000/100+0x30;
else
dis_tab1[4]=' ';
if(date/10>0)
dis_tab1[5]=date%100/10+0x30;
else
dis_tab1[5]=' ';
dis_tab1[6]=date%10+0x30;
}
//定时器1和定时器0初始化
void timer_init()
{
TMOD=0x51; //定时器1工作在计数器方式1,定时器0工作在定时器方式1
TL0=0x00;
TH0=0x4C; //50ms
TL1=0X00;
TH1=0X00; //从0开始记数
i=20; //计数变量附初值
ET0=1; //允许T0中端
EA=1;
TR0=1; //启动T0
TR1=1; //启动T1计数
}
//AD采样
void ADC_init()
{
ADC_CONTR |= 0X80;
for(j = 0;j<10000;j++);
P1ASF = 0X20;
ADC_CONTR = 0XE5;
for(j = 0;j<10000;j++);
}
void get_adcDat()
{
for(n=0;n<6;n++)
{
ADC_CONTR |= 0X08;
for(j = 0;j<2000;j++);
status = 0;
while(status == 0)
status = ADC_CONTR&0X10;
ADC_CONTR &= 0XE7;
adc_dat_hi = ADC_RES;
adc_dat_low=ADC_RESL&0X03;
ad_conv=adc_dat_hi;
ad_conv=(ad_conv<<2)+adc_dat_low;
addata=(float)ad_conv;
addata=addata/256.0;
addata=addata*1.25;
sum+=addata;
}
n=0;
addata=sum/6.0;
sum=0;
}
//按键选择
uchar key_scan(void)
{
if(!R1_KEY||!R2_KEY||!C1_KEY||!C2_KEY||!L_KEY||!auto_R_KEY||!auto_C_KEY||!AD_KEY)
{
delay(10);
if(!R1_KEY) return 1;
if(!R2_KEY) return 2;
if(!C1_KEY) return 3;
if(!C2_KEY) return 4;
if(!L_KEY) return 5;
if(!auto_R_KEY) return 6;
if(!auto_C_KEY) return 7;
if(!AD_KEY) return 8;
}
return 0;
}
//继电器和CD4052模拟开关的选择
void R1_CD4052(void)
{
A0=0;
A1=1;
EN1=0;
EN2=1;
R_relay=1;
C_relay=0;
L_relay=0;
}
void R2_CD4052(void)
{
A0=0;
A1=0;
EN1=0;
EN2=1;
R_relay=1;
C_relay=0;
L_relay=0;
}
void C1_CD4052(void)
{
A0=1;
A1=1;
EN1=0;
EN2=1;
R_relay=0;
C_relay=1;
L_relay=0;
}
void C2_CD4052(void)
{
A0=1;
A1=0;
EN1=0;
EN2=1;
R_relay=0;
C_relay=1;
L_relay=0;
}
void L_CD4052(void)
{
A0=0;
A1=0;
EN1=1;
EN2=0;
R_relay=0;
C_relay=0;
L_relay=1;
}
void VD_CD4052(void)
{
A0=0;
A1=0;
EN1=1;
EN2=1;
R_relay=0;
C_relay=0;
L_relay=0;
}
void main()
{
init_1602();
timer_init();
ADC_init();
while(1)
{
switch(key_scan())
{
case 1:
LCD1602_SetPos(0,0);
LCD1602_DisplayString("R: ");
R1_CD4052();
if(frequence==0)
{
meaturement=0;
}
else
{
meaturement=((float)frequence)+1.0; //补偿
meaturement=10.0/(meaturement*0.693147*0.000001);
meaturement=meaturement/21.0-420.476; //频率转电阻值
meaturement=meaturement-756;
}
// if(meaturement<1800)
// meaturement=meaturement-790;
// else
// meaturement=meaturement-840;
dis_tab2[11]=0xF4;
dis_tab2[12]=' ';
break;
case 2:
LCD1602_SetPos(0,0);
LCD1602_DisplayString("R: ");
R2_CD4052();
if(frequence==0)
{
meaturement=0;
}
else
{
meaturement=((float)frequence);
meaturement=1.0/(meaturement*0.7*0.00001)-20.0;
meaturement=meaturement/2.0;
if(meaturement>=380.0)
meaturement=0.7856*meaturement;
else
if(meaturement<380.0&meaturement>=35.0)
meaturement=0.812*meaturement;
else
meaturement=0.79*meaturement;
if(meaturement<=310)
meaturement=(meaturement-1.2436)/1.0196;
else
meaturement=(meaturement+92.642)/1.213; // y = 1.213x - 92.642
meaturement=0.98*meaturement;
}
dis_tab2[11]='K';
dis_tab2[12]=0xF4; //kΩ
break;
case 3:
LCD1602_SetPos(0,0);
LCD1602_DisplayString("C: ");
C1_CD4052();
if(frequence==0)
{
meaturement=0;
}
else
{
meaturement=(float)frequence*0.965;
meaturement=1.0/(meaturement*0.7*0.0003);
if(meaturement<7)
meaturement=meaturement*0.95-0.04;
}
dis_tab2[11]='n';
dis_tab2[12]='F'; //nf
break;
case 4:
LCD1602_SetPos(0,0);
LCD1602_DisplayString("C: ");
C2_CD4052();
if(frequence==0)
{
meaturement=0;
}
else
{
if(meaturement>=23)
meaturement=((float)frequence)*0.948;
else
meaturement=((float)frequence)*0.943;
meaturement=1.0/(meaturement*0.7*0.03);
}
dis_tab2[11]=0xE4;
dis_tab2[12]='F'; // uF
break;
case 5:
LCD1602_SetPos(0,0);
LCD1602_DisplayString("L: ");
L_CD4052();
if(frequence==0)
{
meaturement=0;
}
else
{
meaturement=((float)frequence);
if(meaturement>2200)
meaturement=2*meaturement/3;
else
if(meaturement>1700)
meaturement=0.585*meaturement;
else
meaturement=meaturement*6.5/12;
meaturement=1000.0/(meaturement*0.001*meaturement*0.2*0.001*3.14159*3.14159);
}
// if(meaturement>160)
// {
// meaturement_1=meaturement;
// meaturement=meaturement/100-0.95;
// meaturement=pow(20,meaturement)+meaturement_1;
// }
// else
// meaturement=meaturement-8;
dis_tab2[11]='m';
dis_tab2[12]='H'; //mH
break;
case 6:
LCD1602_SetPos(0,0);
LCD1602_DisplayString("R: ");
if(R_flag==0) //Ω档
{
R1_CD4052();
if(frequence==0)
{
meaturement=0;
}
else
{
meaturement=((float)frequence)+1.0; //补偿
meaturement=10.0/(meaturement*0.693147*0.000001);
meaturement=meaturement/21.0-420.476; //频率转电阻值
meaturement=meaturement-756;
}
if(meaturement>R_up_limit)
{
R_flag=1;
}
dis_tab2[11]=0xF4;
dis_tab2[12]=' ';
}
if(R_flag==1) //KΩ档
{
R2_CD4052();
if(frequence==0)
{
meaturement=0;
}
else
{
meaturement=((float)frequence);
meaturement=1.0/(meaturement*0.7*0.00001)-20.0;
meaturement=meaturement/2.0;
if(meaturement>=380.0)
meaturement=0.7856*meaturement;
else
if(meaturement<380.0&meaturement>=35.0)
meaturement=0.812*meaturement;
else
meaturement=0.79*meaturement;
if(meaturement<=310)
meaturement=(meaturement-1.2436)/1.0196;
else
meaturement=(meaturement+92.642)/1.213; // y = 1.213x - 92.642
meaturement=0.98*meaturement;
}
if(meaturement<R_low_limit)
{
R_flag=0;
}
dis_tab2[11]='K';
dis_tab2[12]=0xF4;
}
break;
case 7:
LCD1602_SetPos(0,0);
LCD1602_DisplayString("C: ");
if(C_flag==0) //nf档
{
C1_CD4052();
if(frequence==0)
{
meaturement=0;
}
else
{
meaturement=(float)frequence*0.965;
meaturement=1.0/(meaturement*0.7*0.0003);
if(meaturement<7)
meaturement=meaturement*0.95-0.04;
}
if(meaturement>C_up_limit)
{
C_flag=1;
}
dis_tab2[11]='n';
dis_tab2[12]='F';
}
if(C_flag==1) //uf档
{
C2_CD4052();
if(frequence==0)
{
meaturement=0;
}
else
{
if(meaturement>=23)
meaturement=((float)frequence)*0.948;
else
meaturement=((float)frequence)*0.943;
meaturement=1.0/(meaturement*0.7*0.03);
}
if(meaturement<C_low_limit)
{
C_flag=0;
}
dis_tab2[11]=0xE4;
dis_tab2[12]='F';
}
break;
case 8:
LCD1602_SetPos(0,0);
LCD1602_DisplayString("V: ");
VD_CD4052();
get_adcDat();
meaturement=addata;
dis_tab2[11]='V';
}
// integer_process(frequence);
// LCD1602_SetPos(0,0);
// LCD1602_DisplayString(dis_tab1);
decimal_process(meaturement);
LCD1602_SetPos(1,0);
LCD1602_DisplayString(dis_tab2);
}
}
void T0_ISR(void) interrupt 1
{
i--;
if(i==0)
{
TR1=0; //关T1计数
frequence=TH1;
frequence=(frequence<<8)+TL1;
i=20;
TH1=0;
TL1=0;
TR1=1; //开T1计数
}
TL0=0x00;
TH0=0x4C;
}
|
|