Problems in RB0 PIn Interrupt

saj's picture
saj

Offline

Joined: Sep 28, 2018

Replies: 9
Problems in RB0 PIn Interrupt
October 28, 2018 - 10:45pm

hi there,

I am trying to hardware implement with the PIC2520 MCU/External osc fr. 20mhz with the code below for remote control triac-dimmer and ON/OFF control-relay, the all relays is being control successfully from the remote with specified key BUT when its come to about the operation triac dimmer, the light-Bulb connected to triac 

1) Start flickering when power is ON, which sould be remain 'speed' variable=5; at OFF conditon, as getting from 'delay loop' about 2 msec as tested in proteus, ie, 2*5=10msec delay on power up the system.>>>How do to stop the flickering ??

2) After power up and remain on flickering state as above when any botton including botton used for realays if pressed: the bulb getting OFF and then ON on pressing any botton, >>> what  is to be edit in programme.

PROGRAMME::::::::::::::::::::

void delay(int time)  
{
    for(int i=0;i<time;i++)
        for(int j=0;j<800;j++);
}

 

#define tric PORTBbits.RB1
#define ir PORTBbits.RB2

#define relay1 PORTCbits.RC2
#define relay2 PORTCbits.RC3
 

 

int cmd=0;
int speed=5;
unsigned int dat[100];
unsigned int i=0;
unsigned int j=0;

 

void timer(void)        
{
   T0PS0=0; 
   T0PS1=0;
   T0PS2=0;
   PSA=0;      //Timer Clock Source is from Prescaler
   T0CS=0;     //Prescaler gets clock from FCPU (5MHz)
   T08BIT=0;   //16 BIT MODE
   TMR0IE=1;   //Enable TIMER0 Interrupt
   PEIE=1;     //Enable Peripheral Interrupt
   GIE=1;      //Enable INTs globally
   TMR0ON=1;      //Now start the timer!
}

void main() 
{
   ADCON1=0b00001111;
   TRISB1=0; //triac
   TRISB2=1; //ir
   TRISC=0x00;
    relay1=0;
    relay2=0;
    ir=0; 
    tric=0; //at RB1 PIN
    timer(); 
    
   
    
    TRISB0=1;
    INTEDG0 = 0; // Interrupt on falling edge
    INT0IE = 1; //0; //1; // Enable the INT0 external interrupt (RB0) 
    INT0IF = 0; // Clears INT0 External Interrupt Flag bit
    PEIE=1;     //Enable Peripheral Interrupt
    GIE=1;      //Enable INTs globally

    
   while(1)
   {   
       while(ir == 1);
       INT0IE = 0;
       while(ir == 0);
       TMR0=0;
       while(ir == 1);
       i++;
       dat[i]=TMR0;
       
       if(dat[1] > 5000 && dat[1]<12000)
        {
        }
       else
        {
        i=0;
        INT0IE = 1;  
        }

       
       
       
       if(i>=33)
        {
         GIE=0;
         delay(50);
         cmd=0;
         for(j=26;j<34;j++)
            {
              if(dat[j]>1000 && dat[j]<2000)
             cmd<<=1;

              else if(dat[j]>3500 && dat[j]<4500)
            {
             cmd|=0x01;
             cmd<<=1;
            }
            }
        
        
         cmd>>=1;
        
         //------------------------------------------------------------------------
         if(cmd == 0xAF)
            {
            
             relay1^=1;      

            }
         
         else if(cmd == 0x27)
            {
             relay2^=1; 

            }
         
         else if(cmd == 0x07)
            {
             relay3^=1;

            }
         
         else if(cmd == 0xCF)
            {
             relay4^=1; 

            }
    
         else if(cmd == 0x5f)
            {
             speed++;
             if(speed>5)
                {
                 speed=5; 
                }
            }
         
         else if(cmd == 0x9f)
            {
             speed--;
             if(speed<0)
                {
                 speed=0; 
                }
            }
         
         
         
        
         i=0;
         INT0IE = 1; 
         GIE=1;
         
         } // CLOSE if(i>=33)
       
     } //CLOSE while(1)
    
 } //CLOSE void main(void)

//------------------------------------------------------------------------------

void interrupt isr()
{
    if(INT0IF)
    {
        
        //INT0IF=0;
        delay(speed);
        tric=1;
        __delay_us(40); //in 100 at sw power on more flickering bulb, in 50 less flick , 10 even not start
        //for(int t=0;t<100;t++);
        tric=0;
        INT0IF=0;
    }
    
   if(TMR0IF)  //Check if it is TMR0 Overflow ISR   
   {
      TMR0IF=0;
   }
    
}

:::::::::::::::::::::::::::::::::::::::::::::::::::::::

In Hardware site, the zero-xing is used with 4n35 opto and triac is with moc3021.

 

 

Pl suggeset what is getting wrong with the code, is it INT0IE/GIE is at misplaced for which the triac behaving like above point no 2.

tnks..

Aswinth Raj's picture

Offline

Joined: Aug 16, 2016

Replies: 796
I think it is switch de-bouncing problem
October 30, 2018 - 1:34pm

I am not sure what you are trying to ask, but

After power up and remain on flickering state as above when any botton including botton used for realays if pressed: the bulb getting OFF and then ON on pressing any botton, >>> what  is to be edit in programme.

The above problem might be like switch be-bounce where the IR is consider to be a switch. Can you also explain you problem much more simpler I am quite not able to get it. Did you check the hardware pins voltage?

saj's picture
saj

Offline

Joined: Sep 28, 2018

Replies: 9
With the code above-
October 30, 2018 - 2:10pm

With the code above-

1)First problem is that, when power sw in ON, the bulb which is connected(at RB1 PIN) as dimmer-control getting ON  with flickering.> how do i stop it from getting ON on power supply ON.  

2) Second, when ever ANY button of remote is pressed, the bulb going OFF and again ON.

 

tnx

John_Kripto's picture

Offline

Joined: Aug 11, 2018

Replies: 13
Lets address the first issue 
October 30, 2018 - 5:55pm

Lets address the first issue 

1)First problem is that, when power sw in ON, the bulb which is connected(at RB1 PIN) as dimmer-control getting ON  with flickering.> how do i stop it from getting ON on power supply ON.  

In your code you have used RB1 as tric and have declared it as output pin and made it zero in the void main using the following lines of code

   TRISB1=0; //triac
    tric=0; //at RB1 PIN
 

I am not sure what compiler you are using but try using RB1=0 in place of tric=0 just to make sure things for now. Also add a delay of 1sec inside the main menu before getting into the while().

Now power the PIC and check the voltage at pin RB1=0 using a multimeter, measure the voltage at the pin. If not reset the MCU and check if the pin goes to zero for 1sec.

Also make sure you have added a pull down resistor to this pin RB1

 

saj's picture
saj

Offline

Joined: Sep 28, 2018

Replies: 9
XC8 compiler and all this
October 30, 2018 - 7:00pm

XC8 compiler and all this were done but no effect.

I think at startup its calling delay(5) function through  INT0IE=1 ie, with 10msec delay, that is why bulb glowing with approx full dim.

So question is, 1) How do i stop this glowing of the bulb on startup of the power and it will only glow at delay(5) when a pre-defined button is pressed. >>so how do i write in above code.

2) secondly, when ever ANY button of remote is pressed, the bulb's light is bounced back (going OFF and again ON once pressing any remote key) and sometimes start flickering. Even found that if any finger is comes to nearer to 'TSOP-1738' receiver sensor the bulb's light also flickering..>>how do i stop that bounce??

tnx

Aswinth Raj's picture

Offline

Joined: Aug 16, 2016

Replies: 796
I think at startup its
October 31, 2018 - 10:58am

I think at startup its calling delay(5) function through  INT0IE=1 ie, with 10msec delay, that is why bulb glowing with approx full dim.

I dont understand what you are trying to say here. There is no in-built function called delay(5), are you referring to __delay_ms(5)?

How would INT0IE=1 will call a delay function?

There is nothing mentioned like that in the datasheet or in the compiler documentations 

saj's picture
saj

Offline

Joined: Sep 28, 2018

Replies: 9
tnx.
October 31, 2018 - 9:35pm

tnx.

At startup while INT0IF is SET the initialized variable value //int speed=5 is passed as argument to  //delay() then triac gets fired and lamp glow in minimum brightness. If I am wrong please suggest..That is why bulb glow at startup of mcu.

PROBLEMS::Please let me know, Why the light-bulb is getting bounced back  (going OFF and again ON on pressing any remote key) and How do I  stop it in which it will only operate on its pre-defined key pressing????

If it is the case of DE-BOUNCE issue then where and how to put debounce routine in above code for all output pins?????? 

NB: the light-buld is connected with the standard ckt ofMOC3021 and triac and ZC with 4n35 opto. with bridge ckt from transformer.

tnx for your early response.