Example of temperature limit detection for STTS751 in X-NUCLEO-IKS01A3
Dependencies: X_NUCLEO_IKS01A3
Temperature Limit Demo Application with STTS751 based on sensor expansion board X-NUCLEO-IKS01A3
Main function is to show how to detect exceeding of temperature limits using the sensor expansion board and send a notification using UART to a connected PC or Desktop and display it on terminal applications like TeraTerm.
After connection has been established:
- the user can heat up or cool down the board and view the data using an hyper terminal.
Diff: main.cpp
- Revision:
- 5:b747a8948606
- Parent:
- 3:3c8bce3cdf2f
- Child:
- 6:acde79c5a18a
diff -r f25a26f79c62 -r b747a8948606 main.cpp --- a/main.cpp Mon Jun 03 09:48:11 2019 +0000 +++ b/main.cpp Wed Jul 24 14:56:10 2019 +0000 @@ -4,7 +4,7 @@ * @author SRA * @version V1.0.0 * @date 5-March-2019 - * @brief Simple Example application for using the X_NUCLEO_IKS01A3 + * @brief Simple Example application for using the X_NUCLEO_IKS01A3 * MEMS Inertial & Environmental Sensor Nucleo expansion board. ****************************************************************************** * @attention @@ -34,20 +34,20 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** -*/ - +*/ + /* Includes */ #include "mbed.h" #include "XNucleoIKS01A3.h" - + /* Instantiate the expansion board */ static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4); - + /* Retrieve the composing elements of the expansion board */ static STTS751Sensor *t_sensor = mems_expansion_board->t_sensor; DigitalOut myled(LED1); - + volatile int mems_event = 0; uint32_t previous_tick = 0; uint32_t current_tick = 0; @@ -58,104 +58,106 @@ void INT_cb(); /* Helper function for printing floats & doubles */ -static char *print_double(char* str, double v, int decimalDigits=2) +static char *print_double(char *str, double v, int decimalDigits = 2) { - int i = 1; - int intPart, fractPart; - int len; - char *ptr; + int i = 1; + int intPart, fractPart; + int len; + char *ptr; - /* prepare decimal digits multiplicator */ - for (;decimalDigits!=0; i*=10, decimalDigits--); + /* prepare decimal digits multiplicator */ + for (; decimalDigits != 0; i *= 10, decimalDigits--); - /* calculate integer & fractinal parts */ - intPart = (int)v; - fractPart = (int)((v-(double)(int)v)*i); + /* calculate integer & fractinal parts */ + intPart = (int)v; + fractPart = (int)((v - (double)(int)v) * i); - /* fill in integer part */ - sprintf(str, "%i.", intPart); + /* fill in integer part */ + sprintf(str, "%i.", intPart); - /* prepare fill in of fractional part */ - len = strlen(str); - ptr = &str[len]; + /* prepare fill in of fractional part */ + len = strlen(str); + ptr = &str[len]; - /* fill in leading fractional zeros */ - for (i/=10;i>1; i/=10, ptr++) { - if (fractPart >= i) { - break; + /* fill in leading fractional zeros */ + for (i /= 10; i > 1; i /= 10, ptr++) { + if (fractPart >= i) { + break; + } + *ptr = '0'; } - *ptr = '0'; - } - /* fill in (rest of) fractional part */ - sprintf(ptr, "%i", fractPart); + /* fill in (rest of) fractional part */ + sprintf(ptr, "%i", fractPart); - return str; + return str; } /* Simple main function */ -int main() { - /* Attach callback to STTS751 INT */ - t_sensor->attach_int_irq(&INT_cb); - - /* Enable STTS751 temperature sensor */ - t_sensor->enable(); - /* Set ODR to 4Hz */ - t_sensor->set_odr(4.0f); - /* Set Low Temperature Threshold */ - t_sensor->set_low_temp_thr(22.0f); - /* Set High Temperature Threshold */ - t_sensor->set_high_temp_thr(28.0f); - /* Enable Event pin */ - t_sensor->set_event_pin(1); - /* Get beginning status */ - t_sensor->get_temp_limit_status(NULL, NULL, NULL); +int main() +{ + /* Attach callback to STTS751 INT */ + t_sensor->attach_int_irq(&INT_cb); + + /* Enable STTS751 temperature sensor */ + t_sensor->enable(); + /* Set ODR to 4Hz */ + t_sensor->set_odr(4.0f); + /* Set Low Temperature Threshold */ + t_sensor->set_low_temp_thr(22.0f); + /* Set High Temperature Threshold */ + t_sensor->set_high_temp_thr(28.0f); + /* Enable Event pin */ + t_sensor->set_event_pin(1); + /* Get beginning status */ + t_sensor->get_temp_limit_status(NULL, NULL, NULL); + + previous_tick = clock(); + + printf("\r\n--- Starting new run ---\r\n"); - previous_tick = clock(); - - printf("\r\n--- Starting new run ---\r\n"); - - while(1) { - if (mems_event) { - mems_event=0; - uint8_t high_temp = 0, low_temp = 0; - t_sensor->get_temp_limit_status(&high_temp, &low_temp, NULL); - if (high_temp){ - high = 1; - low = 0; - } - if (low_temp){ - low = 1; - high = 0; - } - - t_sensor->get_temperature(&temperature); - myled = 1; - wait(0.1); - myled = 0; - } + while (1) { + if (mems_event) { + mems_event = 0; + uint8_t high_temp = 0, low_temp = 0; + t_sensor->get_temp_limit_status(&high_temp, &low_temp, NULL); + if (high_temp) { + high = 1; + low = 0; + } + if (low_temp) { + low = 1; + high = 0; + } + + t_sensor->get_temperature(&temperature); + myled = 1; + wait(0.1); + myled = 0; + } - current_tick = clock(); - if (((current_tick - previous_tick)/CLOCKS_PER_SEC) >= 2){ - if (!high && !low){ - t_sensor->get_temperature(&temperature); - } - printf("Temp[C]: "); - printf("%7s C", print_double(buffer, temperature)); - if (high){ - printf(" High temperature detected!(>28C) \r\n"); - high = 0; - } else if (low) { - printf(" Low temperature detected!(<22C) \r\n"); - low = 0; - } else { - printf("\r\n"); - } - previous_tick = clock(); + current_tick = clock(); + if (((current_tick - previous_tick) / CLOCKS_PER_SEC) >= 2) { + if (!high && !low) { + t_sensor->get_temperature(&temperature); + } + printf("Temp[C]: "); + printf("%7s C", print_double(buffer, temperature)); + if (high) { + printf(" High temperature detected!(>28C) \r\n"); + high = 0; + } else if (low) { + printf(" Low temperature detected!(<22C) \r\n"); + low = 0; + } else { + printf("\r\n"); + } + previous_tick = clock(); + } } - } } - -void INT_cb() { - mems_event = 1; + +void INT_cb() +{ + mems_event = 1; }