Basic example showing how to use the Compass (LSM303 device with Accelerometer and Magnetometer) present on DISCO_L476VG board.

Dependencies:   BSP_DISCO_L476VG COMPASS_DISCO_L476VG

main.cpp

Committer:
jeromecoutant
Date:
2019-09-24
Revision:
4:875e95338f6d
Parent:
0:211cb2effe6e

File content as of revision 4:875e95338f6d:

#include "mbed.h"
#include "COMPASS_DISCO_L476VG.h"

COMPASS_DISCO_L476VG compass;

DigitalOut led1(LED1);

int main()
{
    int16_t MagBuffer[3];
    int16_t AccBuffer[3];
  
    printf("Compass started (LD5 should blink)\n");
  
    while(1) {
      
        // Read acceleremoter and magnetometer values
        compass.AccGetXYZ(AccBuffer);
        compass.MagGetXYZ(MagBuffer);
        // Display values      
        printf("Acc X = %d\n", AccBuffer[0]);
        printf("Acc Y = %d\n", AccBuffer[1]);
        printf("Acc Z = %d\n", AccBuffer[2]);
        printf("Mag X = %d\n", MagBuffer[0]);
        printf("Mag Y = %d\n", MagBuffer[1]);
        printf("Mag Z = %d\n", MagBuffer[2]);
        printf("\033[6A"); // Moves cursor up x lines (x value is between [ and A)
      
        led1 = !led1;
        ThisThread::sleep_for(1000);
    }
}