View previous topic :: View next topic |
Author |
Message |
notbad
Joined: 10 Jan 2013 Posts: 68
|
Problem with #LOCATE |
Posted: Fri Jan 17, 2014 4:05 pm |
|
|
There is a function that accepts an array pointer as a parameter.
I want to pass a variable address to it and I need some other variables to be stored right after that in ram.
I used #locate to locate variables b,c,d after a.
And I expect the following program to print:
0123456789ABCDEF
but it prints:
012355AB338808DF
What am I doing wrong?
Code: |
#include <16F873A.h> //ccs v4.130
#FUSES NOWDT, HS, PUT, NOBROWNOUT, NOLVP
#use delay(clock=4000000)
#use rs232(BAUD=9600,uart1)
int16 a=0x0123;
int16 b=0x4567;
int16 c=0x89AB;
int16 d=0xCDEF;
#locate b = a+2
#locate c = a+4
#locate d = a+6
int16 m=0x55AB;
int16 n=0x3388;
void send_ram(int8 count, int16 *data)
{
int8 i;
for(i=0; i < count; i+=2)
{
printf("%X" , make8(*data,1));
printf("%X" , make8(*data,0));
data++;
}
}
void main()
{
setup_adc_ports(NO_ANALOGS);
output_high(pin_c5);
send_ram(8,&a);
printf("\r\n" );
while(1);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 17, 2014 8:10 pm |
|
|
Quote: | int16 a=0x0123;
int16 b=0x4567;
int16 c=0x89AB;
int16 d=0xCDEF;
#locate b = a+2
#locate c = a+4
#locate d = a+6
int16 m=0x55AB;
int16 n=0x3388; |
I've never seen #locate used this way. I'm not sure the compiler even
accepts it.
Look at the .SYM file that you get from your program. The compiler
is re-using the memory addresses that were assigned to b and c.
It's using them for m and n, as well, which is probably not what you want.
Quote: |
024-025 a
026-027 m
026-027 b
028-029 n
028-029 c
|
I have always seen #locate used with a constant value (like 0x50), or
a getenv() register address. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19568
|
|
Posted: Sat Jan 18, 2014 2:10 am |
|
|
As an 'add on' to PCM programmers comment, the manual says it all. For the address required by 'locate', it says:
"x is a constant memory address"
Keyword - 'constant'.....
However for #byte, you have:
"x is a C variable or a constant"
However you then need to also reserve the address range used, or m & n, will be put into the same are.
So best sequence is:
1) #reserve a memory area for the variables.
2) Place a at the start of this with #byte.
3) Then place the other variables 'incrementally' using #byte.
Best Wishes |
|
|
notbad
Joined: 10 Jan 2013 Posts: 68
|
|
Posted: Sat Jan 18, 2014 9:15 am |
|
|
If I write "#locate a = 0xA0" before other "#locate"s, it works. Strange. Isn't it? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Sat Jan 18, 2014 1:02 pm |
|
|
Depends on how the compiler is designed. In your original, the variable "a" could have had any memory address so it wasn't "constant" from a memory location perspective. By #locating "a" to a constant location, the compiler may be able to further locate the other variables correctly now. Just speculating mind you. I don't know that for sure. |
|
|
notbad
Joined: 10 Jan 2013 Posts: 68
|
|
Posted: Sun Jan 19, 2014 2:13 am |
|
|
Got it.
Thanks guys. |
|
|
|