CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Array as function parameter

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
georpo



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

Array as function parameter
PostPosted: Mon May 28, 2018 12:35 am     Reply with quote

Hello,
I am making a function which draws a BMP on OLED.
Of course BMP data is stored in arrays like:
Code:

const char table1[]={0,1,2,3,4,5,6,7,8,9,etc,etc,etc}; //Bitmap data 1
const char table2[]={0,1,2,3,4,5,6,7,8,9,etc,etc,etc}; //Bitmap data 2
const char table3[]={0,1,2,3,4,5,6,7,8,9,etc,etc,etc}; //Bitmap data 3

Now each time I call the DrawBMP function I want to pass the array name as parameter so the fucntion will know where the proper data is.
Code:

void DrawBMP(SourceArray){

    int i=0;

    for(i=0;i<10;++i) data=SourceArray[i]; // example code with no use
}


Example to call the function:
Code:

DrawBMP(table1);
or
DrawBMP(table2);
or
DrawBMP(table3);

Of course the above does not work.
It is just what I want to do.

I have it already working with multiple IFs but this creates big size code for no reason.

Any good thought?

Thanks!
_________________
George.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 28, 2018 12:55 am     Reply with quote

You can do it this way. MPLAB vs. 8.92 simulator displays the following
output in the Output window:
Quote:

00 01 02 03 04 05 06 07 08 09

10 11 12 13 14 15 16 17 18 19

20 21 22 23 24 25 26 27 28 29

Test program:
Code:

#include <18F46K22.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT   
#use delay(clock=4M)
#use rs232(UART1, baud=9600, errors)

rom char table1[]={0,1,2,3,4,5,6,7,8,9};
rom char table2[]={0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19};
rom char table3[]={0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29};
 
void DrawBMP(rom *SourceArray)
{
int8 i=0;
int8 data;

for(i=0;i<10;++i)
   {
    data = SourceArray[i];
    printf("%x ", data);
   }

printf("\n\r");
}


//======================================
void main(void)
{
 DrawBMP(table1);
 DrawBMP(table2);
 DrawBMP(table3); 

 while(TRUE);
}
georpo



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

PostPosted: Mon May 28, 2018 1:14 am     Reply with quote

OK, I got it. I will try it right away.

But what is the proper 'non CCS' pointer way to do it?

Thanks!
_________________
George.
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Mon May 28, 2018 3:23 am     Reply with quote

Exactly the same, except leave out the 'ROM'. The difference then is that the array is then stored in RAM...
Key is that what is being passed is the address of the data. Hence the *. This is the key thing missing in your original post.
georpo



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

PostPosted: Mon May 28, 2018 4:48 am     Reply with quote

But I want all arrays to be store in ROM.
That is why they are const type.

Thanks!
_________________
George.
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Mon May 28, 2018 6:21 am     Reply with quote

Yes, so use ROM *.

There is not a 'non CCS' way to handle data in ROM.
Understand that 'normal' processors have a single address space. Linear addressing of the ROM and RAM. The PIC instead uses a Harvard architecture, with two completely separate address spaces for the ROM and RAM. So there is an 'address 0' in RAM, and another 'address 0' in ROM. Hence a pointer _has_ to be told which memory space to use.
const types can't have a pointer constructed to them. Rom types can (this is the big difference). So use rom instead of const, and rom * in the function.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Tue May 29, 2018 9:54 am     Reply with quote

georpo wrote:
But I want all arrays to be store in ROM.
That is why they are const type.


Const does not mean "store in ROM". For a start most processors don't have anything like ROM. Const means various things in C, perhaps most often "cannot be written to". The meaning is allowed to vary from implementation to implementation as required.

On PICs there is a something like ROM: the program flash memory. It is a totally different memory in a totally different address space from RAM. The two cannot mix: a pointer can only be for one or the other.

Read-only data can be implemented a number of ways on PICs. It can be in flash and read as required with special flash reading code. This is what the ROM keyword does. It can be in RAM but copied in from flash at startup and the compiler makes sure it doesn't write to it; though it is possible to write to it with bad pointers. Const can give this, depending on other options. It can also be copied from flash into temporary RAM just before it is used. This is what "PASS STRINGS IN RAM" does; for strings at least. There are other ways to implement read-only data too. All have some catch, often using RAM.
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Tue May 29, 2018 10:21 am     Reply with quote

Sorry, but wrong.

ROM in CCS does mean to store in ROM. The program memory is a read only memory, so is a ROM. The 'special flash code', is _programming_ code....

In CCS, provided you are running in standard mode, ROM means exactly the same as CONST, except the extra data is added to allow pointers to be constructed to the variable.

PASS_STRINGS, is great way of having const's, which can be accessed by a pointer, but this only works for strings, not general data arrays.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed May 30, 2018 5:07 am     Reply with quote

Ttelmah wrote:
Sorry, but wrong.

ROM in CCS does mean to store in ROM. The program memory is a read only memory, so is a ROM. The 'special flash code', is _programming_ code....


Sorry, but that is what I wrote! I also remember the time when PICs didn't have flash and it was ROM, either OTP or UV erasble.

It may be a habit, but if you shut the door in my face that hard again I shall simply not comment further, allowing you to dominate this forum because I simply don't have the time or energy to spend trying to help others in the face of such hostility :-|
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Wed May 30, 2018 8:19 am     Reply with quote

Sorry, but you said
Quote:

Const does not mean "store in ROM".


In CCS it does mean exactly that.

Historically CCS introduced the keyword 'const' long before ANSI used it. It was used as a keyword to say to store the 'variable' in ROM.

This is still the meaning in CCS, unless you select 'ANSI', or explicitly change the const setting.
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed May 30, 2018 9:22 am     Reply with quote

It's one of those 'What's in a name' and 'standards' things....
ROM ( Read Only Memory ) kinda doesn't exist in 'modern' PICs or other micros as you can erase the data,one way or another, so the data is not 'burned forever' unlike the very early PICs and say FPLAs like the 8223. In the latter, you physically burned the Ni wire 'links'..a one shot deal. If you didn't code correctly the device was 'silicon trash'.
The other problem is who decides what the name means, aka who sets the 'standards'. Long, long time ago RS-232 generally meant a 25 pin connector (aka DB25) , then the 'standards' guys came to use a DE-9 connector for 'RS-232'. Not only did the connector size shrink, 'they' reveresed purposed' pins 2 and 3 !!
Today's PICs have several areas of memory. RAM, EPROM, EEPROM, Config fuses, registers to name a few. All areas can be accessed,some easy, some a tad harder and depending on how you learned microcomputers, well..some say tomatoe say tomato.

Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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