Jump to content

Try To Understand Conditional Loop Please Help Me

Recommended Posts


i am try to understand some conditional loop in the code which is written by Dan McFarland (Speed control for a single-phase AC motor using Microchip's Picdem MC demo board and BoostC compiler




code located at http://www.sourceboost.com/Products/BoostC/ExampleCode/cmotor/cmotor.c.html

// FLAGS bits
#define TIMER0_OV_FLAG 0
#define INC_INDEX1_FLAG 4
#define INC_INDEX2_FLAG 5

// FLAGS1 bits
#define DEBOUNCE 0
#define KEY_RS 1
#define KEY_PRESSED 3
#define RUN_STOP 4
#define RESET 5
#define FREQ_UPDATE 6

UCHAR flags ; // res 1 ;Flags registers used to indicate different status
UCHAR flags1

if ( flags & (1<<TIMER0_OV_FLAG) ) // back from Timer0 overflow? btfss flags, TIMER0_OV_FLAG
Update_PWM_Dutycycles(); // Yes, update the PWM duty cycle with new value
Update_Table_Offset(); // Update offsets
flags &= ~(1<<TIMER0_OV_FLAG); // Clear the flag

what it does mean ???

if ( flags & (1<<TIMER0_OV_FLAG) ) // back from Timer0 overflow? btfss flags, TIMER0_OV_FLAG

please help me

Link to post
Share on other sites



The expression is testing for a value of "1" in the TIMER0 overflow flag.


First step:


TIMER0_OV_FLAG is defined as "0", the bit position for the Timer0 overflow flag.

The "1" shifted left "0" places produces a constant with bit 0 = "1" and all the remaining bits="0".


Second step:

Use the value obtained from the previous step to mask all bits in the flags register except the Timer0 oveflow flag by means of a bitwaise AND operation.


The result is "0" or <>"0" depending on the value of the timero0 overflow flag.




For a beginner, in this specific case it may be seem, strange the idea of a "(1 << 0)" that yields a "1", but if the flag of interest was, for example, in bit "3" the shift operation would yield a value of "8" and the bitwaise AND would isolate bit "3" of the flags register.



Is as simple as to look at each operator at a time taking into account the "C" operators precedence..



Best regards


Edited by JorgeF
Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...