
ECE 4180 Team iBox
Fork of 4180_LSM9DS0_lab by
main.cpp@1:ae1cefe9aa38, 2015-01-26 (annotated)
- Committer:
- aswild
- Date:
- Mon Jan 26 06:23:41 2015 +0000
- Revision:
- 1:ae1cefe9aa38
- Parent:
- 0:29ab304ca8ce
- Child:
- 4:a9e3007530a7
updated skeleton code
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:ae1cefe9aa38 | 8 | // uncomment this line to enable the uLCD for Part 3 of the lab |
aswild | 1:ae1cefe9aa38 | 9 | //#define PART_3 |
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 | 1:ae1cefe9aa38 | 15 | // refresh time. set to 500 for part 2 and 50 for part 3 |
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); |
aswild | 1:ae1cefe9aa38 | 21 | |
aswild | 1:ae1cefe9aa38 | 22 | #ifdef PART_3 |
aswild | 1:ae1cefe9aa38 | 23 | uLCD_4DGL lcd(p28, p27, p30);X |
aswild | 1:ae1cefe9aa38 | 24 | #endif |
aswild | 0:29ab304ca8ce | 25 | |
aswild | 0:29ab304ca8ce | 26 | //Init Serial port and LSM9DS0 chip |
aswild | 0:29ab304ca8ce | 27 | void setup() |
aswild | 0:29ab304ca8ce | 28 | { |
aswild | 1:ae1cefe9aa38 | 29 | #ifdef PART_3 |
aswild | 0:29ab304ca8ce | 30 | lcd.baudrate(3000000); |
aswild | 0:29ab304ca8ce | 31 | lcd.background_color(0); |
aswild | 0:29ab304ca8ce | 32 | lcd.cls(); |
aswild | 0:29ab304ca8ce | 33 | |
aswild | 0:29ab304ca8ce | 34 | lcd.printf("Initializing..."); |
aswild | 1:ae1cefe9aa38 | 35 | #endif |
aswild | 1:ae1cefe9aa38 | 36 | |
aswild | 0:29ab304ca8ce | 37 | // Use the begin() function to initialize the LSM9DS0 library. |
aswild | 0:29ab304ca8ce | 38 | // You can either call it with no parameters (the easy way): |
aswild | 0:29ab304ca8ce | 39 | uint16_t status = imu.begin(); |
aswild | 0:29ab304ca8ce | 40 | |
aswild | 0:29ab304ca8ce | 41 | //Make sure communication is working |
aswild | 0:29ab304ca8ce | 42 | pc.printf("LSM9DS0 WHO_AM_I's returned: 0x%X\n", status); |
aswild | 0:29ab304ca8ce | 43 | pc.printf("Should be 0x49D4\n\n"); |
aswild | 0:29ab304ca8ce | 44 | } |
aswild | 0:29ab304ca8ce | 45 | |
aswild | 0:29ab304ca8ce | 46 | int main() |
aswild | 0:29ab304ca8ce | 47 | { |
aswild | 0:29ab304ca8ce | 48 | setup(); //Setup sensor and Serial |
aswild | 0:29ab304ca8ce | 49 | pc.printf("------ LSM0DS0 Demo -----------\n"); |
aswild | 0:29ab304ca8ce | 50 | |
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 |
aswild | 0:29ab304ca8ce | 62 | |
aswild | 0:29ab304ca8ce | 63 | wait_ms(REFRESH_TIME_MS); |
aswild | 0:29ab304ca8ce | 64 | } |
aswild | 0:29ab304ca8ce | 65 | } |