Skip to content
Permalink
Browse files

decoupled update rate from main

  • Loading branch information...
EasonNYC committed Jan 10, 2016
1 parent fb02f61 commit 5c3132a4c73f8a6e00576eb341ccd4532493cf84
Showing with 92 additions and 34 deletions.
  1. +83 −26 bmp_085.c
  2. +2 −0 bmp_085.h
  3. +5 −8 main.c
  4. +2 −0 timer.h
109 bmp_085.c
@@ -14,6 +14,7 @@
#include "stm32f4xx_exti.h"
#include "stdio.h"
#include "math.h"
#include "timer.h"


/** @addtogroup BMP085_Peripheral_Driver
@@ -44,22 +45,24 @@ static short b2 = 0xFFFF;
static short mc = 0xFFFF;
static short md = 0xFFFF;
static long b5 =0x0;

volatile uint8_t procflag = 0; //state machine counter
uint8_t counter = 0; //used to calc update rate
uint32_t PRESSURE_SEALEVEL = 102250; //standard pressure at sealevel
uint8_t counter = 0; //used to calc update rate (Hz)
const uint32_t PRESSURE_SEALEVEL = 102250; //pressure at sealevel in your area

/**
* @struct BMP_READINGS
* @brief A struct which holds the most current bmp readings.
* @brief A struct which holds the most current bmp085 sensor readings.
* @param None
* @retval None
*/
struct BMP_READINGS {
float temperature;
uint32_t pressure;
float altitude;
uint16_t ut;
uint32_t up;
float temperature; //in Cel
uint32_t pressure; //in Pascals
float altitude; //in meters
uint16_t ut;
uint32_t up;
uint8_t hz; //sensor readings per second (Hz)
} bmp;

/**
@@ -506,11 +509,47 @@ uint32_t bmp085_calcP(uint32_t up, uint8_t OSS){
* @retval Float of the current altitude in meters.
*/
float bmp085_calcAlt(void){
float x = pow(bmp.pressure/PRESSURE_SEALEVEL, 0.190295F);
float y = 1.0F-x;
float altitude = 44330.0F * y;
return 44330 * (1 - pow(((float) bmp.pressure/PRESSURE_SEALEVEL), 0.190295));

float x = pow(bmp.pressure/PRESSURE_SEALEVEL, 0.190295F);
float y = 1.0F-x;
float altitude = 44330.0F * y;

return 44330 * (1 - pow(((float) bmp.pressure/PRESSURE_SEALEVEL), 0.190295));
}

/**
* @fn void reset_counter()
* @brief resets number of samples counted to 0. Used in calculating Hz.
* @param None
* @retval None
*/
void bmp085_reset_counter(void){ counter = 0; }

/**
* @fn void bmp085_save_counter(void)
* @brief saves number of samples counted so far to bmp_Readings struct. Used in calculating Hz.
* @param None
* @retval None
*/
void bmp085_save_counter(void){ bmp.hz = counter; }

/**
* @fn void bmp085_update_hz(void)
* @brief Main function which tracks how many readings per second the sensor is taking. Called every loop.
* @param None
* @retval None
*/
void bmp085_update_hz(void) {

//track bmp085 update rate (in Hz)
static volatile uint32_t time = 0;
if( millis() > (time + 1000)){
bmp085_save_counter(); //save Hz
time = millis();
bmp085_reset_counter(); //start over

}

}

/**
@@ -521,6 +560,10 @@ return 44330 * (1 - pow(((float) bmp.pressure/PRESSURE_SEALEVEL), 0.190295));
*/
void bmp085_run(void){

//track readings per second
bmp085_update_hz();

//handle state machine
switch(procflag){
case 0:
//state handled by IRQ
@@ -542,26 +585,29 @@ void bmp085_run(void){
bmp.pressure = bmp085_calcP(bmp.up,3);
bmp.altitude = bmp085_calcAlt();
counter++;
bmp085_requestUT(); //start the next cycle
procflag = 0;
bmp085_requestUT(); //start over
procflag = 0; //reset state machine
break;
}
}

/**
/**
* @fn void bmp085_init()
* @brief initializes bmp085 GPIO,I2C,exInt, calibrates and starts the state machine
* @param None
* @retval None
*/
void bmp085_init(){
void bmp085_init(){

//initialize pins, I2C, and external interrupt
//initialize timer
TIM2_init();

//initialize pins, I2C, and external interrupt
init_gpio();
init_i2c1();
init_ext_int();

//initialize BMP085 and request a temperature reading (starts state machine)
//initialize BMP085 and request a temperature reading (starts state machine)
bmp085_calibration();
bmp085_requestUT();
}
@@ -574,17 +620,20 @@ void bmp085_run(void){
*/
void bmp085_print(){

float far = bmp085_Conv_to_F(bmp085_getTemperature());

printf("Cel: %.2f C || Far: %.2f F || Pr: %d || Alt: %.2f ft|| Hz: %d\n",
bmp085_getTemperature(),
far,
bmp085_getPressure(),
bmp085_get_altitude_feet(),
counter);
float far = bmp085_Conv_to_F(bmp085_getTemperature());
printf("Cel: %.2f C || Far: %.2f F || Pr: %d || Alt: %.2f ft|| Hz: %d\n",
bmp085_getTemperature(),
far,
bmp085_getPressure(),
bmp085_get_altitude_feet(),
bmp085_getHz());

}




/**
* @fn float bmp085_getTemperature(void)
* @brief returns most recent temperature reading in Celsius.
@@ -617,6 +666,14 @@ float bmp085_get_altitude_meters(void){return bmp.altitude; }
*/
float bmp085_get_altitude_feet(void){ return bmp.altitude*3.28084F; }

/**
* @fn uint32_t bmp085_getHz(void)
* @brief returns the update rate of readings from the sensor in Hz.
* @param None
* @retval uint8_t of update rate in Hz.
*/
uint8_t bmp085_getHz(void){ return bmp.hz; }

/**
* @}
*/
@@ -30,5 +30,7 @@ uint32_t bmp085_getPressure(void);
float bmp085_get_altitude_meters(void);
float bmp085_get_altitude_feet(void);
void bmp085_print(void);
void bmp085_reset_counter(void);
uint8_t bmp085_getHz(void);

#endif
13 main.c
@@ -13,7 +13,7 @@
#include <stdlib.h>
#include "stm32f4xx.h"
//#include "cmsis_os.h" // RTOS object definitions
#include "timer.h" // for millis() functionality / refresh rate tracking
//#include "timer.h" // for millis() functionality / refresh rate tracking
#include "bmp_085.h" // pressure sensor


@@ -38,22 +38,19 @@ int main(void)
//bmp085 stuff
bmp085_init();

//timer + update rate stuff
TIM2_init();
extern uint8_t counter;
uint32_t volatile time = millis();
//timer for printing
uint32_t volatile time = 0;

while (1) {

//collect bmp data
bmp085_run();

//track bmp update rate (in Hz) + print collected bmp data to the screen
//print collected bmp data to the screen every second
if( millis() > (time + 1000)){
bmp085_print();
time = millis();
counter = 0;
}
}

}

@@ -87,6 +87,8 @@ void TIM2_init(void)
TIM_Cmd(TIM2, ENABLE);

}


#endif

/**

0 comments on commit 5c3132a

Please sign in to comment.
You can’t perform that action at this time.