Attiny 1614 and MMA8453_n0m1 library

Hello,
I have an MMA8453 accelerometer and I made a code that works perfectly on an Arduino Nano 3 using MMA8453_n0m1.h library.
But now I need to use Attiny 1614 procesor...and I am haveing some problems whit proggraming Attiny 1614 procesor using MMA8453_n0m1.h library in code.
This is the error:

c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp: In member function 'void MMA8453_n0m1::setI2CAddr(int)':
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp:58:21: warning: statement has no effect [-Wunused-value]
  I2CAddr = address; I2CAddr;
                     ^~~~~~~
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp: In member function 'void MMA8453_n0m1::clearInterrupt()':
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp:124:22: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
     if(srcTrans&0x02 == 0x02)
                 ~~~~~^~~~~~~
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp:128:22: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
     if(srcTrans&0x08 == 0x08)
                 ~~~~~^~~~~~~
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp:132:22: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
     if(srcTrans&0x20 == 0x20)
                 ~~~~~^~~~~~~
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp:141:13: warning: variable 'srcFF' set but not used [-Wunused-but-set-variable]
        byte srcFF;
             ^~~~~
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp: In member function 'void MMA8453_n0m1::xyz(int&, int&, int&)':
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp:206:38: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
        x = buf[0] << 2 | buf[1] >> 6 & 0x3;
                          ~~~~~~~~~~~~^~~~~
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp:207:38: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
        y = buf[2] << 2 | buf[3] >> 6 & 0x3;
                          ~~~~~~~~~~~~^~~~~
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp:208:38: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
        z = buf[4] << 2 | buf[5] >> 6 & 0x3;
                          ~~~~~~~~~~~~^~~~~
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp: In function 'void accelISR()':
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp:730:6: warning: 'accelISR' appears to be a misspelled 'signal' handler, missing '__vector' prefix [-Wmisspelled-isr]
 void accelISR(void){
      ^~~~~~~~
Compiling core...
Using precompiled core: C:\Users\mijat\AppData\Local\Temp\arduino\cores\5c54b9a6ae434ea86e3a422138f215c3\core.a
Linking everything together...
"C:\\Users\\mijat\\AppData\\Local\\Arduino15\\packages\\DxCore\\tools\\avr-gcc\\7.3.0-atmel3.6.1-azduino6/bin/avr-gcc" -Wall -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -Wl,--section-start=.text=0x0 -mrelax -mmcu=attiny1614 -o "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287/sketch_oct10a-test_motio_moda.ino.elf" "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287\\sketch\\sketch_oct10a-test_motio_moda.ino.cpp.o" "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287\\libraries\\Wire\\Wire.cpp.o" "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287\\libraries\\Wire\\twi.c.o" "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287\\libraries\\Wire\\twi_pins.c.o" "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287\\libraries\\MMA8453_n0m1\\MMA8453_n0m1.cpp.o" "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287/..\\..\\cores\\5c54b9a6ae434ea86e3a422138f215c3\\core.a" "-LC:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287" -lm
In function 'accelISR':
c:\Users\mijat\Documents\Arduino\libraries\MMA8453_n0m1\MMA8453_n0m1.cpp:730:6: warning: 'accelISR' appears to be a misspelled 'signal' handler, missing '__vector' prefix [-Wmisspelled-isr]
 void accelISR(void"C:\\Users\\mijat\\AppData\\Local\\Arduino15\\packages\\DxCore\\tools\\avr-gcc\\7.3.0-atmel3.6.1-azduino6/bin/avr-objcopy" -O binary -R .eeprom "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287/sketch_oct10a-test_motio_moda.ino.elf" "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287/sketch_oct10a-test_motio_moda.ino.bin"
){
      ^
"C:\\Users\\mijat\\AppData\\Local\\Arduino15\\packages\\DxCore\\tools\\avr-gcc\\7.3.0-atmel3.6.1-azduino6/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287/sketch_oct10a-test_motio_moda.ino.elf" "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287/sketch_oct10a-test_motio_moda.ino.eep"
"C:\\Users\\mijat\\AppData\\Local\\Arduino15\\packages\\DxCore\\tools\\avr-gcc\\7.3.0-atmel3.6.1-azduino6/bin/avr-objcopy" -O ihex -R .eeprom "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287/sketch_oct10a-test_motio_moda.ino.elf" "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287/sketch_oct10a-test_motio_moda.ino.hex"
cmd /C echo. && "C:\\Users\\mijat\\AppData\\Local\\Arduino15\\packages\\DxCore\\tools\\avr-gcc\\7.3.0-atmel3.6.1-azduino6/bin/avr-objdump" --disassemble --source --line-numbers --demangle --section=.text "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287/sketch_oct10a-test_motio_moda.ino.elf" > "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287/sketch_oct10a-test_motio_moda.ino.lst"
 
cmd /C echo. && "C:\\Users\\mijat\\AppData\\Local\\Arduino15\\packages\\DxCore\\tools\\avr-gcc\\7.3.0-atmel3.6.1-azduino6/bin/avr-nm" --numeric-sort --line-numbers --demangle --print-size --format=s "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287/sketch_oct10a-test_motio_moda.ino.elf" > "C:\\Users\\mijat\\AppData\\Local\\Temp\\arduino\\sketches\\68F94FB98ECA067816B1E900D155B287/sketch_oct10a-test_motio_moda.ino.map"
 

This library seems to use pin change interrupts, via "attachinterrupt", and the Attiny1614 is not among the supported processors.

Do you maybe have any suggestion what library to use?

Not familiar with your sensor. You could give the Adafruit library a try.

I just had a quick look at: GitHub - n0m1/MMA8453_n0m1: an Arduino Library for the Freescale MMA8453Q & MMA8452Q Accelerometer

MMA8453_n0m1.h contains this construct:
extern "C" void accelISR(void) __attribute__ ((signal));

Try changing it to just:
extern "C" void accelISR(void);

There is also a potential mess in this sort of construct from the corresponding .cpp which assumes that the edge triggered interrupts are pins 2 and 3 which may not be the case with the ATtiny1614. These instances may have to be tidied up:

void MMA8453_n0m1::shakeMode(int threshold, boolean enableX, boolean enableY, boolean enableZ, boolean enableINT2,int arduinoINTPin)
{
	 if(arduinoINTPin == 2 || arduinoINTPin == 3)
	 {
		arduinoINTPin = arduinoINTPin - 2;
		attachInterrupt(arduinoINTPin,accelISR,FALLING); 
		//DataSheet pg40, When IPOL is ‘0’ (default value) any interrupt event will signaled with a logical 0
	 }
	
	 else
	 {	
		 #ifdef PINCHANGE_INT
		 	pinMode(arduinoINTPin, INPUT); digitalWrite(arduinoINTPin, HIGH);
		 	PCintPort::attachInterrupt(arduinoINTPin,accelISR,FALLING);  
		 #else
		 	Serial.println("no INT on pin, define PINCHANGE_INT");	
		 #endif
	 }
. . .
. . .

I made it work :grinning: :grinning: :grinning: without using the library

#include <Wire.h>

#define MMA8451_ADDRESS 0x1C // I2C address for MMA8451 sensor
#define REG_OUT_X_MSB 0x01   // Register address for X-axis data

void setup() {
  Serial.begin(9600);
  Wire.begin();
  
  // Initialize MMA8451
  writeToRegister(MMA8451_ADDRESS, 0x2A, 0x01); // Set ACTIVE mode
  writeToRegister(MMA8451_ADDRESS, 0x0E, 0x00); // Set range to +/-2g
}

void loop() {
// Read accelerometer data for X, Y, and Z axes
  int16_t xAccel = readFromRegister(MMA8451_ADDRESS, REG_OUT_X_MSB);
  int16_t yAccel = readFromRegister(MMA8451_ADDRESS, REG_OUT_X_MSB + 2);
  int16_t zAccel = readFromRegister(MMA8451_ADDRESS, REG_OUT_X_MSB + 4);
  
  // Convert raw data to acceleration in g
  float xAcceleration = xAccel / 1024.0; // Assuming +/-2g range
  float yAcceleration = yAccel / 1024.0;
  float zAcceleration = zAccel / 1024.0;
  
  // Print the results
  Serial.print("X: ");
  Serial.println(xAcceleration, 4); // 4 decimal places
 

  Serial.print("Y: ");
  Serial.println(yAcceleration, 4);


  Serial.print("Z: ");
  Serial.println(zAcceleration, 4);
  

  delay(500); // Adjust the delay for your desired sampling rate
}

void writeToRegister(uint8_t deviceAddress, uint8_t registerAddress, uint8_t data) {
  Wire.beginTransmission(deviceAddress);
  Wire.write(registerAddress);
  Wire.write(data);
  Wire.endTransmission();
}

int16_t readFromRegister(uint8_t deviceAddress, uint8_t registerAddress) {
  Wire.beginTransmission(deviceAddress);
  Wire.write(registerAddress);
  Wire.endTransmission(false);
  
  Wire.requestFrom(deviceAddress, 2);
  int16_t value = (Wire.read() << 8) | Wire.read();
  
  return value;
}

Great. It is even better if you can solve such problems without using a library. You can mark your post as a solution and maybe it helps someone else.

That's an awful lot of warnings for a library you want to trust, and those particular x&bit == bit checks actually look wrong... (== has higher precedence than &, so they all evaluate to x&1, essentially.)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.