This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

PROBLEM WITH THE C CODE

#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?

Parents Reply Children
  • 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