Skip to content

Commit

Permalink
Very basic whitespace corrections.
Browse files Browse the repository at this point in the history
Replaced many /* */ with //. A few tab and space corrections.
  • Loading branch information
nseidle committed Nov 6, 2012
1 parent 0c07b21 commit 7ecdc22
Showing 1 changed file with 51 additions and 53 deletions.
104 changes: 51 additions & 53 deletions MMA8452Q_Example/MMA8452Q_Example.ino
Expand Up @@ -39,20 +39,20 @@
*/
#include "i2c.h" // not the wire library, can't use pull-ups

// the SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set
// The SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set
#define SA0 1
#if SA0
#define MMA8452_ADDRESS 0x1D // SA0 is high, 0x1C if low
#else
#define MMA8452_ADDRESS 0x1C
#endif

/* Set the scale below either 2, 4 or 8*/
// Set the scale below either 2, 4 or 8
const byte SCALE = 2; // Sets full-scale range to +/-2, 4, or 8g. Used to calc real g values.
/* Set the output data rate below. Value should be between 0 and 7*/
// Set the output data rate below. Value should be between 0 and 7
const byte dataRate = 0; // 0=800Hz, 1=400, 2=200, 3=100, 4=50, 5=12.5, 6=6.25, 7=1.56

/* Pin definitions */
// Pin definitions
int int1Pin = 2; // These can be changed, 2 and 3 are the Arduinos ext int pins
int int2Pin = 3;

Expand All @@ -65,13 +65,13 @@ void setup()

Serial.begin(115200);

/* Set up the interrupt pins, they're set as active high, push-pull */
// Set up the interrupt pins, they're set as active high, push-pull
pinMode(int1Pin, INPUT);
digitalWrite(int1Pin, LOW);
pinMode(int2Pin, INPUT);
digitalWrite(int2Pin, LOW);

/* Read the WHO_AM_I register, this is a good test of communication */
// Read the WHO_AM_I register, this is a good test of communication
c = readRegister(0x0D); // Read WHO_AM_I register
if (c == 0x2A) // WHO_AM_I should always be 0x2A
{
Expand All @@ -90,23 +90,25 @@ void loop()
{
static byte source;

/* If int1 goes high, all data registers have new data */
// If int1 goes high, all data registers have new data
if (digitalRead(int1Pin)==1) // Interrupt pin, should probably attach to interrupt function
{
readAccelData(accelCount); // Read the x/y/z adc values

/* Below we'll print out the ADC values for acceleration
/*
//Below we'll print out the ADC values for acceleration
for (int i=0; i<3; i++)
{
Serial.print(accelCount[i]);
Serial.print("\t\t");
}
Serial.println();*/
Serial.println();
*/

/* Now we'll calculate the accleration value into actual g's */
// Now we'll calculate the accleration value into actual g's
for (int i=0; i<3; i++)
accelG[i] = (float) accelCount[i]/((1<<12)/(2*SCALE)); // get actual g value, this depends on scale being set
/* print out values */
// Print out values
for (int i=0; i<3; i++)
{
Serial.print(accelG[i], 4); // Print g values
Expand All @@ -115,7 +117,7 @@ void loop()
Serial.println();
}

/* If int2 goes high, either p/l has changed or there's been a single/double tap */
// If int2 goes high, either p/l has changed or there's been a single/double tap
if (digitalRead(int2Pin)==1)
{
source = readRegister(0x0C); // Read the interrupt source reg.
Expand All @@ -133,21 +135,21 @@ void readAccelData(int * destination)

readRegisters(0x01, 6, &rawData[0]); // Read the six raw data registers into data array

/* loop to calculate 12-bit ADC and g value for each axis */
// Loop to calculate 12-bit ADC and g value for each axis
for (int i=0; i<6; i+=2)
{
destination[i/2] = ((rawData[i] << 8) | rawData[i+1]) >> 4; // Turn the MSB and LSB into a 12-bit value
if (rawData[i] > 0x7F)
{ // If the number is negative, we have to make it so manually (no 12-bit data type)
{
// If the number is negative, we have to make it so manually (no 12-bit data type)
destination[i/2] = ~destination[i/2] + 1;
destination[i/2] *= -1; // Transform into negative 2's complement #
}
}
}

/* This function will read the status of the tap source register.
And print if there's been a single or double tap, and on what
axis. */
// This function will read the status of the tap source register.
// Print if there's been a single or double tap, and on what axis.
void tapHandler()
{
byte source = readRegister(0x22); // Reads the PULSE_SRC register
Expand Down Expand Up @@ -189,8 +191,8 @@ void tapHandler()
}
}

/* This function will read the p/l source register and
print what direction the sensor is now facing */
// This function will read the p/l source register and
// print what direction the sensor is now facing
void portraitLandscapeHandler()
{
byte pl = readRegister(0x10); // Reads the PL_STATUS register
Expand Down Expand Up @@ -218,34 +220,31 @@ void portraitLandscapeHandler()
Serial.println();
}

/* Initialize the MMA8452 registers
See the many application notes for more info on setting
all of these registers:
http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8452Q
Feel free to modify any values, these are settings that work well for me.
*/
// Initialize the MMA8452 registers
// See the many application notes for more info on setting all of these registers:
// http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8452Q
// Feel free to modify any values, these are settings that work well for me.
void initMMA8452(byte fsr, byte dataRate)
{
MMA8452Standby(); // Must be in standby to change registers

/* Set up the full scale range to 2, 4, or 8g. */
// Set up the full scale range to 2, 4, or 8g.
if ((fsr==2)||(fsr==4)||(fsr==8))
writeRegister(0x0E, fsr >> 2);
else
writeRegister(0x0E, 0);

/* Setup the 3 data rate bits, from 0 to 7 */
// Setup the 3 data rate bits, from 0 to 7
writeRegister(0x2A, readRegister(0x2A) & ~(0x38));
if (dataRate <= 7)
writeRegister(0x2A, readRegister(0x2A) | (dataRate << 3));

/* Set up portrait/landscap registers - 4 steps:
1. Enable P/L
2. Set the back/front angle trigger points (z-lock)
3. Set the threshold/hysteresis angle
4. Set the debouce rate
// For more info check out this app note: http://cache.freescale.com/files/sensors/doc/app_note/AN4068.pdf */
// Set up portrait/landscap registers - 4 steps:
// 1. Enable P/L
// 2. Set the back/front angle trigger points (z-lock)
// 3. Set the threshold/hysteresis angle
// 4. Set the debouce rate
// For more info check out this app note: http://cache.freescale.com/files/sensors/doc/app_note/AN4068.pdf
writeRegister(0x11, 0x40); // 1. Enable P/L
writeRegister(0x13, 0x44); // 2. 29deg z-lock (don't think this register is actually writable)
writeRegister(0x14, 0x84); // 3. 45deg thresh, 14deg hyst (don't think this register is writable either)
Expand All @@ -268,76 +267,75 @@ void initMMA8452(byte fsr, byte dataRate)
writeRegister(0x27, 0xA0); // 4. 200ms (at 800Hz odr) between taps min, this also depends on the data rate
writeRegister(0x28, 0xFF); // 5. 318ms (max value) between taps max

/* Set up interrupt 1 and 2 */
// Set up interrupt 1 and 2
writeRegister(0x2C, 0x02); // Active high, push-pull interrupts
writeRegister(0x2D, 0x19); // DRDY, P/L and tap ints enabled
writeRegister(0x2E, 0x01); // DRDY on INT1, P/L and taps on INT2

MMA8452Active(); // Set to active to start reading
}

/* Sets the MMA8452 to standby mode.
It must be in standby to change most register settings */
// Sets the MMA8452 to standby mode.
// It must be in standby to change most register settings
void MMA8452Standby()
{
byte c = readRegister(0x2A);
writeRegister(0x2A, c & ~(0x01));
}

/* Sets the MMA8452 to active mode.
Needs to be in this mode to output data */
// Sets the MMA8452 to active mode.
// Needs to be in this mode to output data
void MMA8452Active()
{
byte c = readRegister(0x2A);
writeRegister(0x2A, c | 0x01);
}

/* Read i registers sequentially, starting at address
into the dest byte arra */
// Read i registers sequentially, starting at address into the dest byte array
void readRegisters(byte address, int i, byte * dest)
{
i2cSendStart();
i2cWaitForComplete();

i2cSendByte((MMA8452_ADDRESS<<1)); // write 0xB4
i2cSendByte((MMA8452_ADDRESS<<1)); // write 0xB4

This comment has been minimized.

Copy link
@clemrom

clemrom Jan 4, 2013

here we have SA0 at 1 hence MMA8452_ADDRESS is 0x1D but MMA8452_ADDRESS<<1 is 0x3A not 0xB4. Same comment apply in other places in the file.

This comment has been minimized.

Copy link
@nseidle

nseidle Jan 4, 2013

Author Member

Hmmm - I'm guessing 0xB4 is a hold over from example code that we used to start this example. This comment should probably be changed to

// Send write command to the MMA8452

and

// Send read command to the MMA8452
i2cWaitForComplete();

i2cSendByte(address); // write register address
i2cWaitForComplete();

i2cSendStart();
i2cSendByte((MMA8452_ADDRESS<<1)|0x01); // write 0xB5
i2cSendByte((MMA8452_ADDRESS<<1)|0x01); // write 0xB5
i2cWaitForComplete();
for (int j=0; j<i; j++)
{
i2cReceiveByte(TRUE);
i2cWaitForComplete();
dest[j] = i2cGetReceivedByte(); // Get MSB result
dest[j] = i2cGetReceivedByte(); // Get MSB result
}
i2cWaitForComplete();
i2cSendStop();

cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
}

/* read a single byte from address and return it as a byte */
// Read a single byte from address and return it as a byte
byte readRegister(uint8_t address)
{
byte data;

i2cSendStart();
i2cWaitForComplete();

i2cSendByte((MMA8452_ADDRESS<<1)); // write 0xB4
i2cSendByte((MMA8452_ADDRESS<<1)); // Write 0xB4
i2cWaitForComplete();

i2cSendByte(address); // write register address
i2cSendByte(address); // Write register address
i2cWaitForComplete();

i2cSendStart();

i2cSendByte((MMA8452_ADDRESS<<1)|0x01); // write 0xB5
i2cSendByte((MMA8452_ADDRESS<<1)|0x01); // Write 0xB5
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
Expand All @@ -352,16 +350,16 @@ byte readRegister(uint8_t address)
return data;
}

/* Writes a single byte (data) into address */
// Writes a single byte (data) into address
void writeRegister(unsigned char address, unsigned char data)
{
i2cSendStart();
i2cWaitForComplete();

i2cSendByte((MMA8452_ADDRESS<<1));// write 0xB4
i2cSendByte((MMA8452_ADDRESS<<1)); // Write 0xB4
i2cWaitForComplete();

i2cSendByte(address); // write register address
i2cSendByte(address); // Write register address
i2cWaitForComplete();

i2cSendByte(data);
Expand Down

0 comments on commit 7ecdc22

Please sign in to comment.