Dtynin
Dtynin
Why is my BMP280 sensor value calculation incorrect on AtMega32?
I’m working with the BMP280 sensor on an AtMega32 microcontroller, aiming to convert sensor data from raw readings to a human-readable format. Specifically, I need to read calibration values from the sensor’s registers and combine them to get meaningful numbers. I’m able to read most of the calibration values correctly, but one particular value is being miscalculated. The process involves reading the LSB (Least Significant Byte) and MSB (Most Significant Byte) from the sensor, then combining these bytes into a single value. Below is my implementation:
uint8_t cmd[2] = {0xD0, 0};
uint8_t msb1 = 0;
uint8_t lsb1 = 0;
char str[32];

// Reading LSB
cmd[0] = 0x8E;
tw_master_transmit(0x76, cmd, 1, 0);
tw_master_receive(0x76, cmd, sizeof(cmd));
lsb1 = cmd[0];

// Reading MSB
cmd[0] = 0x8F;
tw_master_transmit(0x76, cmd, 1, 0);
tw_master_receive(0x76, cmd, sizeof(cmd));
msb1 = cmd[0];

// Print values
sprintf(str, "%d.%05u\r\n", (int)lsb1, (int)((lsb1 - (int)lsb1) * 100000));
print_string(str);
sprintf(str, "%d.%05u\r\n", (int)msb1, (int)((msb1 - (int)msb1) * 100000));
print_string(str);

// Combining LSB and MSB
uint16_t dig_p1 = (msb1 << 8) | lsb1;

// Print combined value
sprintf(str, "%d.%05u\r\n", (int)dig_p1, (int)((dig_p1 - (int)dig_p1) * 100000));
print_string(str);
uint8_t cmd[2] = {0xD0, 0};
uint8_t msb1 = 0;
uint8_t lsb1 = 0;
char str[32];

// Reading LSB
cmd[0] = 0x8E;
tw_master_transmit(0x76, cmd, 1, 0);
tw_master_receive(0x76, cmd, sizeof(cmd));
lsb1 = cmd[0];

// Reading MSB
cmd[0] = 0x8F;
tw_master_transmit(0x76, cmd, 1, 0);
tw_master_receive(0x76, cmd, sizeof(cmd));
msb1 = cmd[0];

// Print values
sprintf(str, "%d.%05u\r\n", (int)lsb1, (int)((lsb1 - (int)lsb1) * 100000));
print_string(str);
sprintf(str, "%d.%05u\r\n", (int)msb1, (int)((msb1 - (int)msb1) * 100000));
print_string(str);

// Combining LSB and MSB
uint16_t dig_p1 = (msb1 << 8) | lsb1;

// Print combined value
sprintf(str, "%d.%05u\r\n", (int)dig_p1, (int)((dig_p1 - (int)dig_p1) * 100000));
print_string(str);
The value in lsb1 and msb1 are confirmed to be read correctly (verified with a different microcontroller), and the expected combined value should be 38406. However, my MCU is showing -27130. All other values are computed accurately. What could be causing this discrepancy?
1 replies