Question

I am struggling to understand why I get random data while trying to read from multidimensional table that is stored in rom.

I have a table of bitmap character which I want to display on OLED screen. Table of characters is too big to fit into ram and rom is natural place for it. When I try to read elements from the table, data is not the one stored in the table.

Here is what I try to do. I have bitmap declared as a multidimensional array at the begging of the C-file where it is used:

rom const char number[15][4][20] = { {
{0x00, 0x00, 0x00, 0x00, 0xc0, 0xe0, 0xf0, 0x70, 0x78, 0x38, 0x38, 0x38, 0x38, 0x78, 0x70, 0xf0, 0xe0, 0xc0, 0x00, 0x00},// row 1 columns 19
{0x00,...

This is where I try to read the data and print it to the screen:

for(i=0; i<4; i++)
    {
        PutImage(number[digit][i],20,4,offset,i+2);
    }

Implementation of PutImage function:

void PutImage(char ptr[], unsigned char sizex, unsigned char sizey, unsigned char startx, unsigned char starty)
{
unsigned char _page, _column;

//startx += OFFSET;

OledWriteCommand(0xb0+starty);
OledWriteCommand(startx&0x0F);
OledWriteCommand(0x10 | ((startx>>4)&0x0F));

for(_column=0; _column<sizex; _column++)
{
    OledWriteData(ptr[_column]);
}
}

If I change it so that data fits into ram it works just fine. So the problem has to be either that data is not stored correctly in the first place or the way I used it is incorrect.

The Pic I am using is 18F27J53 and datasheet section 7.1 (Table Reads and Table Writes) talks about some Assembler operations that are used for moving bytes between program memory and ram. As I am using C, i am not sure if that is something I need to be aware of or does the compiler know how to handle that.

Was it helpful?

Solution

Ok related question and its answer got me to (hopefully) right track: Can I make a function that accepts both ram and rom pointers in Microchip C18?

And thanks to Pandrei as well for pointing out that the implementation if PutImage could be the cause. I got the code working by making duplicate function PutROMImage which accepts "near rom char*" -type instead of just "char*" which defaults to ram.

So C18 does not allow pointers to point both ram and rom and PutImage -function parameter defaults to ram. So passing a pointer to array which is located in rom causes pointer point to random values.

I had not noticed this deficiency in the code and compiler wasn't smart enough to complain about that.

OTHER TIPS

having the data in ROM (.txt section) or RAM (.data section) has nothing to do with the problem you are facing.

Assuming that the initialization is correct and you initialize all the elements (if you don't, the elements left out will be default initialized to 0), the problem might be in the function's implementation: PutImage. Since you have problems when the size changes, maybe you have some hard-coded values in there...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top