Skip to main content
cyril2399
Associate III
August 25, 2011
Question

ADC value in buffer

  • August 25, 2011
  • 3 replies
  • 1046 views
Posted on August 25, 2011 at 16:49

Hello,

I have a buffer char buf[128];

And when I use ADC it gave me values like 1204 for eg. by ADC1ConvertedValue[1] which is the channel 1 for me.

How could I store the 4 characters 1,2,0 and 4 into my buffer?

I have tried buf[1]=ADC1ConvertedValue[1];

but this store only the first character which is normal.

thanks

    This topic has been closed for replies.

    3 replies

    Tesla DeLorean
    Guru
    August 25, 2011
    Posted on August 25, 2011 at 17:08

    Presuming ADC1Converted is an array of integers, of range 0-9999, a quick binary to decimal-ASCII conversion.

    buf[0]='0' + (char)(ADC1ConvertedValue[1] / 1000);

    buf[1]='0' + (char)((ADC1ConvertedValue[1] / 100) % 10);

    buf[2]='0' + (char)((ADC1ConvertedValue[1] / 10) % 10);

    buf[3]='0' + (char)(ADC1ConvertedValue[1] % 10);

    buf[4] = 0;

    http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628

    http://www.amazon.com/Primer-Plus-5th-Stephen-Prata/dp/0672326965

    http://www.amazon.com/Absolute-Beginners-Guide-C-2nd/dp/0672305100

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    cyril2399
    cyril2399Author
    Associate III
    August 26, 2011
    Posted on August 26, 2011 at 09:40

    thanks clive.

    I understand that the first four are for 

    thousand,

    hundred

    ,decade,

    unit

    but for what buf[4] = 0;

    is used for?

    I don't understand the last...

    Thanks

    Tesla DeLorean
    Guru
    August 26, 2011
    Posted on August 26, 2011 at 15:07

    The 'buf[4] = 0;' is used to create a NUL terminated string, so you can use other standard C string functions to copy, concatenate, etc the 'buf' content.

    It would permit things like puts(buf) to work.

    http://en.wikipedia.org/wiki/C_string

    http://cplus.about.com/od/learningc/ss/strings.htm

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..