imannejaty
Junior Member level 1

The service engineer's greetings
I ardino bmp085 program, I did not understand much of the program, but if it is possible to convert the compiler
Please help me very much of my code vision
- - - Updated - - -
I wrote this program but the amount shown on the LCD error
:
Should sda and scl pin voltage microcontroller atmega16 bmp085 should be given directly to the module should be converted to 3.3 volts and then the module is bmp085
Page 8 Data sheet written by an input voltage of 4.25 volts should not exceed
Do sda and scl base voltage of 4.25 V microcontroller atmega16 more?
Do you have the base voltage of 3.3 volts sda and scl micro micro then be converted?
If you need to:
What modules can easily do this conversion?
I saw this thing on the site, but did not notice much because the language was not English expression
http://avrproject.ru/publ/barometr_na_bmp085/1-1-0-102
I vcc base 3.3-volt modules sda and scl Micro Direct Contact I base foundation sda and scl Contact I Will Micro module is damaged?
I'm quite confused
By God please help me
Many thanks to all your loved ones to serve your people
I ardino bmp085 program, I did not understand much of the program, but if it is possible to convert the compiler
Please help me very much of my code vision
Code:
/****************************************************************************
* BMP085.cpp - BMP085/I2C (Digital Pressure Sensor) library for Arduino *
* Copyright 2010-2012 Filipe Vieira & various contributors *
* *
* This file is part of BMP085 Arduino library. *
* *
* This library is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
/****************************************************************************
* Tested on Arduino Mega with BMP085 Breakout *
* SDA -> pin 20 (no pull up resistors) *
* SCL -> pin 21 (no pull up resistors) *
* XCLR -> not connected *
* EOC -> not connected *
* GND -> pin GND *
* VCC -> pin 3.3V *
* NOTE: SCL and SDA needs pull-up resistors for each I2C bus. *
* 2.2kOhm..10kOhm, typ. 4.7kOhm *
*****************************************************************************/
#include <Wire.h>
#include <BMP085.h>
BMP085::BMP085() {
_dev_address = BMP085_ADDR;
_pressure_waittime[0] = 5; // These are maximum convertion times.
_pressure_waittime[1] = 8; // It is possible to use pin EOC (End Of Conversion)
_pressure_waittime[2] = 14;// to check if conversion is finished (logic 1)
_pressure_waittime[3] = 26;// or running (logic 0) insted of waiting for convertion times.
_cm_Offset = 0;
_Pa_Offset = 0; // 1hPa = 100Pa = 1mbar
oldEMA = 0;
}
void BMP085::init() {
init(MODE_STANDARD, 0, true);
}
void BMP085::init(byte _BMPMode, int32_t _initVal, bool _Unitmeters){
getCalData(); // initialize cal data
calcTrueTemperature(); // initialize b5
setMode(_BMPMode);
_Unitmeters ? setLocalAbsAlt(_initVal) : setLocalPressure(_initVal);
}
byte BMP085::getDevAddr() {
return _dev_address;
}
byte BMP085::getMode(){
return _oss;
}
void BMP085::setMode(byte _BMPMode){
_oss = _BMPMode;
}
void BMP085::setLocalPressure(int32_t _Pa){
int32_t tmp_alt;
_param_datum = _Pa;
getAltitude(&tmp_alt); // calc altitude based on current pressure
_param_centimeters = tmp_alt;
}
void BMP085::setLocalAbsAlt(int32_t _centimeters){
int32_t tmp_Pa;
_param_centimeters = _centimeters;
getPressure(&tmp_Pa); // calc pressure based on current altitude
_param_datum = tmp_Pa;
}
void BMP085::setAltOffset(int32_t _centimeters){
_cm_Offset = _centimeters;
}
void BMP085::sethPaOffset(int32_t _Pa){
_Pa_Offset = _Pa;
}
void BMP085::zeroCal(int32_t _Pa, int32_t _centimeters){
setAltOffset(_centimeters - _param_centimeters);
sethPaOffset(_Pa - _param_datum);
}
void BMP085::getPressure(int32_t *_Pa){
long TruePressure;
calcTruePressure(&TruePressure);
*_Pa = TruePressure / pow((1 - (float)_param_centimeters / 4433000), 5.255) + _Pa_Offset;
// converting from float to int32_t truncates toward zero, 1010.999985 becomes 1010 resulting in 1 Pa error (max).
// Note that BMP085 abs accuracy from 700...1100hPa and 0..+65؛C is +-100Pa (typ.)
}
void BMP085::getAltitude(int32_t *_centimeters){
long TruePressure;
calcTruePressure(&TruePressure);
*_centimeters = 4433000 * (1 - pow((TruePressure / (float)_param_datum), 0.1903)) + _cm_Offset;
// converting from float to int32_t truncates toward zero, 100.999985 becomes 100 resulting in 1 cm error (max).
}
void BMP085::getTemperature(int32_t *_Temperature) {
calcTrueTemperature(); // force b5 update
*_Temperature = ((b5 + 8) >> 4);
}
void BMP085::calcTrueTemperature(){
long ut,x1,x2;
//read Raw Temperature
writemem(CONTROL, READ_TEMPERATURE);
delay(5); // min. 4.5ms read Temp delay
readmem(CONTROL_OUTPUT, 2, _buff);
ut = ((long)_buff[0] << 8 | ((long)_buff[1])); // uncompensated temperature value
// calculate temperature
x1 = ((long)ut - ac6) * ac5 >> 15;
x2 = ((long)mc << 11) / (x1 + md);
b5 = x1 + x2;
}
void BMP085::calcTruePressure(long *_TruePressure) {
long up,x1,x2,x3,b3,b6,p;
unsigned long b4,b7;
int32_t tmp;
#if AUTO_UPDATE_TEMPERATURE
calcTrueTemperature(); // b5 update
#endif
//read Raw Pressure
writemem(CONTROL, READ_PRESSURE+(_oss << 6));
delay(_pressure_waittime[_oss]);
readmem(CONTROL_OUTPUT, 3, _buff);
up = ((((long)_buff[0] <<16) | ((long)_buff[1] <<8) | ((long)_buff[2])) >> (8-_oss)); // uncompensated pressure value
// calculate true pressure
b6 = b5 - 4000; // b5 is updated by calcTrueTemperature().
x1 = (b2* (b6 * b6 >> 12)) >> 11;
x2 = ac2 * b6 >> 11;
x3 = x1 + x2;
tmp = ac1;
tmp = (tmp * 4 + x3) << _oss;
b3 = (tmp + 2) >> 2;
x1 = ac3 * b6 >> 13;
x2 = (b1 * (b6 * b6 >> 12)) >> 16;
x3 = ((x1 + x2) + 2) >> 2;
b4 = (ac4 * (uint32_t) (x3 + 32768)) >> 15;
b7 = ((uint32_t)up - b3) * (50000 >> _oss);
p = b7 < 0x80000000 ? (b7 << 1) / b4 : (b7 / b4) << 1;
x1 = (p >> 8) * (p >> 8);
x1 = (x1 * 3038) >> 16;
x2 = (-7357 * p) >> 16;
*_TruePressure = p + ((x1 + x2 + 3791) >> 4);
}
void BMP085::dumpCalData() {
Serial.println("---cal data start---");
Serial.print("ac1:");
Serial.println(ac1,DEC);
Serial.print("ac2:");
Serial.println(ac2,DEC);
Serial.print("ac3:");
Serial.println(ac3,DEC);
Serial.print("ac4:");
Serial.println(ac4,DEC);
Serial.print("ac5:");
Serial.println(ac5,DEC);
Serial.print("ac6:");
Serial.println(ac6,DEC);
Serial.print("b1:");
Serial.println(b1,DEC);
Serial.print("b2:");
Serial.println(b2,DEC);
Serial.print("mb:");
Serial.println(mb,DEC);
Serial.print("mc:");
Serial.println(mc,DEC);
Serial.print("md:");
Serial.println(md,DEC);
Serial.println("---cal data end---");
}
//PRIVATE methods
void BMP085::getCalData() {
readmem(CAL_AC1, 2, _buff);
ac1 = ((int)_buff[0] <<8 | ((int)_buff[1]));
readmem(CAL_AC2, 2, _buff);
ac2 = ((int)_buff[0] <<8 | ((int)_buff[1]));
readmem(CAL_AC3, 2, _buff);
ac3 = ((int)_buff[0] <<8 | ((int)_buff[1]));
readmem(CAL_AC4, 2, _buff);
ac4 = ((unsigned int)_buff[0] <<8 | ((unsigned int)_buff[1]));
readmem(CAL_AC5, 2, _buff);
ac5 = ((unsigned int)_buff[0] <<8 | ((unsigned int)_buff[1]));
readmem(CAL_AC6, 2, _buff);
ac6 = ((unsigned int)_buff[0] <<8 | ((unsigned int)_buff[1]));
readmem(CAL_B1, 2, _buff);
b1 = ((int)_buff[0] <<8 | ((int)_buff[1]));
readmem(CAL_B2, 2, _buff);
b2 = ((int)_buff[0] <<8 | ((int)_buff[1]));
readmem(CAL_MB, 2, _buff);
mb = ((int)_buff[0] <<8 | ((int)_buff[1]));
readmem(CAL_MC, 2, _buff);
mc = ((int)_buff[0] <<8 | ((int)_buff[1]));
readmem(CAL_MD, 2, _buff);
md = ((int)_buff[0] <<8 | ((int)_buff[1]));
}
void BMP085::writemem(uint8_t _addr, uint8_t _val) {
Wire.beginTransmission(_dev_address); // start transmission to device
Wire.write(_addr); // send register address
Wire.write(_val); // send value to write
Wire.endTransmission(); // end transmission
}
void BMP085::readmem(uint8_t _addr, uint8_t _nbytes, uint8_t __buff[]) {
Wire.beginTransmission(_dev_address); // start transmission to device
Wire.write(_addr); // sends register address to read from
Wire.endTransmission(); // end transmission
Wire.beginTransmission(_dev_address); // start transmission to device
Wire.requestFrom(_dev_address, _nbytes);// send data n-bytes read
uint8_t i = 0;
while (Wire.available()) {
__buff[i] = Wire.read(); // receive DATA
i++;
}
Wire.endTransmission(); // end transmission
}
- - - Updated - - -
I wrote this program but the amount shown on the LCD error
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 /* بخش سخت افزاري ال سي دي و باس تي دبليو آي Rs --> PORTD bit 7 RD --> PORTC bit 2 EN --> PORTC bit 3 D4 --> PORTC bit 4 D5 --> PORTC bit 5 D6 --> PORTC bit 6 D7 --> PORTC bit 7 SCL --> PORTC bit 0 SDA --> PORTC bit 1 در ضمن پايه هاي سوم و چهارم (از شماره 1 تا 8) دو تا ال اي دي نصب ميشه که اگر در زمان خوندن و يا نوشتن روي اي تو پرام خطايي رخ بده روشن ميشن */ //-------------------------------------------------- #include <mega16a.h> #include <BMP085.h> #include <delay.h> #include <stdio.h> #include <stdlib.h> #include <alcd.h> #include <math.h> #define DEVICE_address_write 0xEE #define DEVICE_address_read 0xEF #define PT_writeAddress_register 0xF4 #define PT_resultAddress_register 0xF6 #define T_control_register 0x2E #define oss 0 #define P_control_register (0x34+(oss<<6)) #define P0 101325 #define ACK 1 #define NOACK 0 unsigned char write_to_device(unsigned char ,unsigned char ) ; unsigned char read_from_device(unsigned char , unsigned char *) ; int power(int,int); //******************************* Main Program ************************************************ void main(void) { //**********-----------Variables declaration--------********** short int ac1,ac2,ac3,b1,b2,mb,md; unsigned short int ac4,ac5,ac6; long int mc; long UT,UP,T,P,x1,x2,x3,b3,b5,b6; unsigned long int b4,b7; unsigned char k, *data=0; int i, n1,n2,n3; float alt; char str[16] ,lcd[16]; //**********------------Microcontroller registers------********* // FOSC=4 MHz FSCL = 200kHz TWSR = 0x00; TWBR = 2; // 4000000/(16+2*2) = 200 KHz #asm ("sei") //*********------------Welcome screen----------------********** lcd_init(16); lcd_clear(); lcd_putsf("Hellooo Welcome"); delay_ms(1000); DDRB=(1<<DDB7)|(1<<DDB1)|(1<<DDB0)|(1<<DDB2)|(1<<DDB3); for(i=0;i<5;i++){ PORTB=(1<<PORTB7); delay_ms(100); PORTB=(0<<PORTB7); delay_ms(100); } //*******---Initializing (Reading) calibration data----******* i=500; k=read_from_device(AC1, data); if(!k) PORTB.3=1; ac1=(short int)(data[0]<<8)+data[1]; sprintf(str,"ac1= %d",ac1); lcd_clear(); lcd_puts(str); delay_ms(i); k=read_from_device(AC2, data); if(!k) PORTB.3=1; ac2=(short int)(data[0]<<8)+data[1]; sprintf(str,"ac2= %d",ac2); lcd_gotoxy(0,1); lcd_puts(str); delay_ms(i); k=read_from_device(AC3, data); if(!k) PORTB.3=1; ac3=(short int)(data[0]<<8)+data[1]; sprintf(str,"ac3= %d",ac3); lcd_clear(); lcd_puts(str); delay_ms(i); k=read_from_device(AC4, data); if(!k) PORTB.3=1; ac4=(unsigned short int)(data[0]<<8)+data[1]; sprintf(str,"ac4= %d",ac4); lcd_gotoxy(0,1); lcd_puts(str); delay_ms(i); k=read_from_device(AC5, data); if(!k) PORTB.3=1; ac5=(unsigned short int)(data[0]<<8)+data[1]; sprintf(str,"ac5= %d",ac5); lcd_clear(); lcd_puts(str); delay_ms(i); k=read_from_device(AC6, data); if(!k) PORTB.3=1; ac6=(unsigned short int)(data[0]<<8)+data[1]; sprintf(str,"ac6= %d",ac6); lcd_gotoxy(0,1); lcd_puts(str); delay_ms(i); k=read_from_device(B1, data); if(!k) PORTB.3=1; b1=(short int)(data[0]<<8)+data[1]; sprintf(str,"b1= %d",b1); lcd_clear(); lcd_puts(str); delay_ms(i); k=read_from_device(B2, data); if(!k) PORTB.3=1; b2=(short int)(data[0]<<8)+data[1]; sprintf(str,"b2= %d",b2); lcd_gotoxy(0,1); lcd_puts(str); delay_ms(i); k=read_from_device(MB, data); if(!k) PORTB.3=1; mb=(short int)(data[0]<<8)+data[1]; sprintf(str,"mb= %d",mb); lcd_clear(); lcd_puts(str); delay_ms(i); k=read_from_device(MC, data); if(!k) PORTB.3=1; mc=(long int)(data[0]<<8)+data[1]; sprintf(str,"mc= %d",mc); lcd_gotoxy(0,1); lcd_puts(str); delay_ms(i); k=read_from_device(MD, data); if(!k) PORTB.3=1; md=(short int)(data[0]<<8)+data[1]; sprintf(str,"md= %d",md); lcd_clear(); lcd_puts(str); delay_ms(i); //******--------------Programe Rutine-----------********* while(1){ //*****---------calculating Temperature ---------******* k= write_to_device(PT_writeAddress_register, T_control_register); if(!k) PORTB.2=1; else PORTB.2=0; delay_ms(5); k= read_from_device(0xf6, data) ; if(!k) PORTB.3=1; else PORTB.3=0; UT=(long)((data[0]<<8)+data[1]); x1=(UT-ac6)*ac5/(0x02<<14); x2= mc*(0x02<<10)/(x1+md) ; // (0x02<<10) = 2 power 11 b5=x1+x2; T=(b5+8)/(0x02<<3); n1=((T/10)); n2=((T%10)); lcd_clear(); /* sprintf(str,"UT= %u",UT); lcd_puts(str); delay_ms(1000); lcd_gotoxy(0,1); */ sprintf(str,"T= %d.%d \xdfC",n1,n2); lcd_puts(str); delay_ms(1000); //********---------calculating Pressure ---------********** k= write_to_device(PT_writeAddress_register , P_control_register); if(!k) PORTB.2=1; else PORTB.2=0; delay_ms(5+power(3,oss)); k=read_from_device(0xf6, data); if(!k) PORTB.3=1; else PORTB.3=0; UP=(long) ((data[0]<<8) +data[1]); //UP=(long) ( ( (data[0]<<16) + (data[1]<<8) + (data[2]) ) >> (8-oss) ) ; b6=b5-4000; x1=(b2*(b6*b6/(0x02<<11)))/(0x02<<10); x2=ac2*b6/(0x02<<10); x3=x1+x2; b3=((ac1*4+x3)<< oss +2)/4; x1=ac3*b6/(0x02<<12); x2=(b1*(b6*b6/(0x02<<12)))/(0x02<<15); x3=((x1+x2)+2)/(0x02<<1); b4=ac4*(unsigned long)(x3+32768)/(0x02<<14); b7=((unsigned long)UP-b3)*(50000>>oss); if(b7 < 0x80000000){P=(b7*2)/b4;} else {P=(b7/b4)*2;} x1=(P/(0x02<<7))*(P/(0x02<<7)); x1=(x1*3038)/(0x02<<15); x2=(-7357*P)/(0x02<<15); P=P+(x1+x2+3791)/(0x02<<3); n1=P/10000; n2=P%10000; /* lcd_clear(); sprintf(str,"UP= %u",UP); lcd_puts(str); delay_ms(1000); */ sprintf(str,"P= %d%d pa",n1,n2); lcd_gotoxy(0,1); lcd_puts(str); delay_ms(1000); //********---------calculating Altitude ---------********** alt=pow(P,(1/5.255)); alt=alt/(pow(P0,(1/5.255))); alt=((44330*(1-alt))); lcd_clear(); lcd_putsf("Height above Sea"); delay_ms(1000); ftoa(alt,1,str); sprintf(lcd,"%s m",str); lcd_gotoxy(0,1); lcd_puts(lcd); delay_ms(1000); }// End of while } // End of main //*********** ********************Functions************************************************ //-------------------------------------------------------------------------------- void TWI_start(void) { TWCR = (1<<TWINT)|(0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|(0<<TWWC)|(1<<TWEN)|(0<<TWIE); while ((TWCR & (1<<TWINT)) == 0); } //-------------------------------------------------------------------------------- void TWI_stop(void) { TWCR = (1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC)|(1<<TWEN)|(0<<TWIE); } //-------------------------------------------------------------------------------- void TWI_data_transmit(unsigned char data) { TWDR = data; TWCR = (1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)|(1<<TWEN)|(0<<TWIE); while ((TWCR & (1<<TWINT)) == 0); } //-------------------------------------------------------------------------------- unsigned char TWI_data_receive(unsigned char ack) { if(ack)TWCR = (1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)|(1<<TWEN)|(0<<TWIE); else TWCR = (1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)|(1<<TWEN)|(0<<TWIE); while ((TWCR & (1<<TWINT)) == 0); return TWDR; } //-------------------------------------------------------------------------------- unsigned char status(void) { return(TWSR & 0xf8); } //-------------------------------------------------------------------------------- unsigned char write_to_device(unsigned char address_register ,unsigned char control_register) { TWI_start(); if (status() != 0x08) {TWI_stop(); return 0;} TWI_data_transmit(DEVICE_address_write); if (status() != 0x18) {TWI_stop(); return 0;} TWI_data_transmit(address_register); if (status() != 0x28) {TWI_stop(); return 0;} TWI_data_transmit(control_register); if (status() != 0x28) {TWI_stop(); return 0;} TWI_stop(); return 1; } //----------------------------------------------------------------------------------- unsigned char read_from_device(unsigned char address_register , unsigned char *data) { TWI_start(); if (status() != 0x08) {TWI_stop(); return 0;} TWI_data_transmit(DEVICE_address_write); if (status() != 0x18) {TWI_stop(); return 0;} TWI_data_transmit(address_register); if (status() != 0x28) {TWI_stop(); return 0;} TWI_start(); //Repeated start to read from eeprom if (status() != 0x10) {TWI_stop(); return 0;} TWI_data_transmit(DEVICE_address_read); if (status() != 0x40) {TWI_stop(); return 0;} *data=TWI_data_receive(ACK); // OR data[0]=.... if (status() != 0x50) {TWI_stop(); return 0;} *(data+1)=TWI_data_receive(ACK); if (status() != 0x50) {TWI_stop(); return 0;} *(data+2)=TWI_data_receive(NOACK); if (status() != 0x58) {TWI_stop(); return 0;} TWI_stop(); return 1; } //----------------------------------------------------------------------------------- int power(int i , int j) { int k=0,r=1; for(k=0;k<j;k++){ r=r*i; } return r; }
Should sda and scl pin voltage microcontroller atmega16 bmp085 should be given directly to the module should be converted to 3.3 volts and then the module is bmp085
Page 8 Data sheet written by an input voltage of 4.25 volts should not exceed
Do sda and scl base voltage of 4.25 V microcontroller atmega16 more?
Do you have the base voltage of 3.3 volts sda and scl micro micro then be converted?
If you need to:
What modules can easily do this conversion?
I saw this thing on the site, but did not notice much because the language was not English expression
http://avrproject.ru/publ/barometr_na_bmp085/1-1-0-102
I vcc base 3.3-volt modules sda and scl Micro Direct Contact I base foundation sda and scl Contact I Will Micro module is damaged?
I'm quite confused
By God please help me
Many thanks to all your loved ones to serve your people