#include<reg51.h>
#include <stdio.h>
#include <math.h>
#include <intrins.h>
sbit SCL=P0^3; //移位
sbit RCL1=P0^1; //RCL1,RCL2位锁存
sbit RCL2=P0^2;
sbit SDATA=P0^0; //数据位
unsigned char code led1[ ]={0xc0,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,
0x38,0x39}; //0~9,r,l,c
unsigned char code led2[ ]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,
0xb8,0xb9}; //
unsigned char a1,a2,a3,a4,a5; //a1~a5
unsigned char Data_595;
unsigned int n_LED,yy;
unsigned int count_zheng,zheng;
unsigned int count_yu,yu;
float shijiancha=0;
float xiangjiao,gonglvyinshu;
bit data test_begin=0;
bit bit_v1=0;
bit bit_v2=0;
#define k00 0x8000
#define k01 0x4000
#define k02 0x2000
#define k03 0x1000
#define k10 0x0800
#define k11 0x0400
#define k12 0x0200
#define k13 0x0100
#define k20 0x0080
#define k21 0x0040
#define k22 0x0020
#define k23 0x0010
void timecha( )
{
yu=TL1;
count_zheng=zheng;
count_yu=yu;
shijiancha=(count_zheng*200+(count_yu-56))*12/11.0592;
}
void xiangweicha( )
{
if(shijiancha<=5000)
{
if(shijiancha==0)
a5=10; //阻性
else
a5=11; //感性
xiangjiao=shijiancha*18/1000;
xiangjiao=(xiangjiao/180)*3.1415926;
if (xiangjiao<=0.04)
gonglvyinshu=1;
else
gonglvyinshu=cos(xiangjiao);
}
else
{
if ((shijiancha>15000)&&(shijiancha<=20000))
{
a5=12; //容性
xiangjiao=(20000-shijiancha)*18/1000;
xiangjiao=(xiangjiao/180)*3.1415926;
if (xiangjiao<=0.04)
{
gonglvyinshu=1;
}
else
{
gonglvyinshu=cos(xiangjiao);
}
}
}
gonglvyinshu=gonglvyinshu*1000;
}
void part( )
{
unsigned int zhongjian;
zhongjian=gonglvyinshu;
if(zhongjian<1000)
{
a1=0;
a2=zhongjian/100;
a3=(zhongjian/10)%10;
a4=zhongjian%10;
}
if(zhongjian==1000)
{
a1=1;
a2=0;
a3=0;
a4=0;
a5=10;
}
}
void Dtat_Out(unsigned int n_LED,unsigned char Data_595)
{
unsigned char y;
RCL2=1; // 输出锁存时钟
RCL1=1;
RCL2=0; //段码清零
for(y=0;y<8;y++)
{
SCL=0; // 数据输入时钟
SDATA=0;
SCL=1;
}
RCL2=1;
RCL1=0; //发送位选信号
for(y=0;y<16;y++)
{
SCL=0;
if((n_LED&0x0001)==0x0001) { SDATA=1; }
if((n_LED&0x0001)!=0x0001) { SDATA=0; }
n_LED>>=1;
SCL=1;
}
RCL1=1;
RCL2=0; //发送段选
for(y=0;y<8;y++)
{
SCL=0;
if((Data_595&0x80)==0x80) { SDATA=1; }
if((Data_595&0x80)!=0x80) { SDATA=0; }
Data_595<<=1;
SCL=1;
}
RCL2=1;
for(yy=0;yy<500;yy++); //延时
}
void xianshi()
{
Dtat_Out(k10,led1[a5]); //r,l,c;
Dtat_Out(k20,led1[a4]);
Dtat_Out(k21,led1[a3]);
Dtat_Out(k22,led1[a2]);
Dtat_Out(k23,led2[a1]);
}
void exint0( ) interrupt 0
{
bit_v1=1;
}
void exint1( ) interrupt 2
{
bit_v2=1;
}
void int_t1( ) interrupt 3
{
zheng++;
}
void initial( )
{
TH1=56;
TL1=56;
TMOD=0X20; // 8位自动重装
ET1=1; // 允许T1中断
IT0=0;
IT1=0;
EX0=1;
EX1=1;
EA=1; //CPU开中断
}
void main(void)
{
initial( );
while(1)
{
if (bit_v1&&!test_begin)
{
TR1=1; //允许T1开始计数
test_begin=1;
}
if(test_begin&&bit_v2)
{
TR1=0;// T1停止级数
timecha( );
xiangweicha( );
part( );
xianshi();
initial( );
test_begin=0;
zheng=0;
yu=0;
}
}
}
|