max197调试的时候,误差有点大 呵呵!!!根据的几天的调试观察,我怀疑是max197片子的问题,但苦于手里头没有多余的max197,问题一直还没有解决 哈哈
#include <stdio.h>
#include <AT89X52.h>
#include<intrins.h>
#include<absacc.h>
#define Dlcd P0 //XBYTE[0x8000]//显示的地址
sbit rs = P1^0; //1=数据; 0=指令;
sbit rw = P1^1; //1=读; 0=写;
sbit e = P1^2; //1=使能?
#define unchar unsigned char
#define unint unsigned int
unchar distab[8] ;
unint voltage;
#define AD199 XBYTE[0x4000]
sbit HBEN= P1^3;
sbit INT197= P3^3;
/*-------------延时--------------------------*/
void delay(unsigned char i) //225不稳定,确保安全230-250 约0.5ms
{
while(--i);
}
void delay1s(void)
{
unsigned char i,j,k;
for(k=50;k>0;k--)
for(i=10;i>0;i--)
for(j=248;j>0;j--);
}
/*---------------写数据------------------------*/
lcd_write_data(unsigned char c)
{
delay(230);
rs=1;
rw=0;
Dlcd =c;
e=1;
_nop_();
_nop_();
e=0;
}
/*---------------写指令------------------------*/
lcd_write_command(unsigned char c)
{
delay(230);
rs=0;
rw=0;
Dlcd =c;
e=1;
_nop_();
_nop_();
e=0;
}
/*------------------初始化-----------------*/
void init_lcd (void)
{
lcd_write_command (0x30); //基本指令集
lcd_write_command (0x01); //清屏,地址指针指向00H
lcd_write_command (0x06); //光标的移动方向
lcd_write_command (0x0c); //开显示,关游标
}
/************************************************************************************************
lcd_write_sentence函数:
指定起始行,起始列,连续写入字符串;注意:字符串长度必须小于8;行:1-4;列:0-7
*************************************************************************************************/
void lcd_write_sentence(unchar row,unchar col,unchar *string,unchar lenth)
{
unchar i=0;
lcd_write_command (0x30);
switch(row)
{
case 1:
lcd_write_command(0x80+col); //第一行
break;
case 2:
lcd_write_command(0x90+col); //第二行
break;
case 3:
lcd_write_command(0x88+col); //第三行
break;
case 4:
lcd_write_command(0x98+col); //第四行
break;
default:
lcd_write_command(0x80); //缺省为第一行
break;
}
while(lenth-->0)
{
lcd_write_data(string[i]); //写入字符串
i++;
}
}
unint AD199Start(unchar M)
{
long voltage;
unchar d_h,d_l,i;
unint tem,max=0,min=10000;
voltage=0;
for(i=0;i<M+2;i++) //连续采样N次,去掉最大值和最小值,再平均
{
AD199=0x40;//开始转化
while(INT197==1);
HBEN=0;
d_l=AD199;
HBEN=1;
d_h=AD199;
tem=d_h*0x0100+d_l;
if(tem>max)
max=tem;
if(tem<min)
min=tem;
voltage+=tem;
}
voltage=voltage-max-min;
voltage=voltage/M;
voltage=(5.000/4.096)*voltage;
return (unint)voltage;
}
void getAD199(void)
{
voltage=AD199Start(10);
distab[7]=voltage/10000; //分别取出显示
voltage=voltage%10000;
if(distab[7]==0)
distab[7]=0x10;
distab[6]=voltage/1000;
voltage=voltage%1000;
distab[5]=voltage/100;
voltage=voltage%100;
distab[4]=voltage/10;
voltage=voltage%10;
distab[3]=voltage;
distab[2]=0x10;
distab[1]=0x10;
distab[0]=0x10;
init_lcd ();
lcd_write_sentence(1,0,"The voltage is:",15);
lcd_write_command(0x30);
lcd_write_command(0x90);
lcd_write_data(0x20);
lcd_write_data(0x30+distab[6]);
lcd_write_sentence(2,1,".",1);
lcd_write_data(0x30+distab[5]);
lcd_write_data(0x30+distab[4]);
lcd_write_data(0x30+distab[3]);
lcd_write_sentence(2,3,"V",1);
}
/*------------------主程序--------------------*/
void main ()
{
init_lcd ();
while(1)
{
getAD199();
delay1s();
}
}
硬件连接图见附件