A quick and dirty demo of the Xadow M0 acceleromoeter values displayed on the Xadow OLED 0.96" (using the SSD1308 128x64 OLED Driver with I2C interface library).

Dependencies:   mbed SSD1308_128x64_I2C_opt XadowGPS BMP180 ADXL345_I2C MPU9250 USBDevice

Committer:
ruevs
Date:
Tue Jan 05 13:25:28 2016 +0000
Revision:
2:7abdbccdf0c8
Parent:
0:b017c907d53d
Child:
5:50051611b5bd
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ruevs 2:7abdbccdf0c8 1 #define __STDC_LIMIT_MACROS 1
ruevs 2:7abdbccdf0c8 2 #include <stdint.h>
seeed 0:b017c907d53d 3
ruevs 2:7abdbccdf0c8 4 #include "ADXL345_I2C.h"
ruevs 2:7abdbccdf0c8 5 #include "SSD1308.h"
ruevs 2:7abdbccdf0c8 6
ruevs 2:7abdbccdf0c8 7 //#define DEBUG
seeed 0:b017c907d53d 8
seeed 0:b017c907d53d 9 #ifdef DEBUG
seeed 0:b017c907d53d 10 #include "USBSerial.h" // To use USB virtual serial, a driver is needed, check http://mbed.org/handbook/USBSerial
seeed 0:b017c907d53d 11 #define LOG(args...) pc.printf(args)
seeed 0:b017c907d53d 12 USBSerial pc;
seeed 0:b017c907d53d 13 #else
seeed 0:b017c907d53d 14 #define LOG(args...)
seeed 0:b017c907d53d 15 #endif
seeed 0:b017c907d53d 16
ruevs 2:7abdbccdf0c8 17 #define MAX_ACC_AXIS 3
seeed 0:b017c907d53d 18 ADXL345_I2C accelerometer(P0_5, P0_4);
seeed 0:b017c907d53d 19
ruevs 2:7abdbccdf0c8 20 // Xhadow - OLED 128x64 is connected with I2C
ruevs 2:7abdbccdf0c8 21 I2C i2c(P0_5, P0_4); // SDA, SCL
ruevs 2:7abdbccdf0c8 22 SSD1308 oled = SSD1308(i2c, SSD1308_SA0);
ruevs 2:7abdbccdf0c8 23
ruevs 2:7abdbccdf0c8 24 AnalogIn AD00(P0_11);
ruevs 2:7abdbccdf0c8 25 AnalogIn AD01(P0_12);
ruevs 2:7abdbccdf0c8 26 AnalogIn ChargerStatus(P0_13); // ADC input from VCC through a 200K/200K divider with extara 100K pull down on !DONE and 49.9K on !CHRG
ruevs 2:7abdbccdf0c8 27
ruevs 2:7abdbccdf0c8 28 DigitalOut blue_led(P0_20, 0);
ruevs 2:7abdbccdf0c8 29 DigitalOut white_led(P0_23, 1);
ruevs 2:7abdbccdf0c8 30 DigitalOut Bus3V3En(P0_14, 1); // Pin that controls the BUS-3V3 (3.3V) regulator output to the Xadow Bus
ruevs 2:7abdbccdf0c8 31
ruevs 2:7abdbccdf0c8 32 InterruptIn test_int(P0_7);
ruevs 2:7abdbccdf0c8 33
ruevs 2:7abdbccdf0c8 34 void test_pin_int(void)
ruevs 2:7abdbccdf0c8 35 {
ruevs 2:7abdbccdf0c8 36 white_led = !white_led;
ruevs 2:7abdbccdf0c8 37 Bus3V3En = !Bus3V3En;
ruevs 2:7abdbccdf0c8 38 }
ruevs 2:7abdbccdf0c8 39
seeed 0:b017c907d53d 40 int main()
seeed 0:b017c907d53d 41 {
ruevs 2:7abdbccdf0c8 42 int readings[MAX_ACC_AXIS] = {0, 0, 0};
ruevs 2:7abdbccdf0c8 43 int MAXreadings[MAX_ACC_AXIS] = {INT16_MIN, INT16_MIN, INT16_MIN};
ruevs 2:7abdbccdf0c8 44 int MINreadings[MAX_ACC_AXIS] = {INT16_MAX, INT16_MAX, INT16_MAX};
ruevs 2:7abdbccdf0c8 45
ruevs 2:7abdbccdf0c8 46 char display_buf[17];
ruevs 2:7abdbccdf0c8 47
ruevs 2:7abdbccdf0c8 48 test_int.rise(test_pin_int);
seeed 0:b017c907d53d 49
seeed 0:b017c907d53d 50 LOG("Starting ADXL345 test...\n");
seeed 0:b017c907d53d 51 LOG("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
ruevs 2:7abdbccdf0c8 52
ruevs 2:7abdbccdf0c8 53 oled.writeString(0, 0, "Accelerometer:");
ruevs 2:7abdbccdf0c8 54 oled.writeString(1, 0, " Curr Min Max");
seeed 0:b017c907d53d 55
seeed 0:b017c907d53d 56 //Go into standby mode to configure the device.
seeed 0:b017c907d53d 57 accelerometer.setPowerControl(0x00);
seeed 0:b017c907d53d 58
seeed 0:b017c907d53d 59 //Full resolution, +/-16g, 4mg/LSB.
seeed 0:b017c907d53d 60 accelerometer.setDataFormatControl(0x0B);
seeed 0:b017c907d53d 61
seeed 0:b017c907d53d 62 //3.2kHz data rate.
seeed 0:b017c907d53d 63 accelerometer.setDataRate(ADXL345_3200HZ);
seeed 0:b017c907d53d 64
seeed 0:b017c907d53d 65 //Measurement mode.
seeed 0:b017c907d53d 66 accelerometer.setPowerControl(0x08);
seeed 0:b017c907d53d 67
seeed 0:b017c907d53d 68 while (1) {
seeed 0:b017c907d53d 69 accelerometer.getOutput(readings);
ruevs 2:7abdbccdf0c8 70
ruevs 2:7abdbccdf0c8 71 for (int i=0; i<MAX_ACC_AXIS; ++i) {
ruevs 2:7abdbccdf0c8 72 if ((int16_t)readings[i] > (int16_t)MAXreadings[i]) {
ruevs 2:7abdbccdf0c8 73 MAXreadings[i] = readings[i];
ruevs 2:7abdbccdf0c8 74 }
ruevs 2:7abdbccdf0c8 75 if ((int16_t)readings[i] < (int16_t)MINreadings[i]) {
ruevs 2:7abdbccdf0c8 76 MINreadings[i] = readings[i];
ruevs 2:7abdbccdf0c8 77 }
ruevs 2:7abdbccdf0c8 78
ruevs 2:7abdbccdf0c8 79 snprintf(display_buf, sizeof(display_buf), "%c%5i%5i%5i",
ruevs 2:7abdbccdf0c8 80 'X'+i, (int16_t)readings[i], (int16_t)MINreadings[i], (int16_t)MAXreadings[i] );
ruevs 2:7abdbccdf0c8 81 oled.writeString(2+i, 0, display_buf);
ruevs 2:7abdbccdf0c8 82 LOG("%c:%i|%i|%i\r\n", 'X'+i, (int16_t)readings[i], (int16_t)MINreadings[i], (int16_t)MAXreadings[i] );
ruevs 2:7abdbccdf0c8 83 }
ruevs 2:7abdbccdf0c8 84
ruevs 2:7abdbccdf0c8 85 /* snprintf(display_buf, sizeof(display_buf), "Ch: %u", ChargerStatus.read_u16());
ruevs 2:7abdbccdf0c8 86 oled.writeString(2+MAX_ACC_AXIS, 0, display_buf);
ruevs 2:7abdbccdf0c8 87
ruevs 2:7abdbccdf0c8 88 snprintf(display_buf, sizeof(display_buf), "Ch: %f", 3.3*(float)ChargerStatus.read_u16()/UINT16_MAX);
ruevs 2:7abdbccdf0c8 89 oled.writeString(2+MAX_ACC_AXIS+1, 0, display_buf);
ruevs 2:7abdbccdf0c8 90 */
ruevs 2:7abdbccdf0c8 91 snprintf(display_buf, sizeof(display_buf), "Bat:%.2fV", 3.3*ChargerStatus);
ruevs 2:7abdbccdf0c8 92 oled.writeString(2+MAX_ACC_AXIS+2, 0, display_buf);
ruevs 2:7abdbccdf0c8 93
ruevs 2:7abdbccdf0c8 94 LOG("Ch: %f\r\n", ChargerStatus.read());
ruevs 2:7abdbccdf0c8 95 LOG("Chu: %u\r\n", ChargerStatus.read_u16());
ruevs 2:7abdbccdf0c8 96 LOG("A0: %f\r\n", (float)AD00.read());
ruevs 2:7abdbccdf0c8 97 LOG("A1: %f\r\n", AD01.read());
ruevs 2:7abdbccdf0c8 98
ruevs 2:7abdbccdf0c8 99 blue_led = !blue_led;
ruevs 2:7abdbccdf0c8 100 // white_led = !white_led; // toggled by pin P0_7 interrupt
ruevs 2:7abdbccdf0c8 101
ruevs 2:7abdbccdf0c8 102 // deepsleep(); // Deep sleep until external interrupt // The wakeup pin on Xadow is on the sme buton at RESET - no good.
seeed 0:b017c907d53d 103
seeed 0:b017c907d53d 104 wait(1);
seeed 0:b017c907d53d 105 }
seeed 0:b017c907d53d 106
seeed 0:b017c907d53d 107 }