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

Problem with #LOCATE

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



Joined: 10 Jan 2013
Posts: 68

View user's profile Send private message

Problem with #LOCATE
PostPosted: Fri Jan 17, 2014 4:05 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 17, 2014 8:10 pm     Reply with quote

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: 19499

View user's profile Send private message

PostPosted: Sat Jan 18, 2014 2:10 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jan 18, 2014 9:15 am     Reply with quote

If I write "#locate a = 0xA0" before other "#locate"s, it works. Strange. Isn't it?
jeremiah



Joined: 20 Jul 2010
Posts: 1345

View user's profile Send private message

PostPosted: Sat Jan 18, 2014 1:02 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Jan 19, 2014 2:13 am     Reply with quote

Got it.
Thanks guys.
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