新人求助,因为近几天完全没有进展,所以发帖求助,另一方面,虽然是半成品,但测低频还是蛮准的(小数点后一位),可以当个参考。出现问题如下:低频时比较准,高频时显示频率就会不停改变而且误差较大。
仿真图如下:
程序如下:
#include<reg52.h>
#include<string.h>
#define u8 unsigned char
sbit Gate_ctl=P1^7;sbit Gate=P3^2;
sbit RW=P2^5;sbit RS=P2^6;sbit EN=P2^7;
u8 ledbuf[8],frequency[9],hand[10]="frequency:";
unsigned long count1,count0,overflow1,overflow0;
double fx,f0;
int i,f;
unsigned long fc,Nx,N0,a;
//*****************************************//
void delay(u8 time);
void lcd_init();
void interrupt_init();
void lcd_write_com(u8 com);
void lcd_write_data(u8 date);
void lcd_send(u8 i,u8 j,u8 date[],u8 all);
void lcd_math();
//*****************************************//
void interrupt_init()
{
TMOD=0xD9;
T2CON=0x00;
PT2=1;
RCAP2H=0x4c;RCAP2L=0x00;
TH2=RCAP2H;TL2=RCAP2L;
TH1=0;TL1=0;
TH0=0;TL0=0;
ET1=1;ET0=1;ET2=1;
TR1=1;TR0=1;TR2=1;
EA=1;
}
void main()
{
f0=921600;
lcd_init();
lcd_send(1,0,hand,10);
interrupt_init();
while(1)
{
while(Gate==1);//Gate=0时跳出
TR2=0;
TR0=0;TR1=0;
Nx=(overflow1*65536+TL1+TH1*256);
N0=overflow0*65536+TL0+TH0*256;
fx=(double)((Nx*f0)/N0);
fc=(unsigned long)(fx*100+0.5);
lcd_math();
lcd_send(2,3,frequency,9);
overflow1=0;overflow0=0;
TH1=0;TL1=0;TH0=0;TL0=0;
TR2=1;
TR1=1;TR0=1;
while(Gate!=1);//Gate=1时跳出
}
}
void time0() interrupt 1 //T0
{ overflow0++;}
void time1() interrupt 3 //T1
{ overflow1++;}
void time2() interrupt 5 //T2
{ TF2=0;
i++;
if(i==20)
{ i=0;
Gate_ctl=!Gate_ctl;
TH2=RCAP2H;TL2=RCAP2L;
}
}
void lcd_write_data(u8 date)//LCD写数据初始化
{
RW=0;
RS=1;
P0=date;
delay(100);
EN=1;
delay(100);
EN=0;
}
void lcd_write_com(u8 com)//LCD写命令初始化
{
RW=0;
RS=0;
P0=com;
delay(100);
EN=1;
delay(100);
EN=0;
}
void lcd_init()
{
EN=0;
lcd_write_com(0x38);//8线,2行,5*7;
lcd_write_com(0x0c);//屏幕显示,光标不显示,光标不闪烁
lcd_write_com(0x06);//光标右移,屏幕不动
lcd_write_com(0x01);//清屏
}
void lcd_math()//整数转换为字符;
{
u8 n=8;
strcpy(frequency,"0000000.0");
do{
if(n==7)
{
n--;
fc=fc*10;
}
else
{
frequency[n]=fc%10+'0';
n--;
}
}while(fc/=10);
}
void lcd_send(u8 i,u8 j,u8 date[],u8 all)//(行,列,数组,数组内字符个数)数组送入12864
{
u8 m;
if(i==1)
{
lcd_write_com(0x80+j);
}
else
{
lcd_write_com(0x80+0x40+j);
}
for(m=0;m<all;m++)
{
lcd_write_data(date[m]);
delay(1);
}
}
void delay(u8 time)
{
while(time--);
}
Keil代码与Proteus仿真下载:
测试3.rar
(103.81 KB, 下载次数: 13)
|