|
|
View previous topic :: View next topic |
Author |
Message |
Dale Botkin Guest
|
Another printf question... |
Posted: Sat Dec 01, 2001 12:56 am |
|
|
OK, let's say I have an array of messages:
const char messages[5][10] = {
"Message 1",
"Message 2",
"Message 3",
"Message 4",
"Message 5" };
and I want to select and print one of the messages. Can I do this:
printf(lcd_putc,"\%C\n",messages[x][0]);
or do I need to:
char buffer[10];
memcpy(buffer, messages[x][0], 10);
printf(lcd_putc,"\%C\n",buffer);
Either way it compiles without error messages. I doubt the first way will work, since it's too easy... am I right?
___________________________
This message was ported from CCS's old forum
Original Post ID: 1412 |
|
|
Tomi Guest
|
Re: Another printf question... |
Posted: Sat Dec 01, 2001 10:11 am |
|
|
The first way will work fine. I have just a small note about your code: it would be better to use strings with length of 2^N to help the compiler to calculate the address of the string.
For example, the address calculation and byte access in your case looks like this:
const char messages[5][10] // length: 10 bytes
.................... printf("\%C\n",messages[x][0]);
*
003A: CLRF 77 // "implies a multiplication by 10"
003B: CLRF 78
003C: MOVF 22,W
003D: BCF 03,0
003E: BTFSC 23,0
003F: ADDWF 77,F
0040: RRF 77,F
0041: RRF 78,F
0042: BTFSC 23,1
0043: ADDWF 77,F
0044: RRF 77,F
0045: RRF 78,F
0046: BTFSC 23,2
0047: ADDWF 77,F
0048: RRF 77,F
0049: RRF 78,F
004A: BTFSC 23,3
004B: ADDWF 77,F
004C: RRF 77,F
004D: RRF 78,F
004E: BTFSC 23,4
004F: ADDWF 77,F
0050: RRF 77,F
0051: RRF 78,F
0052: BTFSC 23,5
0053: ADDWF 77,F
0054: RRF 77,F
0055: RRF 78,F
0056: BTFSC 23,6
0057: ADDWF 77,F
0058: RRF 77,F
0059: RRF 78,F
005A: BTFSC 23,7
005B: ADDWF 77,F
005C: RRF 77,F
005D: RRF 78,F
005E: BCF 0A,3
005F: BCF 0A,4
0060: GOTO 0A7
........
00A7: MOVF 78,W
00A8: CALL 004 // access the string
And if you use 16-bytes long strings:
const char messages[5][16] // 16-byte strings
.................... printf("\%C\n",messages[x][0]);
*
0099: SWAPF 21,W
009A: MOVWF 77
009B: MOVLW F0
009C: ANDWF 77,F
009D: MOVF 77,W
009E: CALL 004 // access the string
___________________________
This message was ported from CCS's old forum
Original Post ID: 1413 |
|
|
Dale B. Guest
|
Re: Another printf question... |
Posted: Sat Dec 01, 2001 11:11 am |
|
|
Cool, thanks for the response! On the one hand the compiler didn't complain so I thought it might be OK, but wasn't sure what I would end up with and I haven't breadboarded this yet.
I'm debating what to do about the message length. I'm storing messages for a 2x16 LCD, so I really want 16-character strings, but that makes the array 17 bytes (16 + \0). I'd cut it to 15 characters, but a couple of the messages really need to be 16 characters long... I'll have to play with that some if memory gets tight.
Thanks!
Dale
___________________________
This message was ported from CCS's old forum
Original Post ID: 1414 |
|
|
Tomi Guest
|
Re: Another printf question... |
Posted: Sun Dec 02, 2001 10:27 am |
|
|
<font face="Courier New" size=-1>Here is my code snippet to use 16chr LCDs without that terminating \0.
The definition is not too elegant:
(The "Message 4" is terminated after the "4" character)
const char messages[5][16] = {
{'M','e','s','s','a','g','e',' ','1','a','b','c','d','e','f','g'},
{'M','e','s','s','a','g','e',' ','2','a','b','c','d','e','f','g'},
{'M','e','s','s','a','g','e',' ','3','a','b','c','d','e','f','g'},
{'M','e','s','s','a','g','e',' ','4',0},
{'M','e','s','s','a','g','e',' ','5','a','b','c','d','e','f','g'}
};
These are NOT strings but character arrays (no terminating \0 added automatically). Printing is terminated when either a terminating \0 found or the 16th character is displayed:
void DisplayString(char stringID)
{
char i,what;
for (i=0;i<16;i++) { // max. 16 chars are displayed
what = messages[stringID][i]; // copy the char into RAM to avoid the double access of the ROM
if (!what) break; // Terminating NULL found
lcd_putc(what);
}
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 1421 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|