- The easiest way to use rprintf is to simply give it a string in quotes:
rprintf("Hello");
In order for this action to have any effect, the rprintf system must first have been initialized by calling rprintfInit(location), where location is one of a set of pre-defined functions for specifying where to print to:
rprintfInit(lcdDataWrite); // initialize rprintf to print to the LCD
- A "string" is really an array of characters. Characters have the type char, and are 8-bit numbers that use the standard ASCII encoding where each number specifies a character. You can assign a character in single quotes to a char variable, and it will evaluate to its ASCII code. The following chunk of code from lcdtest.c defines an array of characters 41 elements long, then in a for loop sets the first 40 elements to be sequential ASCII characters beginning with 'A'. Finally it sets the last element of the array to '0'. '0' is known as the "null character", and must terminate or be the last element of a string in RAM to be printed correctly. The function rprintfStr(stringinRAM) then prints a null-terminated character array from RAM.
#define MAXCOL 40
int main(void) {
char str2[MAXCOL+1];
// Allocate RAM for a test String
// but don't initialize the string.
rprintfInit(lcdDataWrite); // initialize rprintf to print to the LCD
// Store Characters in the test string in RAM
for (i=0; i<MAXCOL; i++) {
str2[i] = ('A' + i);
}
str2[MAXCOL] = '\0'; //RAM String must be NULL terminated
rprintfStr(str2); //print a string from RAM
}
- rprintf can also print numbers in a variety of ways. The %d special character can be inserted into a string, where it acts as a placeholder for a decimal number. In this case, rprintf takes 2 arguments, the string and then the number to replace %d. rprintf takes as many additional arguments as there are %d characters in the string.
u08 k;
...
rprintf("This is the number one: %d", 1); // print a decimal number
k=2;
rprintf("These are two other numbers: %d %d", k, 4); // print a decimal number from a variable
Similar to %d, %x lets you print hexadecimal numbers and %c lets you print characters.
- rprintfNum lets you print numbers with a variety of options:
// print a formatted decimal number
// - use base 10
// - use 8 characters
// - the number is signed [TRUE]
// - pad with '.' periods
rprintfNum(10, 8, TRUE, '.', 1234);
output will be
+...1234
- There are a number of other rprintf functions and options. See demos 5 and 6 lcdtest.c and vt100test.c for examples.