Converting Integer to String using puts

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys,

i am currently using atmega48 and a rs232 interface is hooked up to the hyperterminal.

i want to return some integer stored in the microcontroller to be displayed on the hyperterminal.

i've already set up the streams correctly through fdevopen so that i could use putchar, puts etc etc.

but i am not sure how to convert this integer to a string so that i could use puts.

i tried the function toascii() in ctype.h and also itoa() but it doesn't seem to work as the characters displayed on the hyperterminal were weird one.

can you guy show me an example please?

thank you. cheers !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How about posting the relevant code for the itoa() function. Perhaps you did not leave enough room in your string array for the null terminator?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what ive done was

unsigned char *s
unsigned int a = 1000;
SIGNAL(SIG_USART_RECV)
{
itoa(1000,s,10);
puts(s);
}

if this right ? as for the third argument for itoa .. im not sure what it means after reading the manual.
how does it work ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But you are not assigning anything to s. I is just a pointer with no value. You need to supply an array of characters large enough to fit the string plus the trailing NULL. For an unsigned int, this means a 6 character array. Try defining s as:

unsigned char s[6];

Also, you don't say how you are setting up to use puts(). How you do this will depend on what compiler you are using.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SIGNAL() and SIG_USART_RECV are deprecated - look at using ISR() instead.

Also itoa() and puts() inside an ISR are never a great idea. Far better to simply set a flag in the ISR and then do the "long" work back in the main() loop when it sees this flag.

Cliff