converting c program to picaxe basic - i2c functions

Texy

Senior Member
Hi,
can someone cast an eye over my picaxe conversion code and see if I,m getting this right? It will be for a BMP085 I2C pressure sensor I have on order.

Code:
long bmp085ReadTemp(void)
{
 i2cSendStart();
 i2cWaitForComplete();
 
 i2cSendByte(BMP085_W); // write 0xEE
 i2cWaitForComplete();
 
 i2cSendByte(0xF4); // write register address
 i2cWaitForComplete();
 
 i2cSendByte(0x2E); // write register data for temp
 i2cWaitForComplete();
 
 i2cSendStop();
 
 delay_ms(10); // max time is 4.5ms
 
 return (long) bmp085ReadShort(0xF6);
}
short bmp085ReadShort(unsigned char address)
{
 char msb, lsb;
 short data;
 
 i2cSendStart();
 i2cWaitForComplete();
 
 i2cSendByte(BMP085_W); // write 0xEE
 i2cWaitForComplete();
 
 i2cSendByte(address); // write register address
 i2cWaitForComplete();
 
 i2cSendStart();
 
 i2cSendByte(BMP085_R); // write 0xEF
 i2cWaitForComplete();
 
 i2cReceiveByte(TRUE);
 i2cWaitForComplete();
 msb = i2cGetReceivedByte(); // Get MSB result
 i2cWaitForComplete();
 
 i2cReceiveByte(FALSE);
 i2cWaitForComplete();
 lsb = i2cGetReceivedByte(); // Get LSB result
 i2cWaitForComplete();
 
 i2cSendStop();
 
 data = msb << 8;
 data |= lsb;
 
 return data;
}
and I think it converts to :

Code:
hi2csetup i2cmaster, 101110, i2cslow_8, i2cbyte  ; set up bmp085 i2c device
.
.
.
readtemp:
;word read returned in b3,b2 (w1)
hi2cout $F4,($2E)
hi2cin $F6,(b3,b2)
return
The datasheet for the sensor is here :
http://www.bosch-sensortec.com/content/language1/downloads/BST-BMP085-DS000-05.pdf

Cheers

T.
 

westaust55

Moderator
Looking at page 13 of the datasheet, you seem to be on the right track.

A few comments regarding your posted code snippet:
1. change _101110 to %101110 - better to always have all 8 bits shown so no confusion - seems you may be cutting and pasting from something like word with embeded chars
2. not sure where you got that i2c slave address from (maybe what we see is "distored by initial symbol) but from datasheet page 16 it indicates %11101110
3. you cannot use "readtemp:" as a label since readtemp is a PICAXE BASIC command

Also from page 13 you need to read in the calibration constants first to be used with the math for temp and pressure.
This is similar to what I had to do with the HOPERF pressure sensor that I did some work with and posted on the forum some time ago now.
 
Last edited:

Texy

Senior Member
Thanks WA,
1..yes, I had the full %11101110, but forgot to disable the parse option
2..see 1 above
3..Yes, I was just doodling code at work, so didn't have access to PE, corrected now.

The part arrived in the post yesterday, and I have the following code working :

Code:
; BMP085 pressure sensor play
; <Texy> 27/8/11


#picaxe 20x2
setfreq m8


#terminal 9600
pause 1000
symbol    BMP_AC1=8554
symbol    BMP_AC2=64369    ; 64369 -1167
symbol    BMP_AC3=51087    ; 51087 -14449
symbol    BMP_AC4=35165
symbol    BMP_AC5=1        ; 25000/32768 = 0.7629
symbol    BMP_AC6=18048
symbol    BMP_B1=5498
symbol    BMP_B2=62
symbol    BMP_MB=32678    ; 32678 -32678
symbol    BMP_MC=54461    ; 54461 -11075
symbol    BMP_MD=2432


;let dirsc = 101000 ; 1= o/p, 0 = i/p
let dirsb = %1111111 ; 1= o/p, 0 = i/p


hi2csetup i2cmaster, %11101110, i2cslow_8, i2cbyte  ; set up bmp085 i2c device


sertxd("Start!",cr,lf)
;gosub readcalibration


main:


gosub readtemperature
sertxd("temp=",#w1,cr,lf)


gosub readpressure
sertxd("pressure=",#w1,cr,lf)
pause 1000
goto main




;************************ subroutines **********************************


readcalibration:
hi2cin $AA,(b3,b2)
sertxd("$AA=",#w1,cr,lf)
hi2cin $AC,(b3,b2)
sertxd("$AC=",#w1,cr,lf)
hi2cin $AE,(b3,b2)
sertxd("$AE=",#w1,cr,lf)
hi2cin $B0,(b3,b2)
sertxd("$B0=",#w1,cr,lf)
hi2cin $B2,(b3,b2)
sertxd("$B2=",#w1,cr,lf)
hi2cin $B4,(b3,b2)
sertxd("$B4=",#w1,cr,lf)
hi2cin $B6,(b3,b2)
sertxd("$B6=",#w1,cr,lf)
hi2cin $B8,(b3,b2)
sertxd("$B8=",#w1,cr,lf)
hi2cin $BA,(b3,b2)
sertxd("$BA=",#w1,cr,lf)
hi2cin $BC,(b3,b2)
sertxd("$BC=",#w1,cr,lf)
hi2cin $BE,(b3,b2)
sertxd("$BE=",#w1,cr,lf)


return


readi2cbyte:
;address sent in b0, word read returned in b3,b2 (w1)
hi2cin b0,(b3,b2)
return


readtemperature:
;word read returned in b3,b2 (w1)
hi2cout $F4,($2E)
hi2cin $F6,(b3,b2)
return


readpressure:
;word read returned in b3,b2 (w1)
hi2cout $F4,($34)
hi2cin $F6,(b3,b2)
return




end
This seems to return valid data, at least with the temperature reading. Unfortunately the calculations required are probably beyond the scope of any picaxe chip, so I will still need to calculate via a spreadsheet/lookup table method :

Temprature :
Code:
    x1 = ((long)ut - ac6) * ac5 >> 15;
    x2 = ((long) mc << 11) / (x1 + md);
    b5 = x1 + x2;
    *temperature = (b5 + 8) >> 4;
Pressure :
Code:
    b6 = b5 - 4000;
    x1 = (b2 * (b6 * b6 >> 12)) >> 11;
    x2 = ac2 * b6 >> 11;
    x3 = x1 + x2;
    b3 = (((int32_t) ac1 * 4 + x3) + 2)/4;
    x1 = ac3 * b6 >> 13;
    x2 = (b1 * (b6 * b6 >> 12)) >> 16;
    x3 = ((x1 + x2) + 2) >> 2;
    b4 = (ac4 * (unsigned long) (x3 + 32768)) >> 15;
    b7 = ((unsigned long) up - b3) * (50000 >> OSS);
    p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;
    x1 = (p >> 8) * (p >> 8);
    x1 = (x1 * 3038) >> 16;
    x2 = (-7357 * p) >> 16;
    *pressure = p + ((x1 + x2 + 3791) >> 4);
Texy
 

Texy

Senior Member
Temperature :
Code:
    x1 = ((long)ut - ac6) * ac5 >> 15;
    x2 = ((long) mc << 11) / (x1 + md);
    b5 = x1 + x2;
    *temperature = (b5 + 8) >> 4;

Texy
Of course now that I know the calibrate constants and to pre-calculate the shifts, this simplifies too :

x1=(ut-18048) * 25000/32768
x2=-11075*2048 /(x1+2432)
b5=x1+x2
*temperature=(b5+8)/16

.but this is still not exactly easy within the confides of 16 bit maths.
Texy
 

westaust55

Moderator

Colin Hedley

New Member
Altimiter bmp sensor.

Hi there, i am really interested in how this is comming along.

I have started collecting items to build an altimeter/vario for paragliding and the paramotor i am building. I hope to also have it with a built in RPM and engine Temp gauge.
I intend to output to an Axe-oled display

My programing knowlage is really lacking so i would be interested in how far this has come?

I understand that a look-up table would be needed (stored on an eprom?) would it be possable to access the sensor I2C and the Eprom and the Oled display all from the same picaxe?

I have one of the Sheild kits i intend on using for this...

Many thanks Colin
 

Texy

Senior Member
I decided it was too much work for the Picaxe. For one thing, with the BMP085, you will need to read the temperature and the pressure values in order to compute the altitude. A lookup table works fine if you only need to cross reference the pressure reading only, but with two parameters to take into consideration, it makes it somewhat painful.
On the plus side, it has pushed me towards the Arduino platform, at least for this particular project. As you can see above, the readngs can be computed real-time, with no need for a lookup table. Its a great peice of kit, and can be used as a single chip configuration, just like the picaxe.

Regards,
Texy
 

Colin Hedley

New Member
HI Texy, I had wondered If I should move this project to something with hotter maths power.
Basic is the only language I have ever realy used, all those years ago, back when I was at school...and I love the picaxe for its ease of use, I see what you mean about the size the table would become to do what I want, I guess I wouldnt have much space left for my other processes even if it would all fit.
If you have a link to any info you have on your Arduino projec, I would be very greatfull to just get a look at the code and see if it makes any sence.
Many thanks Colin.
Ps im still inlove with picaxes, and will keep using them as a universal mcu for items around my Museums interactive exhibits.
 

Texy

Senior Member
HI Texy, I had wondered If I should move this project to something with hotter maths power.
Basic is the only language I have ever realy used, all those years ago, back when I was at school...and I love the picaxe for its ease of use, I see what you mean about the size the table would become to do what I want, I guess I wouldnt have much space left for my other processes even if it would all fit.
If you have a link to any info you have on your Arduino projec, I would be very greatfull to just get a look at the code and see if it makes any sence.
Many thanks Colin.
Ps im still inlove with picaxes, and will keep using them as a universal mcu for items around my Museums interactive exhibits.
Yes C/C++ is a step up in complexity from basic, but there is so much code, examples and tutorials out there that you only need a search to help with any particular problem. I,d recommend the offical arduino forum to start with,a quick search for BMP085 will get plenty of code examples.
http://arduino.cc/forum/

As this is non-picaxe related, feel free to PM me for info in what I,ve done so far.

Rgds,
Texy
 
Top