#include<reg51.h>
sfr port= 0x80; void main() { port=~port; }
This code was successfully compiled...but yu expect the port pins are complemented only once..But what i experiened when used with a simulator software, was the port pins get complemented for infinite times( somthing like the 'port=~port;' instruction was put in a while(1) loop..)
What could be the wrong?
priyan, first of all, why don't you place an infinite loop in main? second, once your main terminates what happens is compiler dependent - it might be that your processor repeatedly resets which is the reason for the behavior you see.
but yu expect the port pins are complemented only once
No. We don't expect that. You do.
We have read enough of the documentation, particularly the tutorials, to know that falling through the end of main() is not typically something one should do in an embedded C program if one doesn't want unexpected things to happen.
Your expectations are.
Then please help me stating what should be the code for my need. The code should complement the port pins only once. Please help me.
You have already been notified that your expectation that it is ok to fall out of main() is ok.
Since it isn't, change your code so that it doesn't end. There are no OS available to catch you, so add an infinite loop.
Even if i add an infinite loop like
while(1) { port1 = ~port1; } The same thing happens(actually that was my problem). It means that i need to make it happen only once. I can do like below
int f=0; while(1) { if(f==0) port1 = ~port1; f++; } This keeps the statement port1= ~port1; happen only once theoratically.But it doesnt actually.
Think about what happens when f gets bigger than an int...
And if you post any more code, put the proper "pre" tags around it so we can read it easier.
while (1) { port1 = ~port1; }
Why do you think that an infinite loop _around_ the code line that inverts the pins should not result in constantly toggling pins?
What do you think happen if you add an empty infinite loop _after_ you have inverted the port pins once?
int f = 0; while (1) { if (f == 0) port1 = ~port1; f++; }
What do you think happens when you keep on incrementing f again and again? What will happen when f reaches the limit for possible values?
By the way - why not post the source code as I do - formatted as code and not inlined in the text?
All of the previous answers tell you why your code is wrong. But the reason that your first post is in an infinite loop is beacuse Keil forces an LJMP to main at the end of your code. This is to prevent the very problem discribed, that is over running your code with no possible exit from main. You can only prevent this by inserting a proper 'forever' loop. Please read and run some of the sample code in the 'Getting Started' manual. This action is well defined in the manual. Bradford