problem 3 for HW 2

Committer:
lzzcd001
Date:
Wed Feb 18 14:51:12 2015 +0000
Revision:
0:b6366ac1f5b1
`problem 3 for HW 2`

### Who changed what in which revision?

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