Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of tilt_angles by
ENGO333_MMA7660.cpp
00001 #include "ENGO333_MMA7660.h" 00002 00003 const float GRAVITY = 9.8086; 00004 00005 ENGO333_MMA7660::ENGO333_MMA7660() : i2c(p28, p27) 00006 { 00007 SetActiveMode(); 00008 SetSamplingRateAM(MMA7660_AMSR8); 00009 measAccel[0] = measAccel[1] = measAccel[2] = 0; 00010 } 00011 00012 bool ENGO333_MMA7660::TestConnection() 00013 { 00014 char data[3] = {128, 128, 128}; 00015 bool alert; 00016 00017 // Check data validity 00018 alert = false; 00019 i2c.readBytes(MMA7660_ADDRESS, MMA7660_XOUT_REG, data, 3); 00020 for (int i = 0; i < 3; ++i) { 00021 if (data[i] > 63) 00022 alert = true; 00023 } 00024 00025 if (alert == false) { 00026 return true; 00027 } else { 00028 return false; 00029 } 00030 } 00031 00032 void ENGO333_MMA7660::SetActiveMode() 00033 { 00034 i2c.writeOneByte(MMA7660_ADDRESS, MMA7660_MODE_REG, 0x01); 00035 } 00036 00037 void ENGO333_MMA7660::SetSamplingRateAM(MMA7660_AMSR_t samplingRate) 00038 { 00039 char oldValue, newValue; 00040 oldValue = i2c.readOneByte(MMA7660_ADDRESS, MMA7660_SR_REG); 00041 oldValue = oldValue & 0x07; 00042 newValue = oldValue | samplingRate; 00043 i2c.writeOneByte(MMA7660_ADDRESS, MMA7660_SR_REG, newValue); 00044 } 00045 00046 void ENGO333_MMA7660::ReadAccelerometers() 00047 { 00048 char data[3]; 00049 bool alert; 00050 00051 // Check data validity 00052 do { 00053 alert = false; 00054 i2c.readBytes(MMA7660_ADDRESS, MMA7660_XOUT_REG, data, 3); 00055 for (int i = 0; i < 3; ++i) { 00056 if (data[i] > 63) 00057 alert = true; 00058 if (data[i] > 31) 00059 data[i] += 128 + 64; 00060 } 00061 } while (alert); 00062 00063 measAccel[0] = (signed char)(data[0]) / MMA7660_SCALE * GRAVITY; 00064 measAccel[1] = (signed char)(data[1]) / MMA7660_SCALE * GRAVITY; 00065 measAccel[2] = (signed char)(data[2]) / MMA7660_SCALE * GRAVITY; 00066 } 00067 00068 float ENGO333_MMA7660::GetAccelX() const 00069 { 00070 return measAccel[0]; 00071 } 00072 00073 float ENGO333_MMA7660::GetAccelY() const 00074 { 00075 return measAccel[1]; 00076 } 00077 00078 float ENGO333_MMA7660::GetAccelZ() const 00079 { 00080 return measAccel[2]; 00081 } 00082
Generated on Sat Jul 16 2022 15:16:04 by
1.7.2
