標題:
電阻電容電感芯片主程序
[打印本頁]
作者:
ning21
時間:
2017-5-6 09:03
標題:
電阻電容電感芯片主程序
/************************************
*********STC89C51**********
**********12MHz晶振********/
#include "reg51.h"
#define uint8 unsigned char
#define uint16 unsigned int
#define uchar unsigned char
#define uint unsigned int
uint16 cnt2ms; //10ms計數(shù)器
uint8 cnt1s; //1秒計數(shù)器
//電阻--------------------------------
#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; //脈沖個數(shù)
unsigned int Pulsenum; //脈沖個數(shù)
uint8 start=0; //
uint16 PulseTime; //10ms計數(shù)器
uint16 Pulse; //10ms計數(shù)器
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};
//數(shù)碼管位選
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內(nèi)的脈沖個數(shù),頻率=脈沖個數(shù)
if(CH==1){
RZ=(Z1/x);//測量1K到1000K
RX=RZ-1000;
RX=RX>>1;//RX/2
RX=RX*10;//數(shù)值擴大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;}//無脈沖后所有的數(shù)字歸零
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;//數(shù)值往后移動 保存上一次的阻值
//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;
}
}
}
/*******************************************************************************
* 函 數(shù) 名: Exti0_interrupt
* 函數(shù)功能: /T0引腳下降沿進入中斷
* 入口參數(shù): 無
* 返 回: 無
*******************************************************************************/
void T1_interrupt(void) interrupt 3 using 0
{
Pulsenum++; //保存測得脈搏值
}
/********************************************************************
函數(shù)功能:定時器/計數(shù)器1中斷處理
入口參數(shù):null
返 回:null
備 注:null
********************************************************************/
void Timer0IntProc() interrupt 1 using 0 //2ms
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
cnt2ms++;
I++;
//數(shù)碼管掃描
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;
}
}
作者:
zf@@zf
時間:
2020-4-23 23:25
LZ=(x*37*C1)/1000000;
LZ=LZ*x;
LZ=1000000/LZ;
if(x==0)LZ=0;
這幾行代碼是什么意思呢
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1