Microchip Technology DM164130-9 User Manual

Page of 101
Lessons
 2012 Microchip Technology Inc.
DS41628B-page 67
EXAMPLE 3-31: 
3.8.7
C Language
3.8.7.1
BOTH 
This version utilizes global variables. Unlike local variables, global variables have no 
function scope, meaning that they are visible to every function within the same source 
file where it is declared. It is good practice to uniquely identify global variables such as 
preceding each variable with an underscore.
This byte is modified in the check_switch function, and the result is returned to the 
main loop. 
EXAMPLE 3-32: 
Notice how the bytes, delay and direction, were declared inside of main. These cannot 
be modified anywhere outside of main. Also, notice how check_switch returns an 
unsigned char byte to the main loop. In ‘C’, only one variable can be returned. 
RotateLeft:
    bcf     STATUS, C   ;clear the carry
    rrcf    LATC,f      ;rotate the LEDs (through carry) and turn on the next LED to the right
    btfss   STATUS,C    ;did the bit rotate into the carry (i.e. was DS1 just lit?)
    bra     MainLoop
    bsf     LATC, 3     ;yes, it did and now start the sequence over again by turning on DS4
    bcf     STATUS, C   ;clear the carry
    bra     MainLoop    ;repeat this program forever
unsigned char _previous_state = SWITCH_UP; //global variable
void main(void) {
    unsigned char delay;
    unsigned char direction;
.....
}
unsigned char check_switch(void){
...
..
}