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 support@ccsinfo.com

where is the problem ?

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



Joined: 07 Sep 2003
Posts: 60
Location: Buenos Aires (Argentina)

View user's profile Send private message Send e-mail MSN Messenger

where is the problem ?
PostPosted: Tue Sep 13, 2005 10:03 am     Reply with quote

using the following code without initialize index
will hung-up the MC(16f877a) only some times, when
local_funtion() is called at firsttime after power-on, any idea ?


// gloabal declaration

const int cursorX[] = {16,13,11,10,8,7,6,5,4,3,16,9};
int index;

//

loacal_funtion(){

int xPostion;
xPosition = cursorX[index];

}
_________________
Best Regards
Daniel H. Sagarra
La Plata (Argentina)
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Tue Sep 13, 2005 10:12 am     Reply with quote

Then initialize RAM at reset so your index variable will be zero.

Or declare it as
Code:
int index=0;

_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: where is the problem ?
PostPosted: Tue Sep 13, 2005 11:51 am     Reply with quote

Calamar wrote:
using the following code without initialize index
will hung-up the MC(16f877a) only some times, when
local_funtion() is called at firsttime after power-on, any idea ?


// gloabal declaration

const int cursorX[] = {16,13,11,10,8,7,6,5,4,3,16,9};
int index;

//

loacal_funtion(){

int xPostion;
xPosition = cursorX[index];

}


const arrays are hangled with return with literals in the w reg. So if index is a random value, what are the chances that the program instruction that it will jump to (call actually) is a retlw? Slim to none and thus explains the problem. Better programming practice would be to do a bounds check to make sure that the index is within the range of cursorX.
Calamar



Joined: 07 Sep 2003
Posts: 60
Location: Buenos Aires (Argentina)

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Tue Sep 13, 2005 3:06 pm     Reply with quote

You both are righ I must should initialize index, verify bounds
and so on, but why the MC get lost ?perhaps mark did it
but I didnt get it
_________________
Best Regards
Daniel H. Sagarra
La Plata (Argentina)
Calamar



Joined: 07 Sep 2003
Posts: 60
Location: Buenos Aires (Argentina)

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Tue Sep 13, 2005 3:06 pm     Reply with quote

You both are righ I must should initialize index, verify bounds
and so on, but why the MC get lost ?perhaps mark did it
but I didnt get it Embarassed
_________________
Best Regards
Daniel H. Sagarra
La Plata (Argentina)
Ttelmah
Guest







PostPosted: Tue Sep 13, 2005 3:42 pm     Reply with quote

The 'key', is in how constant arrays are stored on the 16 family chips. On these, there are no instructions to access the program memory. So how do you store the numbers?. The answer, is the instruction 'RETLW', which returns to the program that called it, with the byte held in the rest of the word holding the instruction. So to access a constant array, what is done, is a subroutine is called, which then adds the index to the current program counter address, and jumps 'into' the array, where there is a RETLW instruction to return the required byte.
If your index is outside the range of the array, you will be jumping to another location in memory, with a return address on the stack, and never return.
CCS, do no bounds checking, because it costs time. Hence you have to either ensure that the value will always be legitimate, or implement your own bounds check.

Best Wishes
Calamar



Joined: 07 Sep 2003
Posts: 60
Location: Buenos Aires (Argentina)

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Sep 14, 2005 10:02 pm     Reply with quote

It`s, clear now, thank you and mark
_________________
Best Regards
Daniel H. Sagarra
La Plata (Argentina)
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