Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to put text strings in PIC ROM using C??

Status
Not open for further replies.

Mr RB

Well-Known Member
Hi, I'm working on a small application using a 12F675 and a serial LCD. The PIC only has 64 ram and 1kb ROM. I can't use a bigger PIC due to special circumstances.

I need to store a number of text strings in ROM efficiently and access them with minimal code size. When I used to do LCD routines in assembler I would store the text strings in PIC ROM doing something like this;

Code:
ORG 0x300
 dt = {"Up","Down"}

Which gave a result something like this;

Code:
ORG 0x300
 retlw 'U'
 retlw 'p'
 retlw 0x00
 retlw 'D'
 retlw 'o'
 retlw 'w'
 retlw 'n'
 retlw 0x00

So the text strings are all stored using just 1 ROM per character (Plus a NULL) and can be read from a table read (an indirect addressed call).

What is the simplest way to do this in C?? Or is there another simple way to store text strings using just 1 ROM per character? I don't mind using some inline assembler in the C code and I can adapt an assembler table read function easily enough.

It's the storing part that has me puzzled. In MikroC I can use something like;

Code:
void messages (void) ORG 0x300
{
  asm retlw 85;  // U
  asm retlw 80;  // p
  asm retlw 0;  // NULL
}

But that is truly ugly code! :( Does anyone have a nice solution for this?
 
Thanks Pommie! I did try that but the ROM and rom keywords were not supported.

But after reading up I found that in MikroC it's done like this;
const char blah[6] = "Hello";

and in MikroC PRO like this;
const char code blah[6] = "Hello";

And I checked the asm output it uses retlw's in ROM so that part is solved.

I still can't use any of the MikroC inbuilt LCD routines to access the string, or my own string routines, I get a pointer type error which is to be expected I suppose.

I did try some tests like
thechar = blah;
which compiles to about 27 instructions which is not too disasterous allowing for all the compiler guff that has to work on every pic and every ROM page.

It does let me specify const char* type as a function argument so it was easy to code up a new LCD_String_Out() function. Still larger than I would like at 49 ROM but the option is there to put all the messages in 1 string and force it to an absolute ROM location, then use a few asm instructions to read the table, hopefully it won't come to that I've still got about 30 ROM locations left hehe.

Thanks again! :)
 
FYI, BoostC uses 7 instructions per blah[x] access. However, one of them is a call to a system routine and so your first blah[x] adds 76 instructions.

Mike.
 
Yeouch! I don't feel so bad about the 27 instructions now. In MikroC it is about 19 insts for the =blah[x] then another 8 for the common code for the table call. :)
 
Actually, I think I was a little unkind to BoostC. On a 629 it appears to add 24 words to the rom space. I didn't realize that it optimized out the actual string when not used.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top