ad7705.c//主程序
#include<reg52.h>
#include"lcd1602.h"
#include<absacc.h>
#include<intrins.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
sbit AD7705_DATA=P3^0; // AD7705 input & output data
sbit AD7705_CLK=P3^1; // AD7705 CLK
sbit AD7705_DRDY=P3^2; // AD7705 data ready
sbit AD7705_CS =P1^5; // AD7705 CS
uint Counter="0",Counter1=0;
long int T_VoltageTotal=0;
uint T_Voltage=0, T_VoltageDel=0,T_VoltageQP=0,T_VoltageMax=0;//现在值、中间处理值、去皮值,与最大值
unsigned char MSB_Data,LSB_Data;
uchar a[5]={0,0,0,0,0};
uchar b[5]={1,0,0,0,5};
//分别对应
uchar code row1[]={" NOW:***** g "};
uchar code row2[]={" MAX:***** g "};
uchar row1adr[16]={0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F};//LCD地址
uchar row2adr[16]={0x80+0x40,0x81+0x40,0x82+0x40,0x83+0x40,0x84+0x40,0x85+0x40,0x86+0x40,0x87+0x40,0x88+0x40,0x89+0x40,0x8A+0x40,0x8B+0x40,0x8C+0x40,0x8D+0x40,0x8E+0x40,0x8F+0x40};//LCD地址
uchar i; //定义变量
void lcdscan();//更新显示的数据
void lcdscan1();//初始化二行数据
void Delayms(unsigned char ms);
void AD7705_Start(); // 通道的初始化
void AD7705_Read (); //对转换数据进行处理
void AD7705_Write_Reg(unsigned char Data_byte); //对芯片写数据
unsigned char AD7705_Read_Reg(); //读取AD转换的数据
main()
{
lcdrw=0;//lcd写数据
lcdon();//初始化LCD
lcdscan1();
IT1=0;
EX1=1;
EA = 1;
P0 = 0xff;
P1 = 0xff;
P2 = 0xff;
P3 = 0xff;
Counter = 0;
AD7705_Start();
delay(200);
for(i=0;i<10;i++) //启动去皮
{
AD7705_Read();
T_VoltageTotal+=T_VoltageDel;
}
T_VoltageQP=(uint)(T_VoltageTotal/10);
//****************
T_VoltageTotal=0;
while(1)
{
Counter++;
Counter1++;
AD7705_Read();
T_VoltageTotal+=T_VoltageDel;
if(Counter==8)
{
T_Voltage=(uint)(T_VoltageTotal/8);
T_Voltage-=T_VoltageQP;
if(T_Voltage>63000)
T_Voltage=0;
if(T_Voltage>T_VoltageMax)
T_VoltageMax= T_Voltage;
if(Counter1==160)
{
a[0] =(uchar)(T_Voltage/10000);
a[1] =(uchar)((T_Voltage-a[0]*10000)/1000);
a[2] =(uchar)((T_Voltage-a[0]*10000-a[1]*1000)/100);
a[3] =(uchar)((T_Voltage-a[0]*10000-a[1]*1000-a[2]*100)/10);
a[4] =(uchar)(T_Voltage-a[0]*10000-a[1]*1000-a[2]*100-a[3]*10);
b[0]=(uchar)(T_VoltageMax/10000);
b[1]=(uchar)((T_VoltageMax-b[0]*10000)/1000);
b[2]=(uchar)((T_VoltageMax-b[0]*10000-b[1]*1000)/100);
b[3]=(uchar)((T_VoltageMax-b[0]*10000-b[1]*1000-b[2]*100)/10);
b[4]=(uchar)(T_VoltageMax-b[0]*10000-b[1]*1000-b[2]*100-b[3]*10);
lcdscan();
Counter1=0;
}
T_VoltageTotal=0;
Counter=0;
}
}
}
//******初始化LCD二行******//
void lcdscan1()
{
lcdrw=0;//lcd写数据
//第一行
for(i=0;i<16;i++)
{
lcdwrite(row1adr);
lcdshuju(row1);
delay(10);
}
//第二行
for(i=0;i<16;i++)
{
lcdwrite(row2adr);
lcdshuju(row2);
delay(10);
}
}
//***更新显示的数据***//
void lcdscan()
{
lcdrw=0;//lcd写数据
//第一行
for(i=6;i<11;i++)
{
lcdwrite(row1adr);
lcdshuju(a[i-6]+0x30);
delay(1);
}
//第二行
for(i=6;i<11;i++)
{
lcdwrite(row2adr);
lcdshuju(b[i-6]+0x30);
delay(1);
}
}
void Delayms(unsigned char ms)
// relay ms
{ unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
void AD7705_Start()
{
AD7705_Write_Reg(0x20);
AD7705_Write_Reg(0x0c);
AD7705_Write_Reg(0x10);
AD7705_Write_Reg(0x7E);
}
/************对转换数据进行处理**********************/
void AD7705_Read()
{
AD7705_Write_Reg(0x38);
MSB_Data = AD7705_Read_Reg();
LSB_Data = AD7705_Read_Reg();
T_VoltageDel=(uint)MSB_Data;
T_VoltageDel=T_VoltageDel<<8;
T_VoltageDel = T_VoltageDel|LSB_Data;
}
/*******************************************/
void AD7705_Write_Reg(unsigned char Data_byte)
{
unsigned char i;
AD7705_CS = 0;
AD7705_CLK = 1;
_nop_();
_nop_();
for(i = 0; i < 8; i++)
{
AD7705_DATA = (bit)(Data_byte & 0x80);
AD7705_CLK = 0;
_nop_();
_nop_();
_nop_();
AD7705_CLK = 1;
Data_byte <<= 1;
}
_nop_();
AD7705_CS = 1;
}
unsigned char AD7705_Read_Reg()
{
unsigned char i,Data_read;
AD7705_CS = 0;
AD7705_CLK = 1;
_nop_();
P3 = 0xff;
for(i = 0; i < 8; i++)
{
AD7705_CLK = 0;
_nop_();
_nop_();
Data_read <<= 1;
Data_read |= (unsigned char)AD7705_DATA;
_nop_();
_nop_();
AD7705_CLK = 1;
_nop_();
_nop_();
}
_nop_();
AD7705_CS = 1;
AD7705_DRDY = 1;
return(Data_read);
}
void EX0IN(void) interrupt 2 //去皮
{
Counter=0;
Counter1=0;
T_VoltageTotal=0;
for(i=0;i<10;i++) //启动去皮
{
AD7705_Read();
T_VoltageTotal+=T_VoltageDel;
}
T_VoltageQP=(uint)(T_VoltageTotal/10);
T_VoltageTotal=0;
T_VoltageMax=0;
}
void time0(void) interrupt 3
{
TR0 = 0;
TR0 = 1;
}
lcd1602//子程序
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define lcd_data P0
sbit lcdrs="P2"^5;
sbit lcdrw="P2"^6;
sbit lcden="P2"^7;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=255;y>0;y--);
}//1毫秒左右延时
//lcdwrite() LCD写指令
void lcdwrite(uchar write)
{
lcdrs=0;
lcd_data=write;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
//lcdshuju() LCD写数据
void lcdshuju(uchar shuju)
{
lcdrs=1;
lcd_data=shuju;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
//lcdon() LCD显示初始化
void lcdon()
{
lcden=0;
lcdwrite(0x38);//显示模式
lcdwrite(0x0c);//开显示,光标不闪烁
lcdwrite(0x06);//光标设置
lcdwrite(0x01);
}
用户3888809 2019-4-9 13:42