PIC 16F877A controller On mikroC compiler

Thread Starter

Muhammad Muaaz Khan

Joined Apr 26, 2016
20
Please Can any one help me on this code I am working with PIC 16F877A controller On mikroC compiler. My code work right but i it not stop while loop(1) when I pressed switch PORTD.F2 can any one tell me how I stop it and how I am going to Exit from whole loop(1) when I pressed PORTD.F2 == 0

C:
void main()
{
  TRISD.F0 = 1; //Configure 1st bit of PORTD as input
TRISD.F1 = 1; //Configure 1st bit of PORTD as input
TRISD.F2 = 1; //Configure 1st bit of PORTD as input
TRISD.F3 = 1; //Configure 1st bit of PORTD as input
TRISB.F0 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F1 = 0; //Makes PORTB1 or RB0 Output Pin
    TRISB.F2 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F3 = 0; //Makes PORTB1 or RB0 Output Pin
    TRISB.F4 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F5 = 0; //Makes PORTB1 or RB0 Output Pin
    TRISB.F6 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F7 = 0; //Makes PORTB1 or RB0 Output Pin

    while(1) //Infinite Loop
   {
    if(PORTD.F1 == 0)   //If the switch is pressed
    {
    PORTB.F0 = 1; //LED ON
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 1; //LED OFF
    PORTB.F4 = 1; //LED ON
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 1; //LED ON
    Delay_ms(2000); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    }
    if(PORTD.F0 == 0)   //If the switch is pressed
    {
    PORTB.F1 = 1; //LED ON
    PORTB.F0 = 0; //LED OFF
    PORTB.F2 = 1; //LED ON
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 1; //LED ON
    PORTB.F6 = 1; //LED ON
    PORTB.F7 = 0; //LED OFF
    Delay_ms(2000); //1 Second Delay
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF

    }
    if(PORTD.F3 == 0)   //If the switch is pressed
    {
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(5000); //1 Second Delay
    }
    if(PORTD.F2 == 0)   //If the switch is pressed
    {
    while(1) //Infinite Loop
  {
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 1; //LED ON
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED ON
    Delay_ms(500); //1 Second Delay
    PORTB.F3 = 1; //LED ON
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 1; //LED ON
    PORTB.F2 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(1000); //1 Second Delay
    PORTB.F0 = 1; //LED ON
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 1; //LED ON
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(100); //1 Second Delay   Both legs are off
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFf
    Delay_ms(500); //1 Second Delay   second leg Left leg
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 1; //LED ON
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(500); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 1; //LED ON
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 1; //LED ON
    Delay_ms(1000); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 1; //LED ON
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 1; //LED ON
    PORTB.F7 = 0; //LED OFF
    Delay_ms(100); //1 Second Delay   Both legs are off
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFf
    Delay_ms(1000); //1 Second Delay
    }
    }
    }
   }
Moderators note: please use code tags for pieces of code
 
Last edited by a moderator:

ErnieM

Joined Apr 24, 2011
8,046
PLEASE USE CODE TAGS!!!

One post is quite enough too.

When you press the button on port d.2 you start an infinite loop. Under what condition do you wish to exit this loop?

Once you determine that you can use the break command to leave the loop.
 

Thread Starter

Muhammad Muaaz Khan

Joined Apr 26, 2016
20
PLEASE USE CODE TAGS!!!

One post is quite enough too.

When you press the button on port d.2 you start an infinite loop. Under what condition do you wish to exit this loop?

Once you determine that you can use the break command to leave the loop.
because i wish taht when i press another button i exit from that loop and work that function which switch i pressed
 

JohnInTX

Joined Jun 26, 2012
4,113
how does i use break command
if(condition_is_true) break; // break out of a loop.

Test your switches like
if ( (PORTD.F2==0) || (PORTD.F0==0) ) break; // exit 'while' loop if either switch input is 0

You can do the test in the 'while' too
while !( (PORTD.F2==0) || (PORTD.F0==0) ) {
// do the LEDs while no switches are pushed
}
Welcome to AAC.
Good luck.
 

Thread Starter

Muhammad Muaaz Khan

Joined Apr 26, 2016
20
if(condition_is_true) break; // break out of a loop.

Test your switches like
if ( (PORTD.F2==0) || (PORTD.F0==0) ) break; // exit 'while' loop if either switch input is 0

You can do the test in the 'while' too
while !( (PORTD.F2==0) || (PORTD.F0==0) ) {
// do the LEDs while no switches are pushed
}
Welcome to AAC.
Good luck.
Sir I want to do that when i press switch PORTD.F3 it will go out while(2) loop and do the function which i declared in PORTD.F3

C:
void main()
{
  TRISD.F0 = 1; //Configure 1st bit of PORTD as input
TRISD.F1 = 1; //Configure 1st bit of PORTD as input
TRISD.F2 = 1; //Configure 1st bit of PORTD as input
TRISD.F3 = 1; //Configure 1st bit of PORTD as input
TRISB.F0 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F1 = 0; //Makes PORTB1 or RB0 Output Pin
    TRISB.F2 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F3 = 0; //Makes PORTB1 or RB0 Output Pin
    TRISB.F4 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F5 = 0; //Makes PORTB1 or RB0 Output Pin
    TRISB.F6 = 0; //Makes PORTB0 or RB0 Output Pin
   TRISB.F7 = 0; //Makes PORTB1 or RB0 Output Pin

    while(1) //Infinite Loop
   {
    if(PORTD.F1 == 0)   //If the switch is pressed
    {
    PORTB.F0 = 1; //LED ON
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 1; //LED OFF
    PORTB.F4 = 1; //LED ON
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 1; //LED ON
    Delay_ms(2000); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    }
    if(PORTD.F0 == 0)   //If the switch is pressed
    {
    PORTB.F1 = 1; //LED ON
    PORTB.F0 = 0; //LED OFF
    PORTB.F2 = 1; //LED ON
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 1; //LED ON
    PORTB.F6 = 1; //LED ON
    PORTB.F7 = 0; //LED OFF
    Delay_ms(2000); //1 Second Delay
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF

    }
    if(PORTD.F3 == 0)   //If the switch is pressed
    {
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(5000); //1 Second Delay
    }
    if(PORTD.F2 == 0)   //If the switch is pressed
    if((PORTD.F3 == 0))break;   //If the switch is pressed
    {
    while(2) //Infinite Loop
  {
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 1; //LED ON
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED ON
    Delay_ms(500); //1 Second Delay
    PORTB.F3 = 1; //LED ON
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 1; //LED ON
    PORTB.F2 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(1000); //1 Second Delay
    PORTB.F0 = 1; //LED ON
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 1; //LED ON
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(100); //1 Second Delay   Both legs are off
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFf
    Delay_ms(500); //1 Second Delay   second leg Left leg
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 1; //LED ON
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFF
    Delay_ms(500); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 1; //LED ON
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 1; //LED ON
    Delay_ms(1000); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 1; //LED ON
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 1; //LED ON
    PORTB.F7 = 0; //LED OFF
    Delay_ms(100); //1 Second Delay   Both legs are off
    PORTB.F0 = 0; //LED OFF
    PORTB.F1 = 0; //LED OFF
    PORTB.F2 = 0; //LED OFF
    PORTB.F3 = 0; //LED OFF
    PORTB.F4 = 0; //LED OFF
    PORTB.F5 = 0; //LED OFF
    PORTB.F6 = 0; //LED OFF
    PORTB.F7 = 0; //LED OFf
    Delay_ms(1000); //1 Second Delay
    }
    }
    }
   }
Moderators note: AGAIN, Please use code tags for pieces of code
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,113
'break' breaks you out of the current DO, WHILE or FOR loop. Your 'break' is not in any of these loops.
Since 'while(x)' will loop as long as 'x' is TRUE i.e. evaluates to a non-zero value, then you can put the switch tests where 'x' is.
Have you drawn a flow chart of how you want this to work? If not, do one and post it. It does not have to be fancy.
I would recommend that you study up on the WHILE, BREAK and CONTINUE keywords in C, as well as how expressions such as if (PORTx.xx ==0) are evaluated to TRUE and FALSE. Finally, in my previous post, I used the logical OR operator ( || ). That along with the logical AND operator( && ) is important to know about.
My advice is to
1) draw out what your program is supposed to do in a flow chart. Trace out the code flow by hand to see what happens when your switches are pushed.
2) study up on C constructs so that you actually know how they are used rather than trying things at random. Use that knowledge to implement the control flow shown on your flow chart.
We can help you from there.
And yes, stop posting your question in other threads all over the boards. Those posts will be deleted and you will be kicked out if you persist.
Good luck.
 

Thread Starter

Muhammad Muaaz Khan

Joined Apr 26, 2016
20
Sir this program work rightly but What i do When I pressed switch 4 it continuously work it not stop But i want that when i pressed another any switch except switch 4 it stop working of switch 4 and work which switch i pressed.
C:
[*]void main()
[*]{
[*]  TRISD.F0 = 1; //Configure 1st bit of PORTD as input
[*]TRISD.F1 = 1; //Configure 1st bit of PORTD as input
[*]TRISD.F2 = 1; //Configure 1st bit of PORTD as input
[*]TRISD.F3 = 1; //Configure 1st bit of PORTD as input
[*]TRISB.F0 = 0; //Makes PORTB0 or RB0 Output Pin
[*]   TRISB.F1 = 0; //Makes PORTB1 or RB0 Output Pin
[*]    TRISB.F2 = 0; //Makes PORTB0 or RB0 Output Pin
[*]   TRISB.F3 = 0; //Makes PORTB1 or RB0 Output Pin
[*]    TRISB.F4 = 0; //Makes PORTB0 or RB0 Output Pin
[*]   TRISB.F5 = 0; //Makes PORTB1 or RB0 Output Pin
[*]    TRISB.F6 = 0; //Makes PORTB0 or RB0 Output Pin
[*]   TRISB.F7 = 0; //Makes PORTB1 or RB0 Output Pin
[*]

[*]   while(1) //Infinite Loop
[*]   {
[*]   if(PORTD.F1 == 0) //If the switch1 is pressed
[*]   {
[*]    PORTB.F0 = 1; //LED ON
[*]    PORTB.F1 = 0; //LED OFF
[*]    PORTB.F2 = 0; //LED OFF
[*]    PORTB.F3 = 1; //LED OFF
[*]    PORTB.F4 = 1; //LED ON
[*]    PORTB.F5 = 0; //LED OFF
[*]    PORTB.F6 = 0; //LED OFF
[*]    PORTB.F7 = 1; //LED ON
[*]    Delay_ms(2000); //1 Second Delay
[*]    PORTB.F0 = 0; //LED OFF
[*]    PORTB.F3 = 0; //LED OFF
[*]    PORTB.F4 = 0; //LED OFF
[*]    PORTB.F5 = 0; //LED OFF
[*]    PORTB.F6 = 0; //LED OFF
[*]    PORTB.F7 = 0; //LED OFF
[*]   }
[*]   if(PORTD.F0 == 0) //If the switch 2 is pressed
[*]   {
[*]    PORTB.F1 = 1; //LED ON
[*]    PORTB.F0 = 0; //LED OFF
[*]    PORTB.F2 = 1; //LED ON
[*]    PORTB.F3 = 0; //LED OFF
[*]    PORTB.F4 = 0; //LED OFF
[*]    PORTB.F5 = 1; //LED ON
[*]    PORTB.F6 = 1; //LED ON
[*]    PORTB.F7 = 0; //LED OFF
[*]    Delay_ms(2000); //1 Second Delay
[*]    PORTB.F1 = 0; //LED OFF
[*]    PORTB.F2 = 0; //LED OFF
[*]    PORTB.F3 = 0; //LED OFF
[*]    PORTB.F4 = 0; //LED OFF
[*]    PORTB.F5 = 0; //LED OFF
[*]    PORTB.F6 = 0; //LED OFF
[*]    PORTB.F7 = 0; //LED OFF
[*]

[*]   }
[*]   if(PORTD.F3 == 0) //If the switch 3 is pressed
[*]   {
[*]    PORTB.F0 = 0; //LED OFF
[*]    PORTB.F1 = 0; //LED OFF
[*]    PORTB.F2 = 0; //LED OFF
[*]    PORTB.F3 = 0; //LED OFF
[*]    PORTB.F4 = 0; //LED OFF
[*]    PORTB.F5 = 0; //LED OFF
[*]    PORTB.F6 = 0; //LED OFF
[*]    PORTB.F7 = 0; //LED OFF
[*]    Delay_ms(5000); //1 Second Delay
[*]   }
[*]   if(PORTD.F2 == 0) //If the switch 4 is pressed
[*]   if((PORTD.F3 == 0))break; //If the switch is pressed
[*]   {
[*]   while(2) //Infinite Loop
[*]{
[*]    PORTB.F0 = 0; //LED OFF
[*]    PORTB.F1 = 1; //LED ON
[*]    PORTB.F2 = 0; //LED OFF
[*]    PORTB.F3 = 0; //LED OFF
[*]    PORTB.F4 = 0; //LED OFF
[*]    PORTB.F5 = 0; //LED OFF
[*]    PORTB.F6 = 0; //LED OFF
[*]    PORTB.F7 = 0; //LED ON
[*]    Delay_ms(500); //1 Second Delay
[*]    PORTB.F3 = 1; //LED ON
[*]    PORTB.F0 = 0; //LED OFF
[*]    PORTB.F1 = 1; //LED ON
[*]    PORTB.F2 = 0; //LED OFF
[*]    PORTB.F4 = 0; //LED OFF
[*]    PORTB.F5 = 0; //LED OFF
[*]    PORTB.F6 = 0; //LED OFF
[*]    PORTB.F7 = 0; //LED OFF
[*]    Delay_ms(1000); //1 Second Delay
[*]    PORTB.F0 = 1; //LED ON
[*]    PORTB.F1 = 0; //LED OFF
[*]    PORTB.F2 = 1; //LED ON
[*]    PORTB.F3 = 0; //LED OFF
[*]    PORTB.F4 = 0; //LED OFF
[*]    PORTB.F5 = 0; //LED OFF
[*]    PORTB.F6 = 0; //LED OFF
[*]    PORTB.F7 = 0; //LED OFF
[*]    Delay_ms(100); //1 Second Delay   Both legs are off
[*]    PORTB.F0 = 0; //LED OFF
[*]    PORTB.F1 = 0; //LED OFF
[*]    PORTB.F2 = 0; //LED OFF
[*]    PORTB.F3 = 0; //LED OFF
[*]    PORTB.F4 = 0; //LED OFF
[*]    PORTB.F5 = 0; //LED OFF
[*]    PORTB.F6 = 0; //LED OFF
[*]    PORTB.F7 = 0; //LED OFf
[*]    Delay_ms(500); //1 Second Delay   second leg Left leg
[*]    PORTB.F0 = 0; //LED OFF
[*]    PORTB.F1 = 0; //LED OFF
[*]    PORTB.F2 = 0; //LED OFF
[*]    PORTB.F3 = 0; //LED OFF
[*]    PORTB.F4 = 0; //LED OFF
[*]    PORTB.F5 = 1; //LED ON
[*]    PORTB.F6 = 0; //LED OFF
[*]    PORTB.F7 = 0; //LED OFF
[*]    Delay_ms(500); //1 Second Delay
[*]    PORTB.F0 = 0; //LED OFF
[*]    PORTB.F1 = 0; //LED OFF
[*]    PORTB.F2 = 0; //LED OFF
[*]    PORTB.F3 = 0; //LED OFF
[*]    PORTB.F4 = 0; //LED OFF
[*]    PORTB.F5 = 1; //LED ON
[*]    PORTB.F6 = 0; //LED OFF
[*]    PORTB.F7 = 1; //LED ON
[*]    Delay_ms(1000); //1 Second Delay
[*]    PORTB.F0 = 0; //LED OFF
[*]    PORTB.F1 = 0; //LED OFF
[*]    PORTB.F2 = 0; //LED OFF
[*]    PORTB.F3 = 0; //LED OFF
[*]    PORTB.F4 = 1; //LED ON
[*]    PORTB.F5 = 0; //LED OFF
[*]    PORTB.F6 = 1; //LED ON
[*]    PORTB.F7 = 0; //LED OFF
[*]    Delay_ms(100); //1 Second Delay   Both legs are off
[*]    PORTB.F0 = 0; //LED OFF
[*]    PORTB.F1 = 0; //LED OFF
[*]    PORTB.F2 = 0; //LED OFF
[*]    PORTB.F3 = 0; //LED OFF
[*]    PORTB.F4 = 0; //LED OFF
[*]    PORTB.F5 = 0; //LED OFF
[*]    PORTB.F6 = 0; //LED OFF
[*]    PORTB.F7 = 0; //LED OFf
[*]    Delay_ms(1000); //1 Second Delay
[*]   }
[*]   }
[*]   }
[*]   }
[*]
 

Thread Starter

Muhammad Muaaz Khan

Joined Apr 26, 2016
20
  1. C:
    [*]*]void main()
    [*]{
    [*]TRISD.F0 = 1; //Configure 1st bit of PORTD as input
    [*]TRISD.F1 = 1; //Configure 1st bit of PORTD as input
    [*]TRISD.F2 = 1; //Configure 1st bit of PORTD as input
    [*]TRISD.F3 = 1; //Configure 1st bit of PORTD as input
    [*]TRISB.F0 = 0; //Makes PORTB0 or RB0 Output Pin
    [*]  TRISB.F1 = 0; //Makes PORTB1 or RB0 Output Pin
    [*]   TRISB.F2 = 0; //Makes PORTB0 or RB0 Output Pin
    [*]  TRISB.F3 = 0; //Makes PORTB1 or RB0 Output Pin
    [*]   TRISB.F4 = 0; //Makes PORTB0 or RB0 Output Pin
    [*]  TRISB.F5 = 0; //Makes PORTB1 or RB0 Output Pin
    [*]   TRISB.F6 = 0; //Makes PORTB0 or RB0 Output Pin
    [*]  TRISB.F7 = 0; //Makes PORTB1 or RB0 Output Pin
    [*]
    
    [*]while(1) //Infinite Loop
    [*]{
    [*]if(PORTD.F1 == 0) //If the switch1 is pressed
    [*]{
    [*]   PORTB.F0 = 1; //LED ON
    [*]   PORTB.F1 = 0; //LED OFF
    [*]   PORTB.F2 = 0; //LED OFF
    [*]   PORTB.F3 = 1; //LED OFF
    [*]   PORTB.F4 = 1; //LED ON
    [*]   PORTB.F5 = 0; //LED OFF
    [*]   PORTB.F6 = 0; //LED OFF
    [*]   PORTB.F7 = 1; //LED ON
    [*]   Delay_ms(2000); //1 Second Delay
    [*]   PORTB.F0 = 0; //LED OFF
    [*]   PORTB.F3 = 0; //LED OFF
    [*]   PORTB.F4 = 0; //LED OFF
    [*]   PORTB.F5 = 0; //LED OFF
    [*]   PORTB.F6 = 0; //LED OFF
    [*]   PORTB.F7 = 0; //LED OFF
    [*]}
    [*]if(PORTD.F0 == 0) //If the switch 2 is pressed
    [*]{
    [*]   PORTB.F1 = 1; //LED ON
    [*]   PORTB.F0 = 0; //LED OFF
    [*]   PORTB.F2 = 1; //LED ON
    [*]   PORTB.F3 = 0; //LED OFF
    [*]   PORTB.F4 = 0; //LED OFF
    [*]   PORTB.F5 = 1; //LED ON
    [*]   PORTB.F6 = 1; //LED ON
    [*]   PORTB.F7 = 0; //LED OFF
    [*]   Delay_ms(2000); //1 Second Delay
    [*]   PORTB.F1 = 0; //LED OFF
    [*]   PORTB.F2 = 0; //LED OFF
    [*]   PORTB.F3 = 0; //LED OFF
    [*]   PORTB.F4 = 0; //LED OFF
    [*]   PORTB.F5 = 0; //LED OFF
    [*]   PORTB.F6 = 0; //LED OFF
    [*]   PORTB.F7 = 0; //LED OFF
    [*]
    
    [*]}
    [*]if(PORTD.F3 == 0) //If the switch 3 is pressed
    [*]{
    [*]   PORTB.F0 = 0; //LED OFF
    [*]   PORTB.F1 = 0; //LED OFF
    [*]   PORTB.F2 = 0; //LED OFF
    [*]   PORTB.F3 = 0; //LED OFF
    [*]   PORTB.F4 = 0; //LED OFF
    [*]   PORTB.F5 = 0; //LED OFF
    [*]   PORTB.F6 = 0; //LED OFF
    [*]   PORTB.F7 = 0; //LED OFF
    [*]   Delay_ms(5000); //1 Second Delay
    [*]}
    [*]if((PORTD.F2 == 0))break; //If the switch is pressed
    [*]{
    [*]while(2) //Infinite Loop
    [*]{
    [*]   PORTB.F0 = 0; //LED OFF
    [*]   PORTB.F1 = 1; //LED ON
    [*]   PORTB.F2 = 0; //LED OFF
    [*]   PORTB.F3 = 0; //LED OFF
    [*]   PORTB.F4 = 0; //LED OFF
    [*]   PORTB.F5 = 0; //LED OFF
    [*]   PORTB.F6 = 0; //LED OFF
    [*]   PORTB.F7 = 0; //LED ON
    [*]   Delay_ms(500); //1 Second Delay
    [*]   PORTB.F3 = 1; //LED ON
    [*]   PORTB.F0 = 0; //LED OFF
    [*]   PORTB.F1 = 1; //LED ON
    [*]   PORTB.F2 = 0; //LED OFF
    [*]   PORTB.F4 = 0; //LED OFF
    [*]   PORTB.F5 = 0; //LED OFF
    [*]   PORTB.F6 = 0; //LED OFF
    [*]   PORTB.F7 = 0; //LED OFF
    [*]   Delay_ms(1000); //1 Second Delay
    [*]   PORTB.F0 = 1; //LED ON
    [*]   PORTB.F1 = 0; //LED OFF
    [*]   PORTB.F2 = 1; //LED ON
    [*]   PORTB.F3 = 0; //LED OFF
    [*]   PORTB.F4 = 0; //LED OFF
    [*]   PORTB.F5 = 0; //LED OFF
    [*]   PORTB.F6 = 0; //LED OFF
    [*]   PORTB.F7 = 0; //LED OFF
    [*]   Delay_ms(100); //1 Second Delay   Both legs are off
    [*]   PORTB.F0 = 0; //LED OFF
    [*]   PORTB.F1 = 0; //LED OFF
    [*]   PORTB.F2 = 0; //LED OFF
    [*]   PORTB.F3 = 0; //LED OFF
    [*]   PORTB.F4 = 0; //LED OFF
    [*]   PORTB.F5 = 0; //LED OFF
    [*]   PORTB.F6 = 0; //LED OFF
    [*]   PORTB.F7 = 0; //LED OFf
    [*]   Delay_ms(500); //1 Second Delay   second leg Left leg
    [*]   PORTB.F0 = 0; //LED OFF
    [*]   PORTB.F1 = 0; //LED OFF
    [*]   PORTB.F2 = 0; //LED OFF
    [*]   PORTB.F3 = 0; //LED OFF
    [*]   PORTB.F4 = 0; //LED OFF
    [*]   PORTB.F5 = 1; //LED ON
    [*]   PORTB.F6 = 0; //LED OFF
    [*]   PORTB.F7 = 0; //LED OFF
    [*]   Delay_ms(500); //1 Second Delay
    [*]   PORTB.F0 = 0; //LED OFF
    [*]   PORTB.F1 = 0; //LED OFF
    [*]   PORTB.F2 = 0; //LED OFF
    [*]   PORTB.F3 = 0; //LED OFF
    [*]   PORTB.F4 = 0; //LED OFF
    [*]   PORTB.F5 = 1; //LED ON
    [*]   PORTB.F6 = 0; //LED OFF
    [*]   PORTB.F7 = 1; //LED ON
    [*]   Delay_ms(1000); //1 Second Delay
    [*]   PORTB.F0 = 0; //LED OFF
    [*]   PORTB.F1 = 0; //LED OFF
    [*]   PORTB.F2 = 0; //LED OFF
    [*]   PORTB.F3 = 0; //LED OFF
    [*]   PORTB.F4 = 1; //LED ON
    [*]   PORTB.F5 = 0; //LED OFF
    [*]   PORTB.F6 = 1; //LED ON
    [*]   PORTB.F7 = 0; //LED OFF
    [*]   Delay_ms(100); //1 Second Delay   Both legs are off
    [*]   PORTB.F0 = 0; //LED OFF
    [*]   PORTB.F1 = 0; //LED OFF
    [*]   PORTB.F2 = 0; //LED OFF
    [*]   PORTB.F3 = 0; //LED OFF
    [*]   PORTB.F4 = 0; //LED OFF
    [*]   PORTB.F5 = 0; //LED OFF
    [*]   PORTB.F6 = 0; //LED OFF
    [*]   PORTB.F7 = 0; //LED OFf
    [*]   Delay_ms(1000); //1 Second Delay
    [*]}
    [*]}
    [*]}
    [*]}
    [/LIST]
    AGAIN, Please use code tags when posting pieces of code
 

Thread Starter

Muhammad Muaaz Khan

Joined Apr 26, 2016
20
Int he follwoing image you can understand what i want to do. My source code work rightly but it not stop the function of portD.F2 because there is an while(2) loop how can I stop functioning of PortD.f2 when i preesed any other switch Portd.f3 or any other siwtch.
 

Attachments

Top