problem 2 for HW 2

Dependencies:   4DGL-uLCD-SE 4180_LSM9DS0_lab LSM9DS0 mbed

Fork of 4180_LSM9DS0_lab by Allen Wild

Committer:
lzzcd001
Date:
Wed Feb 18 14:50:45 2015 +0000
Revision:
6:dbd765c256a2
Parent:
4:a9e3007530a7
problem 2 for HW 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aswild 0:29ab304ca8ce 1 // LSM9DS90/uLCD Demo
aswild 0:29ab304ca8ce 2 // ECE 4180 Lab Code Template
aswild 0:29ab304ca8ce 3
aswild 0:29ab304ca8ce 4 #include "mbed.h"
aswild 0:29ab304ca8ce 5 #include "LSM9DS0.h"
aswild 0:29ab304ca8ce 6 #include "uLCD_4DGL.h"
aswild 0:29ab304ca8ce 7
aswild 4:a9e3007530a7 8 // uncomment this line to enable the uLCD for Part 4 of the lab
aswild 4:a9e3007530a7 9 //#define PART_4
aswild 1:ae1cefe9aa38 10
aswild 0:29ab304ca8ce 11 // SDO_XM and SDO_G are pulled up, so our addresses are:
aswild 0:29ab304ca8ce 12 #define LSM9DS0_XM_ADDR 0x1D // Would be 0x1E if SDO_XM is LOW
aswild 0:29ab304ca8ce 13 #define LSM9DS0_G_ADDR 0x6B // Would be 0x6A if SDO_G is LOW
aswild 0:29ab304ca8ce 14
aswild 4:a9e3007530a7 15 // refresh time. set to 500 for part 2 and 50 for part 4
aswild 1:ae1cefe9aa38 16 #define REFRESH_TIME_MS 500
aswild 0:29ab304ca8ce 17
aswild 0:29ab304ca8ce 18 // Verify that the pin assignments below match your breadboard
aswild 0:29ab304ca8ce 19 LSM9DS0 imu(p9, p10, LSM9DS0_G_ADDR, LSM9DS0_XM_ADDR);
aswild 1:ae1cefe9aa38 20 Serial pc(USBTX, USBRX);
lzzcd001 6:dbd765c256a2 21 Serial device(p13, p14);
aswild 1:ae1cefe9aa38 22
aswild 4:a9e3007530a7 23 #ifdef PART_4
aswild 1:ae1cefe9aa38 24 uLCD_4DGL lcd(p28, p27, p30);X
aswild 1:ae1cefe9aa38 25 #endif
aswild 0:29ab304ca8ce 26
aswild 0:29ab304ca8ce 27 //Init Serial port and LSM9DS0 chip
aswild 0:29ab304ca8ce 28 void setup()
aswild 0:29ab304ca8ce 29 {
aswild 4:a9e3007530a7 30 #ifdef PART_4
aswild 0:29ab304ca8ce 31 lcd.baudrate(3000000);
aswild 0:29ab304ca8ce 32 lcd.background_color(0);
aswild 0:29ab304ca8ce 33 lcd.cls();
aswild 0:29ab304ca8ce 34
aswild 0:29ab304ca8ce 35 lcd.printf("Initializing...");
aswild 1:ae1cefe9aa38 36 #endif
aswild 1:ae1cefe9aa38 37
aswild 0:29ab304ca8ce 38 // Use the begin() function to initialize the LSM9DS0 library.
aswild 0:29ab304ca8ce 39 // You can either call it with no parameters (the easy way):
aswild 0:29ab304ca8ce 40 uint16_t status = imu.begin();
aswild 0:29ab304ca8ce 41
aswild 0:29ab304ca8ce 42 //Make sure communication is working
aswild 0:29ab304ca8ce 43 pc.printf("LSM9DS0 WHO_AM_I's returned: 0x%X\n", status);
aswild 0:29ab304ca8ce 44 pc.printf("Should be 0x49D4\n\n");
aswild 0:29ab304ca8ce 45 }
aswild 0:29ab304ca8ce 46
aswild 0:29ab304ca8ce 47 int main()
aswild 0:29ab304ca8ce 48 {
aswild 0:29ab304ca8ce 49 setup(); //Setup sensor and Serial
aswild 0:29ab304ca8ce 50 pc.printf("------ LSM0DS0 Demo -----------\n");
aswild 0:29ab304ca8ce 51 while (true)
aswild 0:29ab304ca8ce 52 {
aswild 0:29ab304ca8ce 53 // TODO - add code here to read compass or accelerometer data
aswild 1:ae1cefe9aa38 54 // and print it to the USB serial port (part 2) and display it on the uLCD (part 3)
aswild 0:29ab304ca8ce 55
aswild 0:29ab304ca8ce 56 // Compass Trigonometry tip: you can retrieve the compass heading (in degrees) directly from
aswild 0:29ab304ca8ce 57 // the IMU library. Example:
aswild 0:29ab304ca8ce 58 // imu.readMag();
aswild 0:29ab304ca8ce 59 // float heading = imu.calcHeading();
aswild 0:29ab304ca8ce 60 // Remember that x = length*cos(heading) and y = length*sin(heading)
aswild 0:29ab304ca8ce 61 // to convert from degrees to radians (for sin/cos functions), multiply by pi/180
lzzcd001 6:dbd765c256a2 62 imu.readMag();
lzzcd001 6:dbd765c256a2 63 imu.readAccel();
lzzcd001 6:dbd765c256a2 64 imu.readTemp();
lzzcd001 6:dbd765c256a2 65 float heading = imu.calcHeading();
lzzcd001 6:dbd765c256a2 66 //if(device.readable()) {
lzzcd001 6:dbd765c256a2 67 // pc.putc(device.getc());
lzzcd001 6:dbd765c256a2 68 //}
lzzcd001 6:dbd765c256a2 69 //if(pc.readable()) {
lzzcd001 6:dbd765c256a2 70 // device.putc(pc.getc());
lzzcd001 6:dbd765c256a2 71 //}
lzzcd001 6:dbd765c256a2 72 pc.printf("Accelaration_x: %d\n", imu.ax_raw);
lzzcd001 6:dbd765c256a2 73 pc.printf("Accelaration_y: %d\n", imu.ay_raw);
lzzcd001 6:dbd765c256a2 74 pc.printf("Accelaration_z: %d\n", imu.az_raw);
lzzcd001 6:dbd765c256a2 75 pc.printf("Temperature in C: %2.4f\n", imu.temperature_c);
lzzcd001 6:dbd765c256a2 76 pc.printf("Heading: %2.4f\n", heading);
aswild 0:29ab304ca8ce 77 wait_ms(REFRESH_TIME_MS);
aswild 0:29ab304ca8ce 78 }
aswild 0:29ab304ca8ce 79 }