Hi, I want to know whether below code will perform expected task or not ?
void test(unsigned char const xdata* buffer);
unsigned char magicString[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
void main(void) { unsigned char i;
while(1) { for(i=0; i<8; i++) { magicString[i] += 1; } test(magicString); } }
void test(unsigned char const xdata* buffer) { unsigned char j;
for (j=8; j>0; j++) { send(*buffer++); // another function } }
Will test() and send() function works ok ? Will send() will send all 8 byte of data of magicString or not ? is there any logic mistake ?
Thanks in advance ?
Will send() will send all 8 byte of data of magicString or not ?
No, it will send 248 bytes of pure garbage. Take a look at the for statement to find out why.
Why not get your answers by running it in the simulator?
That would be overkill.
Though, this is a fairly basic C question and has nothing to do with Keil tools in particular.
Could it be homework ?
Sorry, its a kind of typing mistake...Corrected it:
for (j=8; j>0; j--) { send(*buffer++); // another function } }
I can do it on simulator and it works fine... but while i m running real time, its giving unpredicted behavior... i just wrote a simple modified piece of code, actual code is a bit different, but the piece giving error is that i wrote... do u find anything strange in that ?
I can do it on simulator and it works fine... but while i m running real time, its giving unpredicted behavior... sounds as if you are addressing xdata you do not have (activated). 1) what derivative do you use (all numbers and letters) 2) external RAM? if yes to 2) 3) what RAM (all numbers and letters) 4) what speed RAM? 5) what clock speed the uC? 6) what Vcc the uC, what Vcc the RAM
"That would be overkill."
Posting the questions here would certainly also qualify as overkill.
"...but while i m running real time, its giving unpredicted behavior..."
And that behavior is what, exactly?
behavior is as sometime processor is getting reset as there is nothing in the code which will cause reset. But as i have seen that my idata is getting corrupted somehow with strange pattern every time, so Program Counter is reset to 0x00 (Reset Vector Address)... and strange thing is that it only due to above code snippet... i doubt for the microcontroller....but not confirmed... so i just raised a question that whether is there something wrong to the above piece of code ? Pointer arithmetic is not proper or casting ... ??
Well, i am using a microcontroller with 256 byte internal data memory and 2K XRAM. i am using Large memory model which causes all local/global data stored in XRAM. Speed of uController may not cause this issue ....
Watchdog?
You haven't shown send().
behavior is as sometime processor is getting reset as there is nothing in the code which will cause reset.
Might be a hardware issue. Too little information to make this more than a guess, though.
Well, i am using a microcontroller with 256 byte internal data memory and 2K XRAM.
Would you please be so kind and not make life hard for the people who are trying to help you, free of charge ? Please post the exact model of microcontroller you're using. This information might not mean much to you, but it might be crucial for finding out what's going wrong.
Please post the exact model of microcontroller you're using.
I already posted, but evidently should have highlighted
"1) what derivative do you use (all numbers and letters)"
Erik
Maybe.
Your pointer is to Xdata, but the array is in the defualt space. For large RAM model it will work.
Well, my processor if basically a dual core consist of C51 MPU code and DSP Compute Engine core. But i think the problem is independent of processor, and somehow related to the code generated by Keil. There are 2 WD Timers. One is hardware and one is 8051 s/w. I disabled both, so no issue with rebooting by WD timer reset.
The strange behavior i observed in above code snippet is, when i call the test() function as below, system is misbehaving:
void main(void) { unsigned char i; while(1) { for(i=0; i<8; i++) { magicString[i] += 1; } test(&magicString); // Passing address } } void test(unsigned char const xdata* buffer) {...}
But if i called the test() as below in above code, the system works fine:
test((unsigned char xdata *)&magicString); //Casting test(magicString); // W/o casting
Its hard time for me to understand whats wrong with passing address of magicString i.e. test(&magicString) ?
Can anybody explain me why the last 2 test() work fine and not the first.
"&magicString" is not the address of the first element of the array magicString[]. That's where the error is.
What you actually want is "&magicString[0]" or just "magicString", which both are a pointer to the first element of magicString.