|
|
View previous topic :: View next topic |
Author |
Message |
Tyler Coen Guest
|
Storing Strings in ROM |
Posted: Tue Jul 02, 2002 1:53 pm |
|
|
<font face="Courier New" size=-1>Hi,
I'm trying to store strings in ROM. Because pointers to constants can't be used, I am having a hard time doing this efficiently. Using a seperate strcpy for each string uses a lot of extra ROM.
Anyone have a good solution for doing this? Here is what I'm using now:
const uint str_stop[5] = "Stop";
const uint str_start[6] = "Start";
#define STR_INDEX_STOP 0
#define STR_INDEX_START 1
void StrToLCD(int strIndex) {
int s[20];
switch(strIndex) {
case STR_INDEX_STOP: strcpy(s, str_stop); break;
case STR_INDEX_START: strcpy(s, str_start); break;
default: s[0] = '\0'; break;
}
// Write s to LCD (not shown)
}
Except I have about 50 strings. I'm using a PIC18f452.
Thanks,
Tyler</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 5300 |
|
|
Eric Minbiole Guest
|
Re: Storing Strings in ROM |
Posted: Tue Jul 02, 2002 3:14 pm |
|
|
<font face="Courier New" size=-1>Here's one way to do it. (It's similar to an approach someone mentioned a while ago, but I couldn't find the original link.) Use #ROM to manually put your strings into the ROM. (Unfortunately, you have to list each character separately, as in #ROM 0x1000 = { 'S', 't', 'a', 'r', 't' } )
After that, you can use the READ_PROGRAM_EEPROM() function to read from the table. The STR_INDEX_xxx's would be the 32bit offsets to the string's start loc. Your StrToLCD function could then be simplified to take directly from ROM and put to the LCD.
I would think this approach would save considerable ROM. Let me know how you make out.
:= I'm trying to store strings in ROM. Because pointers to constants can't be used, I am having a hard time doing this efficiently. Using a seperate strcpy for each string uses a lot of extra ROM.</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 5302 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Storing Strings in ROM |
Posted: Tue Jul 02, 2002 3:41 pm |
|
|
:= I'm trying to store strings in ROM. Because pointers to constants can't be used, I am having a hard time doing this efficiently. Using a seperate strcpy for each string uses a lot of extra ROM.
--------------------------------------------------
Here's a previous thread on the topic. Scroll down to
the bottom, and follow all the links for various sample code.
<a href="http://www.pic-c.com/forum/general/posts/2857.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/2857.html</a>
___________________________
This message was ported from CCS's old forum
Original Post ID: 5303 |
|
|
johnpcunningham Guest
|
Re: Storing Strings in ROM |
Posted: Tue Jul 02, 2002 3:48 pm |
|
|
<font face="Courier New" size=-1>You could use an address pointer to point to each ROM string. Below is just an idea that can be used or modified to write to a port or changed to write out the RS232, I2C, etc.
const char string_1[] = "HELLO";
void write()
{
char i = 0;
// 0x00 means a NULL character i.e. end of string
while (string1[i] != 0x00){
PORTA = string1[i++];
}
</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 5305 |
|
|
R.J.Hamlett Guest
|
Re: Storing Strings in ROM |
Posted: Wed Jul 03, 2002 2:43 am |
|
|
:=<font face="Courier New" size=-1>Hi,
:=
:= I'm trying to store strings in ROM. Because pointers to constants can't be used, I am having a hard time doing this efficiently. Using a seperate strcpy for each string uses a lot of extra ROM.
:=
:=Anyone have a good solution for doing this? Here is what I'm using now:
:=
:=const uint str_stop[5] = "Stop";
:=const uint str_start[6] = "Start";
:=#define STR_INDEX_STOP 0
:=#define STR_INDEX_START 1
:=
:=void StrToLCD(int strIndex) {
:=
:=int s[20];
:=
:=switch(strIndex) {
:= case STR_INDEX_STOP: strcpy(s, str_stop); break;
:= case STR_INDEX_START: strcpy(s, str_start); break;
:= default: s[0] = '\0'; break;
:=}
:=// Write s to LCD (not shown)
:=
:=}
:=
:=Except I have about 50 strings. I'm using a PIC18f452.
:=
:=Thanks,
My solution, is to use 'macro strings', and access these as arrays.
The definition becomes:
const messages[]= {
"Manual\0Setup\0Clock\0Alarms\0Hours\0Minutes\0Cal\0Purge\0Wash\0Sample\0Cal NH3\0Cal Ph\0"
};
You can then have either 'fixed' addresses for the actual message locations in the array - ie.
#define MANUAL 0
#define SETUP 7
#define CLK 13
#define ALARMS 19
or write a piece of 'scanner' code, which looks through the array for the '\0' markers, and puts the next address as the string location into a pointer array.
The latter has a problem at present with the compiler, where it will incorrectly only return one byte of a two byte pointer, in some specific circumstances. So if you have an array of character pointers :
char *msg[40];
It will return a 16bit pointer if you select 'msg[1]', but codes the return as only an 8bit pointer value if you select 'msg[n]', with 'n' being a variable!...
You can 'bodge' round this, by instead asking it to return the value into a long integer, and then casting this into the required pointer.
Obviously the 'fixed location' method, is the smallest, requiring only one set of 'setup' code for a single array, and having no code overhead in the use of the strings.
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 5314 |
|
|
Hans Wedemeyer Guest
|
Re: Storing Strings in ROM |
Posted: Wed Jul 03, 2002 7:10 am |
|
|
I don't think this example will ever work...
Have you tried compiling it ?
___________________________
This message was ported from CCS's old forum
Original Post ID: 5320 |
|
|
johnpcunningham Guest
|
Re: Storing Strings in ROM |
Posted: Wed Jul 03, 2002 12:39 pm |
|
|
Made some modifications to my previous post and it should work for you.
JC
___________________________
This message was ported from CCS's old forum
Original Post ID: 5338 |
|
|
Hans Wedemeyer Guest
|
Re: Storing Strings in ROM... That's better ! |
Posted: Wed Jul 03, 2002 5:45 pm |
|
|
That's better :-)
Would not want to you slack off, just because it's a vacation :-)
___________________________
This message was ported from CCS's old forum
Original Post ID: 5343 |
|
|
johnpcunningham Guest
|
Hans.......get a life you judgemental jerk |
Posted: Thu Jul 04, 2002 8:45 am |
|
|
___________________________
This message was ported from CCS's old forum
Original Post ID: 5356 |
|
|
gruveemaan
Joined: 17 Oct 2003 Posts: 4
|
|
Posted: Fri Oct 17, 2003 3:40 pm |
|
|
Hi JC,
could you please let me know how to get the code.
thanks! |
|
|
burnsy
Joined: 18 Oct 2003 Posts: 35 Location: Brisbane, Australia
|
StringToRom14.exe |
Posted: Sat Oct 18, 2003 1:17 am |
|
|
This might help. I will try and remember how it works....
I needed to to store strings to display on an lcd. All my strings were of fixed length (16 bytes).. If each character was only 7bits long, you fit two in each word. I would store them like this..
// Note Exchange2
#define LCDMSG_PWRUPMSG0 0x00
#rom 0x0100={0x104E,0x37F4,0x32A0,0x22F8,0x31E8,0x30EE,0x33E5,0x1920}
// V1.58
#define LCDMSG_PWRUPMSG1 0x01
#rom 0x0108={0x1020,0x1020,0x1056,0x18AE,0x1AB8,0x1020,0x1020,0x1020}
// INSERT NOTES
#define LCDMSG_INSERTN 0x02
#rom 0x0110={0x1020,0x24CE,0x29C5,0x2954,0x104E,0x27D4,0x22D3,0x1020}
Each msg would have a number, and a fixed starting address. All you do is calc the starting address to retrieve the string.
eg
void
wr_line2lcd(uchar x)
{
uchar u;
uint t;
/* wr lcd direct with byte, with bytes stored in flash */
/* calc offset to msg (16 bytes per msg) */
t = x;
t <<= 3;
t += LCD_MSGS; /* offset to table in rom */
/* copy each byte to lcd_putchar() */
for (u = 0 ; u < 0x10 ; u++ )
{
/* first byte */
x = read_flash(t + u,0);
lcd_putchar(x);
/* second byte */
x = read_flash(t + u,1);
lcd_putchar(x);
}
}
if you want fixed string lenghts or not, here is a handy VB utility I wrote that will allow you to type in a string and it will convert the string into ROM = (xxxxh,xxxxxh....) code for you. Follow the link.
http://home.iprimus.com.au/jtek/pic/stringtorom14.exe
hope this helps _________________ This is the last code change until its ready.... |
|
|
|
|
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
|