liudr:
RAM usage:
I haven't looked closely at the RAM usage of the LCD libraries vs using HardwareSerial but it isn't obvious
that a LCD library would use more ram than the HardwareSerial buffers now that both TX and RX are
interrupt driven. HardwareSerial uses 128 bytes. I'm guessing that the LCD library will use less RAM.
Unless you go into the arduino hardware library file and manually remove the hardware serial buffer yourself, which I wouldn't do, even thought I know how to, you will always have that buffer regardless you use any serial commands or not. The serial object is also always there, instantiated regardless you use serial command or not. I see this as a huge convenience made by arduino team. If you don't use liquidcrystal library and only use serial print char arrays, yes, you save memory in sram and flash. So I have to say you thought wrong.
You are mistaken on the existence of the serial object (and serial code) when serial is not used
and on the resources used by serial vs LiquidCrystal.
Take a closer look at what really happens when creating the final .elf image.
Go run avr-objdump on the final .elf image created by the IDE.
Given the compiler and link options used by the IDE,
the linker will remove any function or data elements that are not referenced.
When the serial functions are not used, the HardwareSerial functions and the data they reserve
(rx_buffer and tx_buffer on an UNO board) are removed from the final linked image.
Serial uses more RAM, significantly more RAM depending
on which board is being used vs either of the two LiquidCrystal libraries.
Serial also use more flash than the stock LiquidCrystal Library
when using the mega 1280.
--- bill
proof can be seen below.
Here are some actual real numbers on flash and RAM usage for two "hello world" lcd sketches
using Arduino 1.0.1
One sketch uses the LiquidCrystal library and one that uses serial prints to a smart backpack.
Here is the LiquidCrystal sketch:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
Here is the equivalent using the serial port
(ignore baud rate for now as that does not affect code or data size)
void setup() {
// set up the LCD's number of columns and rows:
Serial.begin(19200);
Serial.print("\e[20;4L~");
delay(500);
// Print a message to the LCD.
Serial.print("hello, world!");
}
void loop() {
// NOTE: Serial backpack device uses 1 based vs 0 based addressing
// this will position the cursor to first column, second line from top.
Serial.print("\e[1;2H~");
// print the number of seconds since reset:
Serial.print(millis()/1000);
}
And here is the sizing information for each when using an UNO board:
LiquidCrystal
Idx Name Size VMA LMA File off Algn
0 .data 0000001e 00800100 000009ec 00000a60 2**0
1 .text 000009ec 00000000 00000000 00000074 2**1
2 .bss 0000001e 0080011e 00000a0a 00000a7e 2**0
FM LiquidCrystal
Idx Name Size VMA LMA File off Algn
0 .data 00000036 00800100 00000c3e 00000cb2 2**0
1 .text 00000c3e 00000000 00000000 00000074 2**1
2 .bss 00000020 00800136 00000c74 00000ce8 2**0
Serial
Idx Name Size VMA LMA File off Algn
0 .data 00000030 00800100 00000918 0000098c 2**0
1 .text 00000918 00000000 00000000 00000074 2**1
2 .bss 000000b0 00800130 00000948 000009bc 2**0
You can see in the LiquidCrystal sizes that clearly the two 64 byte buffers
are not in the .bss area.
You can see that the LiquidCrystal code (.text) is larger than the serial code
and FM LiquidCrystal is quite a bit larger.
The RAM result was as I had predicted:
The RAM (.bss) usage was larger for the Serial implementation.
One thing that really surprised me in this exercise is the difference when using
a MEGA 1280 board vs an Uno board.
The spread on resources really shifts against serial.
I had no idea their MEGA code was that bad....
They way they wrote the code and dealt with the ISRs causes
all the code and data buffers for all serial ports to be dragged in
even if you only use 1 of them.
LiquidCrystal
Idx Name Size VMA LMA File off Algn
0 .data 0000001e 00800200 00000c28 00000c9c 2**0
1 .text 00000c28 00000000 00000000 00000074 2**1
2 .bss 0000001e 0080021e 00000c46 00000cba 2**0
FM LiquidCrystal
Idx Name Size VMA LMA File off Algn
0 .data 00000036 00800200 00000f7a 00000fee 2**0
1 .text 00000f7a 00000000 00000000 00000074 2**1
2 .bss 00000020 00800236 00000fb0 00001024 2**0
Serial
Idx Name Size VMA LMA File off Algn
0 .data 00000030 00800200 00000ed0 00000f44 2**0
1 .text 00000ed0 00000000 00000000 00000074 2**1
2 .bss 000002a5 00800230 00000f00 00000f74 2**0
You can see that the ram usage for the two LiquidCrystal libraries does not
change when moving to the 1280 board while the RAM usage goes up significantly
when using serial.
When using a Mega 1280 board
Serial now uses more code and more ram than the stock LiquidCrystal library.
--- bill