From 20f95c72bf21389ac92f354d8d81f5fe87772fc0 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Tue, 26 Jun 2018 18:03:42 +0200 Subject: [PATCH] BMP085 basics --- Inc/app.h | 4 ++ Makefile | 1 + Src/bmp085.c | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++ Src/main.c | 1 + 4 files changed, 159 insertions(+) create mode 100644 Src/bmp085.c diff --git a/Inc/app.h b/Inc/app.h index 1fb9965..b125b58 100644 --- a/Inc/app.h +++ b/Inc/app.h @@ -13,3 +13,7 @@ extern byte tx_packet[64]; extern volatile byte rx_packet_state, tx_packet_state; void tx_packet_send(void); + +// bmp085.c + +void bmp_init(void); diff --git a/Makefile b/Makefile index 3a43c64..220477d 100644 --- a/Makefile +++ b/Makefile @@ -57,6 +57,7 @@ Src/debug.c \ Src/usb.c \ Src/usbdev.c \ Src/display.c \ +Src/bmp085.c \ /aux/misc/stm/F1-package/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_rcc.c \ /Src/system_stm32f1xx.c \ Src/stm32f1xx_it.c \ diff --git a/Src/bmp085.c b/Src/bmp085.c new file mode 100644 index 0000000..4df07c2 --- /dev/null +++ b/Src/bmp085.c @@ -0,0 +1,153 @@ +#include "stm32f1xx.h" +#include "stm32f1xx_hal.h" + +#include "util.h" +#include "app.h" + +static uint bmp_read(uint reg, uint bytes) +{ + LL_I2C_GenerateStartCondition(I2C1); + while (!LL_I2C_IsActiveFlag_SB(I2C1)) + ; + LL_I2C_TransmitData8(I2C1, 0xee); + while (!LL_I2C_IsActiveFlag_ADDR(I2C1)) + ; + LL_I2C_ClearFlag_ADDR(I2C2); + while (!LL_I2C_IsActiveFlag_TXE(I2C1)) + ; + LL_I2C_TransmitData8(I2C1, reg); + while (!LL_I2C_IsActiveFlag_TXE(I2C1)) + ; + LL_I2C_GenerateStopCondition(I2C1); + + LL_I2C_GenerateStartCondition(I2C1); + while (!LL_I2C_IsActiveFlag_SB(I2C1)) + ; + LL_I2C_AcknowledgeNextData(I2C1, LL_I2C_ACK); + LL_I2C_TransmitData8(I2C1, 0xef); + while (!LL_I2C_IsActiveFlag_ADDR(I2C1)) + ; + LL_I2C_ClearFlag_ADDR(I2C2); + + uint d = 0; + for (uint i=0; i>= (8-oss); + + int X1 = (UT-AC6)*AC5 / (1<<15); + int X2 = MC*(1<<11) / (X1+MD); + int B5 = X1 + X2; + int T = (B5+8) / (1<<4); + *tt = T; + + int B6 = B5 - 4000; + X1 = (B2*(B6*B6/(1<<12))) / (1<<11); + X2 = AC2 * B6 / (1<<11); + int X3 = X1 + X2; + int B3 = (((AC1*4 + X3) << oss) + 2) / 4; + X1 = AC3 * B6 / (1<<13); + X2 = (B1*(B6*B6/(1<<12))) / (1<<16); + X3 = ((X1+X2) + 2) / (1<<2); + uint B4 = (uint)(AC4 * (X3 + 32768)) / (1U<<15); + uint B7 = (uint)(UP-B3) * (uint)(50000>>oss); + int p; + if (B7 < 0x80000000) + p = (B7*2) / B4; + else + p = B7 / B4 * 2; + X1 = (p/(1<<8)) * (p/(1<<8)); + X1 = (X1*3038) / (1<<16); + X2 = (-7357*p) / (1<<16); + p = p + (X1 + X2 + 3791) / (1<<4); + *pp = p; +} + +static u16 bmp_constants[11]; + +void bmp_init(void) +{ + for (uint i=0; i<11; i++) + bmp_constants[i] = bmp_read(0xaa + 2*i, 2); +} + +#if 0 +void run_test(void) +{ + for (;;) + { + debug_puts("Constants:"); + u16 cc[11]; + for (uint i=0; i<11; i++) + { + cc[i] = bmp_read(0xaa + 2*i, 2); + debug_printf(" %04x", cc[i]); + } + debug_puts("\r\n"); + + uint raw_temp = bmp_measure(0x2e, 2); + debug_printf("Raw temperature: %04x\r\n", raw_temp); + + uint oss = 3; // Over-sampling setting + uint raw_press = bmp_measure(0xf4 | (oss<<6), 3); + debug_printf("Raw pressure: %06x\r\n", raw_press); + + int temp, press; + bmp_recalc(raw_temp, raw_press, oss, cc, &temp, &press); + debug_printf("Temperature: %d ddegC\r\n", temp); + debug_printf("Pressure: %d Pa\r\n", press); + } +#endif diff --git a/Src/main.c b/Src/main.c index e77264b..043fe33 100644 --- a/Src/main.c +++ b/Src/main.c @@ -125,6 +125,7 @@ int main(void) /* USER CODE BEGIN 2 */ display_init(); usb_start(&usb); + // bmp_init(); LL_TIM_EnableCounter(TIM4); LL_TIM_EnableIT_UPDATE(TIM4); -- 2.39.2