pic switching code does not work as i want.

Discussion in 'Embedded Systems and Microcontrollers' started by rizwan_1122, Apr 16, 2016.

  1. rizwan_1122

    Thread Starter New Member

    Oct 19, 2015
    2
    0
    hello,
    i am try to code switching code which work some thing like this
    when code start pin 1 on and pin 2 off
    and continuously check if pin 3 off than
    pin 1,2 off
    else
    if last time pin 1 on than do this
    pin 1 off and pin 2 on
    if last time pin 2 on than do this
    pin 1 on pin 2 off


    i try following code;

    Code (C):
    1.  
    2. int a = 0;
    3.  
    4. void main() {
    5.  
    6.   TRISA.F0 = 0;  //pin1
    7.   TRISA.F1 = 0;  //pin2
    8.  
    9.   TRISA.F2 = 1;  // input pin
    10.  
    11.   PORTA.F0 = 1;
    12.   PORTA.F1 = 0;
    13.  
    14.   Delay_ms(100);
    15.  
    16.   while(1){
    17.   if(PORTA.F2 == 0){
    18.   PORTA.F0 = 0;
    19.   PORTA.F1 = 0;
    20.   Delay_ms(1000);
    21.   if( a == 1)
    22.   {  a=0;}
    23.   else{a=1;}
    24.  
    25.   }
    26.   else
    27.   {
    28.   if(a==1){
    29.   PORTA.F0 = 0;
    30.   PORTA.F1 = 1;
    31.   }
    32.   else  {
    33.   PORTA.F0 = 1;
    34.   PORTA.F1 = 0;
    35.   }
    36.   }
    37.  
    38.   }
    39. }
    40.  
    Moderators note: please use code tags for pieces of code
     
    Last edited by a moderator: Apr 16, 2016
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    1,835
    367
    1. put the pins into gpio mode if there aren't already in it.
    2. google read-modify-write.
     
  3. Picbuster

    Member

    Dec 2, 2013
    376
    50
    Do you mean; which pin goes first high 1 or 2 and take action on the first or last one?
    reset all to zero at pin3?
    what should happen with pin3 after set high remain high and keep all zero?
    if you use # define you are able to name the pins like Inp_PinA and Inp_PinB this makes it more readable.
    Picbuster
     
  4. odm4286

    Active Member

    Sep 20, 2009
    155
    5
    What compiler are you using? Also what PIC? Lastly try to work on how you indent your code, it should look like this.

    Code (C):
    1.  
    2. int a = 0;
    3.  
    4. void main()
    5. {
    6.     TRISA.F0 = 0;  //pin1
    7.     TRISA.F1 = 0;  //pin2
    8.  
    9.     TRISA.F2 = 1;  // input pin
    10.  
    11.     PORTA.F0 = 1;
    12.     PORTA.F1 = 0;
    13.  
    14.     Delay_ms(100);
    15.  
    16.     while(1)
    17.     {
    18.         if(PORTA.F2 == 0)
    19.         {
    20.             PORTA.F0 = 0;
    21.             PORTA.F1 = 0;
    22.             Delay_ms(1000);
    23.             if( a == 1)
    24.             {
    25.                 a=0;
    26.             } else {
    27.                 a=1;
    28.             }
    29.         } else {
    30.             if(a==1)
    31.             {
    32.                 PORTA.F0 = 0;
    33.                 PORTA.F1 = 1;
    34.             } else  {
    35.                 PORTA.F0 = 1;
    36.                 PORTA.F1 = 0;
    37.             }
    38.         }
    39.     }
    40. }
    41.  
    If you don't indent properly it makes it extremely hard to see what is nested inside what. Now that I can somewhat read this code, where are your includes? One more bit of advice, comment your code as you go. It is very easy end up "lost"
     
    JohnInTX likes this.
  5. rizwan_1122

    Thread Starter New Member

    Oct 19, 2015
    2
    0
    I have done thanks to all,
     
Loading...