检查Arduino

问题描述 投票:0回答:3
理解,我想在串行连接上打印出当前的内存 /总数。

您可以使用它。它会让您了解自己的位置:

Serial.print(availableMemory()); // free RAM check for debugging. SRAM for ATmega328p = 2048Kb. int availableMemory() { // Use 1024 with ATmega168 int size = 2048; byte *buf; while ((buf = (byte *) malloc(--size)) == NULL); free(buf); return size; }

tron使用

memoryfree
memory-management arduino
3个回答
4
投票

arduinoPlayground上,有有关此问题的详细信息,这就是我发现我一直在使用的方法:

3
投票

// MemoryFree library based on code posted here: // http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213583720/15 // // Extended by Matthew Murdoch to include walking of the free list. #ifndef MEMORY_FREE_H #define MEMORY_FREE_H #ifdef __cplusplus extern "C" { #endif int freeMemory(); #ifdef __cplusplus } #endif #endif memoryfree.cpp:


1
投票

示例草图:

#include <MemoryFree.h> // On Arduino Duemilanove with ATmega328: // // Reported free memory with str commented out: // 1824 bytes // // Reported free memory with str and Serial.println(str) uncommented: // 1810 // // Difference: 14 bytes (13 ascii chars + null terminator) // 14-bytes string //char str[] = "Hello, world!"; void setup() { Serial.begin(115200); } void loop() { //Serial.println(str); Serial.print("freeMemory()="); Serial.println(freeMemory()); delay(1000); }

建立Esben的答案,我写了此优化版本:

int biggestMemoryBlock(uint16_t min,uint16_t max) { if (min==max-1) return min; int size=max; int lastSize=size; byte *buf; while ((buf = (byte *) malloc(size)) == NULL) { lastSize=size; size-=(max-min)/2; }; free(buf); return biggestMemoryBlock(size,lastSize); }; int biggestMemoryBlock() { return biggestMemoryBlock(0,4096); }

在Arduino uno中,只需1毫秒或更短的时间,而不是具有原始功能的13-20毫秒。常数必须至少是板上的总内存(字节)。
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.