2
\$\begingroup\$

I have a PIC32 starter kit and I am making a program such that when I apply input 1, MCU will send this data to my PC using UART and INPUT 1 ON will be displayed on terminal, same goes for input 2 & 3. These inputs are coming from opto couplers and these are basically 3.1v. So whenever I apply these inputs, MCU pin become high, data is send to PC using UART and INPUT 1 ON or INPUT 2 ON is displayed.

But the problem is let say, I have applied input 1 and terminal is displaying INPUT 1 ON and at the same time if I apply input 2, INPUT 1 ON goes off and INPUT 2 ON started displaying. I want that both INPUT 1 ON INPUT 2 ON should be displayed and if I apply input 3 at the same time INPUT 3 ON should also be displayed along with INPUT 1 ON INPUT 2 ON. I dont know how to make logic for this. Can anyone just help me with this. Thanks

CODE:

    if(PORTAbits.RA6 == 0)
    {
        putsUART2(">>Input: 1 ON\r\n");

    }
    if(PORTAbits.RA6 == 1)
    {
        putsUART2(">>Input: 1 OFF\r\n");

    }
    if(PORTAbits.RA7 == 0)
    {
        putsUART2(">>Input: 2 ON\r\n");

    }
    if(PORTAbits.RA7 == 1)
    {
        putsUART2(">>Input: 2 OFF\r\n");

    }
    if(PORTGbits.RG13 == 0)
    {
        putsUART2(">>Input: 3 ON\r\n");

    }
    if(PORTGbits.RG13 == 1)
    {
        putsUART2(">>Input: 3 OFF\r\n");

    }

EDIT

Right now what I am trying to do is if(PORTAbits.RA6 == 0) the it should display Input: 1 ON and if if(PORTAbits.RA6 == 1) then it should display Input: 1 OFF and same for other inputs. But what's happening is when there is no input Input: 1 OFF Input: 2 OFF Input: 3 OFF is displayed but when if(PORTAbits.RA6 == 0) comes it shows Input: 1 ON for just one time and then continuously displays Input: 1 OFF Input: 2 OFF Input: 3 OFF. I dont know why its behaving like this.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Your current code shows (at most) one message, because the second and third parts are in the else statements. Someone deleted J.A. 's answer, but he was totally correct on this part. (Which 'owner' did this deleting an why?? It is half of the solution!)

You second problem is probably that (when you remove the 'else' keywords) each message overwrites the previous one or is written to a separate line. You should write \r\n only once, for instance like this:

char newline = 0;
if(PORTAbits.RA6 == 0){
   putsUART2("Input: 1 ON ");
   newline = 1;
}

if(PORTAbits.RA7 == 0){
   putsUART2("Input: 2 ON ");
   newline = 1;
}

if(PORTGbits.RG13 == 0){
   putsUART2("Input: 3 ON ");
   newline = 1;
}

if( newline ){
   putsUART2("\r\n");
}
\$\endgroup\$
3
  • \$\begingroup\$ Removin else doesnt solve the problem. It is still ignoring the previous one and showing the current input \$\endgroup\$
    – Aircraft
    Commented Aug 25, 2015 at 5:57
  • \$\begingroup\$ As I say, it solves half the problem, because then the code still overwrites the previous line. \$\endgroup\$ Commented Aug 25, 2015 at 6:36
  • \$\begingroup\$ Yeah thats okay. I have edited my code & question can you pls have a look at this. \$\endgroup\$
    – Aircraft
    Commented Aug 25, 2015 at 6:47
0
\$\begingroup\$

UART is serial protocol, so you can't transmit data paralelly as you want over one UART port at the same time, you're "killing" your serial port with this. Read this article from Wikipedia about UART

Try with this idea - check all pins, and according to input states set flags, form string according to that flags and send it to your UART2 output which sends data to your PC terminal.

\$\endgroup\$
2
  • \$\begingroup\$ No that flag idea is not working properly because it is displaying me INPUT 1 ON & INPUT 2 ON and then suddenly it changes and displays INPUT 1 ON \$\endgroup\$
    – Aircraft
    Commented Aug 17, 2015 at 12:57
  • \$\begingroup\$ You didn’t wrote correctly your code. Post it in your question. \$\endgroup\$
    – Junior
    Commented Aug 17, 2015 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.