PIC 16F877A controller On mikroC compiler

Discussion in 'Embedded Systems and Microcontrollers' started by Muhammad Muaaz Khan, Apr 26, 2016.

  1. Muhammad Muaaz Khan

    Thread Starter New Member

    Apr 26, 2016
    20
    0
    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

    Code (C):
    1.  
    2. void main()
    3. {
    4.   TRISD.F0 = 1; //Configure 1st bit of PORTD as input
    5. TRISD.F1 = 1; //Configure 1st bit of PORTD as input
    6. TRISD.F2 = 1; //Configure 1st bit of PORTD as input
    7. TRISD.F3 = 1; //Configure 1st bit of PORTD as input
    8. TRISB.F0 = 0; //Makes PORTB0 or RB0 Output Pin
    9.    TRISB.F1 = 0; //Makes PORTB1 or RB0 Output Pin
    10.     TRISB.F2 = 0; //Makes PORTB0 or RB0 Output Pin
    11.    TRISB.F3 = 0; //Makes PORTB1 or RB0 Output Pin
    12.     TRISB.F4 = 0; //Makes PORTB0 or RB0 Output Pin
    13.    TRISB.F5 = 0; //Makes PORTB1 or RB0 Output Pin
    14.     TRISB.F6 = 0; //Makes PORTB0 or RB0 Output Pin
    15.    TRISB.F7 = 0; //Makes PORTB1 or RB0 Output Pin
    16.  
    17.     while(1) //Infinite Loop
    18.    {
    19.     if(PORTD.F1 == 0)   //If the switch is pressed
    20.     {
    21.     PORTB.F0 = 1; //LED ON
    22.     PORTB.F1 = 0; //LED OFF
    23.     PORTB.F2 = 0; //LED OFF
    24.     PORTB.F3 = 1; //LED OFF
    25.     PORTB.F4 = 1; //LED ON
    26.     PORTB.F5 = 0; //LED OFF
    27.     PORTB.F6 = 0; //LED OFF
    28.     PORTB.F7 = 1; //LED ON
    29.     Delay_ms(2000); //1 Second Delay
    30.     PORTB.F0 = 0; //LED OFF
    31.     PORTB.F3 = 0; //LED OFF
    32.     PORTB.F4 = 0; //LED OFF
    33.     PORTB.F5 = 0; //LED OFF
    34.     PORTB.F6 = 0; //LED OFF
    35.     PORTB.F7 = 0; //LED OFF
    36.     }
    37.     if(PORTD.F0 == 0)   //If the switch is pressed
    38.     {
    39.     PORTB.F1 = 1; //LED ON
    40.     PORTB.F0 = 0; //LED OFF
    41.     PORTB.F2 = 1; //LED ON
    42.     PORTB.F3 = 0; //LED OFF
    43.     PORTB.F4 = 0; //LED OFF
    44.     PORTB.F5 = 1; //LED ON
    45.     PORTB.F6 = 1; //LED ON
    46.     PORTB.F7 = 0; //LED OFF
    47.     Delay_ms(2000); //1 Second Delay
    48.     PORTB.F1 = 0; //LED OFF
    49.     PORTB.F2 = 0; //LED OFF
    50.     PORTB.F3 = 0; //LED OFF
    51.     PORTB.F4 = 0; //LED OFF
    52.     PORTB.F5 = 0; //LED OFF
    53.     PORTB.F6 = 0; //LED OFF
    54.     PORTB.F7 = 0; //LED OFF
    55.  
    56.     }
    57.     if(PORTD.F3 == 0)   //If the switch is pressed
    58.     {
    59.     PORTB.F0 = 0; //LED OFF
    60.     PORTB.F1 = 0; //LED OFF
    61.     PORTB.F2 = 0; //LED OFF
    62.     PORTB.F3 = 0; //LED OFF
    63.     PORTB.F4 = 0; //LED OFF
    64.     PORTB.F5 = 0; //LED OFF
    65.     PORTB.F6 = 0; //LED OFF
    66.     PORTB.F7 = 0; //LED OFF
    67.     Delay_ms(5000); //1 Second Delay
    68.     }
    69.     if(PORTD.F2 == 0)   //If the switch is pressed
    70.     {
    71.     while(1) //Infinite Loop
    72.   {
    73.     PORTB.F0 = 0; //LED OFF
    74.     PORTB.F1 = 1; //LED ON
    75.     PORTB.F2 = 0; //LED OFF
    76.     PORTB.F3 = 0; //LED OFF
    77.     PORTB.F4 = 0; //LED OFF
    78.     PORTB.F5 = 0; //LED OFF
    79.     PORTB.F6 = 0; //LED OFF
    80.     PORTB.F7 = 0; //LED ON
    81.     Delay_ms(500); //1 Second Delay
    82.     PORTB.F3 = 1; //LED ON
    83.     PORTB.F0 = 0; //LED OFF
    84.     PORTB.F1 = 1; //LED ON
    85.     PORTB.F2 = 0; //LED OFF
    86.     PORTB.F4 = 0; //LED OFF
    87.     PORTB.F5 = 0; //LED OFF
    88.     PORTB.F6 = 0; //LED OFF
    89.     PORTB.F7 = 0; //LED OFF
    90.     Delay_ms(1000); //1 Second Delay
    91.     PORTB.F0 = 1; //LED ON
    92.     PORTB.F1 = 0; //LED OFF
    93.     PORTB.F2 = 1; //LED ON
    94.     PORTB.F3 = 0; //LED OFF
    95.     PORTB.F4 = 0; //LED OFF
    96.     PORTB.F5 = 0; //LED OFF
    97.     PORTB.F6 = 0; //LED OFF
    98.     PORTB.F7 = 0; //LED OFF
    99.     Delay_ms(100); //1 Second Delay   Both legs are off
    100.     PORTB.F0 = 0; //LED OFF
    101.     PORTB.F1 = 0; //LED OFF
    102.     PORTB.F2 = 0; //LED OFF
    103.     PORTB.F3 = 0; //LED OFF
    104.     PORTB.F4 = 0; //LED OFF
    105.     PORTB.F5 = 0; //LED OFF
    106.     PORTB.F6 = 0; //LED OFF
    107.     PORTB.F7 = 0; //LED OFf
    108.     Delay_ms(500); //1 Second Delay   second leg Left leg
    109.     PORTB.F0 = 0; //LED OFF
    110.     PORTB.F1 = 0; //LED OFF
    111.     PORTB.F2 = 0; //LED OFF
    112.     PORTB.F3 = 0; //LED OFF
    113.     PORTB.F4 = 0; //LED OFF
    114.     PORTB.F5 = 1; //LED ON
    115.     PORTB.F6 = 0; //LED OFF
    116.     PORTB.F7 = 0; //LED OFF
    117.     Delay_ms(500); //1 Second Delay
    118.     PORTB.F0 = 0; //LED OFF
    119.     PORTB.F1 = 0; //LED OFF
    120.     PORTB.F2 = 0; //LED OFF
    121.     PORTB.F3 = 0; //LED OFF
    122.     PORTB.F4 = 0; //LED OFF
    123.     PORTB.F5 = 1; //LED ON
    124.     PORTB.F6 = 0; //LED OFF
    125.     PORTB.F7 = 1; //LED ON
    126.     Delay_ms(1000); //1 Second Delay
    127.     PORTB.F0 = 0; //LED OFF
    128.     PORTB.F1 = 0; //LED OFF
    129.     PORTB.F2 = 0; //LED OFF
    130.     PORTB.F3 = 0; //LED OFF
    131.     PORTB.F4 = 1; //LED ON
    132.     PORTB.F5 = 0; //LED OFF
    133.     PORTB.F6 = 1; //LED ON
    134.     PORTB.F7 = 0; //LED OFF
    135.     Delay_ms(100); //1 Second Delay   Both legs are off
    136.     PORTB.F0 = 0; //LED OFF
    137.     PORTB.F1 = 0; //LED OFF
    138.     PORTB.F2 = 0; //LED OFF
    139.     PORTB.F3 = 0; //LED OFF
    140.     PORTB.F4 = 0; //LED OFF
    141.     PORTB.F5 = 0; //LED OFF
    142.     PORTB.F6 = 0; //LED OFF
    143.     PORTB.F7 = 0; //LED OFf
    144.     Delay_ms(1000); //1 Second Delay
    145.     }
    146.     }
    147.     }
    148.    }
    149.  
    Moderators note: please use code tags for pieces of code
     
    Last edited by a moderator: Apr 26, 2016
  2. bertus

    Administrator

    Apr 5, 2008
    15,649
    2,348
    Hello,

    I moved your post to its own thread, as you hijacked an existing thread.

    Bertus
     
  3. Muhammad Muaaz Khan

    Thread Starter New Member

    Apr 26, 2016
    20
    0
    ok thank you
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    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.
     
  5. Muhammad Muaaz Khan

    Thread Starter New Member

    Apr 26, 2016
    20
    0
    how does i use break command
     
  6. Muhammad Muaaz Khan

    Thread Starter New Member

    Apr 26, 2016
    20
    0
    because i wish taht when i press another button i exit from that loop and work that function which switch i pressed
     
  7. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    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.
     
  8. Muhammad Muaaz Khan

    Thread Starter New Member

    Apr 26, 2016
    20
    0
    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

    Code (C):
    1.  
    2. void main()
    3. {
    4.   TRISD.F0 = 1; //Configure 1st bit of PORTD as input
    5. TRISD.F1 = 1; //Configure 1st bit of PORTD as input
    6. TRISD.F2 = 1; //Configure 1st bit of PORTD as input
    7. TRISD.F3 = 1; //Configure 1st bit of PORTD as input
    8. TRISB.F0 = 0; //Makes PORTB0 or RB0 Output Pin
    9.    TRISB.F1 = 0; //Makes PORTB1 or RB0 Output Pin
    10.     TRISB.F2 = 0; //Makes PORTB0 or RB0 Output Pin
    11.    TRISB.F3 = 0; //Makes PORTB1 or RB0 Output Pin
    12.     TRISB.F4 = 0; //Makes PORTB0 or RB0 Output Pin
    13.    TRISB.F5 = 0; //Makes PORTB1 or RB0 Output Pin
    14.     TRISB.F6 = 0; //Makes PORTB0 or RB0 Output Pin
    15.    TRISB.F7 = 0; //Makes PORTB1 or RB0 Output Pin
    16.  
    17.     while(1) //Infinite Loop
    18.    {
    19.     if(PORTD.F1 == 0)   //If the switch is pressed
    20.     {
    21.     PORTB.F0 = 1; //LED ON
    22.     PORTB.F1 = 0; //LED OFF
    23.     PORTB.F2 = 0; //LED OFF
    24.     PORTB.F3 = 1; //LED OFF
    25.     PORTB.F4 = 1; //LED ON
    26.     PORTB.F5 = 0; //LED OFF
    27.     PORTB.F6 = 0; //LED OFF
    28.     PORTB.F7 = 1; //LED ON
    29.     Delay_ms(2000); //1 Second Delay
    30.     PORTB.F0 = 0; //LED OFF
    31.     PORTB.F3 = 0; //LED OFF
    32.     PORTB.F4 = 0; //LED OFF
    33.     PORTB.F5 = 0; //LED OFF
    34.     PORTB.F6 = 0; //LED OFF
    35.     PORTB.F7 = 0; //LED OFF
    36.     }
    37.     if(PORTD.F0 == 0)   //If the switch is pressed
    38.     {
    39.     PORTB.F1 = 1; //LED ON
    40.     PORTB.F0 = 0; //LED OFF
    41.     PORTB.F2 = 1; //LED ON
    42.     PORTB.F3 = 0; //LED OFF
    43.     PORTB.F4 = 0; //LED OFF
    44.     PORTB.F5 = 1; //LED ON
    45.     PORTB.F6 = 1; //LED ON
    46.     PORTB.F7 = 0; //LED OFF
    47.     Delay_ms(2000); //1 Second Delay
    48.     PORTB.F1 = 0; //LED OFF
    49.     PORTB.F2 = 0; //LED OFF
    50.     PORTB.F3 = 0; //LED OFF
    51.     PORTB.F4 = 0; //LED OFF
    52.     PORTB.F5 = 0; //LED OFF
    53.     PORTB.F6 = 0; //LED OFF
    54.     PORTB.F7 = 0; //LED OFF
    55.  
    56.     }
    57.     if(PORTD.F3 == 0)   //If the switch is pressed
    58.     {
    59.     PORTB.F0 = 0; //LED OFF
    60.     PORTB.F1 = 0; //LED OFF
    61.     PORTB.F2 = 0; //LED OFF
    62.     PORTB.F3 = 0; //LED OFF
    63.     PORTB.F4 = 0; //LED OFF
    64.     PORTB.F5 = 0; //LED OFF
    65.     PORTB.F6 = 0; //LED OFF
    66.     PORTB.F7 = 0; //LED OFF
    67.     Delay_ms(5000); //1 Second Delay
    68.     }
    69.     if(PORTD.F2 == 0)   //If the switch is pressed
    70.     if((PORTD.F3 == 0))break;   //If the switch is pressed
    71.     {
    72.     while(2) //Infinite Loop
    73.   {
    74.     PORTB.F0 = 0; //LED OFF
    75.     PORTB.F1 = 1; //LED ON
    76.     PORTB.F2 = 0; //LED OFF
    77.     PORTB.F3 = 0; //LED OFF
    78.     PORTB.F4 = 0; //LED OFF
    79.     PORTB.F5 = 0; //LED OFF
    80.     PORTB.F6 = 0; //LED OFF
    81.     PORTB.F7 = 0; //LED ON
    82.     Delay_ms(500); //1 Second Delay
    83.     PORTB.F3 = 1; //LED ON
    84.     PORTB.F0 = 0; //LED OFF
    85.     PORTB.F1 = 1; //LED ON
    86.     PORTB.F2 = 0; //LED OFF
    87.     PORTB.F4 = 0; //LED OFF
    88.     PORTB.F5 = 0; //LED OFF
    89.     PORTB.F6 = 0; //LED OFF
    90.     PORTB.F7 = 0; //LED OFF
    91.     Delay_ms(1000); //1 Second Delay
    92.     PORTB.F0 = 1; //LED ON
    93.     PORTB.F1 = 0; //LED OFF
    94.     PORTB.F2 = 1; //LED ON
    95.     PORTB.F3 = 0; //LED OFF
    96.     PORTB.F4 = 0; //LED OFF
    97.     PORTB.F5 = 0; //LED OFF
    98.     PORTB.F6 = 0; //LED OFF
    99.     PORTB.F7 = 0; //LED OFF
    100.     Delay_ms(100); //1 Second Delay   Both legs are off
    101.     PORTB.F0 = 0; //LED OFF
    102.     PORTB.F1 = 0; //LED OFF
    103.     PORTB.F2 = 0; //LED OFF
    104.     PORTB.F3 = 0; //LED OFF
    105.     PORTB.F4 = 0; //LED OFF
    106.     PORTB.F5 = 0; //LED OFF
    107.     PORTB.F6 = 0; //LED OFF
    108.     PORTB.F7 = 0; //LED OFf
    109.     Delay_ms(500); //1 Second Delay   second leg Left leg
    110.     PORTB.F0 = 0; //LED OFF
    111.     PORTB.F1 = 0; //LED OFF
    112.     PORTB.F2 = 0; //LED OFF
    113.     PORTB.F3 = 0; //LED OFF
    114.     PORTB.F4 = 0; //LED OFF
    115.     PORTB.F5 = 1; //LED ON
    116.     PORTB.F6 = 0; //LED OFF
    117.     PORTB.F7 = 0; //LED OFF
    118.     Delay_ms(500); //1 Second Delay
    119.     PORTB.F0 = 0; //LED OFF
    120.     PORTB.F1 = 0; //LED OFF
    121.     PORTB.F2 = 0; //LED OFF
    122.     PORTB.F3 = 0; //LED OFF
    123.     PORTB.F4 = 0; //LED OFF
    124.     PORTB.F5 = 1; //LED ON
    125.     PORTB.F6 = 0; //LED OFF
    126.     PORTB.F7 = 1; //LED ON
    127.     Delay_ms(1000); //1 Second Delay
    128.     PORTB.F0 = 0; //LED OFF
    129.     PORTB.F1 = 0; //LED OFF
    130.     PORTB.F2 = 0; //LED OFF
    131.     PORTB.F3 = 0; //LED OFF
    132.     PORTB.F4 = 1; //LED ON
    133.     PORTB.F5 = 0; //LED OFF
    134.     PORTB.F6 = 1; //LED ON
    135.     PORTB.F7 = 0; //LED OFF
    136.     Delay_ms(100); //1 Second Delay   Both legs are off
    137.     PORTB.F0 = 0; //LED OFF
    138.     PORTB.F1 = 0; //LED OFF
    139.     PORTB.F2 = 0; //LED OFF
    140.     PORTB.F3 = 0; //LED OFF
    141.     PORTB.F4 = 0; //LED OFF
    142.     PORTB.F5 = 0; //LED OFF
    143.     PORTB.F6 = 0; //LED OFF
    144.     PORTB.F7 = 0; //LED OFf
    145.     Delay_ms(1000); //1 Second Delay
    146.     }
    147.     }
    148.     }
    149.    }
    150.  
    Moderators note: AGAIN, Please use code tags for pieces of code
     
    Last edited by a moderator: Apr 27, 2016
  9. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    '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.
     
  10. Muhammad Muaaz Khan

    Thread Starter New Member

    Apr 26, 2016
    20
    0
    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.
    Code (C):
    1.  
    2.  
    3. [*]void main()
    4. [*]{
    5. [*]  TRISD.F0 = 1; //Configure 1st bit of PORTD as input
    6. [*]TRISD.F1 = 1; //Configure 1st bit of PORTD as input
    7. [*]TRISD.F2 = 1; //Configure 1st bit of PORTD as input
    8. [*]TRISD.F3 = 1; //Configure 1st bit of PORTD as input
    9. [*]TRISB.F0 = 0; //Makes PORTB0 or RB0 Output Pin
    10. [*]   TRISB.F1 = 0; //Makes PORTB1 or RB0 Output Pin
    11. [*]    TRISB.F2 = 0; //Makes PORTB0 or RB0 Output Pin
    12. [*]   TRISB.F3 = 0; //Makes PORTB1 or RB0 Output Pin
    13. [*]    TRISB.F4 = 0; //Makes PORTB0 or RB0 Output Pin
    14. [*]   TRISB.F5 = 0; //Makes PORTB1 or RB0 Output Pin
    15. [*]    TRISB.F6 = 0; //Makes PORTB0 or RB0 Output Pin
    16. [*]   TRISB.F7 = 0; //Makes PORTB1 or RB0 Output Pin
    17. [*]
    18.  
    19. [*]   while(1) //Infinite Loop
    20. [*]   {
    21. [*]   if(PORTD.F1 == 0) //If the switch1 is pressed
    22. [*]   {
    23. [*]    PORTB.F0 = 1; //LED ON
    24. [*]    PORTB.F1 = 0; //LED OFF
    25. [*]    PORTB.F2 = 0; //LED OFF
    26. [*]    PORTB.F3 = 1; //LED OFF
    27. [*]    PORTB.F4 = 1; //LED ON
    28. [*]    PORTB.F5 = 0; //LED OFF
    29. [*]    PORTB.F6 = 0; //LED OFF
    30. [*]    PORTB.F7 = 1; //LED ON
    31. [*]    Delay_ms(2000); //1 Second Delay
    32. [*]    PORTB.F0 = 0; //LED OFF
    33. [*]    PORTB.F3 = 0; //LED OFF
    34. [*]    PORTB.F4 = 0; //LED OFF
    35. [*]    PORTB.F5 = 0; //LED OFF
    36. [*]    PORTB.F6 = 0; //LED OFF
    37. [*]    PORTB.F7 = 0; //LED OFF
    38. [*]   }
    39. [*]   if(PORTD.F0 == 0) //If the switch 2 is pressed
    40. [*]   {
    41. [*]    PORTB.F1 = 1; //LED ON
    42. [*]    PORTB.F0 = 0; //LED OFF
    43. [*]    PORTB.F2 = 1; //LED ON
    44. [*]    PORTB.F3 = 0; //LED OFF
    45. [*]    PORTB.F4 = 0; //LED OFF
    46. [*]    PORTB.F5 = 1; //LED ON
    47. [*]    PORTB.F6 = 1; //LED ON
    48. [*]    PORTB.F7 = 0; //LED OFF
    49. [*]    Delay_ms(2000); //1 Second Delay
    50. [*]    PORTB.F1 = 0; //LED OFF
    51. [*]    PORTB.F2 = 0; //LED OFF
    52. [*]    PORTB.F3 = 0; //LED OFF
    53. [*]    PORTB.F4 = 0; //LED OFF
    54. [*]    PORTB.F5 = 0; //LED OFF
    55. [*]    PORTB.F6 = 0; //LED OFF
    56. [*]    PORTB.F7 = 0; //LED OFF
    57. [*]
    58.  
    59. [*]   }
    60. [*]   if(PORTD.F3 == 0) //If the switch 3 is pressed
    61. [*]   {
    62. [*]    PORTB.F0 = 0; //LED OFF
    63. [*]    PORTB.F1 = 0; //LED OFF
    64. [*]    PORTB.F2 = 0; //LED OFF
    65. [*]    PORTB.F3 = 0; //LED OFF
    66. [*]    PORTB.F4 = 0; //LED OFF
    67. [*]    PORTB.F5 = 0; //LED OFF
    68. [*]    PORTB.F6 = 0; //LED OFF
    69. [*]    PORTB.F7 = 0; //LED OFF
    70. [*]    Delay_ms(5000); //1 Second Delay
    71. [*]   }
    72. [*]   if(PORTD.F2 == 0) //If the switch 4 is pressed
    73. [*]   if((PORTD.F3 == 0))break; //If the switch is pressed
    74. [*]   {
    75. [*]   while(2) //Infinite Loop
    76. [*]{
    77. [*]    PORTB.F0 = 0; //LED OFF
    78. [*]    PORTB.F1 = 1; //LED ON
    79. [*]    PORTB.F2 = 0; //LED OFF
    80. [*]    PORTB.F3 = 0; //LED OFF
    81. [*]    PORTB.F4 = 0; //LED OFF
    82. [*]    PORTB.F5 = 0; //LED OFF
    83. [*]    PORTB.F6 = 0; //LED OFF
    84. [*]    PORTB.F7 = 0; //LED ON
    85. [*]    Delay_ms(500); //1 Second Delay
    86. [*]    PORTB.F3 = 1; //LED ON
    87. [*]    PORTB.F0 = 0; //LED OFF
    88. [*]    PORTB.F1 = 1; //LED ON
    89. [*]    PORTB.F2 = 0; //LED OFF
    90. [*]    PORTB.F4 = 0; //LED OFF
    91. [*]    PORTB.F5 = 0; //LED OFF
    92. [*]    PORTB.F6 = 0; //LED OFF
    93. [*]    PORTB.F7 = 0; //LED OFF
    94. [*]    Delay_ms(1000); //1 Second Delay
    95. [*]    PORTB.F0 = 1; //LED ON
    96. [*]    PORTB.F1 = 0; //LED OFF
    97. [*]    PORTB.F2 = 1; //LED ON
    98. [*]    PORTB.F3 = 0; //LED OFF
    99. [*]    PORTB.F4 = 0; //LED OFF
    100. [*]    PORTB.F5 = 0; //LED OFF
    101. [*]    PORTB.F6 = 0; //LED OFF
    102. [*]    PORTB.F7 = 0; //LED OFF
    103. [*]    Delay_ms(100); //1 Second Delay   Both legs are off
    104. [*]    PORTB.F0 = 0; //LED OFF
    105. [*]    PORTB.F1 = 0; //LED OFF
    106. [*]    PORTB.F2 = 0; //LED OFF
    107. [*]    PORTB.F3 = 0; //LED OFF
    108. [*]    PORTB.F4 = 0; //LED OFF
    109. [*]    PORTB.F5 = 0; //LED OFF
    110. [*]    PORTB.F6 = 0; //LED OFF
    111. [*]    PORTB.F7 = 0; //LED OFf
    112. [*]    Delay_ms(500); //1 Second Delay   second leg Left leg
    113. [*]    PORTB.F0 = 0; //LED OFF
    114. [*]    PORTB.F1 = 0; //LED OFF
    115. [*]    PORTB.F2 = 0; //LED OFF
    116. [*]    PORTB.F3 = 0; //LED OFF
    117. [*]    PORTB.F4 = 0; //LED OFF
    118. [*]    PORTB.F5 = 1; //LED ON
    119. [*]    PORTB.F6 = 0; //LED OFF
    120. [*]    PORTB.F7 = 0; //LED OFF
    121. [*]    Delay_ms(500); //1 Second Delay
    122. [*]    PORTB.F0 = 0; //LED OFF
    123. [*]    PORTB.F1 = 0; //LED OFF
    124. [*]    PORTB.F2 = 0; //LED OFF
    125. [*]    PORTB.F3 = 0; //LED OFF
    126. [*]    PORTB.F4 = 0; //LED OFF
    127. [*]    PORTB.F5 = 1; //LED ON
    128. [*]    PORTB.F6 = 0; //LED OFF
    129. [*]    PORTB.F7 = 1; //LED ON
    130. [*]    Delay_ms(1000); //1 Second Delay
    131. [*]    PORTB.F0 = 0; //LED OFF
    132. [*]    PORTB.F1 = 0; //LED OFF
    133. [*]    PORTB.F2 = 0; //LED OFF
    134. [*]    PORTB.F3 = 0; //LED OFF
    135. [*]    PORTB.F4 = 1; //LED ON
    136. [*]    PORTB.F5 = 0; //LED OFF
    137. [*]    PORTB.F6 = 1; //LED ON
    138. [*]    PORTB.F7 = 0; //LED OFF
    139. [*]    Delay_ms(100); //1 Second Delay   Both legs are off
    140. [*]    PORTB.F0 = 0; //LED OFF
    141. [*]    PORTB.F1 = 0; //LED OFF
    142. [*]    PORTB.F2 = 0; //LED OFF
    143. [*]    PORTB.F3 = 0; //LED OFF
    144. [*]    PORTB.F4 = 0; //LED OFF
    145. [*]    PORTB.F5 = 0; //LED OFF
    146. [*]    PORTB.F6 = 0; //LED OFF
    147. [*]    PORTB.F7 = 0; //LED OFf
    148. [*]    Delay_ms(1000); //1 Second Delay
    149. [*]   }
    150. [*]   }
    151. [*]   }
    152. [*]   }
    153. [*]
     
  11. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    See line 73. Which loop are you breaking out of and what is the result?
    How about that flow chart?
     
  12. Muhammad Muaaz Khan

    Thread Starter New Member

    Apr 26, 2016
    20
    0
    1. Code (C):
      1.  
      2. [*]*]void main()
      3. [*]{
      4. [*]TRISD.F0 = 1; //Configure 1st bit of PORTD as input
      5. [*]TRISD.F1 = 1; //Configure 1st bit of PORTD as input
      6. [*]TRISD.F2 = 1; //Configure 1st bit of PORTD as input
      7. [*]TRISD.F3 = 1; //Configure 1st bit of PORTD as input
      8. [*]TRISB.F0 = 0; //Makes PORTB0 or RB0 Output Pin
      9. [*]  TRISB.F1 = 0; //Makes PORTB1 or RB0 Output Pin
      10. [*]   TRISB.F2 = 0; //Makes PORTB0 or RB0 Output Pin
      11. [*]  TRISB.F3 = 0; //Makes PORTB1 or RB0 Output Pin
      12. [*]   TRISB.F4 = 0; //Makes PORTB0 or RB0 Output Pin
      13. [*]  TRISB.F5 = 0; //Makes PORTB1 or RB0 Output Pin
      14. [*]   TRISB.F6 = 0; //Makes PORTB0 or RB0 Output Pin
      15. [*]  TRISB.F7 = 0; //Makes PORTB1 or RB0 Output Pin
      16. [*]
      17.  
      18. [*]while(1) //Infinite Loop
      19. [*]{
      20. [*]if(PORTD.F1 == 0) //If the switch1 is pressed
      21. [*]{
      22. [*]   PORTB.F0 = 1; //LED ON
      23. [*]   PORTB.F1 = 0; //LED OFF
      24. [*]   PORTB.F2 = 0; //LED OFF
      25. [*]   PORTB.F3 = 1; //LED OFF
      26. [*]   PORTB.F4 = 1; //LED ON
      27. [*]   PORTB.F5 = 0; //LED OFF
      28. [*]   PORTB.F6 = 0; //LED OFF
      29. [*]   PORTB.F7 = 1; //LED ON
      30. [*]   Delay_ms(2000); //1 Second Delay
      31. [*]   PORTB.F0 = 0; //LED OFF
      32. [*]   PORTB.F3 = 0; //LED OFF
      33. [*]   PORTB.F4 = 0; //LED OFF
      34. [*]   PORTB.F5 = 0; //LED OFF
      35. [*]   PORTB.F6 = 0; //LED OFF
      36. [*]   PORTB.F7 = 0; //LED OFF
      37. [*]}
      38. [*]if(PORTD.F0 == 0) //If the switch 2 is pressed
      39. [*]{
      40. [*]   PORTB.F1 = 1; //LED ON
      41. [*]   PORTB.F0 = 0; //LED OFF
      42. [*]   PORTB.F2 = 1; //LED ON
      43. [*]   PORTB.F3 = 0; //LED OFF
      44. [*]   PORTB.F4 = 0; //LED OFF
      45. [*]   PORTB.F5 = 1; //LED ON
      46. [*]   PORTB.F6 = 1; //LED ON
      47. [*]   PORTB.F7 = 0; //LED OFF
      48. [*]   Delay_ms(2000); //1 Second Delay
      49. [*]   PORTB.F1 = 0; //LED OFF
      50. [*]   PORTB.F2 = 0; //LED OFF
      51. [*]   PORTB.F3 = 0; //LED OFF
      52. [*]   PORTB.F4 = 0; //LED OFF
      53. [*]   PORTB.F5 = 0; //LED OFF
      54. [*]   PORTB.F6 = 0; //LED OFF
      55. [*]   PORTB.F7 = 0; //LED OFF
      56. [*]
      57.  
      58. [*]}
      59. [*]if(PORTD.F3 == 0) //If the switch 3 is pressed
      60. [*]{
      61. [*]   PORTB.F0 = 0; //LED OFF
      62. [*]   PORTB.F1 = 0; //LED OFF
      63. [*]   PORTB.F2 = 0; //LED OFF
      64. [*]   PORTB.F3 = 0; //LED OFF
      65. [*]   PORTB.F4 = 0; //LED OFF
      66. [*]   PORTB.F5 = 0; //LED OFF
      67. [*]   PORTB.F6 = 0; //LED OFF
      68. [*]   PORTB.F7 = 0; //LED OFF
      69. [*]   Delay_ms(5000); //1 Second Delay
      70. [*]}
      71. [*]if((PORTD.F2 == 0))break; //If the switch is pressed
      72. [*]{
      73. [*]while(2) //Infinite Loop
      74. [*]{
      75. [*]   PORTB.F0 = 0; //LED OFF
      76. [*]   PORTB.F1 = 1; //LED ON
      77. [*]   PORTB.F2 = 0; //LED OFF
      78. [*]   PORTB.F3 = 0; //LED OFF
      79. [*]   PORTB.F4 = 0; //LED OFF
      80. [*]   PORTB.F5 = 0; //LED OFF
      81. [*]   PORTB.F6 = 0; //LED OFF
      82. [*]   PORTB.F7 = 0; //LED ON
      83. [*]   Delay_ms(500); //1 Second Delay
      84. [*]   PORTB.F3 = 1; //LED ON
      85. [*]   PORTB.F0 = 0; //LED OFF
      86. [*]   PORTB.F1 = 1; //LED ON
      87. [*]   PORTB.F2 = 0; //LED OFF
      88. [*]   PORTB.F4 = 0; //LED OFF
      89. [*]   PORTB.F5 = 0; //LED OFF
      90. [*]   PORTB.F6 = 0; //LED OFF
      91. [*]   PORTB.F7 = 0; //LED OFF
      92. [*]   Delay_ms(1000); //1 Second Delay
      93. [*]   PORTB.F0 = 1; //LED ON
      94. [*]   PORTB.F1 = 0; //LED OFF
      95. [*]   PORTB.F2 = 1; //LED ON
      96. [*]   PORTB.F3 = 0; //LED OFF
      97. [*]   PORTB.F4 = 0; //LED OFF
      98. [*]   PORTB.F5 = 0; //LED OFF
      99. [*]   PORTB.F6 = 0; //LED OFF
      100. [*]   PORTB.F7 = 0; //LED OFF
      101. [*]   Delay_ms(100); //1 Second Delay   Both legs are off
      102. [*]   PORTB.F0 = 0; //LED OFF
      103. [*]   PORTB.F1 = 0; //LED OFF
      104. [*]   PORTB.F2 = 0; //LED OFF
      105. [*]   PORTB.F3 = 0; //LED OFF
      106. [*]   PORTB.F4 = 0; //LED OFF
      107. [*]   PORTB.F5 = 0; //LED OFF
      108. [*]   PORTB.F6 = 0; //LED OFF
      109. [*]   PORTB.F7 = 0; //LED OFf
      110. [*]   Delay_ms(500); //1 Second Delay   second leg Left leg
      111. [*]   PORTB.F0 = 0; //LED OFF
      112. [*]   PORTB.F1 = 0; //LED OFF
      113. [*]   PORTB.F2 = 0; //LED OFF
      114. [*]   PORTB.F3 = 0; //LED OFF
      115. [*]   PORTB.F4 = 0; //LED OFF
      116. [*]   PORTB.F5 = 1; //LED ON
      117. [*]   PORTB.F6 = 0; //LED OFF
      118. [*]   PORTB.F7 = 0; //LED OFF
      119. [*]   Delay_ms(500); //1 Second Delay
      120. [*]   PORTB.F0 = 0; //LED OFF
      121. [*]   PORTB.F1 = 0; //LED OFF
      122. [*]   PORTB.F2 = 0; //LED OFF
      123. [*]   PORTB.F3 = 0; //LED OFF
      124. [*]   PORTB.F4 = 0; //LED OFF
      125. [*]   PORTB.F5 = 1; //LED ON
      126. [*]   PORTB.F6 = 0; //LED OFF
      127. [*]   PORTB.F7 = 1; //LED ON
      128. [*]   Delay_ms(1000); //1 Second Delay
      129. [*]   PORTB.F0 = 0; //LED OFF
      130. [*]   PORTB.F1 = 0; //LED OFF
      131. [*]   PORTB.F2 = 0; //LED OFF
      132. [*]   PORTB.F3 = 0; //LED OFF
      133. [*]   PORTB.F4 = 1; //LED ON
      134. [*]   PORTB.F5 = 0; //LED OFF
      135. [*]   PORTB.F6 = 1; //LED ON
      136. [*]   PORTB.F7 = 0; //LED OFF
      137. [*]   Delay_ms(100); //1 Second Delay   Both legs are off
      138. [*]   PORTB.F0 = 0; //LED OFF
      139. [*]   PORTB.F1 = 0; //LED OFF
      140. [*]   PORTB.F2 = 0; //LED OFF
      141. [*]   PORTB.F3 = 0; //LED OFF
      142. [*]   PORTB.F4 = 0; //LED OFF
      143. [*]   PORTB.F5 = 0; //LED OFF
      144. [*]   PORTB.F6 = 0; //LED OFF
      145. [*]   PORTB.F7 = 0; //LED OFf
      146. [*]   Delay_ms(1000); //1 Second Delay
      147. [*]}
      148. [*]}
      149. [*]}
      150. [*]}
      151. [/LIST]
      152.  
      AGAIN, Please use code tags when posting pieces of code
     
  13. Muhammad Muaaz Khan

    Thread Starter New Member

    Apr 26, 2016
    20
    0
    it does not working
     
  14. dannyf

    Well-Known Member

    Sep 13, 2015
    1,830
    365
    It should.
     
  15. Muhammad Muaaz Khan

    Thread Starter New Member

    Apr 26, 2016
    20
    0
    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.
     
  16. dannyf

    Well-Known Member

    Sep 13, 2015
    1,830
    365
    I'm confused. If your code worked as you said, we are done.

    If your work didn stop where you wanted it to, your code didn't work.

    So the two statements cannot be correct at the same time.
     
  17. Muhammad Muaaz Khan

    Thread Starter New Member

    Apr 26, 2016
    20
    0
    my code work but not work for while loop
     
  18. dannyf

    Well-Known Member

    Sep 13, 2015
    1,830
    365
    If your code works then it is all set.
     
  19. Muhammad Muaaz Khan

    Thread Starter New Member

    Apr 26, 2016
    20
    0
    But it did not work with second wile loop which i used in PortD.F2
     
  20. dannyf

    Well-Known Member

    Sep 13, 2015
    1,830
    365
    So which one isn't it?
     
Loading...