How many bytes will printf() take up in a non debug-monitor environment? Also, is it easy to set up printf() to map to a second UART? I will need control of the UART ISR so that I can handle receives appropriately. How will this confict with printf()?
#pragma maxargs (20) will set the number of bytes used by variable arg. functions such as printf to 20. The default for the small model is 15, for the large 40. See page 34 of the latest C51 v6.02 C51.pdf manual. Mapping printf to any device is simple. First, write your own putchar(). One that can print to either UART based upon some file static var. This will automatically replace the library version. Then, write a device switcher function that controls the file static var. that putchar uses to determine which UART to use. Printf will use what ever function has the name putchar. - Mark
Thanks Mark. But also, how much space will the function take? I've heard people say that printf() may take up to 2K of code space. Meaning that if printf() is used in the embedded application, then 2K of code space will be eaten up. If thats the case then I'll have to come up with my own transmission mechanism.
Oh goodness yes, printf is fat. Just build a small file with only main() and no printf call and note the size of the code in the .m51. Then make a call to printf in this file, build and check the .m51 file again. This will tell you the size of printf for a given model. For protocols I'd skip printf. I always use some variant of:
STX | message class | class subtype | body | checksum
void sendPacket(Major maj, Minor min, char *pBody, U8 bodyLen) { U8 idx; U8 checksum; putchar(STX); checksum = STX; checksum += putEscapedChar(maj); checksum += putEscapedChar(min); checksum += putEscapedChar(bodyLen); checksum += putEscapedChar(~bodyLen); for (idx = 0; idx < bodyLen; ++idx) { checksum += putEscapedChar(*pBody++); } putEscapedChar(checksum); }
would please tell me more about the folloing function: putEscapedChar(checksum)
I'd write my own putchar() like this:
char putchar(char outChar) { TI = 0; SBUF = outChar; while (!TI); return outChar; }
void putFiltByte(U8 outByte) { if (STX == outByte || DLE == outByte) { // Escape this data char. putchar(DLE); // Ensure this data char. is never an STX or DLE. outByte += ' '; } putchar(outByte); }
View all questions in Keil forum