[英]Getting Array Values from the user on PIC18?
First let me show what's working then I will show what's not working. 首先,让我展示什么在工作,然后再展示什么在工作。 This code gives the right result.
此代码给出正确的结果。
unsigned long timeOn;
long d[10];
d[0] = 8;
d[1] = 6;
d[2] = 0;
d[3] = 0;
d[4] = 0;
timeOn = 10000*d[0] + 1000*d[1] + 100*d[2] + 10*d[3] + d[4] ;
printf("%lu",timeOn);
The output : 86000 输出 :86000
If I want the user to input the following values I get a different result and this is the code I have. 如果我希望用户输入以下值,则会得到不同的结果,这就是我的代码。
unsigned long timeOn;
long d[10];
int i;
for(i = 0; i < 5 ; i++)
{
while (!ConsoleIsGetReady());
d[i] = ConsoleGet();
}
timeOn = 10000*d[0] + 1000*d[1] + 100*d[2] + 10*d[3] + d[4] ;
printf("%lu",timeOn);
BYTE ConsoleGet(void)
{
char Temp;
while(IFS1bits.U2RXIF == 0);
Temp = U2RXREG;
IFS1bits.U2RXIF = 0;
return Temp;
}
The output : 619328 输出 :619328
Isn't this suppose to work the same way? 这不是应该以相同的方式工作吗? So how come when the user inputs the values I get a different result?
那么,当用户输入值时,如何得到不同的结果呢? Thank you!
谢谢!
Looks like the console outputs a character (ie code in ASCII), not an integer. 看起来控制台输出一个字符(即ASCII代码),而不是整数。 Just try
d[i] = ConsoleGet() - '0';
只需尝试
d[i] = ConsoleGet() - '0';
in your read loop. 在您的读取循环中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.