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.
Dependents: IMOB_without_encryption_txpowermax
AccelSensor.cpp
00001 #include "mbed.h" 00002 #include "AccelSensor.h" 00003 00004 AccelSensor::AccelSensor(PinName sda, PinName scl) : _i2c(sda, scl) { 00005 //No need to initialise anything else. 00006 } 00007 00008 void AccelSensor::init() { 00009 char c = readRegister(WHO_AM_I); // Read WHO_AM_I register 00010 standby(); // Must be in standby to change registers 00011 char scale = GSCALE; // Set up the full scale range to 2, 4, or 8g. 00012 if (scale > 8) scale = 8; //Easy error check 00013 scale >>= 2; // Neat trick, see page 22. 00 = 2G, 01 = 4G, 10 = 8G 00014 writeRegister(XYZ_DATA_CFG, scale); 00015 active(); // Set to active to start reading 00016 } 00017 00018 void AccelSensor::standby() { 00019 char c = readRegister(CTRL_REG1); 00020 writeRegister(CTRL_REG1, c & ~(0x01)); //Clear the active bit to go into standby 00021 } 00022 00023 void AccelSensor::active() { 00024 char c = readRegister(CTRL_REG1); 00025 writeRegister(CTRL_REG1, c | 0x01); //Set the active bit to begin detection 00026 } 00027 00028 void AccelSensor::readData(int *destination) { 00029 char rawData[6]; // x/y/z accel register data stored here 00030 readRegisters(OUT_X_MSB, 6, rawData); // Read the six raw data registers into data array 00031 // Loop to calculate 12-bit ADC and g value for each axis 00032 for(int i = 0; i < 3 ; i++) { 00033 int value = (rawData[i*2] << 8) | rawData[(i*2)+1]; //Combine the two 8 bit registers into one 12-bit number 00034 value >>= 4; //The registers are left align, here we right align the 12-bit integer 00035 // If the number is negative, we have to make it so manually (no 12-bit data type) 00036 if (rawData[i*2] > 0x7F) { 00037 value |= 0xFFFFF << 12; 00038 } 00039 destination[i] = value; 00040 } 00041 } 00042 00043 void AccelSensor::readRegisters(char reg, int range, char* dest) { 00044 int ack = 0; 00045 _i2c.start(); 00046 ack = _i2c.write((ADDRESS << 1)); 00047 ack = _i2c.write(reg); 00048 _i2c.start(); 00049 ack = _i2c.write((ADDRESS << 1) | 0x01); 00050 for (int i = 0; i < range - 1; i++) dest[i] = _i2c.read(1); 00051 dest[range - 1] = _i2c.read(0); 00052 _i2c.stop(); 00053 } 00054 00055 char AccelSensor::readRegister(char reg) { 00056 int ack = 0; 00057 _i2c.start(); 00058 ack = _i2c.write((ADDRESS << 1)); 00059 ack = _i2c.write(reg); 00060 _i2c.start(); 00061 ack = _i2c.write((ADDRESS << 1) | 0x01); 00062 char result = _i2c.read(0); 00063 _i2c.stop(); 00064 return result; 00065 } 00066 00067 void AccelSensor::writeRegister(char reg, char data) { 00068 int ack = 0; 00069 _i2c.start(); 00070 ack = _i2c.write((ADDRESS << 1)); 00071 ack = _i2c.write(reg); 00072 ack = _i2c.write(data); 00073 _i2c.stop(); 00074 }
Generated on Thu Aug 11 2022 02:21:06 by
1.7.2