Flow control for time2 and RB0pin Interrupt

Submitted by lara on

Hi There,

I am tring to understand flow control of folling code got from tutorials for dimmer control was written in Hi. tech c 16f887 mcu with timer2(8bit) and RB0 pin interrupt.

//---------------ARRAY VARIABLE DEFINITION ------------------------------------------------
unsigned char dimmerspeed[1]={0};       
unsigned char dimmerdelay[1]={0};

#define DIMMER_MAX 5
#define DIMMER_1             PORTAbits.RA1
#define ZERO_CROSS         PORTBbits.RB0

//------------------------------------------Initial ISR-------------------------------------------------------
void interrupt intisr(void)                                                      

   
   if(INTCONbits.INTF)               //Check RB0 PIN INTERRUPT FOR ZC FLAG
{
   {
      TMR2=0x0;   
      dimmerdelay[0]=(DIMMER_MAX-dimmerspeed[0])<<1;  
      timer2Isr();
      INTCONbits.INTF   =0;
   }
   
   else (PIR1bits.TMR2IF)               // check the timer2 over flow interrupt flag 
   {
   timer2Isr();                              //FLAG SET 2.048 mSec
   PIR1bits.TMR2IF =0;
   }

}

//-------------------------------------------------------------------------------------------------------------
void main() 
{
  
initHardware();

   while(1)               // wait forever for  the data received and ready  
   {
         

   
      switch(command)   // swich on 
      {
     
   
      case 0x80:                                 //remote key dimmer down
               
               if(dimmerspeed[0]){
               dimmerspeed[0]--;
              
               }
               break;
      case 0x00:                                    //remote key dimmer up
                  
               
               if(dimmerspeed[0]<DIMMER_MAX){
               dimmerspeed[0]++;
               }
               
               break;
      default :    
               break; 
      }//switch (command)
     
   
   }//WHILE
   
}//MAIN

 

//--------------------------------timer2 ISR--------------------------------------------------------------------------------------

void timer2Isr()    // every  2.048 ms
{
   if(dimmerdelay[0])
   {
      DIMMER_1=0;   
      if(!(dimmerdelay[0]&0x80))
         dimmerdelay[0]--;
   }else
   {
      DIMMER_1=1;
      dimmerdelay[0]=0xFF;   
   }
}

What i understand that, after mcu power up, RB0 interrupt has detected under the RB0 ISR>> timer2 is reset at 0 >>dimmerdelay[0] is updated at 0b1010>>timer2isr is called.

 UNDER ISR,

if(dimmerdelay[0])//  is true and entering to block

DIMMER_1=0;   // triac turned off

if(!(dimmerdelay[0]&0x80))// evalue to 0b1111 1111 ie, true.

dimmerdelay[0]--; // WHY it is decrementined ????? and after that if . ------------------------->QUESTION NO 1.

if(dimmerdelay[0]) // is false, ie when dimmerspeed[0] is '5' then

dimmerdelay[0]=0xFF;    // WHY 'FF' ------------------------------------------QUESTION NO 2

How 10/8/6/4/2/0 msec is being related with timer2 interrupt(ie 2.048 msec) after detecting INTF at RB0.

tnx for reply..