Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] if and else use in pic16f877

Status
Not open for further replies.

sumonpic10

Newbie level 5
Joined
Jul 30, 2016
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
99
hello everyone..i have a problem on my project..with pic16f877 microcontroller. My project is a 2 button mode led blinking ..Hope someone could help me..the problem is


when I press the 1st button it working such code.
but when I press the 2nd button it's not working such the code. which I programming in 2nd condition......it's only read 2 line of my code

Code:
 ( PORTB=0x00;         // PORTB all low
  PORTB=0x0f;          // PORTB 0b00001111 High)
this 2 line.................................
below my code..........................


void main() 
{

     TRISC=0xff;     // PORTC as input
     TRISB=0;        // PORTB as output
      PORTC=0xff;
      PORTB=0;

     while(1)

     {
         if(PORTC.f4==0)   // when press RC4 button
           {

             PORTB=0xff;            //PORTB all high
             Delay_ms(500);         // Delay time
             PORTB=0x00;            // all low
             Delay_ms(500);         // Delay time

            }

  else if(PORTC.f5==0)    // when press RC5 button

  {

     PORTB=0x00;         // PORTB all low
     PORTB=0x0f;          // PORTB 0b00001111 High
     Delay_ms(500);       // Delay time
     PORTB=0x0f;          // PORTB 0b11110000 high
     Delay_ms(500);            // Delay time


  }
            
            else                  // no press any button
            {
              PORTB=0x00;    // output all low
            }


     }

}
 
Last edited by a moderator:

Hi,

there are some issues:

even for yourself: formating the code makes it more visible what pieces of code belong together:

Code:
void main() 
{

	TRISC=0xff;     // PORTC as input
	TRISB=0;        // PORTB as output
	PORTC=0xff;
	PORTB=0;

	while(1)
	{
		if(PORTC.f4==0)   // when press RC4 button
		{
			PORTB=0xff;            //PORTB all high
			Delay_ms(500);         // Delay time
			PORTB=0x00;            // all low
			Delay_ms(500);         // Delay time
		}
		
		else if(PORTC.f5==0)    // when press RC5 button
			{
				[COLOR="#FF0000"]PORTB=0x00;         // PORTB all low  ** this is useless, because it is overwritten with the next line**[/COLOR]
				PORTB=0x0f;          // PORTB 0b00001111 High
				Delay_ms(500);       // Delay time
				[COLOR="#FF0000"]PORTB=[U]0xF0[/U];          // PORTB 0b11110000 high ** before it was wrong: PORTB=0x0f **[/COLOR]
				Delay_ms(500);            // Delay time
			}
	   
			else                  // no press any button
			{
				PORTB=0x00;    // output all low
		}
	}
}

Klaus
 

hello everyone..i have a problem on my project..with pic16f877 microcontroller. My project is a 2 button mode led blinking ..Hope someone could help me..the problem is


when I press the 1st button it working such code.
but when I press the 2nd button it's not working such the code. which I programming in 2nd condition......it's only read 2 line of my code

Code:
 ( PORTB=0x00;         // PORTB all low
  PORTB=0x0f;          // PORTB 0b00001111 High)
this 2 line.................................
below my code..........................


void main() 
{

     TRISC=0xff;     // PORTC as input
     TRISB=0;        // PORTB as output
      PORTC=0xff;
      PORTB=0;

     while(1)

     {
         if(PORTC.f4==0)   // when press RC4 button
           {

             PORTB=0xff;            //PORTB all high
             Delay_ms(500);         // Delay time
             PORTB=0x00;            // all low
             Delay_ms(500);         // Delay time

            }

  else if(PORTC.f5==0)    // when press RC5 button

  {

     PORTB=0x00;         // PORTB all low
     PORTB=0x0f;          // PORTB 0b00001111 High
     Delay_ms(500);       // Delay time
     PORTB=0x0f;          // PORTB 0b11110000 high
     Delay_ms(500);            // Delay time


  }
            
            else                  // no press any button
            {
              PORTB=0x00;    // output all low
            }


     }

}

Do you have full code and proteus file ?
 

You should remove the else condition before evaluation of the 2nd button.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top