初级会员
积分 53
金钱 53
注册时间 2012-12-22
在线时间 2 小时
5 金钱
如下,这是程序。,恳请有经验的人帮我改改,谢谢
#include <reg52.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCL = P1^1;
sbit SDA = P1^0;
sbit lcdrs=P2^6;
sbit lcdwr=P2^5;
sbit lcden_1602=P2^7;
uchar MSB;
uchar LSB;
unsigned long T;
long X1;
long X2;
long B5;
signed short AC1;
signed short AC2;
signed short AC3;
unsigned short AC4;
unsigned short AC5;
unsigned short AC6;
signed short B1;
signed short B2;
signed short MB;
signed short MC;
signed short MD;
long UT;
uchar num;
uchar ge,shi,bai,qian,wan,shiwan,baiwan,qianwan,wanwan;
void delay(uchar t)
{
uchar b;
for(t;t>0;t--)
for(b=50;b>0;b--);
}
void write_com_1602(uchar command) //写命令
{
lcdrs=0;
lcdwr=0;
P0=command;
delay(2);
lcden_1602=1;
delay(2);
lcden_1602=0;
delay(2);
lcden_1602=1;
}
void write_data_1602(uchar in_data) //写数据
{
lcdrs=1;
lcdwr=0;
P0=in_data;
delay(2);
lcden_1602 = 1;
delay(2);
lcden_1602 = 0;
delay(2);
lcden_1602 = 1;
}
void init_1602()//初始化
{
lcden_1602=0;
write_com_1602(0x01); //清屏
write_com_1602(0x06); //AC自动加一,画面不移动
write_com_1602(0x0c); //开显示,显示光标,光标闪
write_com_1602(0x38); //8位数据接口,2行显示5×7点阵
}
void delay_ms(uchar del_ms) //2ms
{
uint a;
for(del_ms;del_ms>0;del_ms--)
{
for(a=260;a>0;a--);
}
}
void IIC_start()
{
SDA=1;
delay(1);
SCL=1;
delay(1);
SDA=0;
delay(1);
SCL=0;
}
void IIC_end()
{
SCL=0;
delay(1);
SDA=0;
delay(1);
SCL=1;
delay(1);
SDA=1;
delay(1);
}
void IIC_check_respons()
{
uchar num_0=250;
SCL=0;
delay(1);
SDA=1;
delay(1);
SCL=1;
delay(1);
while((SDA==1)&&(num_0>0))num_0--;
SCL=0;
}
void IIC_receive_respons()
{
SCL=0;
delay(1);
SDA=0;
delay(1);
SCL=1;
delay(3);
SCL=0;
}
void write_IIC(uchar iic_data)
{
uchar a;
for(a=0;a<8;a++)
{
SCL=0;
iic_data=iic_data<<1;
SDA=CY;
delay(1);
SCL=1;
delay(1);
}
IIC_check_respons();
}
unsigned char read_IIC()
{
uchar IIC_receive;
uchar c;
SDA=1;
for(c=0;c<8;c++)
{
SCL=1;
delay(1);
IIC_receive=IIC_receive<<1;
IIC_receive=SDA|IIC_receive;
SCL=0;
delay(1);
}
IIC_receive_respons();
return IIC_receive;
}
short read_EEPROM_BMP085(uchar reg_EEPROM_addr) //
{
uchar msb,lsb;
short ut;
IIC_start(); //开始
write_IIC(0xee);//085地址写操作
write_IIC(reg_EEPROM_addr);
IIC_start(); //重新开始
write_IIC(0xef);//
msb=read_IIC();
IIC_receive_respons();
/*
IIC_start(); //开始
write_IIC(0xee);//085地址写操作
write_IIC(reg_EEPROM_addr);
IIC_start(); //重新开始
write_IIC(0xef);//读操作
*/
lsb=read_IIC();
IIC_end();
ut=(msb<<8)+lsb;
return ut;
}
void init_BMP085()
{
AC1 = read_EEPROM_BMP085(0xAA);
AC2 = read_EEPROM_BMP085(0xAC);
AC3 = read_EEPROM_BMP085(0xAE);
AC4 = read_EEPROM_BMP085(0xB0);
AC5 = read_EEPROM_BMP085(0xB2);
AC6 = read_EEPROM_BMP085(0xB4);
B1 = read_EEPROM_BMP085(0xB6);
B2 = read_EEPROM_BMP085(0xB8);
MB = read_EEPROM_BMP085(0xBA);
MC = read_EEPROM_BMP085(0xBC);
MD = read_EEPROM_BMP085(0xBE);
}
/*
void write_BMP085(uchar BMP085_data)
{
write_IIC(BMP085_data);
}
*/
uchar read_BMP085(uchar reg_addr)
{
uchar value;
IIC_start();
write_IIC(0xee);//085地址写操作
write_IIC(reg_addr);
IIC_start();
write_IIC(0xef);//读
value = read_IIC();
return value;
}
void measure(uchar measure_BMP085)
{
IIC_start();
write_IIC(0xee);//085地址写操作
write_IIC(0xf4);//085固定寄存器地址 ,表示要进行测量。
write_IIC(measure_BMP085);//如(0x2e),温度测量; //寄存器数据,进行温度或者压力测试
IIC_end();
}
signed long calculate_temperature()
{
long t; //temperature,温度
X1=(UT-AC6)*AC5;
X1=X1/pow(2,15);
X2=(MC*pow(2,11))/(X1+MD) ;
B5=(X1+X2);
t=(B5+8)/pow(2,4); //真实温度
return t;
}
void main()
{
init_1602();
delay_ms(100); //上电延时
init_BMP085();//初始化并读取EEPROM
write_com_1602(0x80+15); //命令
write_data_1602('>'); //写数据
//!****如果在此函数中循环代表数据读取出错*****!/
while((AC1==0)||(AC1==0xffff)||(AC2==0)||(AC2==0xffff)||(AC3==0)||(AC3==0xffff)||(AC4==0)||(AC4==0xffff)||(AC5==0)||(AC5==0xffff)||(AC6==0)||(AC6==0xffff)||(B1==0)||(B1==0xffff)||(B2==0)||(B2==0xffff)||(MB==0)||(MB==0xffff)||(MC==0)||(MC==0xffff)||(MD==0)||(MD==0xffff))
{
init_BMP085();
}
//~*******提示初始化完毕********~//
write_com_1602(0x80+0x40); //命令
write_data_1602('T'); //写数据
/*****************************************/
/***************主执行函数****************/
/*****************************************/
while(1)
{
measure(0x2e);
delay_ms(7);
MSB=read_BMP085(0xF6); //MSB
IIC_receive_respons();
LSB=read_BMP085(0xF7); //MSB
IIC_end();
UT = MSB<<8+LSB;
T=calculate_temperature();
ge = T%10;
shi = T%100/10;
bai = T%1000/100;
qian= T%10000/1000;
wan = T%100000/10000;
shiwan= T%1000000/100000;
baiwan= T%10000000/1000000;
qianwan= T%100000000/10000000;
//wanwan = T%1000000000/100000000;//千万位用于测试
//write_com_1602(0x82); //命令
//write_data_1602(qianwan+0x30); //写数据
write_com_1602(0x83); //命令
write_data_1602(wanwan+0x30); //写数据
write_com_1602(0x84); //命令
write_data_1602(baiwan+0x30); //写数据
write_com_1602(0x85); //命令
write_data_1602(shiwan+0x30); //写数据
write_com_1602(0x86); //命令
write_data_1602(wan+0x30); //写数据
write_com_1602(0x87); //命令
write_data_1602(qian+0x30); //写数据
write_com_1602(0x88); //命令
write_data_1602(bai+0x30); //写数据
write_com_1602(0x89); //命令
write_data_1602(shi+0x30); //写数据
write_com_1602(0x90); //命令
write_data_1602(ge+0x30); //写数据
}
}
我来回答