Why is main() an infinite loop itself :(

Status
Not open for further replies.

newbee

Newbie level 3
Joined
Mar 17, 2006
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,325
hi

i m studying 8051 with keil and wro a program without an infinite loop, but the simulation on proteus keeps on running, the program repeats itself

here is the code (very simple): i expect nothing even if i press the button sinceit will run and finish very fast


#include <REGX51.H>
#include <stdio.h>

#define button P1_0 // definitions//
#define led P1_1

void main(void) // main program//
{
button=0; // initials //
led=0;


while (button) //when button pressed//
{
led =!led; // complement the led //
while(button) //wait until release the button//
{ }
}

}
 

#include <REGX51.H>
#include <stdio.h>

#define button P1_0 // definitions//
#define led P1_1
bit exit;

void main(void) // main program//
{
button=0; // initials //
led=0;

while (button==0) //when button NOT pressed//
{ }


exit=0;
while ( exit==0 )
{
led =!led; // complement the led //
while(button==1) //wait until release the button//
{ }
if (button==0) exit=1; //release the button//

}

}
 




both programs are working in the same way

i) button on.........led on
ii) button off.........led off
iii) loop this forever

but i expect nothing from my program since it will finish untill i press the button and not retun to top again.
 

Hi Newbee,

I don't use keil that much, and I don't use the '51, but I can answer your question, I think. You will need to take a look at the listing file your compiler gives you to see exactly what is happening in your code.
Yes, it should complete in a few micro seconds, and that is it. However, some compilers put a sleep instruction at the end, if your code does not loop. This is to prevent the program from running on, into undefined memory locations. If you don't tell your program how to end, then the program counter just keeps right on incrementing, off into uncharted memory.
So the idea of a 'super loop' is quite common on micros. You initialize, then put a lable, your program, then send it back to the lable, and it keeps on forever.
Now, I don't know how keil handles it, so if you look at your listing file (and you may have to tell the compiler to do this, in your compiler preferences settings,) you should see the equivelent instructions in assembly. This will tell you how your code is being handled.
Hope this is clear, it is very late at night where I am, and I'm bagged. If you have questions, i'll try to help you.
Regards,
Robert
 

Why don't you debug that program first in keil?...it will help you sloving that porblem easily.
 

hi BeeBop

thankyou for your help, i found the answer:

our program should have an infinite loop since it has no operating system to return to. if we have no super loop the program will cycle until the power is removed from it
 

Hi newbee,

you right! your program will cycle with infinite loop.

if you want your program run only once.you have to add self jump when finish your job: etc. while(1);
 

beamrobot is correct , and i suggest you set button =1 before read the buttion,it is because of the hardware!
 

For every embedded application you need infinite loop.

Why? Beacuse without software infinite loop very soon after reset your controller will execute all instructions of your application. After that controller will execute nop or some other instructions at the end of your application code. After execution all of this instructions controller will start execution at reset point again. So you will have long unwanted hardware loop instead simple software infinite loop

So if you want embedded application with predictable countinous work you need software infinite loop. You can implement this loop on different ways with assembler or C.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…