如何通过Arduino将16位整数写入SRAM?
问题的根源是:我有一个Arduino Due、一个MPU6050加速计和一个23lcv512。MPU给我16位有符号整数。我想将数据保存到SRAM,并在测量后将其读回,然后通过串行发送到PC。发送到PC不是问题。问题在于,此SRAM具有128k PC 8位地址。我的数字是16位。我不能直接写。这是我的密码。我用以下代码测试了RAM: `如何通过Arduino将16位整数写入SRAM?,arduino,ram,16-bit,Arduino,Ram,16 Bit,问题的根源是:我有一个Arduino Due、一个MPU6050加速计和一个23lcv512。MPU给我16位有符号整数。我想将数据保存到SRAM,并在测量后将其读回,然后通过串行发送到PC。发送到PC不是问题。问题在于,此SRAM具有128k PC 8位地址。我的数字是16位。我不能直接写。这是我的密码。我用以下代码测试了RAM: ` void循环(){ int i=0; 串行打印(“写入字节:”); 对于(i=0;i
void循环(){
int i=0;
串行打印(“写入字节:”);
对于(i=0;i<70000;i++){
//串行打印(“写入字节:”);
//连载印刷(一);
//Serial.println();
SRAM.writeByte(起始地址,i);
起始地址=起始地址+1;
}
串行打印(“写入结束”);
i=0;
起始地址=0;
对于(i=0;i<300;i++){
串行打印(“读取字节:”);
串行打印(SRAM.readByte(起始地址));
Serial.println();
Serial.println();
起始地址=起始地址+1;
延迟(100);
}
}`
我添加了23LC库。如果运行,则从RAM读回数字,但在255之后,它再次开始读取0。我知道为什么会这样。但我不知道如何解决这个问题
我尝试使用writeBlock命令,但它只适用于char变量。Char变量比整数需要更多的空间。我没有太多
是否有人可以编写一个示例代码,将16位带符号整数写入sram 我在下面评论了您原始代码中最明显的问题:
void loop() {
int i = 0;
Serial.print("Write Byte: ");
for (i = 0; i < 70000; i++) { // since i is a 16bit int, 70,000 is out of range.
SRAM.writeByte(START_ADDRESS, i); // cool you wrote 1 byte, where is the other write?
START_ADDRESS = START_ADDRESS + 1; // try to keep all caps names for constants.
// this will make your code easier to read, trust me!
}
Serial.print("Write End");
i = 0;
START_ADDRESS = 0;
for (i = 0; i < 300; i++) {
Serial.print("Read Byte: ");
Serial.print(SRAM.readByte(START_ADDRESS)); // you read 1 byte, you can't expect a 16 bit
// value out of that.
Serial.println();
Serial.println();
START_ADDRESS = START_ADDRESS + 1;
delay(100);
}
}
void循环(){
int i=0;
串行打印(“写入字节:”);
对于(i=0;i<70000;i++){//由于i是16位整数,70000超出范围。
SRAM.writeByte(START_ADDRESS,i);//酷您写了1个字节,另一个写在哪里?
START\u ADDRESS=START\u ADDRESS+1;//尝试保留常量的所有大写名称。
//这将使您的代码更容易阅读,相信我!
}
串行打印(“写入结束”);
i=0;
起始地址=0;
对于(i=0;i<300;i++){
串行打印(“读取字节:”);
Serial.print(SRAM.readByte(START_ADDRESS));//如果读取1个字节,则不能期望16位
//从中获得价值。
Serial.println();
Serial.println();
起始地址=起始地址+1;
延迟(100);
}
}
这里有一个更合理的方法,它存储无符号整数,但可以很容易地更改为有符号整数
#define SRAM_SIZE (128UL << 10) // we have 128K of SRAM available.
// The U and L make this value an unsigned long.
// ALWAYS use unsigned values for addresses.
void loop()
{
Serial.print(F("Writing sequential numbers into SRAM...")); // _always_ store string constants in flash.
// save your RAM for more interesting stuff.
for (unsigned long i = 0; i < SRAM_SIZE; i += 2) // filling SRAM
{
// this is the (truncated from 0-65535) value we'll write.
unsigned int value = static_cast<unsigned int>(i & 0xFFFF);
SRAM.writeByte(i, value & 0xFF); // write lowest 8 bits
SRAM.writeByte(i + 1, (value >> 8) & 0xFF); // write next 8 bits.
}
Serial.println(F("done."));
// read back
Serial.println(F("SRAM contents (16-bits unsigned values):"));
for (unsigned long i = 0; i < SRAM_SIZE; i += 2) // reading all SRAM in 16-bit chunks.
{
Serial.print(i, HEX);
Serial.print(F(": "));
// read two consecutive bytes and pack them into a 16-bit integer.
unsigned int value = SRAM.readByte(i) + (static_cast<unsigned int>(SRAM.readByte(i+1)) << 8); // the cast is necessary.
Serial.println(value);
}
delay(100);
}
#定义SRAM_大小(128UL我已经在下面评论了您原始代码中最明显的问题:
void loop() {
int i = 0;
Serial.print("Write Byte: ");
for (i = 0; i < 70000; i++) { // since i is a 16bit int, 70,000 is out of range.
SRAM.writeByte(START_ADDRESS, i); // cool you wrote 1 byte, where is the other write?
START_ADDRESS = START_ADDRESS + 1; // try to keep all caps names for constants.
// this will make your code easier to read, trust me!
}
Serial.print("Write End");
i = 0;
START_ADDRESS = 0;
for (i = 0; i < 300; i++) {
Serial.print("Read Byte: ");
Serial.print(SRAM.readByte(START_ADDRESS)); // you read 1 byte, you can't expect a 16 bit
// value out of that.
Serial.println();
Serial.println();
START_ADDRESS = START_ADDRESS + 1;
delay(100);
}
}
void循环(){
int i=0;
串行打印(“写入字节:”);
对于(i=0;i<70000;i++){//由于i是16位整数,70000超出范围。
SRAM.writeByte(START_ADDRESS,i);//酷您写了1个字节,另一个写在哪里?
START\u ADDRESS=START\u ADDRESS+1;//尝试保留常量的所有大写名称。
//这将使您的代码更容易阅读,相信我!
}
串行打印(“写入结束”);
i=0;
起始地址=0;
对于(i=0;i<300;i++){
串行打印(“读取字节:”);
Serial.print(SRAM.readByte(START_ADDRESS));//如果读取1个字节,则不能期望16位
//从中获得价值。
Serial.println();
Serial.println();
起始地址=起始地址+1;
延迟(100);
}
}
这里有一个更合理的方法,它存储无符号整数,但可以很容易地更改为有符号整数
#define SRAM_SIZE (128UL << 10) // we have 128K of SRAM available.
// The U and L make this value an unsigned long.
// ALWAYS use unsigned values for addresses.
void loop()
{
Serial.print(F("Writing sequential numbers into SRAM...")); // _always_ store string constants in flash.
// save your RAM for more interesting stuff.
for (unsigned long i = 0; i < SRAM_SIZE; i += 2) // filling SRAM
{
// this is the (truncated from 0-65535) value we'll write.
unsigned int value = static_cast<unsigned int>(i & 0xFFFF);
SRAM.writeByte(i, value & 0xFF); // write lowest 8 bits
SRAM.writeByte(i + 1, (value >> 8) & 0xFF); // write next 8 bits.
}
Serial.println(F("done."));
// read back
Serial.println(F("SRAM contents (16-bits unsigned values):"));
for (unsigned long i = 0; i < SRAM_SIZE; i += 2) // reading all SRAM in 16-bit chunks.
{
Serial.print(i, HEX);
Serial.print(F(": "));
// read two consecutive bytes and pack them into a 16-bit integer.
unsigned int value = SRAM.readByte(i) + (static_cast<unsigned int>(SRAM.readByte(i+1)) << 8); // the cast is necessary.
Serial.println(value);
}
delay(100);
}
#定义SRAM_大小(128UL