#include <reg52.h> //示波器
#include <intrins.h>
#include "LIB.h"
#define uchar unsigned char
#define uint unsigned int
#define command 0
#define dat 1
#define Disp_On 0x3f //定义管脚的指令值
#define Disp_Off 0x3e
#define coloum 0x40 //从第一列开始显示
#define page 0xb8 //从第一页开始显示
#define startline 0xc0 //从第一行开始显示
uchar fre[3];
uchar m=0;
uint frequency;
uchar max,min,p_p;
uchar pp[2];
uchar shuju[64]; //用于存放一组扫描数据
sbit E=P2^0;
sbit RW=P2^1;
sbit RS=P2^2;
sbit CS2=P2^3;
sbit CS1=P2^4;
sbit RST=P2^5;
sbit W=P2^6;
sbit R=P2^7;
sbit flag=P3^2;
sbit start=P3^0;
sbit ret=P3^1;
void lcd_delay(uchar i);
void lcd_initial();
void lcd_send(uchar da_co,uchar i);
void lcd_write(uchar x,uchar y,uchar a); //在x页,y列开始写入数据
void lcd_char(uchar x,uchar y,uchar code *hanzi); //在指定位置上写入一个汉字
void lcd_number(uchar x,uchar y,uchar code *shuzi); //在指定位置上写入一个数字
void lcd_zimu(uchar x,uchar y,uchar code *zimu); //在指定位置上写入一个字母
void lcd_display(); //在液晶上显示波形
void sample(); //ADC0804采样函数
void clear(); //清楚上次的点
void clear_screen(); //清屏,实际电路去掉此函数
void delay(uchar i);
void max_min(); //计算波形的峰峰值
void biankuang();
void t0();
void t1();
void fre_display(); //频率显示
void lcd_delay(uchar i)
{ while(--i);
}
void delay(uchar i)
{ uchar a,b;
for(a=0;a<i;a++)
for(b=0;b<125;b++)
{;}
}
void lcd_initial()
{ RST=0;
lcd_delay(70);
RST=1;
CS1=1;
CS2=1;
_nop_();
lcd_send(command,Disp_Off);
lcd_send(command,Disp_On);
}
void lcd_send(uchar da_co,uchar i)
{ RW=0;
RS=da_co;
P0=i;
E=1;
;
E=0;
_nop_();
}
void lcd_write(uchar x,uchar y,uchar a) //在指定位置上写入一个字节的数据
{ lcd_send(command,page+x);
lcd_send(command,coloum+y);
lcd_send(dat,a);
}
void lcd_char(uchar x,uchar y,uchar code *hanzi)
{ uchar i,j;
for(i=0;i<2;i++)
{ lcd_send(command,page+x+i);
lcd_send(command,coloum+y);
for(j=0;j<16;j++)
{lcd_send(dat,hanzi[16*i+j]); //自摸共有32个字节
}
}
}
void lcd_number(uchar x,uchar y,uchar code *shuzi)
{ uchar i,j;
for(i=0;i<2;i++)
{lcd_send(command,page+x+i);
lcd_send(command,coloum+y);
for(j=0;j<8;j++)
lcd_send(dat,shuzi[8*i+j]);
}
}
void lcd_zimu(uchar x,uchar y,uchar code *zimu)
{ uchar i,j;
for(i=0;i<2;i++)
{lcd_send(command,page+x+i);
lcd_send(command,coloum+y);
for(j=0;j<10;j++)
lcd_send(dat,zimu[10*i+j]);
}
}
void sample()
{ uint i,value;
uchar j;
for(j=0;j<64;j++)
{ R=1;W=1;
W=0;
W=1;
lcd_delay(60); //145us
R=0;
i=P1;
_nop_();
R=1; //读数完毕
value=i*235/1000;
shuju[j]=value;
}
}
void lcd_display()
{ uchar i,x;
uchar j;
for(i=0;i<64;i++) //取出一个采样数据
{ j=shuju[i];
if(j>55) x=0;
else if(j>47) x=1;
else if(j>39) x=2;
else if(j>31) x=3;
else if(j>23) x=4;
else if(j>15) x=5;
else if(j>7) x=6;
else x=7;
lcd_write(x,i,da[j]);
}
}
void clear()
{ uchar i,x;
uchar j;
for(i=0;i<64;i++) //取出一个采样数据
{ j=shuju[i];
if(j>55) x=0;
else if(j>47) x=1;
else if(j>39) x=2;
else if(j>31) x=3;
else if(j>23) x=4;
else if(j>15) x=5;
else if(j>7) x=6;
else x=7;
lcd_write(x,i,0x00);
}
}
void clear_screen()
{ uchar i,j;
for(i=0;i<8;i++)
for(j=0;j<128;j++)
{ if(j<64)
{CS1=0;CS2=1;
lcd_write(i,j,0x00);
}
else
{CS1=1;CS2=0;
lcd_write(i,j-64,0x00);
}
}
}
void max_min()
{ uchar i;
max=shuju[0];
min=shuju[0];
for(i=1;i<64;i++)
{ if(shuju[i]>max)
max=shuju[i];
else if(shuju[i]<min)
min=shuju[i];
}
p_p=max-min;
pp[0]=p_p/10;
pp[1]=p_p%10;
CS1=1;CS2=0;
lcd_number(6,11,xx[pp[0]]);
lcd_number(6,19,dian);
lcd_number(6,25,xx[pp[1]]);
}
void biankuang()
{ uchar i;
CS1=0;CS2=1;
for(i=0;i<64;i++)
{lcd_write(0,i,0x01);
lcd_write(7,i,0x80);
}
CS1=1;CS2=0;
for(i=0;i<8;i++)
lcd_write(i,0,0xff);
}
void t0() interrupt 1 //计数
{}
void t1() interrupt 3 //定时
{ m++;
TH1=0x3c;
TL1=0xb0;
if(m==20)
{m=0;
TR0=0;TR1=0;
ret=0;ret=1; //停止计数
// TH1=0x3c;
// TL1=0xb0;
frequency=256*TH0+TL0;
fre[0]=frequency/100;
fre[1]=frequency%100/10;
fre[2]=frequency%10;
TH0=0x00;TL0=0x00;
TR0=1;TR1=1;
start=0;start=1;
}
}
void fre_display()
{ CS1=1;CS2=0;
lcd_number(2,11,xx[fre[0]]);
lcd_number(2,19,xx[fre[1]]);
lcd_number(2,27,xx[fre[2]]);
}
main()
{
lcd_initial();
clear_screen();
biankuang();
CS1=1;CS2=0;
lcd_char(0,5,pin);
lcd_char(0,21,lv);
lcd_zimu(2,43,H);
lcd_zimu(2,53,Z);
lcd_zimu(6,43,V);
lcd_char(4,5,feng);
lcd_char(4,21,feng);
lcd_char(4,37,zhi);
ret=0;ret=1; //将D触发器清零
TMOD=0x95; //T0计数模式,T1定时模式需INT1置位
TH0=0x00;TL0=0x00;
TH1=0x3c;TL1=0xb0;
ET0=1;ET1=1;EA=1;PT1=1;
TR0=1;TR1=1; //开T0,T1
start=0;start=1; //开启正脉冲
while(1)
{ sample();
max_min();
CS1=0;CS2=1;
lcd_display();
delay(500);
clear();
fre_display();
}
}