Jump to content


Photo

Try To Understand Conditional Loop Please Help Me

pic18f4431 single phase ac motor speed c

1 reply to this topic

#1 amardeep

amardeep

    Newbrie

  • Members
  • 1 posts

Posted 18 January 2015 - 05:19 AM

hello 

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.sourceboo...r/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 



#2 JorgeF

JorgeF

    Super Enthusiast

  • EstablishedMember
  • PipPipPip
  • 273 posts
  • Gender:Male
  • Location:ES @ Europe, third rock from the Sun

Posted 20 January 2015 - 09:41 PM

Hi

 

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

 

First step:

(1 << TIMER0_OV_FLAG) 

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

Jorge


Edited by JorgeF, 20 January 2015 - 09:41 PM.




Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users