gpt4 book ai didi

c - 如何在void函数中插入for循环?

转载 作者:行者123 更新时间:2023-11-30 21:39:30 25 4
gpt4 key购买 nike

是否可以在void function中插入for循环

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++;
}
}
}

从上面的代码中,在void解码中启用代码UIO1?在 super 终端中输入,它将回复ADC值。随后是 UIO2? 直到 UIO4?

while(1)循环中,代码用于扫描UIO1直到UIO4。实际上UIO1直到UIO4都有模拟值可以读取为数字值。我使用 for 循环,因为它看起来更简单。

问题是,在 void Decode 循环中,我收到错误:下标值既不是数组也不是指针。如何读取结果值?

最佳答案

main 中的

resultdecode 中的 result 是两个不同的变量。

如果您希望decode了解main结果,您应该将其作为参数传递:

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 */
}

关于c - 如何在void函数中插入for循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32347331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com