我正在使用带有 adafruit FRAM 内存突破的 arduino。我正在尝试获取以下内容以从数组中提取值,然后将它们保存到 FRAM 中。问题是保存的值不正确。需要进行什么转换才能存储这些值?
#include <Wire.h>
#include "Adafruit_FRAM_I2C.h"
String incomingData = "244,1,1,7,13,1,0";
Adafruit_FRAM_I2C fram = Adafruit_FRAM_I2C();
uint16_t framAddr = 0;
void setup(void) {
Serial.begin(9600);
if (fram.begin()) { // you can stick the new i2c addr in here, e.g. begin(0x51);
Serial.println("Found I2C FRAM");
} else {
Serial.println("I2C FRAM not identified \r\n");
}
uint8_t input[16];
incomingData.getBytes(input,16);
uint8_t *str;
int i_ctr;
uint8_t arr[16][16];
for(i_ctr = 0,str = strtok(input,","); str!= NULL; i_ctr++, str= strtok(NULL,","))
{
strcpy( arr[i_ctr], str);
}
fram.write8(0x0, arr[0]);
fram.write8(0x1, arr[1]);
fram.write8(0x2, arr[2]);
fram.write8(0x3, arr[3]);
fram.write8(0x4, arr[4]);
fram.write8(0x5, arr[5]);
fram.write8(0x6, arr[6]);
fram.write8(0x7, 77); //test
Serial.println(fram.read8(0x0));
Serial.println(fram.read8(0x1));
Serial.println(fram.read8(0x2));
Serial.println(fram.read8(0x3));
Serial.println(fram.read8(0x4));
Serial.println(fram.read8(0x5));
Serial.println(fram.read8(0x6));
Serial.println(fram.read8(0x7));
}
输出:
218
234
250
10
26
42
58
77
最佳答案
arr[i]
是一个地址,你实际写入的是地址的低字节(看看打印的值是如何相隔 16 的)。如果您想获得传入数据的数值,请将代码更改为
uint8_t arr[16]; // One dimension is enough
for (....) {
arr[i_ctr] = atoi(str);
}
PS:你没有收到任何编译器警告吗?
关于c - 将数组值写入内存时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41883988/