problem using two switch at same time PIC16f877

General discussion on mikroElektronika website & forums.
Post Reply
Author
Message
ravi.gupta1910
Posts: 5
Joined: 26 Sep 2015 19:49

problem using two switch at same time PIC16f877

#1 Post by ravi.gupta1910 » 01 Oct 2015 22:38

hello friends , i am using two switches in a program with pic16f877A, one switch is used as external interrupt (at RB0/INT)and other is used as a normal switch (at RC0), my program and my proteous file is attached





(if both switches are active)
when i run this simulation, and press switch at RB0, output leds i.e. PORTD.B7 and PORTD.B1 are start blinking, instead of steady light. when i pressed again the RB0 sw, next two leds are start blinking and

(if only interrupt switch active)

all led are glow steady as desired,


(but when both switches are activated )

output leds at portd starts blinking

i dont why this happen, is any wrong in my code then please help me . i am new with pic microcontrollers. please
Attachments
sbit LCD_RS at RC2_bit;<br />sbit LCD_EN at RC3_bit;<br />sbit LCD_D4 at RC4_bit;<br />sbit LCD_D5 at RC5_bit;<br />sbit LCD_D6 at RC6_bit;<br />sbit LCD_D7 at RC7_bit;<br /><br />sbit LCD_RS_Direction at TRISC2_bit;<br />sbit LCD_EN_Direction at TRISC3_bit;<br />sbit LCD_D4_Direction at TRISC4_bit;<br />sbit LCD_D5_Direction at TRISC5_bit;<br />sbit LCD_D6_Direction at TRISC6_bit;<br />sbit LCD_D7_Direction at TRISC7_bit;<br /><br />sbit SWITCH  at RC0_bit;<br />unsigned int cnt;<br />                                // Old state flag<br /><br /><br />  void interrupt(void)<br />{<br /><br />      cnt++;<br /><br />        //PORTD++;<br />         // delay_ms(100);<br />         //Increment PORTD value by one<br />        //delay_ms(50);<br />  INTCON.INTF = 0;          // clear the interrupt flag<br />}<br /> unsigned int SWITCH1()<br />{<br />   switch (SWITCH)<br />       {<br />       case 1 :<br />       Lcd_Out(2,13,&quot;EXT&quot;);<br />         PORTD.B7 = 0;<br />         PORTD.B1 = 0;<br />         PORTD.B6 = 0;<br />         PORTD.B5 = 0;<br />         PORTD.B4 = 1;<br />         PORTD.B3 = 0;<br />         PORTD.B2 = 0;<br />         PORTD.B0= 1;<br />         break ;<br /><br />           case 0 :<br />         Lcd_Out(2,13,&quot;INT&quot;);<br />         PORTD.B7 = 0;<br />         PORTD.B1 = 0;<br />         PORTD.B6 = 0;<br />         PORTD.B5 = 0;<br />         PORTD.B4 = 0;<br />         PORTD.B3 = 0;<br />         PORTD.B2 = 0;<br />         PORTD.B0= 0;<br />          break ;<br />           }<br />         }<br /><br />void main()<br />{<br />  TRISB = 0b00000011;<br />  ADCON1 = 0b00001110;<br />  TRISA = 0b00000011;<br />  PORTD= 0;<br />  TRISD = 0b00000000;<br />  INTCON.GIE = 1;                  //Enable Global Interrupt<br />  INTCON.INTE = 1;                //Enable RB0/INT external Interrupt<br />  INTCON.PEIE = 0;                //Disable all unmasked peripheral interrupt<br />  OPTION_REG.INTEDG = 1;    //Interrupt on rising edge<br /><br />  cnt = 0;<br />  TRISC = 0x01;<br />  Lcd_Init();<br />  Lcd_Cmd(_LCD_CURSOR_OFF);<br />  Lcd_Cmd(_LCD_CLEAR);<br />  Lcd_Out (1,4,&quot;RAW007&quot;);<br />  Lcd_Out (2,1,&quot;SW2 = &quot;);<br />  Lcd_Out (3,-3,&quot;SW1= &quot;);<br />  Lcd_Out (4,-3,&quot; PGRM1 &quot;);<br /><br /> while(1)<br />  {<br />        SWITCH1();<br />      switch (cnt)<br />      {<br />      case 1 :<br /><br />       Lcd_Chr(3,9,8+48);<br />       Lcd_Chr_CP('O');<br />       Lcd_Chr_CP('H');<br />       Lcd_Chr_CP('M');<br />         PORTD.B7 = 1;<br />         PORTD.B1 = 1;<br />         PORTD.B6 = 0;<br />         PORTD.B5 = 0;<br />         PORTD.B4 = 0;<br />         PORTD.B3 = 0;<br />         PORTD.B2 = 0;<br />         PORTD.B0= 0;<br />         break;<br /><br />      case  2 :<br /><br />       Lcd_Chr(3,9,4+48);<br />       Lcd_Chr_CP('O');<br />       Lcd_Chr_CP('H');<br />       Lcd_Chr_CP('M');<br />       PORTD.B7 = 0;<br />       PORTD.B1 = 0;<br />       PORTD.B6 = 1;<br />       PORTD.B5 = 0;<br />       PORTD.B4 = 0;<br />       PORTD.B3 = 0;<br />       PORTD.B2 = 1;<br />       PORTD.B0= 0;<br />       break ;<br /><br />      case  3  :<br /><br />      Lcd_Chr(3,9,2+48);<br />       Lcd_Chr_CP('O');<br />       Lcd_Chr_CP('H');<br />       Lcd_Chr_CP('M');<br />       PORTD.B7 = 0;<br />       PORTD.B1 = 0;<br />       PORTD.B6 = 0;<br />       PORTD.B5 = 1;<br />       PORTD.B4 = 0;<br />       PORTD.B3 = 1;<br />       PORTD.B2 = 0;<br />       PORTD.B0= 0;<br />       cnt = 0;<br />       break ;<br /><br /><br />}<br />}<br />}
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;

sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;

sbit SWITCH at RC0_bit;
unsigned int cnt;
// Old state flag


void interrupt(void)
{

cnt++;

//PORTD++;
// delay_ms(100);
//Increment PORTD value by one
//delay_ms(50);
INTCON.INTF = 0; // clear the interrupt flag
}
unsigned int SWITCH1()
{
switch (SWITCH)
{
case 1 :
Lcd_Out(2,13,"EXT");
PORTD.B7 = 0;
PORTD.B1 = 0;
PORTD.B6 = 0;
PORTD.B5 = 0;
PORTD.B4 = 1;
PORTD.B3 = 0;
PORTD.B2 = 0;
PORTD.B0= 1;
break ;

case 0 :
Lcd_Out(2,13,"INT");
PORTD.B7 = 0;
PORTD.B1 = 0;
PORTD.B6 = 0;
PORTD.B5 = 0;
PORTD.B4 = 0;
PORTD.B3 = 0;
PORTD.B2 = 0;
PORTD.B0= 0;
break ;
}
}

void main()
{
TRISB = 0b00000011;
ADCON1 = 0b00001110;
TRISA = 0b00000011;
PORTD= 0;
TRISD = 0b00000000;
INTCON.GIE = 1; //Enable Global Interrupt
INTCON.INTE = 1; //Enable RB0/INT external Interrupt
INTCON.PEIE = 0; //Disable all unmasked peripheral interrupt
OPTION_REG.INTEDG = 1; //Interrupt on rising edge

cnt = 0;
TRISC = 0x01;
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out (1,4,"RAW007");
Lcd_Out (2,1,"SW2 = ");
Lcd_Out (3,-3,"SW1= ");
Lcd_Out (4,-3," PGRM1 ");

while(1)
{
SWITCH1();
switch (cnt)
{
case 1 :

Lcd_Chr(3,9,8+48);
Lcd_Chr_CP('O');
Lcd_Chr_CP('H');
Lcd_Chr_CP('M');
PORTD.B7 = 1;
PORTD.B1 = 1;
PORTD.B6 = 0;
PORTD.B5 = 0;
PORTD.B4 = 0;
PORTD.B3 = 0;
PORTD.B2 = 0;
PORTD.B0= 0;
break;

case 2 :

Lcd_Chr(3,9,4+48);
Lcd_Chr_CP('O');
Lcd_Chr_CP('H');
Lcd_Chr_CP('M');
PORTD.B7 = 0;
PORTD.B1 = 0;
PORTD.B6 = 1;
PORTD.B5 = 0;
PORTD.B4 = 0;
PORTD.B3 = 0;
PORTD.B2 = 1;
PORTD.B0= 0;
break ;

case 3 :

Lcd_Chr(3,9,2+48);
Lcd_Chr_CP('O');
Lcd_Chr_CP('H');
Lcd_Chr_CP('M');
PORTD.B7 = 0;
PORTD.B1 = 0;
PORTD.B6 = 0;
PORTD.B5 = 1;
PORTD.B4 = 0;
PORTD.B3 = 1;
PORTD.B2 = 0;
PORTD.B0= 0;
cnt = 0;
break ;


}
}
}
schematic.PNG (36.6 KiB) Viewed 1980 times

User avatar
biljana.nedeljkovic
mikroElektronika team
Posts: 1043
Joined: 30 Jun 2015 15:15

Re: problem using two switch at same time PIC16f877

#2 Post by biljana.nedeljkovic » 05 Oct 2015 14:08

Hello,

I have tested the code on EasyPIC v7 Development board only with diodes, because RB0 pin is used for LCD as well on the Easy PIC v7, and I had no problems with blinking. You should consider trying out the actual hardware (simple breadboard and diodes maybe, if not more complex board).

Unfortunately, Proteus is not verified environment for the mikroE compilers, so I haven't tested it in Proteus.

Best regards,
Biljana

Post Reply

Return to “Website & Forums General Discussion”