include<reg52.h>
#include<intrins.h>
#include<absacc.h>
#define uint unsigned int
#define uchar unsigned char
#define max197 XBYTE[0xefff]
sbit EN=P2^0;
sbit RS=P2^1;
sbit CS=P2^2;
sbit HBEN=P2^3;
sbit duan=P2^6;
uchar templ,temph,flag;
float dianya;
uint temp,temp1[25];
unsigned long voltage;
void delay(uint i)
{
while(i--);
}
void delay1(uint z)
{
int x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
void write_com(uchar com)
{
RS=0;
P1=com;
delay1(5);
EN=1;
delay1(5);
EN=0;
}
void write_data(uchar date)
{
RS=1;
P1=date;
delay1(5);
EN=1;
delay1(5);
EN=0;
}
void iniTIal()
{
EN=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void printc(uchar lcd_data) //输出一个字符到LCD
{
write_data(lcd_data);
}
void prints(uchar *lcd_string) //输出一个字符串到LCD
{
uchar i=0;
while(lcd_string[i]!=0x00)
{
write_data(lcd_string[i]);
i++;
}
}
void setPosiTIon(uchar x) //LCD光标定位到x处
{
write_com(0x80+x);
}
uchar TempBuffer[7];
void IntToStr(uint temp, uchar *str, uchar n)
{
uchar a[7]; char i, j; //取得整数值到数组
dianya=temp/4096.0;
voltage=dianya*100000;
a[0]=voltage/10000;
a[1]=voltage%10000/1000;
a[2]=voltage%1000/100;
a[3]=voltage%100/10;
a[4]=voltage%10;
for(i=0; i<5; i++) //转成ASCII码
a[i]=a[i]+'0';
for(i=0; a[i]=='0' && i<=5; i++);
for(j=5-n; j<i; j++) //填充空格
{ *str=' '; str++; }
for(; i<5; i++)
{ *str=a[i]; str++; } //加入有效的数字
*str=' ';
}
void iniTIal_max197()
{
//uchar *AD;
// AD=&max197;
//单片机初始化
EA=1;
IT1=1;
EX1=1;
duan=0;
//初始化MAX197
//*AD=0x40;
}
void main()
{
uchar i;
iniTIal();
initial_max197();
//CS=0;
max197=0x40;
setPosition(0x00);
prints("The voltage is :");
while(1)
{
//采集15次;
for(i=0;i<15;i++)
{
flag=0;
max197=0x40;
while(!flag);
HBEN=1;
temph=max197;
_nop_();
HBEN=0;
templ=max197;
temp=temph;
temp<<=8;
temp1[i]=temp|templ;
}
temp=0;
for(i=0;i<15;i++)
{
temp=temp+temp1[i];
}
temp=temp/15;//取平均值
IntToStr(temp,&TempBuffer[0],5);
setPosition(0x40);
printc(TempBuffer[0]);
printc(TempBuffer[1]);
printc('.');
printc(TempBuffer[2]);
printc(TempBuffer[3]);
printc(TempBuffer[4]);
prints("mv");
delay1(500);
max197=0x40;
}
}
void convert() interrupt 2 using 2
{
flag=1;
}