2.4. PIC16 C Sequence Control

• While loops

• Break, continue, goto

• If, else, switch

Conditional branching operations are a basic feature of any program. These must be properly organized so that the program structure is maintained and confusion avoided. The program then is easy to understand and more readily modified and upgraded.

While Loops

The basic while (condition) provides a logical test at the start of a loop, and the statement block is executed only if the condition is true. It may, however, be desirable that the loop block be executed at least once, particularly if the test condition is affected within the loop. This option is provided by the do..while(condition) syntax. The difference between these alternatives is illustrated in Figure 2.7. The WHILE test occurs before the block and the DO WHILE after.

Figure 2.7. Comparison of (a) While and (b) Do..While Loop


The program DOWHILE shown in Listing 2.9 includes the same block of statements contained within both types of loop. The WHILE block is not executed because the loop control variable has been set to 0 and is never modified. By contrast, ‘count’ is incremented within the DO WHILE loop before being tested, and the loop therefore is executed.

Listing 2.9. DOWHILE.C Contains Both Types of ‘While’ Loop

// DOWHILE.C

// Comparison of WHILE and DO WHILE loops

♯include “16F877A.H”

main()

{

int outbyte1=0;

int outbyte2=0;

int count;

// This loop is not executed ..............

count=0;

while (count!=0)

{ output_C(outbyte1);

outbyte1++;

count−;

}

// This loop is executed...................

count=0;

do

{ output_C(outbyte2);

outbyte2++;

count-−;

} while (count!=0);

while(1){};

}

Break, Continue, and Goto

It may sometimes be necessary to break the execution of a loop or block in the middle of its sequence (Figure 2.8). The block must exited in an orderly way, and it is useful to have the option of restarting the block (continue) or proceeding to the next one (break). Occasionally, an unconditional jump may be needed, but this should be regarded as a last resort, as it tends to threaten the program stability. It is achieved by assigning a label to the jump destination and executing a goto..label.

Figure 2.8. Break, Continue, and Goto


The use of these control statements is illustrated in Listing 2.10. The events that trigger break and continue are asynchronous (independent of the program timing) inputs from external switches, which allows the counting loop to be quit or restarted at any time.

Listing 2.10. Continue, Break, and Goto

// CONTINUE.C

// Continue, break, and goto jumps

♯include “16F877A.H”

♯use delay(clock=4000000)

main()

{

int outbyte;

again: outbyte=0; // Destination of goto

while(1)

{

output_C(outbyte); // Foreground operation

delay_ms(10);

outbyte++; // Increments Port C

if (!input(PIN_D0)) continue; // Skip other tests if input 0 low

if (!input(PIN_D1)) break; // Terminate loop if input 1 low

delay_ms(100); // Debounce inputs

if (outbyte==100) goto again; // Restart at 100

}

}

The goto againis triggered by the count reaching a set value, which could be better achieved by using the While condition. In a more complex program, exiting a function in this way risks disrupting program control, since the function is not properly terminated. The significance of this should become clearer when functions are analyzed later.

If..Else and Switch..Case

We have seen the basic if control option, which allows a block to be executed or skipped conditionally. The elseoption allows an alternate sequence to be executed, when the ifblock is skipped. We also need a multichoice selection, which is provided by the switch..casesyntax. This tests a variable value and provides a set of alternative sequences, one of which is selected depending on the test result.

These options are illustrated in flowchart form in Figures 2.9 and 2.10, and the if..elseand switch..casesyntax is shown in Listing 2.11. The control statement switch(variable)tests the value of the variable used to select the option block. The keyword case n:is used to specify the value for each option. Note that each option block must be terminated with break, which causes the remaining blocks to be skipped. A default block is executed if none of the options is taken.

Listing 2.11. Comparison of Switch and If..Else Control

// SWITCH.C

// Switch and if..else sequence control

// Same result from both sequences

///////////////////////////////////////////////////////////////////////

♯include “16F877A.h”

void main()

{

int8 inbits;

while(1)

{

inbits=input_D(); // Read input byte

switch(inbits) // Test input byte

{

case 1: output_C(1); // Input=0×01, output=0×01

break; // Quit block

case 2: output_C(3); // Input=0×02, output=0×03

break; // Quit block

case 3: output_C(7); // Input=0×03, output=0×07

break; // Quit block

default:output_C(0); // If none, output=0×00

}

if (input(PIN_D0)) output_C(1); // This block same effect

if (input(PIN_D1)) output_C(2);

if (input(PIN_D0) && input(PIN_D1)) output_C(7);

else output_C(0);

}

}

Figure 2.9. Comparison of (a) If and (b) If..Else


Figure 2.10. Switch..Case Branching Structure


The same effect can be achieved using if..else, but switch..caseprovides a more elegant solution for implementing multichoice operations, such as menus. If the case options comprise more than one statement, they are best implemented using a function block call, as explained in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.198.12