Using BMP180 sensor with PIC18F252

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
GerryAttrick
 
Posts: 3
Joined: Sun Dec 21, 2014 6:44 pm

Using BMP180 sensor with PIC18F252

Post by GerryAttrick »

I bought a couple of your BMP180 sensors which I have used initially with my Raspberry Pi. They both work fine when connected to the Pi and I plan to use one of them as part of a Pi weather station. I've also been experimenting using a 18F252 microcontroller but I'm having a few teething problems, to put it mildly.

One minor point: Do I need to use external pull-up resistors for the I2C interface, or does the sensor board already have them in place?

My main problem, however, is attempting to read the UP value. I'm using the peripheral I2C library in C and have no trouble at all reading the calibration parameters, chip ID and the MSB and LSB values for UT. I'm using a while loop to attempt to read UT and UP at regular time intervals. If I comment out the UP section UT reads correctly. If I include UP, the value of UT alters, usually becoming negative. If I make OSS zero and ignore XLSB, then the two routines for UT and UP are almost identical, with the exception of the value written into the 0xf4 register (0x2e for UT; 0x34 for UP). It seems that any attempt to read registers 0xf6 and 0xf7 for UP upsets any further attempts to read UT. Very odd. Any help and suggestions gratefully received.

One final question: How are negative values represented in the BMP180? The BMP180 class for use with the Pi has a method for signed words. This just seems to check whether the MSB is greater than 127 and ,if so, subtract this from 256. The LSB isn't changed. If it's a two's complement word, why does it need changing at all?

Thanks!

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Using BMP180 sensor with PIC18F252

Post by adafruit_support_mike »

GerryAttrick wrote:One minor point: Do I need to use external pull-up resistors for the I2C interface, or does the sensor board already have them in place?
Kind of..

The BMP180 breakout has level-shifters on the I2C pins, so the chip itself always sees 3.3v signals and the external circuit can use any voltage between 3v and 5v. That isolates the actual I2C pull-up resistors connected to the chip from anything beyond the edges of the PCB.

The level shifter has externally-visible pull-ups tied to the Vin pin, but those may or may not work as I2C pull-ups depending on how you're sending power the the BMP180 breakout.

The good news is that it almost never hurts to add another 4.7k resistor to an I2C line. If it isn't strictly necessary, it doesn't do any harm.

GerryAttrick wrote:It seems that any attempt to read registers 0xf6 and 0xf7 for UP upsets any further attempts to read UT. Very odd. Any help and suggestions gratefully received.
Are you waiting long enough between reads?

It takes the ADC 5ms to do a standard resolution pressure reading, and 3ms to do a standard resolution temperature reading. If you want higher resolution for the pressure reading, it can take up to 75ms.
GerryAttrick wrote:One final question: How are negative values represented in the BMP180?
AFAIK, the BMP180 doesn't use negative numbers.

Temperature and pressure are properties that only have positive values. 0.00...Pa is hard vacuum and 0.00...K is absolute zero. There's nothing on the other side of either one.

User avatar
GerryAttrick
 
Posts: 3
Joined: Sun Dec 21, 2014 6:44 pm

Re: Using BMP180 sensor with PIC18F252

Post by GerryAttrick »

Many thanks to adafruit_support_mike for his thoughts. Sorry to take so long to follow up but Christmas festivities and a corrupted SD card on my Pi have got in the way of progress.

One of the topics in the general projects forum discussed the use of the old BMP085 sensor with the Pi. It seems that one of the python classes that I was using was nonsense in terms of its treatment of signed and unsigned integers, so its results for temperature and pressure were not accurate, although pretty close to true values. Needless to say, I'm sticking to the latest Adafruit library at the moment. This imports the Adafruit_BMP.GPIOI2C (?) module. How can I find out what methods this module provides and how they work? In particular, there are methods for reading signed and unsigned 16-bit integers. I'm not at all sure what I'm talking about here but as I understand that Python integers are signed 32-bit integers so how is the type conversion done? (Sorry if this is off-topic for this forum).

As for using the BMP180 with the PIC16F252, I'm still struggling. Page 15 of the Bosch BMP180 data sheet gives the algorithm for calculating temperature and pressure and I've followed this, as does the adafruit program, of course. I can read all the parameters correctly as signed or unsigned 16-bit integers and calculate temperature correctly in a loop at timed intervals. I can also include two (or more) correct reads of UT per loop and eliminate any further calculations and everything's fine. If I then write 0x34 (instead of 0x2e) into reg 0xf4, then I should be reading UP instead of UT (with oss=0, XLSB =0). Instead, even the UT value is incorrect. I've tried increasing the wait time before reading (up to 20 ms) as well as incuding delays after each of the I2C functions, without any success.

I know this is not really an Adafruit problem, more a Microchip problem, but any advice would be greatly appreciated.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Using BMP180 sensor with PIC18F252

Post by adafruit_support_mike »

All our RasPi code is in our Github repo:

https://github.com/adafruit/Adafruit-Ra ... ython-Code

WRT bit widths, Python, like most interpreted languages, is 'loosely typed'.. it doesn't make a distinction between signed/unsigned values, and only makes a loose distinction between integers and floating point numbers. The interpreter chooses whatever data representation is most appropriate to the operation, and can translate between representations on the fly.

User avatar
GerryAttrick
 
Posts: 3
Joined: Sun Dec 21, 2014 6:44 pm

Re: Using BMP180 sensor with PIC18F252

Post by GerryAttrick »

Thanks again to adafruit_support_mike for his help and for pointing me in the direction of Raspi sourcecode. Derrr........ I think I've finally realised what I've been doing wrong when using the microcontroller with the breakout board. Basically, I've been trying to set up the control register (0xf4), then the delay, followed by the read of 0xf6 and 0xf7 all in one data transaction. This approach seems to work OK for the calibration parameters; I can only assume that the delay affects the transfer of data to the registers (?). A clue to this (which I had been ignoring) was the first displayed value of UT was always 32768 i.e. 0x8000, which are the reset values of 0xf6 and 0xf7. On the second read, these registers contained the value from the previous read.

So in a loop containing only timed reads for UT, the values seem OK after the initial value of 32768. But when trying to read UP as well in the loop, the two values are effectively swapped, with UP displaying the UT value and vice versa. Calculated pressure and temperature values were then completely bananas. By stopping the transaction after setting up the control register, delaying, then starting a new transaction to read MSB and LSB, there is no problem.

Of course, if I'd read the Bosch data sheet (pages 20 and 21) properly in the first place, I'd have saved myself a lot of bother :-).

Thanks again for you help.

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”