Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 2 months ago.
FRAM
Hello, I'm new to programming. Here I'm trying to store 3-axis vailues to FRAM. I could able to store string " hello world" . Now I'm trying to store 3-axis data. I failed to davalop the function. Could some one help me.
while (true) {
accelerometer.getOutput(readings);
pc.printf("\r\n%i, %i, %i", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
static char myString[] = "A Test String!\r\n";
char readString[sizeof(myString)];
int start = s.read_us();
//Write bytes
mem.writeBytes(myString,0,sizeof(myString));
int writeEnd = s.read_us();
//Read bytes
mem.readBytes(readString,0,sizeof(readString));
int end = s.read_us();
//Print read bytes to Serial to verify correctness
for (int i=0; i<sizeof(readString); i++) pc.printf("%c",readString[i]);
pc.printf("\r\n");
pc.printf("Write completed in %d microsecs.\r\n",writeEnd-start);
pc.printf("Read completed in %d microsecs.\r\n",end-writeEnd);
pc.printf("Status byte: 0x%x\r\n",mem.readStatusRegister());
wait(0.5);
}
And the 3-axis readings function is
void ADXL345_I2C::getOutput(int* readings){
char buffer[6];
multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
readings[2] = (int)buffer[5] << 8 | (int)buffer[4];
Thank you.
1 Answer
10 years, 2 months ago.
I'm guessing that the format for readBytes and writeBytes are (void *, int offset, int length) in which case is should be as simple as:
void ADXL345_I2C::getOutput(int* readings){
char buffer[6];
char readBuffer[6];
multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
mem.writeBytes(buffer,0, 6);
mem.readBytes(readBuffer,0,6);
readings[0] = (int)readBuffer[1] << 8 | (int)readBuffer[0];
readings[1] = (int)readBuffer[3] << 8 | (int)readBuffer[2];
readings[2] = (int)readBuffer[5] << 8 | (int)readBuffer[4];
}
I don't know the construct of your mem.writeBytes but appears to want a string
so you need to convert the Integer values into a string and then mem.writeByte that string
use sprintf() function
i.e.
char *newstring
/*do following three times n=0 to 2*/
sprintf(newstring,"%d",reading[n]);
mem.writeByte(newstring,0,sizeof(newstring));
Regards
posted by Martin Simpson 21 Aug 2015Yes, It's working. thank you.
posted by Mohan gandhi Vinnakota 21 Aug 2015