-5

is there possible to insert for loop in void function ?

void decode(unsigned char* msg)
{

    int result[5];// can store our value
    unsigned char lala[50];

    if (strstr(msg, "UI01?") != msg) // write in Hyperterminal
    {
        sprintf(lala, "UI01 %d \r\n", result[0]); // UIO1 ADC Value
        sendString(lala);
    }
    else if (strstr(msg, "UI02?") != msg) // write in Hyperterminal
    {
        sprintf(lala, "UI02 %d \r\n", result[1]); // UIO2 ADC Value
        sendString(lala);
    }
    else if (strstr(msg, "UI03?") != msg) // write in Hyperterminal
    {
        sprintf(lala, "UI02 %d \r\n", result[2]); // UIO3 ADC Value
        sendString(lala);
    }
    else if (strstr(msg, "UI04?") != msg) // write in Hyperterminal
    {
        sprintf(lala, "UI02 %d \r\n", result[3]); // UIO4 ADC Value
        sendString(lala);
    }
}


int main()
{

    int result[5]; // can store four value
    int a = 0;     // start from UI0-UI4

    while (1)

    {
        for (a = 0; a < 5; a++)
        {
            AD1CHS0bits.CH0SA = a;  // UI0-UI4
            AD1CHS0bits.CH0NA = 0;  // Vreferences as channel 0 -ve input
            AD1CON1bits.ADON = 1;   // 1 = A/D Converter module is operating
            AD1CON1bits.SAMP = 1;   // sapling mode
            __delay32(50);          // delay after sampling
            AD1CON1bits.SAMP = 0;   //sampling bit  
            while (!AD1CON1bits.DONE);
            result[a] = ADC1BUF0;   // have 4 result
        }
    }

    if (U1STAbits.URXDA == 1) //check if data avilbe
     {
         rxbuf [len] = U1RXREG;
         if (rxbuf[len]== '\r')//press enter
         {
             sendChar (rxbuf[len]);
             sendChar ('\n'); //new line
             decode (rxbuf); // calling decode funtion
             len = 0 ;
         }
         else
         {
             sendChar (rxbuf[len]);
             len++;
         }         
     }
}

from the code above, in void decode enable the code UIO1? is type in Hyperterminal, it will reply ADC value. Followed by UIO2? until UIO4? .

in the while(1) loop, the code is used to scan UIO1 until UIO4. Actually UIO1 until UIO4 have analog value to be read in digital value. I am using for loop, because it look more simple.

The problem is, in the void decode loop, I got error: subscripted value is neither array nor pointer . How to read the result value ?

Nazif Jaafar
  • 21
  • 1
  • 11
  • 1
    why should `void` prevent a `for` loop? – glglgl Sep 02 '15 at 07:47
  • 4
    And if you don't show us your `for` variant of the `decode()` function, we cannot tell what's wrong. – glglgl Sep 02 '15 at 07:48
  • 2
    Where did you call the `decode` function? – Vito Royeca Sep 02 '15 at 07:49
  • 2
    `int result[5]; // can store four value`...why four? – Sourav Ghosh Sep 02 '15 at 07:51
  • @jovit.royeca I have update the code. calling the `decode` @SouravGhosh , because, i decide to store four value. I think five value also can store – Nazif Jaafar Sep 02 '15 at 07:54
  • What is the data type of `rxbuf` ? Maybe you are passing a wrong data type to `decode` ? – Vito Royeca Sep 02 '15 at 08:07
  • You probably want to add a check for NULL on the return value of each strstr call. – Lundin Sep 02 '15 at 08:37
  • 3
    Anyway this is just incoherent ramblings... In the headline you ask how to insert a for loop. Then in the question you claim that you use a loop in the decode function, even though none is present. And then you ask about a compiler error which is not present in the code. And then finally you accept an answer related to parameter passing. – Lundin Sep 02 '15 at 08:41

1 Answers1

0

result in main and result in decode are two different variables.

If you want decode to know about the result from main you should pass it as a parameter:

void decode(unsigned char* msg, int result[5])
{
    // Remove declaration of result 
    // Keep the rest of the code
}

int main()
{
  /* other code */
            decode (rxbuf, result); // calling decode funtion
  /* other code */
}
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82