View previous topic :: View next topic |
Author |
Message |
Pekub
Joined: 20 Dec 2005 Posts: 16
|
Pointer to constant |
Posted: Mon Apr 04, 2011 7:42 am |
|
|
Compiler: 4.119
Hello,
I tried a sample for using pointer to constant from CCS web site.
The same is in CCS manual.
This error appears:
*** Error 46 "Bdt12_Main.c" Line 7(16,17): Expecting an =
*** Error 49 "Bdt12_Main.c" Line 14(8,11): Expecting LVALUE such as a variable name or * expression
2 Errors, 0 Warnings.
Code: |
#include <16F1824.h>
const char version[] = "PRODUCT ID V1.01";
const char *ptr;
#zero_ram
void main()
{
// A simple example showing the assignment of a pointer to a constant with the address of a constant string:
ptr = &version[0];
while(TRUE)
;
}
|
What is the problem ?
Thank's |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Mon Apr 04, 2011 9:19 am |
|
|
Read the manual......
It is down to understanding that in the PIC, you have a separate memory space for program memory, and for the RAM. Different addresses. On many of the older chips, there was no hardware ability to actually read a specified location in program memory. So a 'pointer' could not be constructed. This is the CCS default.
Now on modern chips the ability does exist, but for reverse compatibility, and because doing this involves a significant amount of extra code, the _ROM_ data type is used for a constant for which a pointer can be constructed (you can change this with a setting). Alternatively, if you use 'ANSI' for the compiler setup, 'CONST' is no longer stored in ROM, but becomes a 'read only' definition for a RAM variable, for which a pointer can then be constructed, but at the cost of using a lot more RAM....
Best Wishes |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Mon Apr 04, 2011 9:32 am |
|
|
I guess You should assign the value during declaration.. Try this..
const char *ptr= &product[0] |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 04, 2011 12:00 pm |
|
|
I made a test program that uses the "rom" keyword that Ttelmah refers to.
It displays the following output in the MPLAB simulator for vs. 4.119.
This output is correct.
Quote: |
ptr = 0fef
PRODUCT ID V1.01
PRODUCT ID V1.01 |
Code: | #include <16F1824.h>
#fuses INTRC_IO,NOWDT
#use delay(clock=4000000)
#use rs232(baud=9600, UART1, ERRORS)
char rom version[] = "PRODUCT ID V1.01";
char rom *ptr;
void display_string(rom *str)
{
printf("%s \r", str);
}
//=====================
void main()
{
ptr = version;
printf("ptr = %lx \r", ptr);
display_string(version);
display_string(ptr);
while(1);
} |
|
|
|
Pekub
Joined: 20 Dec 2005 Posts: 16
|
|
Posted: Tue Apr 05, 2011 3:11 am |
|
|
Thank you PCM programmer and Ttelmah also. This is what I needed.
In my opinion, use keyword "rom" is not sufficiently explained in the manual. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Apr 05, 2011 3:39 am |
|
|
The 'best' explanation, is the _readme_ with the compiler. This covers things that are relatively recent (though 'ROM' has been used for over a year!), and not fully in the manual. This gives the current syntax for ROM, and the keywords to change between the ROM, and const behaviour.
To work with a PIC, and CCS, you need to use the following bits of paperwork:
The C programming language or a near equivalent.
The chip's data sheet
The CCS manual
The readme.txt, and fuses.txt
The Microchip reference manuals for the specific peripherals and chip versions.
You then 'have a chance'!....
Best Wishes |
|
|
|