pic switching code does not work as i want.

Thread Starter

rizwan_1122

Joined Oct 19, 2015
2
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;

C:
int a = 0;

void main() {

  TRISA.F0 = 0;  //pin1
  TRISA.F1 = 0;  //pin2

  TRISA.F2 = 1;  // input pin

  PORTA.F0 = 1;
  PORTA.F1 = 0;

  Delay_ms(100);

  while(1){
  if(PORTA.F2 == 0){
  PORTA.F0 = 0;
  PORTA.F1 = 0;
  Delay_ms(1000);
  if( a == 1)
  {  a=0;}
  else{a=1;}

  }
  else
  {
  if(a==1){
  PORTA.F0 = 0;
  PORTA.F1 = 1;
  }
  else  {
  PORTA.F0 = 1;
  PORTA.F1 = 0;
  }
  }

  }
}
Moderators note: please use code tags for pieces of code
 
Last edited by a moderator:

Picbuster

Joined Dec 2, 2013
1,047
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;

C:
int a = 0;

void main() {

  TRISA.F0 = 0;  //pin1
  TRISA.F1 = 0;  //pin2

  TRISA.F2 = 1;  // input pin

  PORTA.F0 = 1;
  PORTA.F1 = 0;

  Delay_ms(100);

  while(1){
  if(PORTA.F2 == 0){
  PORTA.F0 = 0;
  PORTA.F1 = 0;
  Delay_ms(1000);
  if( a == 1)
  {  a=0;}
  else{a=1;}

  }
  else
  {
  if(a==1){
  PORTA.F0 = 0;
  PORTA.F1 = 1;
  }
  else  {
  PORTA.F0 = 1;
  PORTA.F1 = 0;
  }
  }

  }
}
Moderators note: please use code tags for pieces of code
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
 

odm4286

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

C:
int a = 0;

void main()
{
    TRISA.F0 = 0;  //pin1
    TRISA.F1 = 0;  //pin2

    TRISA.F2 = 1;  // input pin

    PORTA.F0 = 1;
    PORTA.F1 = 0;

    Delay_ms(100);

    while(1)
    {
        if(PORTA.F2 == 0)
        {
            PORTA.F0 = 0;
            PORTA.F1 = 0;
            Delay_ms(1000);
            if( a == 1)
            { 
                a=0;
            } else {
                a=1;
            }
        } else {
            if(a==1)
            {
                PORTA.F0 = 0;
                PORTA.F1 = 1;
            } else  {
                PORTA.F0 = 1;
                PORTA.F1 = 0;
            }
        }
    }
}
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"
 
Top