51單片機(jī)串口輸出BMP085程序源碼
《51單片機(jī)串口輸出BMP085程序源碼》由會(huì)員分享,可在線閱讀,更多相關(guān)《51單片機(jī)串口輸出BMP085程序源碼(9頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、//***************************************
// BMP085 IIC2aê?3ìDò
// ê1ó?μ¥???úSTC89C51
// ?§??£o11.0592M
// ??ê?£oμ???′??ú?úê?
// ±àò??·?3 Keil uVision2
// 2???oê?§í???24c04í¨D?3ìDò
// ê±??£o2012?ê5??1è?
// QQ£o531389319
//****************************************
#include
2、de
3、DA=P1^1; //IICêy?Yòy???¨ò? #define BMP085_SlaveAddress 0xee //?¨ò??÷?t?úIIC×ü???Dμ?′óμ??· #define OSS 0 // Oversampling Setting (note: code is not set up to use other OSS values) typedef unsigned char BYTE; typedef unsigned short WORD; uchar ge,shi,bai,qian,wan,shiwan;
4、 //??ê?±?á? int dis_data; //±?á? short ac1; short ac2; short ac3; unsigned short ac4; unsigned short ac5; unsigned short ac6; short b1; short b2; short mb; short mc; short md; int temperature; long pressure; void delay(unsigned int k); //void I
5、nitLcd(); //3?ê??ˉlcd1602 //void WriteDataLCM(uchar dataW); //void WriteCommandLCM(uchar CMD,uchar Attribc); //void DisplayOneChar(uchar X,uchar Y,uchar DData); void conversion(long temp_data); void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data
6、); //μ¥??D′è?êy?Y uchar Single_Read(uchar REG_Address); //μ¥???áè??ú2???′??÷êy?Y void Multiple_Read(uchar,uchar); //á?D?μ??áè??ú2???′??÷êy?Y //------------------------------------ void Delay5us(); void Delay5ms();
7、 void BMP085_Start(); void BMP085_Stop(); void BMP085_SendACK(bit ack); bit BMP085_RecvACK(); void BMP085_SendByte(BYTE dat); BYTE BMP085_RecvByte(); void BMP085_ReadPage(); void BMP085_WritePage(); //----------------------------------- //********************************************* //′?
8、?ú3?ê??ˉ //9600 bps @ 11.059 MHz void init_uart() { TMOD=0x21; TH1=0xfd; TL1=0xfd; SCON=0x50; PS=1; //′??ú?D??éè?a??ó??è??±e TR0=1; //???ˉ?¨ê±?÷ TR1=1; ET0=1; //′ò?a?¨ê±?÷0?D?? ES=1; EA=1; } //*********′??úêy?Y·¢?í****************** void SeriPushSend(uchar send_data)
9、{ SBUF=send_data; while(!TI);TI=0; } //********************************************************* void conversion(long temp_data) { shiwan=temp_data/100000+0x30 ; temp_data=temp_data%100000; //è?óà???? wan=temp_data/10000+0x30 ; temp_data=temp_data%10000; //è?óà???? qian=temp_dat
10、a/1000+0x30 ; temp_data=temp_data%1000; //è?óà???? bai=temp_data/100+0x30 ; temp_data=temp_data%100; //è?óà???? shi=temp_data/10+0x30 ; temp_data=temp_data%10; //è?óà???? ge=temp_data+0x30; } /*******************************/ void delay(unsigned int k) { unsigned int i,
11、j;
for(i=0;i 12、d Delay5us()
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
/**************************************
?óê±5oá??(STC90C52RC@12M)
2?í?μ? 13、1¤×÷?·?3,Dèòaμ÷??′?oˉêy
μ±??ó?1Tμ?MCUê±,??μ÷??′??óê±oˉêy
**************************************/
void Delay5ms()
{
WORD n = 560;
while (n--);
}
/**************************************
?eê?D?o?
**************************************/
void BMP085_Start()
{
SDA = 1; 14、//à-??êy?Y??
SCL = 1; //à-??ê±?ó??
Delay5us(); //?óê±
SDA = 0; //2úéú???μ??
Delay5us(); //?óê±
SCL = 0; //à-μíê±?ó??
}
/**************************************
�1D?o?
******************************** 15、******/
void BMP085_Stop()
{
SDA = 0; //à-μíêy?Y??
SCL = 1; //à-??ê±?ó??
Delay5us(); //?óê±
SDA = 1; //2úéúé?éy??
Delay5us(); //?óê±
}
/**************************************
·¢?íó|′eD?o?
è??ú2?êy:a 16、ck (0:ACK 1:NAK)
**************************************/
void BMP085_SendACK(bit ack)
{
SDA = ack; //D′ó|′eD?o?
SCL = 1; //à-??ê±?ó??
Delay5us(); //?óê±
SCL = 0; //à-μíê±?ó??
Delay5us(); //?óê±
}
/**** 17、**********************************
?óê?ó|′eD?o?
**************************************/
bit BMP085_RecvACK()
{
SCL = 1; //à-??ê±?ó??
Delay5us(); //?óê±
CY = SDA; //?áó|′eD?o?
SCL = 0; //à-μíê±?ó??
Delay5us(); 18、 //?óê±
return CY;
}
/**************************************
?òIIC×ü??·¢?íò???×??úêy?Y
**************************************/
void BMP085_SendByte(BYTE dat)
{
BYTE i;
for (i=0; i<8; i++) //8????êy?÷
{
dat <<= 1; //ò?3?êy?Yμ?×?????
SDA = CY; 19、//?íêy?Y?ú
SCL = 1; //à-??ê±?ó??
Delay5us(); //?óê±
SCL = 0; //à-μíê±?ó??
Delay5us(); //?óê±
}
BMP085_RecvACK();
}
/**************************************
′óIIC×ü???óê?ò???×??úêy?Y
**************************************/
BYTE BMP085_ 20、RecvByte()
{
BYTE i;
BYTE dat = 0;
SDA = 1; //ê1?ü?ú2?é?à-,×?±??áè?êy?Y,
for (i=0; i<8; i++) //8????êy?÷
{
dat <<= 1;
SCL = 1; //à-??ê±?ó??
Delay5us(); //?óê±
dat |= SDA; //?áêy?Y
SCL = 0; //à-μíê±?ó??
Del 21、ay5us(); //?óê±
}
return dat;
}
/*
//μ¥×??úD′è?BMP085?ú2?êy?Y*******************************
void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data)
{
BMP085_Start(); //?eê?D?o?
BMP085_SendByte(SlaveAddress); //·¢?íéè±?μ??·+D′D?o?
BMP085_Send 22、Byte(REG_Address); //?ú2???′??÷μ??·
BMP085_SendByte(REG_data); //?ú2???′??÷êy?Y
BMP085_Stop(); //·¢?íí£?1D?o?
}
*/
/*
//μ¥×??ú?áè?BMP085?ú2?êy?Y********************************
uchar Single_Read(uchar REG_Address)
{ uchar REG_data;
BMP085_Start(); 23、 //?eê?D?o?
BMP085_SendByte(BMP085_SlaveAddress); //·¢?íéè±?μ??·+D′D?o?
BMP085_SendByte(REG_Address); //·¢?í′?′¢μ¥?aμ??·
BMP085_Start(); //?eê?D?o?
BMP085_SendByte(BMP085_SlaveAddress+1); //·¢?íéè±?μ??·+?áD?o?
REG_data=BMP085_RecvBy 24、te(); //?á3???′??÷êy?Y
BMP085_SendACK(1);
BMP085_Stop(); //�1D?o?
return REG_data;
}
*/
//*********************************************************
//?á3?BMP085?ú2?êy?Y,á?D?á???
//*********************************************************
short Multip 25、le_read(uchar ST_Address)
{
uchar msb, lsb;
short _data;
BMP085_Start(); //?eê?D?o?
BMP085_SendByte(BMP085_SlaveAddress); //·¢?íéè±?μ??·+D′D?o?
BMP085_SendByte(ST_Address); //·¢?í′?′¢μ¥?aμ??·
BMP085_Start(); //?eê?D?o?
BMP085_S 26、endByte(BMP085_SlaveAddress+1); //·¢?íéè±?μ??·+?áD?o?
msb = BMP085_RecvByte(); //BUF[0]′?′¢
BMP085_SendACK(0); //??ó|ACK
lsb = BMP085_RecvByte();
BMP085_SendACK(1); //×?oóò???êy?YDèòa??NOACK
BMP085_Stop(); 27、 //í£?1D?o?
Delay5ms();
_data = msb << 8;
_data |= lsb;
return _data;
}
//********************************************************************
long bmp085ReadTemp(void)
{
BMP085_Start(); //?eê?D?o?
BMP085_SendByte(BMP085_SlaveAddress); //·¢?íéè±?μ??·+D′D?o?
BM 28、P085_SendByte(0xF4); // write register address
BMP085_SendByte(0x2E); // write register data for temp
BMP085_Stop(); //·¢?íí£?1D?o?
delay(10); // max time is 4.5ms
return (long) Multiple_read(0xF6);
}
//**************************************************** 29、*********
long bmp085ReadPressure(void)
{
long pressure = 0;
BMP085_Start(); //?eê?D?o?
BMP085_SendByte(BMP085_SlaveAddress); //·¢?íéè±?μ??·+D′D?o?
BMP085_SendByte(0xF4); // write register address
BMP085_SendByte(0x34); // write register data for press 30、ure
BMP085_Stop(); //·¢?íí£?1D?o?
delay(20); // max time is 4.5ms
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
//return (long) bmp085ReadShort(0xF6);
}
//************************************************************* 31、*
//3?ê??ˉBMP085£??ù?YDèòa??2???pdf??DDDT??**************
void Init_BMP085()
{
ac1 = Multiple_read(0xAA);
ac2 = Multiple_read(0xAC);
ac3 = Multiple_read(0xAE);
ac4 = Multiple_read(0xB0);
ac5 = Multiple_read(0xB2);
ac6 = Multiple_read(0xB4);
b1 = Multiple_read(0xB6);
b2 = Multiple_read 32、(0xB8);
mb = Multiple_read(0xBA);
mc = Multiple_read(0xBC);
md = Multiple_read(0xBE);
}
//***********************************************************************
void bmp085Convert()
{
unsigned int ut;
unsigned long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
ut = bmp0 33、85ReadTemp(); // ?áè????è
// ut = bmp085ReadTemp(); // ?áè????è
up = bmp085ReadPressure(); // ?áè??1??
// up = bmp085ReadPressure(); // ?áè??1??
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature = ((b5 + 8) >> 4);
//******* 34、******
b6 = b5 - 4000;
// Calculate B3
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)< 35、>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
pressure = p+((x1 + x2 + 3791)>>4);
}
//*********************************************************
//** 36、****?÷3ìDò********
//*********************************************************
void main()
{
delay(100); //é?μ??óê±
init_uart();
Init_BMP085(); //3?ê??ˉBMP085
while(1) //?-?·
{
Init_BMP085();
bmp085Convert();
conversion(tempe 37、rature);
SeriPushSend('T'); //???è??ê?
SeriPushSend(':');
SeriPushSend(bai);
SeriPushSend(shi);
SeriPushSend('.');
SeriPushSend(ge);
SeriPushSend(' ');
SeriPushSend(' ');
conversion(pressure);
SeriPushSend('P'); //??ê??1??
SeriPushSend(':');
SeriPushSend(shiwan);
SeriPushSend(wan);
SeriPushSend(qian);
SeriPushSend('.');
SeriPushSend(bai);
SeriPushSend(shi);
SeriPushSend('K'); //???1μ¥??
SeriPushSend('p');
SeriPushSend('a');
SeriPushSend(0x0d);
SeriPushSend(0x0a);
delay(100);
}
}