From 560b3f6852f569cb5e42eb430152937440a46cd7 Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Sun, 12 Jun 2016 18:24:37 +0200 Subject: BMP280 library and example --- usb-bmp280/usb-bmp280.c | 84 +++++++++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 31 deletions(-) (limited to 'usb-bmp280/usb-bmp280.c') diff --git a/usb-bmp280/usb-bmp280.c b/usb-bmp280/usb-bmp280.c index 0c19545..da9c994 100644 --- a/usb-bmp280/usb-bmp280.c +++ b/usb-bmp280/usb-bmp280.c @@ -36,7 +36,7 @@ #include "usbio.h" #include "bmp280.h" -#define ALTITUDE 353.0 // Altitude of Graz in Austria +#define ALTITUDE_GRAZ 353.0 // Altitude of Graz in Austria #define PIN_CS_S0 PD1 #define REG_CS_S0 PIND @@ -47,7 +47,8 @@ #define OP_CLEARBIT &= ~ #define OP_CHECK & #define PIN_SW(PORTDDRREG, PIN, OP) PORTDDRREG OP (1 << PIN) - +#define PINMODE_OUTPUT(REG, PIN) *(®+1) |= (1 << PIN) //just use DDR instead of PORT +#define PINMODE_INPUT(REG, PIN) *(®+1) &= ~(1 << PIN) //just use DDR instead of PORT #define PINREG(x) x #define DDRREG(x) *(&x+1) #define PORTREG(x) *(&x+2) @@ -59,13 +60,21 @@ #define CS_SENSOR_1(LOWHIGH) (PIN_SW(PORTREG(REG_CS_S1),PIN_CS_S1,LOWHIGH)) #define CS_SENSOR(x,LOWHIGH) CS_SENSOR_##x(LOWHIGH) +/* +=== Our Coefficients === +T1: 28483, T2: 26628, T3: 50 +P1: 37848, P2: -10585, P3: 3024, P4: 9882, P5: -217, P6: -7, P7: 15500, P8: -14600, P9: 6000 + +=== Adafruit Arduino Reference Coefficients === +T1: 28483, T2: 26628, T3: 50 +P1: 37848, P2: -10585, P3: 3024, P4: 9882, P5: -217, P6: -7, P7: 15500, P8: -14600, P9: 6000 +*/ + void printCoeffs(bmp280_sensor *sensor) { - printf("Coeffients\r\n"); - printf("T1: %d, T2: %d, T3: %d\r\n", sensor->dig_T1, sensor->dig_T2, sensor->dig_T3); - printf("P1: %d, P2: %d, P3: %d, P4: %d, P5: %d, P6: %d, P7: %d, P8: %d, P9: %d\r\n",sensor->dig_P1,sensor->dig_P2,sensor->dig_P3,sensor->dig_P4,sensor->dig_P5,sensor->dig_P6,sensor->dig_P7,sensor->dig_P8,sensor->dig_P9); - printf("H1: %d, H2: %d, H3: %d, H4: %d, H5: %d, H6: %d\r\n",sensor->dig_H1,sensor->dig_H2,sensor->dig_H3 - ,sensor->dig_H4,sensor->dig_H5,sensor->dig_H6); + printf("T1: %u, T2: %d, T3: %d\r\n", sensor->dig_T1, sensor->dig_T2, sensor->dig_T3); + printf("P1: %u, P2: %d, P3: %d, P4: %d, P5: %d, P6: %d, P7: %d, P8: %d, P9: %d\r\n",sensor->dig_P1,sensor->dig_P2,sensor->dig_P3,sensor->dig_P4,sensor->dig_P5,sensor->dig_P6,sensor->dig_P7,sensor->dig_P8,sensor->dig_P9); + //printf("H1: %d, H2: %d, H3: %d, H4: %d, H5: %d, H6: %d\r\n",sensor->dig_H1,sensor->dig_H2,sensor->dig_H3,sensor->dig_H4,sensor->dig_H5,sensor->dig_H6); } int main(void) @@ -81,40 +90,53 @@ int main(void) bmp280_sensor sensor0; bmp280_sensor sensor1; + uint8_t sensor0_available = 0; + uint8_t sensor1_available = 0; + + //CS Output + PINMODE_OUTPUT(REG_CS_S0,PIN_CS_S0); + PINMODE_OUTPUT(REG_CS_S1,PIN_CS_S1); + //CS High CS_SENSOR(0,HIGHv); CS_SENSOR(1,HIGHv); SPI_Init(BMP280_LUFA_SPIO_OPTIONS); - bmp280_init(&sensor0, &PORTREG(REG_CS_S0), PIN_CS_S0); - bmp280_init(&sensor1, &PORTREG(REG_CS_S1), PIN_CS_S1); - - printf("BMP280 initialized"); + printf("USB-SPI-BMP280 starting"); + + bmp280_result tmppres; + float altitude; + for(;;) { - printf("\r\n== Sensor 0 ==\r\n"); - printCoeffs(&sensor0); - float temp = bmp280_readTemp(&sensor0); - float pressure = bmp280_readPressure(&sensor0); - printf("\r\nPressure: %.2f Pa @ %.2f degC\r\n", pressure, temp); + if (sensor0_available) + { + printCoeffs(&sensor0); + tmppres = bmp280_readTempAndPressure(&sensor0); + altitude = bmp280_calcAltitude(tmppres.pressure, BMP280_AVERAGE_SEA_LEVEL_PRESSURE); + printf("\r\nPressure: %.2f Pa @ %.2f degC\r\n", tmppres.pressure, tmppres.temperature); + printf("Altitude: %.2fm (guessing)\r\n", altitude); + sensor0_available = bmp280_check_chipid(&sensor0); + } else { + printf(" not available\r\n"); + sensor0_available = bmp280_init(&sensor0, &PORTREG(REG_CS_S0), PIN_CS_S0); + } printf("\r\n== Sensor 1 ==\r\n"); - printCoeffs(&sensor1); - temp = bmp280_readTemp(&sensor1); - pressure = bmp280_readPressure(&sensor1); - printf("\r\nPressure: %.2f Pa @ %.2f degC\r\n", pressure, temp); + if (sensor1_available) + { + printCoeffs(&sensor1); + tmppres = bmp280_readTempAndPressure(&sensor1); + altitude = bmp280_calcAltitude(tmppres.pressure, BMP280_AVERAGE_SEA_LEVEL_PRESSURE); + printf("\r\nPressure: %.2f Pa @ %.2f degC\r\n", tmppres.pressure, tmppres.temperature); + printf("Altitude: %.2fm (approximate)\r\n", altitude); + sensor1_available = bmp280_check_chipid(&sensor1); + } else { + printf(" not available\r\n"); + sensor1_available = bmp280_init(&sensor1, &PORTREG(REG_CS_S1), PIN_CS_S1); + } usbio_task(); - int16_t BytesReceived = usbio_bytes_received(); - while(BytesReceived > 0) { - int ReceivedByte = fgetc(stdin); - if(ReceivedByte != EOF) { - if (ReceivedByte == '!') - reset2bootloader(); - } - BytesReceived--; - } - - _delay_ms(2000); // Pause for 5 seconds. + _delay_ms(3000); // Pause for 5 seconds. } } -- cgit v1.2.3