AC Energy monitor IC library memory optimisation

HI all,

I’m busy ( have done V0.xx) making a library to use a Maxim 78M6613 energy monitor IC ( with current and voltage sensor)
to be used with an uno (atmega 328) and the mega ( 2560).
my protoype is working on an Uno
the interface is very easy to implement but this results in a lot of memory usage ( 7k ),
edit** I m unsure as to where the strings are stored and would like to minimise the SRAM and flash footprint of this

I access memory adresses from 0x20 up to 0xF2, so I only use a small amount of unique characters sent over serial as ASCII
is there a way that I can store these 26 characters ie. 16 hex,),=,?,+,-,Z,W,L,P,O,K
in an array in flash and refer to them in my Serial commands

commands are sent as ASCII strings like a serial print… but i used streaming library to get things nicely inline

commands are as follows :

Serial << ")20?" << crl;             // get channel 1 temperature

where the command breaks down to
) : controller memory access
20 : hex address the value is stored in ( in this case , an on-board temperature reading in degrees C)
? : reply should be a decimal format
: end of message

a sample method is here :

I think it stores both these strings as separate entities ( the one i send, and the one i pass to my response handler),

	double PowerMonitor::vrms(int channel) // returns rms Voltage +VVV.vvv (26) (66)		
	{
	switch(channel)
		{
		case 1:
			Serial << ")26?" << crl;// get channel 1 
			return processResponse(")26");
		break;
		
		case 2:
			Serial << ")66?" << crl;// get channel 2
			return processResponse(")66");
		break;
		}
	}

regards,
DD

...but this results in a lot of memory usage ( 7k )...

Flash or SRAM? Which are we trying to optimize?

SRAM

sorry 'bout that...

Which board (Arduino) are you using?

currently using an Uno ( Atmega 328)
but this shield will be used along with GSM/GPRS or Ethernet/Wifi and Xbee
So I’d like to keep the RAM usage as low as possible

Can you post the whole library sofar?

some tips - don't do serial output in a library (unless to talk to a sensor) - put tables in progmem - try to do all in integer domain (so no floats if possible) - refactor reusable parts to functions to minimize footprint and stack consumption.

LocalgHost:
currently using an Uno ( Atmega 328)
So I’d like to keep the RAM usage as low as possible

Certainly understandable but before trying to reduce SRAM usage you need to first determine how much is being used. This is from the Uno webpage…

Summary

Flash Memory 32 KB (ATmega328) of which 0.5 KB used by bootloader
SRAM 2 KB (ATmega328)

…but this results in a lot of memory usage ( 7k [SRAM] )…

How did you determine that your library is using 7 KB of SRAM?

it adds around 7k when included in a sketch, obviously not all of this is SRAM,

I'm looking for ways so try and minimise SRAM usage in this library, it bothers me that I don't know where the quoted strings are stored that I use in my library.

would it be wise to use something like http://www.controllerprojects.com/2011/05/23/determining-sram-usage-on-arduino/to determine the SRAM usage ?

Ive had the 328 "lock up" before when I was working with a SM5100B-D GSM/GPRS module due to all the AT command strings and the POST and PUT strings I used in the sketch.

Trying to avoid this library using unnecessary amounts of SRAM in sketches and to minimise its flash footprint as well, to account for people using it not knowing all the implications of memory usage,

such as using this with ethernet and an LCD..

LocalgHost: it adds around 7k when included in a sketch, obviously not all of this is SRAM,

I'm looking for ways so try and minimise SRAM usage in this library, it bothers me that I don't know where the quoted strings are stored that I use in my library.

the 7k reported by the IDE does not include the SRAM at all -- it is only determined as it runs (especially if you have recursive functions and such).

All of your quoted strings are stored in SRAM unless you've specifically told them to be stored in flash (with the PROGMEM keyword or by using the F macro)

LocalgHost:
would it be wise to use something like controllerprojects.com determine the SRAM usage ?

That would be helpful however…

freeRam returns (total memory) - (static data) - (stack space) - (heap). For the first optimization pass you should focus just on static data (like string constants). That’s the part that you have the most control over. The simplest way to determine static data size is with a command-line program named avr-size. There have been several posts on this forum about how to use it. After searching, if you cannot find anything helpful, report back.