LSM303DLHC Full Driver: Readings For Acc, Mag and Temp; Class Method for frequently-used 13 sensor parameters; Class Method to direct modify registers; Support Calibration (offset+scale);
Diff: main.cpp
- Revision:
- 0:7864abfabe2f
- Child:
- 3:522d01930e6a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Jun 06 07:40:45 2016 +0000 @@ -0,0 +1,42 @@ +#include "mbed.h" +#include "LSM303DLHC.h" + +LSM303DLHC sensor(p9,p10); +Serial pc(USBTX,USBRX); +Ticker ticker; + +float acc[3] = { 0 }; +float mag[3] = { 0 }; +float tmp[1] = { 0 }; + +void CallBack(); + +int main(){ + pc.baud(921600); + ticker.attach(&CallBack, 0.2); // read and refresh disp with 200ms interval + + sensor.MCtrl(LSM303DLHC::GN1); // set mag range +/-1.3Gausee + sensor.MCtrl(LSM303DLHC::DR3); // set mag date rate 7.5Hz + sensor.ACtrl(LSM303DLHC::G8); // set acc range +/-8g + sensor.ACtrl(LSM303DLHC::ODR2); // set acc date rate 10Hz + sensor.ACtrl(LSM303DLHC::HPF_OFF); // disable internal acc HPF + + float acc_offset[3] = { 6.5, 5, 12.5 }; // acc offset + float acc_scale[3] = { 3.9, 4, 4 }; // acc scale + sensor.AccCal(acc_offset,acc_scale); // set acc calibration + // refer to LSM303DLHC.h for more overloaded details for ACtrl() MCtrl() TCtrl() + + while (1) + wait_us(1); +} + +void CallBack(){ + sensor.GetAcc(acc); + sensor.GetMag(mag); + sensor.GetTemp(tmp); + + pc.printf("A %8.2f %8.2f %8.2f\n\r", acc[0], acc[1], acc[2]); + pc.printf("M %8.2f %8.2f %8.2f\n\r", mag[0], mag[1], mag[2]); + pc.printf("T %8.2f\n\r", tmp[0]); +} + \ No newline at end of file