|
|
View previous topic :: View next topic |
Author |
Message |
Calamar
Joined: 07 Sep 2003 Posts: 60 Location: Buenos Aires (Argentina)
|
where is the problem ? |
Posted: Tue Sep 13, 2005 10:03 am |
|
|
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
|
|
Posted: Tue Sep 13, 2005 10:12 am |
|
|
Then initialize RAM at reset so your index variable will be zero.
Or declare it as _________________ 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
|
Re: where is the problem ? |
Posted: Tue Sep 13, 2005 11:51 am |
|
|
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)
|
|
Posted: Tue Sep 13, 2005 3:06 pm |
|
|
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)
|
|
Posted: Tue Sep 13, 2005 3:06 pm |
|
|
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) |
|
|
Ttelmah Guest
|
|
Posted: Tue Sep 13, 2005 3:42 pm |
|
|
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)
|
|
Posted: Wed Sep 14, 2005 10:02 pm |
|
|
It`s, clear now, thank you and mark _________________ Best Regards
Daniel H. Sagarra
La Plata (Argentina) |
|
|
|
|
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
|