Here is the main routine and the sensor routine. I looked through the datasheet and didn't find any information on big/little endian. I see how you were dissecting the bytes, but why would it do lower byte for one %u and the higher byte for the next %u. I would have thought it would have just given you the lower byte each time. It seems like the question is what is going on with the l modifyer between the 2 methods. They both compile fine, but the first way doesn't show anything on the screen.
Code:
#include "pic.h"
#include "string.h"
#include "87x_i2c_master.h"
#include "lcd_wintek_wdc2401p_rev1.h"
#include "BMP085_rev2.h"
#include "delay_jrb.h"
#include "math.h"
#include "stdio.h"
#define _XTAL_FREQ 8000000
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_OFF & CP_OFF);
char LCD_string[25];
char temp_str[10], pres_str[10];
int main(void)
{
ADCON1 = 0b00000110; // Set PORTA to all digital I/O
TRISA = 0b000000; // All RAs are outputs for the LCD
TRISB = 0b00000000; // All RBs are outputs for the LCD
TRISD = 0b00000111; // Set RD0, RD1, and RD2 to inupt and the rest to output
PSPMODE = 0; // Parallel Slave Port Mode Select bit, 0 = PORTD functions in general purpose I/O mode
long temperature;
long pressure;
long *temperature_ptr = &temperature;
long *pressure_ptr = &pressure;
LCDInitialize(); // Initialize the LCD
i2c_init(); // Initialize the I2C protocol
while(1)
{
Take_Measurement(temperature_ptr, pressure_ptr);
// For whatever stupid reason, I can't get this to work
//sprintf(LCD_string, "T:%ld, P:%ld", temperature, pressure);
// So I gave up and connected the string together bit by bit
sprintf(temp_str, "%ld", temperature);
sprintf(pres_str, "%ld", pressure);
strcpy(LCD_string, "T:");
strcat(LCD_string, temp_str);
strcat(LCD_string, ", P:");
strcat(LCD_string, pres_str);
LCDWriteString(LCD_string);
}
return 0;
}
Code:
/*
BMP085_rev2 removes the BMP085 initialization call that captures
the calibration coefficients. These values have already been
captured and are now being hard coded to reduce code space.
*/
#include "delay_jrb.h"
#include "BMP085_rev1.h"
#include "87x_i2c_master.h"
#include "pic.h"
#define BMP085_ADDRESS 0xEE // I2C address of BMP085
#define OSS 3
/*
// Old calibration constant variable declarations
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
*/
// Calibration constants initialized with known values.
int ac1 = 0x2117;
int ac2 = 0xFB5C;
int ac3 = 0xC79E;
unsigned int ac4 = 0x8803;
unsigned int ac5 = 0x617E;
unsigned int ac6 = 0x5B34;
int b1 = 0x157A;
int b2 = 0x0040;
int mb = 0x8000;
int mc = 0xD4BD;
int md = 0x0980;
static long x1, x2, b5, b6, x3, b3, p;
static unsigned long b4, b7;
// Read 2 bytes from the BMP085
// First byte will be from 'address'
// Second byte will be from 'address'+1
int bmp085ReadInt(unsigned char address)
{
unsigned char msb, lsb;
i2c_start();
i2c_write(BMP085_ADDRESS);
i2c_write(address);
DelayUs(10);
i2c_repStart();
i2c_write(BMP085_ADDRESS | 0x01);
msb = i2c_read(1);
lsb = i2c_read(0);
i2c_stop();
return (int) ((int) msb<<8 | lsb);
}
unsigned long bmp085ReadLong3(unsigned char address)
{
unsigned char msb, lsb, xlsb;
i2c_start();
i2c_write(BMP085_ADDRESS);
i2c_write(address);
DelayUs(10);
i2c_repStart();
i2c_write(BMP085_ADDRESS | 0x01);
msb = i2c_read(1);
lsb = i2c_read(1);
xlsb = i2c_read(0);
i2c_stop();
unsigned long result = msb;
result <<= 8;
result += lsb;
result <<= 8;
result += xlsb;
result >>= 8-OSS;
return result;
//return (long) ((long) msb<<16 | (long)lsb << 8 | (long)xlsb) >> (8-OSS);
}
/*
// Do not need initialize since calibration coefficients are already known
void bmp085_init(void)
{
ac1 = bmp085ReadInt(0xAA);
ac2 = bmp085ReadInt(0xAC);
ac3 = bmp085ReadInt(0xAE);
ac4 = bmp085ReadInt(0xB0);
ac5 = bmp085ReadInt(0xB2);
ac6 = bmp085ReadInt(0xB4);
b1 = bmp085ReadInt(0xB6);
b2 = bmp085ReadInt(0xB8);
mb = bmp085ReadInt(0xBA);
mc = bmp085ReadInt(0xBC);
md = bmp085ReadInt(0xBE);
}
*/
void bmp085_start_convert_pressure(void)
{
i2c_start();
i2c_write(BMP085_ADDRESS);
i2c_write(0xF4);
i2c_write(0x34 + (OSS<<6));
DelayUs(10);
i2c_stop();
}
void bmp085_start_convert_temp(void)
{
i2c_start();
i2c_write(BMP085_ADDRESS);
i2c_write(0xF4);
i2c_write(0x2E);
DelayUs(10);
i2c_stop();
}
long bmp085_read_temp(void)
{
long raw;
raw = bmp085ReadInt(0xF6);
return raw;
}
unsigned long bmp085_read_pressure(void)
{
unsigned long up;
up = bmp085ReadLong3(0xF6);
return up;
}
void bmp085_convert_temp(long raw, long *temperature_ptr)
{
x1 = ((long)raw - (long)ac6) * (long)ac5 >> 15;
x2 = ((long) mc << 11) / (x1 + (long)md);
b5 = x1 + x2;
*temperature_ptr = (b5 + 8L) >> 4;
}
// bmp085_convert_temp required before this call!!
void bmp085_convert_pressure(unsigned long up, long* pressure_ptr)
{
b6 = b5 - 4000L;
x1 = ((signed long)b2 * (b6 * b6 >> 12)) >> 11;
x2 = (signed long)ac2 * b6 >> 11;
x3 = x1 + x2;
b3 = ((((signed long)ac1 * 4L + x3) << OSS) + 2) >> 2;
x1 = (signed long)ac3 * b6 >> 13;
x2 = ((signed long)b1 * (b6 * b6 >> 12)) >> 16;
x3 = ((x1 + x2) + 2) >> 2;
b4 = ((unsigned long)ac4 * (unsigned long)(x3 + 32768L)) >> 15;
b7 = (up - (unsigned long)b3) * (50000L >> OSS);
p = (signed long)((b7 < 0x80000000) ? (b7 * 2L) / b4 : (b7 / b4) * 2L);
x1 = (p >> 8) * (p >> 8);
x1 = (x1 * 3038L) >> 16;
x2 = (-7357 * p) >> 16;
*pressure_ptr = p + ((x1 + x2 + 3791L) >> 4);
}
void Take_Measurement(long *temperature_ptr, long *pressure_ptr)
{
long raw;
long up;
bmp085_start_convert_temp();
DelayMs(5);
raw = bmp085_read_temp();
bmp085_start_convert_pressure();
DelayMs(26);
up = bmp085_read_pressure();
bmp085_convert_temp(raw, temperature_ptr);
bmp085_convert_pressure(up, pressure_ptr);
}