what is the use of empty while loop in embedded programming?

Thread Starter

khatus

Joined Jul 2, 2018
95
what is the use of empty while loop in embedded programming?

e.g while(test expression);

actually i can't understand one line from this code

/*
Generating a delay of 1 ms in PIC18F4550 using Timer1
www.electronicwings.com
*/

#include "osc_config.h" /* Configuration header file */
#include <pic18f4550.h> /* Contains PIC18F4550 specifications */

#define Pulse LATB /* Define Pulse as LATB to output on PORTB */

void Timer1_delay();

void main()
{
OSCCON=0x72; /* Configure oscillator frequency to 8MHz */
TRISB=0; /* Set as output port */
Pulse=0xff; /* Send high on PortB */

while(1)
{
Pulse=~Pulse; /* Toggle PortB at 500 Hz */
Timer1_delay(); /* Call delay which provide desired delay */
}
}

void Timer1_delay()
{
/* Enable 16-bit TMR1 register, No pre-scale, internal clock,timer OFF */
T1CON=0x80;

TMR1=0xf830; /* Load count for generating delay of 1ms */
T1CONbits.TMR1ON=1; /* Turn ON Timer1 */
while(PIR1bits.TMR1IF==0); /* Wait for Timer1 overflow interrupt flag */
TMR1ON=0; /* Turn OFF timer */
TMR1IF=0; /* Make Timer1 overflow flag to '0' */
}


please explain the line "For ex: this type of while loop (without body) is used in wait & signal simaphore. "
 

MrChips

Joined Oct 2, 2009
30,712
An endless loop can be created with the following statement:

while(1);

This may or may not be a typical programming scenario but it is possible. Many embedded systems are event driven, i.e. waiting for something to happen. Furthermore, events are commonly handled by hardware interrupts. Hence there might be nothing for the main program to do until an interrupt occurs.

In low power, portable and battery powered systems, it is very common to put the system into SLEEP, LOW POWER, or STOP mode in order to conserve battery power. In such cases, the program would put the system into a low power mode and then wait for an interrupt to occur.
 

WBahn

Joined Mar 31, 2012
29,978
In the line of code you highlighted, the variable PIR1bits.TMR1IF is changed by the timer. The loop continues until the timer overflows and sets the bit to 1, causing the loop to fail so that the code can continue executing with the next line.
 

ebp

Joined Feb 8, 2018
2,332
It is very common to use an infinite loop in MAIN in embedded C, since the intent is to execute something repeatedly forever. I can't recall ever having used anything else in my C programs for microcontrollers.

I would argue, though with little passion, that a hardware status bit, in this case the overflow flag (which is not used for interrupt purposes in the code example, though it can be), is not a "semaphore" (note spelling) in the usual sense. A semaphore is usually a variable or more elaborate structure that is used for purposes of signaling among processes and is assigned value by those processes. If might be something like a variable Timer1InUse that is set by a process that that is using Timer 1 so other processes won't use it. It looks like the Wikipedia article on semaphores is OK, but there are probably lots of descriptions of semaphores and how to use them to be found on the web. The overflow flag for a counter can be set and cleared by the program, but not usually to communicate with other parts of the program, so I wouldn't refer to it as as semaphore, though others might.

In the example, the loop without a body is simply going to execute continuously until the condition for termination of the while is met. This type of construct is useful and common, but can lead to a program hanging forever in that loop if the condition isn't met, so care is required to be sure the condition has been properly constructed. Depending on debugging tools available, sometimes it is helpful to actually do something with an I/O pin (e.g. toggling) in what would otherwise be an empty loop. This can change timing, but it can also be very useful as a temporary measure to provide clues as to where a program is hanging. If I had an unused I/O or two on the processor for a project, I would sometimes set them up to drive LEDs for this purpose.
 
Last edited:

Thread Starter

khatus

Joined Jul 2, 2018
95
In the line of code you highlighted, the variable PIR1bits.TMR1IF is changed by the timer. The loop continues until the timer overflows and sets the bit to 1, causing the loop to fail so that the code can continue executing with the next line.
There are no statements under while loop.So "The loop continues" means there is nothing to execute.So the program only waits there until TMR1IF flag bit become 1 (which is the required condition for while loop)
 
Last edited:

nsaspook

Joined Aug 27, 2009
13,082
There is no statements under while loop.So the loop continues means there is nothing to execute.So the program only waits there until TMR1IF flag bit become 1 (which is the required condition for while loop)
It does this with XC8 2.0

Code:
512:              while(PIR1bits.TMR1IF==0);
0DA4  A09E     BTFSS PIR1, 0, ACCESS
0DA6  D7FE     BRA 0xDA4
 

WBahn

Joined Mar 31, 2012
29,978
There are no statements under while loop.So "The loop continues" means there is nothing to execute.So the program only waits there until TMR1IF flag bit become 1 (which is the required condition for while loop)
Uh... look at the code you posted. What do you call

TMR1ON=0; /* Turn OFF timer */
TMR1IF=0; /* Make Timer1 overflow flag to '0' */

Even if those statements weren't after the while() loop, the next thing to happen would be for the function to terminate and control to be returned to the next statement after the function call.
 

Thread Starter

khatus

Joined Jul 2, 2018
95
Ok let me clear .

Timer1 starts count from 0xf830.When it reaches 0xffff it makes TMR1IF=1.At the time of counting (i.e from 0xf830 to 0xffff) the empty while loop Wait for Timer1 overflow interrupt flag. When the overflow interrupt flag is high, the empty while loop fails .And the code continue executing the next line i.e
TMR1ON=0; /* Turn OFF timer */
TMR1IF=0; /* Make Timer1 overflow flag to '0' */
 

spinnaker

Joined Oct 29, 2009
7,830
Ok let me clear .

Timer1 starts count from 0xf830.When it reaches 0xffff it makes TMR1IF=1.At the time of counting (i.e from 0xf830 to 0xffff) the empty while loop Wait for Timer1 overflow interrupt flag. When the overflow interrupt flag is high, the empty while loop fails .And the code continue executing the next line i.e
TMR1ON=0; /* Turn OFF timer */
TMR1IF=0; /* Make Timer1 overflow flag to '0' */

You aren't being clear. Is there a question in there?
 
Top