EN0
Member
Hello Everyone!
I'm having some difficulty with the BMP085 sensor and the PIC18F4220. The code can be viewed on codepad here, or below:
And here is the error:
The .map file allocates the sections only prior to the error, so that was no help, and I can't view my memory usage since it didn't build.
Anybody know what's going on?
Thanks,
Austin
I'm having some difficulty with the BMP085 sensor and the PIC18F4220. The code can be viewed on codepad here, or below:
Code:
/************************************************************************
*
* Module: BMP085_1.C
* Description: Code to test BMP085 sensor functionality.
* Line length: 120 characters [only if the length is longer than 80 chars]
* Functions: See Below
*
* Date: Author: Comments:
* 2 Jul 2011 Austin Schaller Created
*
************************************************************************/
//** Include Files ******************************************************
#include <p18f4220.h>
#include <delays.h>
#include <i2c.h>
#pragma config OSC=INTIO2, WDT=OFF, LVP=OFF, DEBUG=ON
//** Defines ************************************************************
#define FOSC 8000000
#define BAUD 9600
#define BMP085_R 0xEF
#define BMP085_W 0xEE
#define OSS 0 // Oversampling Setting (note: code is not set up to use other OSS values)
//** Globals ************************************************************
// Voodoo calibration varibles
short ac1;
short ac2;
short ac3;
short b1;
short b2;
short mb;
short mc;
short md;
unsigned short ac4;
unsigned short ac5;
unsigned short ac6;
//void BMP085_Calibration(void);
//short bmp085ReadShort(unsigned char address);
//** Functions **********************************************************
// delay() goes here
/************************************************************************
*
* Purpose: Round a floating-point number to the nearest integer.
* Passed: Float
* Returned: Int in hexidecimal format.
* Note: None
*
* Date: Author: Comments:
* 2 Jul 2011 Austin Schaller Created
*
************************************************************************/
/*int round(int num)
{
// TBD
}*/
/************************************************************************
*
* Purpose: Will read two sequential 8-bit registers, and return
* a 16-bit value.
* Passed: Unsigned char
* Returned: Short
* Note:
*
* Date: Author: Comments:
* 16 Mar 2011 Austin Schaller Created
*
************************************************************************/
short bmp085ReadShort(unsigned char address)
{
char msb, lsb;
short data;
OpenI2C(MASTER, SLEW_OFF);
AckI2C();
WriteI2C(BMP085_W); // Write 0xEE
AckI2C();
WriteI2C(address); // Write register address
AckI2C();
StartI2C();
WriteI2C(BMP085_R); // Write 0xEF
AckI2C();
ReadI2C();
AckI2C();
msb = ReadI2C(); // Get MSB result
AckI2C();
ReadI2C();
AckI2C();
lsb = ReadI2C(); // Get LSB result
AckI2C();
StopI2C();
data = msb << 8;
data |= lsb;
return data;
}
/************************************************************************
*
* Purpose: Calibrates the BMP085 pressure sensor.
* Passed: None
* Returned: None
* Note:
*
* Date: Author: Comments:
* 16 Mar 2011 Austin Schaller Created
*
************************************************************************/
void BMP085_Calibration(void)
{
ac1 = bmp085ReadShort(0xAA);
ac2 = bmp085ReadShort(0xAC);
ac3 = bmp085ReadShort(0xAE);
ac4 = bmp085ReadShort(0xB0);
ac5 = bmp085ReadShort(0xB2);
ac6 = bmp085ReadShort(0xB4);
b1 = bmp085ReadShort(0xB6);
b2 = bmp085ReadShort(0xB8);
mb = bmp085ReadShort(0xBA);
mc = bmp085ReadShort(0xBC);
md = bmp085ReadShort(0xBE);
}
/************************************************************************
*
* Purpose: Will read the 16-bit temperature value of BMP085 sensor.
* Passed: None
* Returned: Long
* Note:
*
* Date: Author: Comments:
* 16 Mar 2011 Austin Schaller Created
*
************************************************************************/
long bmp085ReadTemp(void)
{
StartI2C();
AckI2C();
WriteI2C(BMP085_W); // Write 0xEE
AckI2C();
WriteI2C(0xF4); // Write register address
AckI2C();
WriteI2C(0x2E); // Write register data for temp
AckI2C();
StopI2C();
Delay1KTCYx(20); // Max time is 4.5ms
return (long) bmp085ReadShort(0xF6);
}
/************************************************************************
*
* Purpose: Will read the 16-bit pressure value from BMP085 sensor.
* Passed: None
* Returned: Long
* Note:
*
* Date: Author: Comments:
* 16 Mar 2011 Austin Schaller Created
*
************************************************************************/
long bmp085ReadPressure(void)
{
long pressure = 0;
StartI2C();
AckI2C();
WriteI2C(BMP085_W); // Write 0xEE
AckI2C();
WriteI2C(0xF4); // Write register address
AckI2C();
WriteI2C(0x34); // Write register data for temp
AckI2C();
StopI2C();
Delay1KTCYx(20); // Max time is 4.5ms
pressure = bmp085ReadShort(0xF6);
pressure &= 0x0000FFFF;
return pressure;
//return (long)bmp085ReadShort(0xF6);
}
/************************************************************************
*
* Purpose: Will read the 16-bit pressure value from BMP085 sensor.
* Passed: Long *temperature, long *pressure
* Returned: None
* Note:
*
* Date: Author: Comments:
* 16 Mar 2011 Austin Schaller Created
*
************************************************************************/
void bmp085Convert(long *temperature, long *pressure)
{
long ut;
long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
ut = bmp085ReadTemp();
ut = bmp085ReadTemp(); // Some bug here, have to read twice to get good data
up = bmp085ReadPressure();
up = bmp085ReadPressure();
// Calculate true temperature
x1 = ((long) ut - ac6) * ac5 >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
*temperature = (b5 + 8) >> 4;
// Calculate true pressure
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6 >> 12)) >> 11;
x2 = ac2 * b6 >> 11;
x3 = x1 + x2;
b3 = (((long)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);
}
void main()
{
long temperature = 0;
long pressure = 0;
//long altitude = 0;
//double temp = 0;
//ioinit();
//i2cInit();
Delay10KTCYx(20); // 100 mS
BMP085_Calibration();
while(1)
{
bmp085Convert(&temperature, &pressure);
//printf("Temperature: %ld (in 0.1 deg C)\n", temperature);
//printf("Pressure: %ld Pa\n\n", pressure);
// For fun, lets convert to altitude
/*temp = (double) pressure/101325;
temp = 1 - pow(temp, 0.19029);
altitude = (int) 44330 * temp;
printf("Altitude: %ldm\n\n", altitude);*/
Delay10KTCYx(200); // 1 sec
}
}
And here is the error:
Code:
Error - section '.code_i2c_open.o' can not fit the section. Section '.code_i2c_open.o' length=0x00000036
The .map file allocates the sections only prior to the error, so that was no help, and I can't view my memory usage since it didn't build.
Anybody know what's going on?
Thanks,
Austin