/** \brief Pressure demo application entry * * After initializing sensor platform board resources, this demonstration will * attach and initialize a barometric sensor installed on the development board. * In the case of the Atmel Xplained development boards, for example, the * platform should be fitted and built for a Sensors Xplained Pressure sensor * board. */ int main(void) { /* Initialize the board (Xplained UC3 or XMEGA & Xplained Sensor boards) * I/O pin mappings and any other configurable resources selected in * the build configuration. */ sensor_platform_init(); /* Attach a descriptor to the existing sensor device. */ sensor_t barometer; sensor_attach(&barometer, SENSOR_TYPE_BAROMETER, 0, 0); if (barometer.err) { puts("\rSensor initialization error."); while (true) { /* Error occurred, loop forever */ } } /* Set the barometer sample mode & altimeter reference values. */ sensor_set_state(&barometer, SENSOR_STATE_LOWEST_POWER); pressure_sea_level(MSL_PRESSURE); if (PRINT_BANNER) { uint32_t id; uint8_t ver; sensor_device_id(&barometer, &id, &ver); printf( "%s\r\nID = 0x%02x ver. 0x%02x\r\n %d-bit Resolution\r\n", barometer.drv->caps.name, (unsigned)id, (unsigned)ver, barometer.hal->resolution); } while (true) { static float P_old = 0; const float P_new = average(barometric_pressure, &barometer); if (fabs(P_new - P_old) > meters_to_pascals(0.5)) { printf("P = %.2f hPa, altimeter: %.1f m\r", (P_new / 100), pressure_altitude(P_new)); } P_old = P_new; } }
int main(void) { /* Initialize the board. * The board-specific conf_board.h file contains the configuration of * the board initialization. */ board_init(); pmic_init(); sysclk_init(); sensor_platform_init(); rtc_init(); PORTE.DIRSET = 0x01; // Init the RTC CLK.RTCCTRL = 0x05; // while ( !( OSC_STATUS & OSC_RC32KRDY_bm ) ); /* Wait for the int. 32kHz oscillator to stabilize. */ PMIC_CTRL |= 0x01; // Set Int. priority level to low in PMIC while( ( RTC_STATUS & 0x01 ) ); // Needed B 4 writing to RTC PER / CNT registers RTC.PER = 0x0400; RTC.CTRL = 0x01; RTC.INTCTRL = 0x01; //Set this to match the interrupt level in PMIC_CTRL sensor_attach(&barometer, SENSOR_TYPE_BAROMETER, 0, 0); sensor_set_state(&barometer, SENSOR_STATE_HIGHEST_POWER); press_data.scaled = true; temp_data.scaled = true; // USART options. static usart_rs232_options_t USART_SERIAL_OPTIONS = { .baudrate = USART_SERIAL_EXAMPLE_BAUDRATE, .charlength = USART_SERIAL_CHAR_LENGTH, .paritytype = USART_SERIAL_PARITY, .stopbits = USART_SERIAL_STOP_BIT }; // Initialize usart driver in RS232 mode usart_init_rs232(USART_SERIAL_EXAMPLE, &USART_SERIAL_OPTIONS); // Send "message header" sendUARTdata(tx_buf, 22); // sysclk_rtcsrc_enable(SYSCLK_SRC_RC2MHZ); // rtc_init(); if (barometer.err) { sendUARTdata(press_err, 39); } else { memset(tx_buf2, 0, 128); sensor_device_id(&barometer, &id, &ver); sprintf((char*)tx_buf2, "%s\r\n\r\nSensor ID: 0x%02x ver: 0x%02x\r\n%d bit resolution\r\n\r\n", barometer.drv->caps.name, (unsigned)id, (unsigned)ver, barometer.hal->resolution); sendUARTdata(tx_buf2, sizeof(tx_buf2)); } sei(); while (true) { } }
/** \brief Inertial sensor demo application entry * * After initializing the Xplained platform and sensor boards, this application * attaches descriptors to the accelerometer and gyroscope devices on * an Xplained inertial sensor board. The sensor data, which is formatted and * printed via printf() after being read, can be viewed with a serial terminal * application on a machine attached to the USB interface on the Xplained * board. */ int main(void) { /* Initialize the Xplained (UC3 or XMEGA) platform & sensor boards. */ sensor_platform_init(); LED_On(ACTIVITY_LED); /* Initialize the MCU sleep manager API and specify a sleep mode. */ sleepmgr_init(); sleepmgr_lock_mode(SLEEP_MODE); #if (USE_ACCEL == true) /* Attach accelerometer */ sensor_attach(&accelerometer, SENSOR_TYPE_ACCELEROMETER, 0, 0); if (accelerometer.err) { puts("\r\nAccelerometer initialization error."); while (true) { /* Error occurred, loop forever */ } } /* Enable motion event */ sensor_set_threshold(&accelerometer, SENSOR_THRESHOLD_MOTION, ACCEL_MOT_THRESH); sensor_add_event(&accelerometer, SENSOR_EVENT_MOTION, acceleration_event, 0, true); /* Put the accelerometer into a low-power mode (if available) */ sensor_set_state(&accelerometer, SENSOR_STATE_LOW_POWER); #endif #if (USE_GYRO == true) /* Attach gyroscope */ sensor_attach(&gyroscope, SENSOR_TYPE_GYROSCOPE, 0, 0); if (gyroscope.err) { puts("\r\nGyroscope initialization error."); while (true) { /* Error occurred, loop forever */ } } sensor_set_sample_rate(&gyroscope, GYRO_SAMPLE_RATE); # if (GYRO_WAKE == true) /* Enable gyroscope new data event for wakeup */ sensor_add_event(&gyroscope, SENSOR_EVENT_NEW_DATA, rotation_event, 0, true); # elif (GYRO_SLEEP == true) /* Put gyro in low-power sleep mode until accelerometer wakes system up */ sensor_set_state(&gyroscope, SENSOR_STATE_SLEEP); # endif #endif while (true) { LED_Off(ACTIVITY_LED);/* turn off while asleep */ /* Put device in low power sleep mode until woken up by an interrupt */ sleepmgr_enter_sleep(); /* enter specified sleep mode */ /* Device has woken up */ LED_On(ACTIVITY_LED); /* turn on while awake */ #if ((USE_GYRO == true) && (GYRO_WAKE == false)) # if (GYRO_SLEEP == true) /* Wake up gyroscope, wait for device to settle */ sensor_set_state(&gyroscope, SENSOR_STATE_NORMAL); delay_ms(GYRO_RESTART_DELAY); /* Read gyroscope and put it back to sleep */ sensor_get_rotation(&gyroscope, &rotation); sensor_set_state(&gyroscope, SENSOR_STATE_SLEEP); # else /* Read gyro in response to accelerometer wake */ sensor_get_rotation(&gyroscope, &rotation); /* read gyro now */ # endif #endif #if (USE_PRINTF == true) const char *const format = SCALED_DATA ? "acc = [%5d, %5d, %5d] rot = [%5d, %5d, %5d]\r\n" : "acc = [%.5x, %.5x, %.5x] rot = [%.5x, %.5x, %.5x]\r\n"; /* Print accelerometer & gyroscope values */ printf(format, /* data from motion event handler */ (int16_t)acceleration.axis.x, (int16_t)acceleration.axis.y, (int16_t)acceleration.axis.z, /* data from sensor_get_rotation() (if GYRO_WAKE==false), or * from the gyro new data event handler */ (int16_t)rotation.axis.x, (int16_t)rotation.axis.y, (int16_t)rotation.axis.z ); #endif /* Minimum active time is 100 msec */ delay_ms(100); } return 0; }
/** * @brief Initiate a sensor device software reset. * * @param sensor The address of an initialized sensor descriptor. * @return bool true if the call succeeds, else false is returned. */ bool sensor_reset(sensor_t *sensor, int arg) { return sensor_set_state(sensor, SENSOR_STATE_RESET); }
/** * @brief Set a sensor device to low-power or standby mode. * * @param sensor The address of an initialized sensor descriptor. * @return bool true if the call succeeds, else false is returned. */ bool sensor_sleep(sensor_t *sensor, int arg) { return sensor_set_state(sensor, SENSOR_STATE_SLEEP); }
/** * \brief Example application entry routine */ int main(void) { /* The sensor_platform_init() function will initialize the system * clock and sensor bus support in addition to configuring the * XMEGA-A3BU and Sensor Xplained boards. * * Use gfx_mono_init() to initialize the monochrome graphical system * API then write a splash screen after enabling the LCD display * backlight and setting the contrast. * * The MCU is going to be put in a sleep mode, so initialize the * sleep manager API with a call to the sleepmgr_init() routine. */ sensor_platform_init(); gfx_mono_init(); sleepmgr_init(); gpio_set_pin_high(NHD_C12832A1Z_BACKLIGHT); st7565r_set_contrast(ST7565R_DISPLAY_CONTRAST_MIN); /* Attach an accelerometer on a Sensors Xplained board. */ sensor_t accelerometer; sensor_attach(&accelerometer, SENSOR_TYPE_ACCELEROMETER, 0, 0); /* Enable the accelerometer low-g (free fall) event. */ sensor_enable_event(&accelerometer, SENSOR_EVENT_LOW_G); /* Set the free fall threshold (low-g event), bandwidth and range. */ sensor_set_threshold(&accelerometer, SENSOR_THRESHOLD_LOW_G, LOW_G_THRESHOLD); sensor_set_bandwidth(&accelerometer, BANDWIDTH); sensor_set_range(&accelerometer, RANGE); while (true) { /* Put the accelerometer into a low-power mode (if available). */ sensor_set_state(&accelerometer, SENSOR_STATE_LOW_POWER); LED_Off(ACCEL_LED); clear_screen(); /* Display the "armed" message and put the MCU in sleep mode. */ gfx_mono_draw_string("ATMEL Drop Demo\r\nXMEGA Powered Down\r\n" "g Sensor Armed", 1, 5, &sysfont); sleepmgr_lock_mode(SLEEP_MODE); sleepmgr_enter_sleep(); /* The following runs after the MCU has been woken by an * external low-g interrupt from the accelerometer. * * Turn on the red LED while falling and put the accelerometer * into a high-power mode (if available) to sample date points. */ LED_On(ACCEL_LED); sensor_set_state(&accelerometer, SENSOR_STATE_HIGHEST_POWER); static scalar_t acceleration_waveform[DATA_SAMPLE_COUNT]; scalar_t acceleration_max = 0; for (int data_count = 0; data_count < DATA_SAMPLE_COUNT; ++data_count) { acceleration_waveform[data_count] = 0; for (int i = 0; i < SAMPLE_AVG_COUNT; ++i) { /* Calculate the gravity vector magnitude. */ vector3_t gvec; sensor_get_vector(&accelerometer, &gvec); scalar_t const acceleration_magnitude = vector3_magnitude(&gvec); /* Store the maximum g magnitude for this * sub-group. */ if (acceleration_magnitude > acceleration_waveform[data_count]) { acceleration_waveform[data_count] = acceleration_magnitude; } /* Store the maximum g magnitude for the whole * data set. */ if (acceleration_magnitude > acceleration_max) { acceleration_max = acceleration_magnitude; } } } clear_screen(); /* Turn the max acceleration into a string and convert to g. */ static char max_g_string[20]; if (acceleration_max > LOW_G_SATURATION) { sprintf(max_g_string, "g Sensor Saturated"); } else { sprintf(max_g_string, "Peak = %02.2f g", acceleration_max / 1000); } /* Print the max g on the monochrome display. */ gfx_mono_draw_string("Drop Detected", 1, 5, &sysfont); gfx_mono_draw_string(max_g_string, 1, 13, &sysfont); gfx_mono_draw_string("Press SW1 for chart", 1, 21, &sysfont); do { LED_Toggle(ACCEL_LED); delay_ms(100); } while (!switch_pressed(SW1)); /* Plot the collected data points to create the waveform chart. */ clear_screen(); screen_border(); for (int data_count = 0; data_count < DATA_SAMPLE_COUNT; ++data_count) { gfx_mono_draw_filled_circle(data_count, 32 - (acceleration_waveform[data_count] / 500), 1, GFX_PIXEL_SET, GFX_WHOLE); } do { LED_Toggle(PROMPT_LED); delay_ms(100); } while (!switch_pressed(SW1)); LED_Off(PROMPT_LED); } }