Skip to content
Permalink
Browse files

Mozzi I2C example converted to use MMA7660 accelerometer

  • Loading branch information...
Roger-random committed Jul 30, 2019
1 parent bcd4a4f commit 67f0d38209dffe1e35d3374c59134d74bae5151b
Showing with 35 additions and 46 deletions.
  1. +35 −46 Mozzi_MMA7660.ino
@@ -1,10 +1,12 @@
/* Example reading an ADXL345 accelerometer
/* Example reading an MMA7660 accelerometer
using I2C communication without blocking audio synthesis,
using Mozzi sonification library.
Demonstrates use of twi_nonblock functions
to replace processor-blocking Wire methods.
This is only a minimum demonstration fragment with no handler for MMA7660 errors.
Note: The twi_nonblock code is not compatible with Teesy 3.1.
Circuit: Audio output on digital pin 9.
@@ -15,8 +17,10 @@
Mozzi help/discussion/announcements:
https://groups.google.com/forum/#!forum/mozzi-users
Marije Baalman 2012.
Small modifications by TIm Barrass to retain Mozzi compatibility.
Original ADXL345 sample code by Marije Baalman 2012.
Small modifications by Tim Barrass to retain Mozzi compatibility.
Converted from ADXL345 to MMA7660 by Roger Cheng
This example code is in the public domain.
*/

@@ -34,43 +38,28 @@ Ead kEnvelope(CONTROL_RATE); // resolution will be CONTROL_RATE

int gain;


#define ADXL345_DEVICE (0x53) // ADXL345 device address

#define ADXL345_POWER_CTL 0x2d
#define ADXL345_DATA_FORMAT 0x31
#define ADXL345_DATAX0 0x32

#define ADXL345_THRESH_INACT 0x25
#define ADXL345_TIME_INACT 0x26
#define ADXL345_ACT_INACT_CTL 0x27


static volatile byte acc_status = 0;
#define ACC_IDLE 0
#define ACC_READING 1
#define ACC_WRITING 2

byte accbytedata[6];

void setup_accelero(){
initialize_twi_nonblock();
byte accbytedata[3];

acc_writeTo(ADXL345_POWER_CTL, 0); // all registers off
acc_writeTo(ADXL345_POWER_CTL, 16); // auto sleep on
#define MMA7660_ADDR 0x4c

// ADXL345_DATA_FORMAT : (self test)(spi)(int_invert)(0)(fullres)(justify)(range..)
// 11 //= B00001011
acc_writeTo( ADXL345_DATA_FORMAT, 11 );
#define MMA7660_X 0x00
#define MMA7660_MODE 0x07
#define MMA7660_STAND_BY 0x00
#define MMA7660_ACTIVE 0x01
#define MMA7660_SR 0x08 //sample rate register
#define AUTO_SLEEP_32 0X02

// set the threshold for inactivity
// acc_writeTo( ADXL345_THRESH_INACT, inactivityThreshold );
// acc_writeTo( ADXL345_TIME_INACT, inactivityTime );
// 0B10001111
// acc_writeTo( ADXL345_ACT_INACT_CTL, B10001111 );
// acc_writeTo(ADXL345_POWER_CTL, B00111000);
void setup_accelero(){
initialize_twi_nonblock();

acc_writeTo(ADXL345_POWER_CTL, 8); // auto sleep off, measure on
acc_writeTo(MMA7660_MODE, MMA7660_STAND_BY);
acc_writeTo(MMA7660_SR, AUTO_SLEEP_32);
acc_writeTo(MMA7660_MODE, MMA7660_ACTIVE);

acc_status = ACC_IDLE;
}
@@ -81,13 +70,13 @@ void initiate_read_accelero(){
// indicate that we are transmitting
// transmitting = 1;
// set address of targeted slave
txAddress = ADXL345_DEVICE;
txAddress = MMA7660_ADDR;
// reset tx buffer iterator vars
txBufferIndex = 0;
txBufferLength = 0;

// put byte in tx buffer
txBuffer[txBufferIndex] = ADXL345_DATAX0;
txBuffer[txBufferIndex] = MMA7660_X; // Start reading from register for X
++txBufferIndex;
// update amount in buffer
txBufferLength = txBufferIndex;
@@ -103,12 +92,12 @@ void initiate_request_accelero(){
// indicate that we are done transmitting
// transmitting = 0;

byte read = twi_initiateReadFrom(ADXL345_DEVICE, 6);
byte read = twi_initiateReadFrom(MMA7660_ADDR, 3);
acc_status = ACC_READING;
}

void finalise_request_accelero(){
byte read = twi_readMasterBuffer( rxBuffer, 6 );
byte read = twi_readMasterBuffer( rxBuffer, 3 );
// set rx buffer iterator vars
rxBufferIndex = 0;
rxBufferLength = read;
@@ -127,8 +116,8 @@ void finalise_request_accelero(){

// Writes val to address register on device
void acc_writeTo(byte address, byte val) {
// Wire.beginTransmission(ADXL345_DEVICE); // start transmission to device
twowire_beginTransmission(ADXL345_DEVICE); // start transmission to device
// Wire.beginTransmission(MMA7660_ADDR); // start transmission to device
twowire_beginTransmission(MMA7660_ADDR); // start transmission to device
// Wire.send(address); // send register address
twowire_send( address );
// Wire.send(val); // send value to write
@@ -148,34 +137,34 @@ void setup(){
}


int accx;
int accy;
int accz;
int8_t accx;
int8_t accy;
int8_t accz;

int envgain;

void updateControl(){
envgain = (int) kEnvelope.next();
if ( envgain < 5 ){
if ( accx > 512){
if ( accx > 32){
aSin.setFreq( 400 );
kEnvelope.start();
}
else if ( accy > 512 ) {
else if ( accy > 32 ) {
aSin.setFreq( 800 );
kEnvelope.start();
}
else if ( accz > 512 ) {
else if ( accz > 32 ) {
aSin.setFreq( 1200 );
kEnvelope.start();
}
}

switch( acc_status ){
case ACC_IDLE:
accx = (int) (accbytedata[1]*256 + accbytedata[0]); // accelerometer x reading
accy = (int) (accbytedata[3]*256 + accbytedata[2]); // accelerometer y reading
accz = (int) (accbytedata[5]*256 + accbytedata[4]); // accelerometer z reading
accx = accbytedata[0]; // accelerometer x reading
accy = accbytedata[1]; // accelerometer y reading
accz = accbytedata[2]; // accelerometer z reading
initiate_read_accelero();
break;
case ACC_WRITING:

0 comments on commit 67f0d38

Please sign in to comment.
You can’t perform that action at this time.