|
|
View previous topic :: View next topic |
Author |
Message |
opvini
Joined: 27 Apr 2012 Posts: 50 Location: Brazil
|
Problem with a multidimensional matrix and pointer |
Posted: Fri May 11, 2012 2:01 am |
|
|
Hello,
I am having problem with a pointer to point to a multidimensional matrix using CCS.
This code dosnt work, why?
Code: |
const unsigned char matrix[2][2] = {1,2,3,4};
int *pointer;
pointer = &matrix;
|
I think I cant do this becouse the matrix is a "const".
But my matrix is very large, 97x8, if it is "const" I spent just 1% of RAM, but is it isnt a const, I spent 51% of RAM o.O
What can I do? |
|
|
opvini
Joined: 27 Apr 2012 Posts: 50 Location: Brazil
|
|
Posted: Fri May 11, 2012 2:21 am |
|
|
I need use a pointer, because I need to point to different matrix and I need to do operators with its address...
I read about the "ROM", like "int rom matrix[][]" but I don't understand very well. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri May 11, 2012 3:22 am |
|
|
just use the keyword 'rom', where you would otherwise use the keyword 'const'. So:
Code: |
rom unsigned char matrix[2][2] = {1,2,3,4};
|
The difference is that historically you couldn't construct a pointer to the PIC ROM. Most of the early chips didn't support an ability to access a specific 'cell' in ROM, so the constant array was simulated, by creating a program, which returned the required byte, when given an index. However later chips do allow a ROM byte to be accessed this way, so CCS added the ability with the 'rom' keyword. Data stored as 'rom', is basically exactly the same as const, but in the former case the extraction code is still added as a header in front of the data, while in the latter case, a slightly more bulky extraction code is used, which supports the handling of pointers.
However your pointer also needs to be told it is to the ROM. Remember that with the PIC, the ROM is in a completely separate memory space to the RAM. So the pointer declaration needs to be:
Which says that 'pointer' is a pointer to an integer stored in the rom space.
Best Wishes |
|
|
opvini
Joined: 27 Apr 2012 Posts: 50 Location: Brazil
|
|
Posted: Fri May 11, 2012 12:32 pm |
|
|
I did:
Code: |
rom unsigned char matrix[] = {1,2,3,4};
rom int *pointer;
pointer = &matrix;
printf("%d %d", *pointer, *(pointer+1));
|
and doesnt work with PIC18F4520... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri May 11, 2012 12:53 pm |
|
|
Much more important, what compiler version?.
Also try 'dereferencing' the value yourself, so declare an int variable, read the value into this, and then print it. It does work, but has to be used 'with care'.
Best Wishes |
|
|
cfernandez
Joined: 18 Oct 2003 Posts: 145
|
ROM or CONST Pointer |
Posted: Sat May 12, 2012 8:20 am |
|
|
Is possible use memcpy with a ROM POINTER ?
For example:
rom unsigned char matrix[4] = {1,2,3,4};
rom char *pointer;
char aux[2];
pointer = &matrix;
memcpy( aux, &pointer[ 2 ], 2 );
printf( %02X %02X\n\r", aux[ 0 ], aux[ 1 ] );
I test this and dont work.
I use the last version of compiler.
Best Regards, |
|
|
opvini
Joined: 27 Apr 2012 Posts: 50 Location: Brazil
|
|
Posted: Sat May 12, 2012 11:05 am |
|
|
I am using the CCS 4.078 and PCD 4.068...
I tryed:
Code: |
int a,b,c;
a = *pointer; b=*(pointer+1); c=*(pointer+2);
printf("%d %d %d",a,b,c);
|
and doesnt work too... |
|
|
cfernandez
Joined: 18 Oct 2003 Posts: 145
|
|
Posted: Sat May 12, 2012 11:26 am |
|
|
My problem is that I have a large ROM Array and I need put in RAM for modify some values.
That's all. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat May 12, 2012 11:32 am |
|
|
With PIC24, ROM can be mapped to data space by using the PSV (program storage visibility) feature. In CCS PCD, it's supported starting with V4.120.
PCD V4.068 is a very old and very buggy version that can't be suggested for serious programming purposes. |
|
|
opvini
Joined: 27 Apr 2012 Posts: 50 Location: Brazil
|
|
Posted: Sat May 12, 2012 11:36 am |
|
|
FvM thanks a lot!
I will update my CCS...
After, do you think doing this I will can acess the ROM, and the code will works? |
|
|
cfernandez
Joined: 18 Oct 2003 Posts: 145
|
|
Posted: Sat May 12, 2012 11:40 am |
|
|
I have the last version and not work this feature.
And in PIC24 not work this
Code: | char s[5];
memcpy( s, "\x01\x02", 2 ); |
and in PIC18 yes!!!
Is very crazy...... |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun May 13, 2012 3:58 am |
|
|
I checked in PCD V4.132 and can't confirm your observations.
All below memcpy actions are working correctly. Not actually expectable, string constants embedded in the code, either with memcpy(), strcpy() or printf() are still using legacy table read instructions. You need to separate the constant string from the copy instruction to take advantage of the more effective PSV methode.
Code: | #device psv=16
char s[5];
rom char c[2] = {"\x01\x02"};
rom char *pr = c;
memcpy( s, "\x01\x02", 2 ); // Is using slow tlbrd instead of PSV!
memcpy( s, c, 2 );
memcpy( s, pr, 2 ); |
Code that should work with PSV according to the syntax in readme.txt, but actually doesn't.
Code: | #device psv=16
char s[5];
rom char c[2] = {"\x01\x02"};
rom char * rom rpr = c;
memcpy( s, rpr, 2 );
pr = rpr;
memcpy( s, pr, 2 ); |
Without PSV in effect, memcpy(s,c,2) and memcpy(s,pr,2) are causing address error traps, as expectable.
Apart from the missing support for embedded constant strings, PSV is working as expected. It's usage is strongly suggested for effective utilisation of the PIC 24 processor. It's mostly abandoning the endless confusion with const strings in PCD.
For the present implementation, see below the PSV related documentation in readme.txt. Unfortunately the feature still isn't listed in the online help more than one year after it has been implemented.
Code: | PSV support for 16-bit PICs has been added:
#device PSV=16
When used, all 'rom' and 'const' keywords are fit into a PSV page of the
program memory, and the contents of the 'rom and 'const' structure/array
are made PSV friendly. 'const' or 'rom' pointers can be assigned to RAM
pointers and still allow access to the data in the program memory.
Example:
#device PSV=16
const char constString[] = "Hello";
const char *constPtr = constString; //constPtr points to program memory in PSV
char *ramPtr = constString; //ramPtr points to program memory in PSV
printf("%s", constPtr);
printf("%s", ramPtr); |
Users of other embedded compilers (e.g. from Microchip) may possibly expect the below construct to work, but it's causing an AE trap with PCD.
Code: | rom char *str = {"Hello new world"};
strcpy(s,str2); |
Best regards,
Frank |
|
|
opvini
Joined: 27 Apr 2012 Posts: 50 Location: Brazil
|
|
Posted: Sun May 13, 2012 6:57 pm |
|
|
I update my CCS and now I can read ROM memory without problems, but now I am having other strange problem:
Code: |
rom int matrix[] = {0x00,0x00,0x60,0x10,0x70,0x90,0x78,0x00};
rom int *pFonte;
int i;
pFonte = &matrix;
for(i=0; i<8; i++){
printf("\f%d) %d\n%d %ld", i, *(pFonte+i), i, (pFonte+i));
delay_ms(2000);
}
|
And the printf() is printing: 0, 0, 96, 16, 112, -112, 120, 0
The 0x90 = 144 is printing -112, why????
Thanks a lot! |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1346
|
|
Posted: Sun May 13, 2012 7:10 pm |
|
|
In printf, %d is a signed value. 0x90 is -112 in 2's compliment signed values. Try %u if you want unsigned. |
|
|
opvini
Joined: 27 Apr 2012 Posts: 50 Location: Brazil
|
|
Posted: Sun May 13, 2012 7:32 pm |
|
|
Thanks jeremiah, it works now!
But if I need to use this value with one variable, I just need to create one unsigned int, and the value will be 144?
Code: |
unsigned int tmp;
tmp = *pFonte;
|
because I test like this, using the printf() with %d to show the tmp variable, and shows -112 again, but using %u shows 144!
But I need use this value with another process... Using unsigned int it will work, right? |
|
|
|
|
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
|