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
problem using two switch at same time PIC16f877
-
- Posts: 5
- Joined: 26 Sep 2015 19:49
problem using two switch at same time PIC16f877
- Attachments
-
- 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
- sbit LCD_RS at RC2_bit;
- biljana.nedeljkovic
- mikroElektronika team
- Posts: 1043
- Joined: 30 Jun 2015 15:15
Re: problem using two switch at same time PIC16f877
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
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