本帖最后由 wkzww2 于 2017-2-27 20:32 编辑
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_Data P1
uchar Ji_s=0,Ji_s1=0,Ji_s2=0,m4,js=0,T_flag;
float R1=100,R2=100;//欧姆
uchar code table[]="THE CAP IS:";
uchar code table1[]="uF";
sbit lcden=P2^6;
sbit lcdrs=P2^4;
sbit lcdrw=P2^5;
sbit _reset=P2^3;
sbit start=P2^7;
uchar jishu=0;
void delay(void)
{
uint y;
for(y=5552;y>0;y--);
}
void write_com(uchar com)
{
lcdrs=0;
lcdrw=0;
P1=com;
delay();
lcden=1;
delay();
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
lcdrw=0;
P1=date;
delay();
lcden=1;
delay();
lcden=0;
}
void init()
{
uchar num,num1;
lcden=0;
write_com(0x38); //显示模式设置
write_com(0x0e); //D=1开显示 C=1显示光标
write_com(0x06); //N=1读或写一个字符后地址指针加一且光标加一
write_com(0x01); //清屏
write_com(0x80); //设置数据地址指针
for(num=0;num<11;num++)
{
write_data(table[num]);
delay();
}
write_com(0xc8);
for(num1=0;num1<2;num1++)
{
write_data(table1[num1]);
delay();
}
}
float Ji_sT()
{
uchar nT;
float T,CAP;
nT=Ji_s; //计数个数减1得到周期数 nT
T=nT; //单位换算
单片机机器周期1US
CAP=1.43*1000*T/(R1+2*R2) ; //多谐振荡器震荡周期
return CAP;
}
void count()
{
float C;
float C1,C2,C3;
uchar bai,shi,ge,sf,bf;
C= Ji_sT();
bai=C/100;
write_com(0xc0);
write_data(0x30+bai);
C3=C/10;
shi= (int)C3%10;
write_com(0xc1);
write_data(0x30+shi);
ge = (int)C%10;
write_com(0xc2);
write_data(0x30+ge);
write_com(0xc3);
write_data(0x2e);
C1= C*10;
sf=(int)C1%10;
write_com(0xc4);
write_data(0x30+sf);
C2=C*100;
bf=(int)C2%10;
write_com(0xc5);
write_data(0x30+bf);
}
void main()
{
init();
TMOD = 0x01; // 定时器0工作在计时模式
TH0 = 0x00;
TL0 = 0x00;
ET0 = 1;
TR0 = 0;
EX0=1;
EA = 1;
while(1)
count();
}
void int0(void) interrupt 0 //第一次中断开始计数,第二个中断停止计数
{
T_flag=!T_flag;
if(T_flag==1)
{
_reset=1;
TR0=1; //开始计时
}
if(T_flag==0)
{ TR0=0; //停止计时
EX0=0; //关闭中断
_reset=0; //停止发出方波
Ji_s=TH0*256+TL0; //计算计数器的值
TH0=0x00; //恢复初值
TL0=0x00;
}
}
CX为待测电容
求问哪里有问题
-
一周热门 更多>