我是新手,最近刚接触单片机编程,要编一个测阻抗的程序,利用谐振电路将测量量转换成频率,用单片机的定时器T0定时一秒,T1计数来计算频率,我编好了程序,但测试结果有偏差,不确定自己的程序测定的频率是不是正确,望大神帮忙看看,指导一下呀,急呀!程序如下:
#include<reg52.h>
#include<intrins.h>
#include <absacc.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
sbit psb=P1^3;
sbit ret=P1^4;
unsigned char code table[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x20,0x3a,
0x2e,0x60,0x43};
uchar command,lcd_bufg,lcd_buf1;
uint count;
unsigned long int cot;
sbit k1=P0^2;
sbit k2=P0^3;
sbit k3=P0^4;
void lcd_int();
void lcd_set();
void display();
void display1();
void display2();
void display3();
void display4();
void clear_lcd();
void shuju1(void);
void shuju2(void);
void shuju3(void);
void mand_init(void);
void write_command(uchar command);
void write_data(uchar data0);
void delay(uint m)
{m=2*m;while(m--);}
void main (void)
{
lcd_int();
lcd_set();
display1();//欢迎使用
while(1)
{
if(k1==0)//zu
{
mand_init();
clear_lcd();
display2();
TR0=1;
TR1=1;
while(TR0==1);
cot=TH1*256+TL1;
shuju1();
}
if(k2==0)//rong
{
mand_init();
clear_lcd();
display3();
TR0=1;
TR1=1;
while(TR0==1);
cot=TH1*256+TL1;
shuju2();
}
if(k3==0)//gan
{
mand_init();
clear_lcd();
display4();
TR0=1;
TR1=1;
while(TR0==1);
cot=TH1*256+TL1;
shuju3();
}
}
}
void clear_lcd(void)
{
command=0x01;write_command(command);
command=0x34;write_command(command);
command=0x30;write_command(command);
}
void lcd_set(void)//lcd设置
{
command=0x34;
write_command(command);
command=0x30;
write_command(command);
command=0x01;
write_command(command);
command=0x06;
write_command(command);
command=0x0c;
write_command(command);
}
void lcd_int(void)//lcd选定
{
ret=0;
delay(20);
ret=1;
_nop_();
psb=1;
_nop_();
}
void write_command(uchar command)//写命令
{
delay(200);
rs=0;
rw=0;
P2=command;
en=1;
_nop_();
_nop_();
en=0;
}
void write_data(uchar data0)//写数据
{
delay(200);
rs=1;
rw=0;
P2=data0;
en=1;
_nop_();
_nop_();
en=0;
}
void display(void)
{
write_command(command);
write_data(lcd_bufg);
write_data(lcd_buf1);
}
void display1(void)//欢迎使用
{command=0x92;lcd_bufg=0xbb;lcd_buf1=0xb6;display();
command=0x93;lcd_bufg=0xd3;lcd_buf1=0xad;display();
command=0x94;lcd_bufg=0xca;lcd_buf1=0xb9;display();
command=0x95;lcd_bufg=0xd3;lcd_buf1=0xc3;display();
}
void display2(void)//电阻为
{command=0x92;lcd_bufg=0xb5;lcd_buf1=0xe7;display();
command=0x93;lcd_bufg=0xd7;lcd_buf1=0xe8;display();
command=0x94;lcd_bufg=0xce;lcd_buf1=0xaa;display();
}
void display3(void)//电容为
{command=0x92;lcd_bufg=0xb5;lcd_buf1=0xe7;display();
command=0x93;lcd_bufg=0xc8;lcd_buf1=0xdd;display();
command=0x94;lcd_bufg=0xce;lcd_buf1=0xaa;display();
}
void display4(void)//电感为
{command=0x92;lcd_bufg=0xb5;lcd_buf1=0xe7;display();
command=0x93;lcd_bufg=0xb8;lcd_buf1=0xd0;display();
command=0x94;lcd_bufg=0xce;lcd_buf1=0xaa;display();
}
void shuju1(void)//电阻值处理
{//cot=((144300000/cot-180)/2;
command=0x8b;lcd_bufg=table[cot/10000];lcd_buf1=table[(cot/1000)%10];display();
command=0x8c;lcd_bufg=table[(cot/100)%10];lcd_buf1=table[(cot/10)%10];display();
command=0x8d;lcd_bufg=table[cot%10];lcd_buf1=0x20;display();
command=0x8e;lcd_bufg=0xa6;lcd_buf1=0xb8;display();
}
void shuju2(void)//电容值处理
{
cot =((1000000000)/(cot*405));
command=0x8b;lcd_bufg=table[cot/10000];lcd_buf1=table[(cot/1000)%10];display();
command=0x8c;lcd_bufg=table[(cot/100)%10];lcd_buf1=table[(cot/10)%10];display();
command=0x8d;lcd_bufg=table[cot%10];lcd_buf1=0x20;display();
command=0x8e;lcd_bufg=0x50;lcd_buf1=0x46;display();
//command=0x8f;lcd_bufg=0xa3;lcd_buf1=0xc6;display();
}
void shuju3(void)//电感值处理
{
command=0x8b;lcd_bufg=table[cot/10000];lcd_buf1=table[(cot/1000)%10];display();
command=0x8c;lcd_bufg=table[(cot/100)%10];lcd_buf1=table[(cot/10)%10];display();
command=0x8d;lcd_bufg=table[cot%10];lcd_buf1=0x20;display();
command=0x8e;lcd_bufg=0x75;lcd_buf1=0x48;display();
//command=0x8f;lcd_bufg=0xa3;lcd_buf1=0xc8;display();
}
void mand_init(void)//定时与计数设置
{ TMOD=0x51;//定时器0定时//1,计数
TH0=0x3c;
TL0=0xb0;
TH1=0x00;
TL1=0x00;
EA=1;
ET0=1;
ET1=1;
}
void jishu(void) interrupt 3 using 2//计数器1计数
{
//cot++;
}
void ss(void) interrupt 1 using 1//定时1秒//定时器0定时
{ count++;
TH0=0x3c;
TL0=0xb0;
if(count==20)
{
TR0=0;TR1=0;}
}
|