2

I am interfacing PIC32 MCU with 24 inputs and UART. When any inputs comes, this data is transferred to PC via uart. I am using multiple if condition like

if(input1==high)
{
  putsUART("input 1 ON\n");
}
if(input2==high)
{
  putsUART("input 2 ON\n");
}

What this if condition is doing is whenever an input comes, it keep on printing the data like

input 1 ON
input 1 ON
input 1 ON

and it goes on. I want it to be displayed once only. For example, if I applied input1, input 1 ON should be printed once and if remove the input input 1 OFF should be displayed once. I don't know how can I run this if condition only once. Please help. Thanks.!

CODE(just for 3 inputs):

 int main()
 {
  TRISAbits.TRISA6 = 1;
  TRISAbits.TRISA7 = 1;
  TRISGbits.TRISG13 = 1;
  while(1)
  {
   if(PORTAbits.RA6 == 0)         //INPUT 1
  {
   putsUART2("Input: 1 ON\n");
   Delayms(1000);
  }
  if(PORTAbits.RA7 == 0)          //INPUT 2
   {
   putsUART2("Input: 2 ON\n");
   Delayms(1000);
  }
  if(PORTAbits.RG13 == 0)          //INPUT 3
  {
   putsUART2("Input: 3 ON\n");
   Delayms(1000);
  }
 }
}
Aircraft
  • 1,666
  • 1
  • 12
  • 26

1 Answers1

5

You need to remember the state of each input, and only output to the UART if it changes.

Something like this, just for one input so you get the idea :

// NB - put these lines outside your loop
uint8_t uLastRA6State = 0;                // last known state of RA6
uint8_t uLastRA7State = 0;                // last known state of RA7
// ... etc. 
// use one variable for each input, or if memory is tight, you could
// alternatively use one byte per 8 pins, but this more "wasteful"
// version is easier to understand the principle

while (1)
{
    // ...

    // NB - put this code inside your loop
    if (PORTAbits.RA6 != uLastRA6State)
    {
        // save current state
        uLastRA6State = uLastRA6State ? 0 : 1;

        // output change
        if (uLastRA6State)
            putsUART2("Input: 1 ON\n");
        else
            putsUART2("Input: 1 OFF\n");
    }

    // ... do the same for other inputs, e.g.
    if (PORTAbits.RA7 != uLastRA7State)
    {
        // ... similar to above, for RA7
    }
}
Roger Rowland
  • 2,002
  • 4
  • 18
  • 36
  • thanks for giving me this idea. But its still displaying continuous values – Aircraft Sep 15 '15 at 07:40
  • @CZAbhinav Well the code above should never output consecutive ON's or OFF's. Are you saying it *is* doing so? Are your inputs changing too fast for your UART buffer? – Roger Rowland Sep 15 '15 at 08:15
  • yes it is doing so. and I just found out that I have not applied any input but still its showing input 1 ON – Aircraft Sep 15 '15 at 08:27
  • 1
    You might need a pull-up resistors on your pins to keep the inputs to a stable/default level. If an input isn't connected to anything, it can randomly be 0 or 1.. – m.Alin Sep 15 '15 at 08:55
  • I am working on PIC32 starter kit, so I think I dont need any pull ups. Inputs are already stable.! – Aircraft Sep 15 '15 at 08:58
  • @CZAbhinav If you look closely at the code I gave you, you can see it's not possible to get consecutive ON's or OFF's reported. Are you sure you followed this pattern exactly, and for every input? I've added comments to make sure you put the code in the right place. – Roger Rowland Sep 15 '15 at 09:00
  • @RogerRowland exact output when I apply input is Input 1: ON Input 1: Off and then these two lines keep on going.! – Aircraft Sep 15 '15 at 09:25
  • @CZAbhinav How are you providing the input? Have you set the TRIS register correctly for that pin? If input is from a physical button or switch, you may be seeing it bouncing. – Roger Rowland Sep 15 '15 at 09:29
  • Inputs are basically 3.1v coming from a circuit and yes I have set the TRIS registers correctly. I have edited my code – Aircraft Sep 15 '15 at 09:37
  • @CZAbhinav You don't specify the exact device, but make sure that the input pin is not an analog input (some are, by default). – Roger Rowland Sep 15 '15 at 09:49
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/29146/discussion-between-czabhinav-and-roger-rowland). – Aircraft Sep 15 '15 at 09:54
  • @RogerRowland Not knowing the level of whoever may be reading this answer, I think you should probably make it clear that you need to store the state of each input with a separate variable. It would probably help this understanding if you had a more meaningful name for "uInput1", such as "last_RA6". – Tut Sep 15 '15 at 11:26
  • @Tut Ok, good idea, I've edited my answer. – Roger Rowland Sep 15 '15 at 11:32
  • Depending on how well your compiler optimises it may be more efficient to use `uLastRA6State ^= 1`. – David Oct 17 '15 at 08:24