/************************************
*********STC89C51**********
**********12MHz晶振********/
#include "reg51.h"
#define uint8 unsigned char
#define uint16 unsigned int
#define uchar unsigned char
#define uint unsigned int
uint16 cnt2ms; //10ms计数器
uint8 cnt1s; //1秒计数器
//电阻--------------------------------
#define Z1 13910053.00
#define Z2 3685279.00
float RZ=0;
unsigned long RX=0;
unsigned long RX2=0;
unsigned long RX3=0;
unsigned long RX4=0;
unsigned long RX5=0;
unsigned long RX6=0;
unsigned long RX7=0;
unsigned long RX8=0;
unsigned long RX9=0;
//电感--------------------------------
float LZ=0;
#define C1 0.05
//电容--------------------------------
float CZ=0;
float CX=0;
unsigned char I=0;
long tom;
unsigned int Pulsecnt=0; //脉冲个数
unsigned int Pulsenum; //脉冲个数
uint8 start=0; //
uint16 PulseTime; //10ms计数器
uint16 Pulse; //10ms计数器
bit flag=0;
uint8 CH=0; //
unsigned long x;
unsigned char LED_s=0;
/********************************按键*****************************************/
sbit K1=P1^0;
sbit K2=P1^1;
sbit K3=P1^2;
sbit DIP=P0^7;
sbit L1=P1^3;
sbit L10=P1^4;
sbit L100=P1^5;
sbit A0=P2^0;
sbit A1=P2^1;
sbit RED=P2^4;
sbit GRE=P2^3;
sbit YEW=P2^2;
unsigned char temp[7];
/********************************0 1 2 3 4 5 6 7 8 9 H L */
unsigned char code dispcode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x89,0xC7};
//数码管位选
sbit B4 = P2^7;
sbit B1 = P3^7;
sbit B2 = P2^5;
sbit B3 = P2^6;
/****延时******/
void delay_1ms(uint x)
{
uint j;
uchar i;
for(j=0;j<x;j++)
{
for(i=0;i<120;i++);
}
}
void Key_SM(void){
if(K1==0){
delay_1ms(5);
if(K1==0){
YEW=1;RED=1;GRE=0;
GRE=0;
//----电感档-------
A0=0;
A1=0;
CH=2;//电感档
while(K1==0){}
}
}
if(K2==0){
delay_1ms(5);
if(K2==0){
YEW=1;GRE=1;
RED=0;
//----电阻档-------
A0=1;
A1=0;
CH=1;//电阻档
while(K2==0){}
}
}
if(K3==0){
delay_1ms(5);
if(K3==0){
YEW=0;GRE=1;RED=1;
//----电容档-------
A0=0;
A1=1;
CH=3;//电容挡
while(K3==0){}
}
}
}
//--------------------主程序--------------------------------
void main()
{
TMOD=0x51; //设置定时器0,方式1:16位定时器
TH1=0;
TL1=0;
TH0=(65536-2000)/256; //设定定时周期
TL0=(65536-2000)%256;
TR0=1;
TR1=1;
ET1=1;
ET0=1;
EA=1;
//----电阻档-------
//----电阻档-------
A0=1;
A1=0;
CH=1;//电阻档
YEW=1;GRE=1;RED=0;
while(1)
{
Key_SM();
if(flag==1){
flag=0;
x=Pulsenum*65536+TH1*256+TL1;//计算1s内的脉冲个数,频率=脉冲个数
if(CH==1){
RZ=(Z1/x);//测量1K到1000K
RX=RZ-1000;
RX=RX>>1;//RX/2
RX=RX*10;//数值扩大10倍
if(RX>=10109){//减去1K阻值
RX=RX-10109;
}else{RX=0;}
if(x==0){RX=0;RX2=0;RX3=0;RX4=0;RX5=0;RX6=0;RX7=0;RX8=0;RX9=0;}//无脉冲后所有的数字归零
if(RX>9999999){RX=9999999; }//RX大于9999999,则RX=9999999
RX2=(RX+RX3+RX4+RX5+RX6+RX7+RX8+RX9)>>3;//求平均提高精度
RX9=RX8;RX8=RX7;RX7=RX6;RX6=RX5;RX5=RX4;RX4=RX3;RX3=RX;//数值往后移动 保存上一次的阻值
//RX2=RX2/10;
if(RX2<=9999){RX2=RX2;L1=1;L10=1;L100=1;}
if(RX2<=99999&&RX2>9999){RX2=RX2/10;L1=0;L10=1;L100=1;}
if(RX2<=999999&&RX2>99999){RX2=RX2/100;L1=1;L10=0;L100=1;}
if(RX2>999999){RX2=RX2/1000;L1=1;L10=1;L100=0;}
tom=RX2;
temp[0] = tom/1000;
tom = tom%1000;
temp[1] = tom/100;
tom = tom%100;
temp[2] = tom/10;
tom = tom%10;
temp[3] = tom;
}
if(CH==2){
LZ=(x*37*C1)/1000000;
LZ=LZ*x;
LZ=1000000/LZ;
if(x==0)LZ=0;
if(LZ>999999)LZ=999999; //限制最高值
//更新电感值显示
if(LZ<=9999){LZ=LZ;L1=1;L10=1;L100=1;}
if(LZ<=99999&&LZ>9999){LZ=LZ/10;L1=0;L10=1;L100=1;}
if(LZ<=999999&&LZ>99999){LZ=LZ/100;L1=1;L10=0;L100=1;}
if(LZ>999999){LZ=LZ/1000;L1=1;L10=1;L100=0;}
tom=LZ;
temp[0] = tom/1000;
tom = tom%1000;
temp[1] = tom/100;
tom = tom%100;
temp[2] = tom/10;
tom = tom%10;
temp[3] = tom;
}
if(CH==3){
CZ=6600000/x;
if(x==0||x>30500)CZ=0;
if(CZ>999999)CZ=999999; //限制最高值
//更新电容值显示
if(CZ<=9999){CZ=CZ;L1=1;L10=1;L100=1;}
if(CZ<=99999&&CZ>9999){CZ=CZ/10;L1=0;L10=1;L100=1;}
if(CZ<=999999&&CZ>99999){CZ=CZ/100;L1=1;L10=0;L100=1;}
if(CZ>999999){LZ=CZ/1000;L1=1;L10=1;L100=0;}
tom=CZ;
temp[0] = tom/1000;
tom = tom%1000;
temp[1] = tom/100;
tom = tom%100;
temp[2] = tom/10;
tom = tom%10;
temp[3] = tom;
}
cnt2ms=0;
Pulsenum=0;
TH1=0;
TL1=0;
TR1=1;
}
}
}
/*******************************************************************************
* 函 数 名: Exti0_interrupt
* 函数功能: /T0引脚下降沿进入中断
* 入口参数: 无
* 返 回: 无
*******************************************************************************/
void T1_interrupt(void) interrupt 3 using 0
{
Pulsenum++; //保存测得脉搏值
}
/********************************************************************
函数功能:定时器/计数器1中断处理
入口参数:null
返 回:null
备 注:null
********************************************************************/
void Timer0IntProc() interrupt 1 using 0 //2ms
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
cnt2ms++;
I++;
//数码管扫描
if(I==1){B2=1;B3=1;B4=1;B1=0;
P0=dispcode[temp[0]];DIP=1;
}
if(I==2){B1=1;B3=1;B4=1;B2=0;
P0=dispcode[temp[1]];DIP=1;
}
if(I==3){B1=1;B2=1;B4=1;B3=0;
P0=dispcode[temp[2]];
if(CH==1){DIP=0;}
}
if(I==4){B1=1;B2=1;B3=1;B4=0;I=0;DIP=1;
P0=dispcode[temp[3]];
}
if(cnt2ms==497) //1s计时到
{
TR1=0;
cnt2ms = 0;
flag=1;
}
}
|