Sample program for interfacing with PNI's RM3100 Breakout Board

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002 * @file        main.cpp
00003 *
00004 * @brief       Sample code for RM3100.
00005 * @authors     Betty Zhang, Daniel Delsuc
00006 * @date        03/03/2017
00007 * @copyright   (C) 2017 PNI Corp
00008 *
00009 * @copyright   This sample code is provided "as is" without express or implied warranty.
00010 *
00011 */
00012 
00013 // Note: This program assumes the RM3100 is connected using the I2C Interface
00014 
00015 #include "main.h"
00016 
00017 // Check if DRDY was triggered
00018 bool DataReady()
00019 {
00020     if (DRDY_PIN == 1)
00021         return true;
00022     else
00023         return false;
00024 }
00025 
00026 int main()
00027 {
00028     pc.baud(115200);
00029     int dataInCount[3];
00030     float x, y, z;
00031 
00032     mag_initialize_sensor();
00033 
00034     mag_set_sample_rate(100); //100Hz
00035 
00036     mag_set_power_mode(SensorPowerModeActive);
00037 
00038     //while (!DataReady()); //wait here, if using DRDY Int
00039     //If not using DRDY, user can check status Reigster 0x34 for dataready.
00040 
00041     //From User Manual Table 3-1 : Geomagnetic Sensor Performance
00042     //50 CycleCount  ~ 20 LSB/uT
00043     //100 CycleCount ~ 38 LSB/uT
00044     //200 CycleCount ~ 75 LSB/uT
00045     //Linear equation: gain (LSB/uT) = (0.3671 * CycleCount + 1.5)
00046 
00047     int CycleCount = CCP0 | (CCP1 << 8);
00048     float gain = 0.3671 * CycleCount + 1.5;
00049 
00050     while(1)
00051     {
00052         //Get sample data in counts
00053         mag_get_sample_data((int*)&dataInCount);
00054         
00055         while (!DataReady()); //wait here, if using DRDY Int
00056     
00057         //Convert to uT
00058         x = (float)dataInCount[0] / gain;
00059         y = (float)dataInCount[1] / gain;
00060         z = (float)dataInCount[2] / gain;
00061         
00062         pc.printf("X: %f, Y: %f, Z: %f\n", x, y, z);
00063         //wait(1);
00064     }
00065     return 0;
00066 }