#include <ADuC845.h>
#include<string.h>
#define uint unsigned int
#define uchar unsigned char
sbit RE1=P2^2;
sbit DE1=P2^1;
sbit RE2=P2^5;
sbit DE2=P2^4;
sbit rs=P2^3;
sbit rw=P2^4;
sbit en=P2^5;
uchar word,flag,date1,date2,date3,date4,date5,date6,date7;
float vol,angle;
long int dat;
uchar code table[]="VOL:";
uchar code table1[]="DAT:";
uchar code table2[]="ANG:";
void Delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void Write_Com(uchar com)
{
rs=0;
en=0;
P0=com;
Delay(5);
en=1;
Delay(5);
en=0;
}
void Write_Date(uchar date)
{
rs=1;
en=0;
P0=date;
Delay(5);
en=1;
Delay(5);
en=0;
}
void Display_Str(uchar *p)
{
uchar i,k;
k=strlen(p);
for(i=0;i<k;i++)
{
Write_Date(*(p+i));
Delay(5);
}
}
void Init()
{
en=0;
rw=0;
Write_Com(0x38);
Write_Com(0x0c);
Write_Com(0x06);
Write_Com(0x01);
Write_Com(0x80);
Display_Str(table);
Write_Com(0x80+0x40);
Display_Str(table1);
PLLCON=0xD0; //设置单片机内核频率为12.582912MHz
SF=0x200;
// ADCMODE=0x1b; //0x13为辅助AD使能,0x23为主AD使能
ADC1CON=0x60; //0x60为辅助ADC单极性输入,0x40为辅助AD双极性输入
// ADC0CON1=0x07; //0x27为主AD单极性输入,0x07为主AD双极性输入
// ADC0CON2=0x40;
EA=1;
ADCMODE=0x13;
ES=1;
T3CON=0x82; //波特率为115200
T3FD=0x2D;
SM0=0;
SM1=1;
REN=1;
// RE1=0;
// DE1=1;
// RE2=0;
// DE2=1
}
void Display_Vol()
{
uchar num1,num2,num3,num4,num5,num6,num7;
float size;
long int date;
vol=dat*0.0000001490116; //辅助AD单极性编码方式
// vol=dat*0.0000002980232-2.5; //AD双极性编码方式
// vol=dat*0.0000001490116-1.25;
// vol=dat*0.000000153; //主AD单极性编码方式
// vol=dat*0.000000305-2.56; //主AD双极性编码方式
if(vol<0)
{
Write_Com(0x84);
Write_Date('-');
size=-vol;
}
else
{
Write_Com(0x84);
Write_Date(' ');
size=vol;
}
num1=size/1;
date=(size-num1)*1000000000;
num2=date/100000000;
num3=date%100000000/10000000;
num4=date%10000000/1000000;
num5=date%1000000/100000;
num6=date%100000/10000;
num7=date%10000/1000;
Write_Com(0x85);
Write_Date(0x30+num1);
Write_Date('.');
Write_Date(0x30+num2);
Write_Date(0x30+num3);
Write_Date(0x30+num4);
Write_Date(0x30+num5);
Write_Date(0x30+num6);
Write_Date(0x30+num7);
Write_Date(' ');
Write_Date('V');
}
void Display_Dat()
{
uchar num1,num2,num3,num4,num5,num6,num7,num8;
num1=dat/10000000;
num2=dat%10000000/1000000;
num3=dat%1000000/100000;
num4=dat%100000/10000;
num5=dat%10000/1000;
num6=dat%1000/100;
num7=dat%100/10;
num8=dat%10;
Write_Com(0x80+0x45);
Write_Date(0x30+num1);
Write_Date(0x30+num2);
Write_Date(0x30+num3);
Write_Date(0x30+num4);
Write_Date(0x30+num5);
Write_Date(0x30+num6);
Write_Date(0x30+num7);
Write_Date(0x30+num8);
}
void Display_Ang()
{
float size;
long int date;
if(vol<0.25)
{
if(vol>-0.25)
{
angle=0;
}
else
{
angle=0.4*vol;
}
}
else
{
angle=0.4*vol;
}
if(angle<0)
{
Write_Com(0x80+0x44);
Write_Date('-');
size=-angle;
}
else
{
Write_Com(0x80+0x44);
Write_Date(' ');
size=angle;
}
date1=size/1;
date=(size-date1)*1000000;
date2=date/100000;
date3=date%100000/10000;
date4=date%10000/1000;
date5=date%1000/100;
date6=date%100/10;
date7=date%10;
Write_Com(0x80+0x45);
Write_Date(0x30+date1);
Write_Date('.');
Write_Date(0x30+date2);
Write_Date(0x30+date3);
Write_Date(0x30+date4);
Write_Date(0x30+date5);
Write_Date(0x30+date6);
Write_Date(0x30+date7);
Write_Date(' ');
Write_Date(0x5E);
}
void Main()
{
Init();
while(1)
{
if(word==0xff)
{
word=0;
ADCMODE=0x12; //重新启动AD转换
ES=0;
while(!flag);
flag=0;
Display_Vol();
Display_Ang();
if(angle>0)
{
SBUF=0x2B;
while(!TI);
TI=0;
}
if(angle<0)
{
SBUF=0x2D;
while(!TI);
TI=0;
}
if(angle==0)
{
SBUF=0x20;
while(!TI);
TI=0;
}
SBUF=0x30+date1;
while(!TI);
TI=0;
SBUF=0x2E;
while(!TI);
TI=0;
SBUF=0x30+date2;
while(!TI);
TI=0;
SBUF=0x30+date3;
while(!TI);
TI=0;
SBUF=0x30+date4;
while(!TI);
TI=0;
SBUF=0x30+date5;
while(!TI);
TI=0;
SBUF=0x30+date6;
while(!TI);
TI=0;
SBUF=0x30+date7;
while(!TI);
TI=0;
ES=1;
}
Display_Vol();
Display_Dat();
// Display_Ang();
}
}
void Adc() interrupt 6
{
dat=ADC1L+ADC1M*256+ADC1H*65536; //读辅助AD数据
RDY1=0;
// dat=ADC0L+ADC0M*256+ADC0H*65536; //读主AD数据
// RDY0=0;
flag=1;
}
void Ser() interrupt 4
{
RI=0;
word=SBUF;
}