开发者

How to combine two received inputs and read it as one whole number in WINAVR?

I want users to enter 2 digits using c=ReceiveByte() command. For example, I want the user to do the following :

Enter 5
Enter 3
Output number 53 in ascii value on screen ( using hyperterminal )
Store number in a single array
Use that number for other loops etc.

My draft code is :

.
.
int c1[3];
c1[0]=ReceiveByte();
c1[1]=ReceiveByte();
.
.
for(i=0;i<3;i++)
 TransmitByte(c1[i]);
.
.

Is this right ? or am I storing the开发者_StackOverflow社区 2 digits incorrectly ?

Thanks alot for your help !


For output, you don't need to modify c1[0] and c1[1] since these already contain the characters as entered. As your code stands you just need to make sure that c1[2] contains a valid character, e.g.

c1[2] = '\n';

BTW, if you need to get the entered number as an int:

int num = (c1[0] - '0') * 10 + (c1[1] - '0');


Since you seem to be receiving bytes from the Receivebyte() function, you ought to store them as bytes (unsigned char), not as integers, since using an integer to store a single byte wastes it's 3 bytes of memory.

Otherwise, your echo implementation should work - even though you might want to add guards against a user sending characters wich are non-numeric.

You will have to convert the ASCII Characters '5' and '3' (ASCII codes 53 and 51) to theire numeric value (subtract 48 from each as numbers range from 48 to 57 in the ASCII coding scheme)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜