Question

What I want to do: Turn the resulting 16bit number (after combining the two 8bit #s) into a string to use with my serial send function

The problem: When using itoa, the result becomes negative once it passes the half way point (passing from the 15th bit to 16th) so it is essentially using the 16th bit as the sign bit (unsurprising) where my number is supposed to be unsigned. sprintf also does this. Note that itoa(16) works perfectly, as it should.

The Hardware: atmega16 micro processor (AVR c) 16bit external ADC connected via SPI bus (sends ADC result as two 8bit numbers)

The code:

uint16_t ADC_result = ADC_data_LSB | (ADC_data_MSB<<8); // Combine both halves of the data
unsigned char *outString = "0123456789abcdef";
itoa(ADC_16_result, outString, 10);
send_A_String(outString);

The result of this is that it prints a 15bit signed instead of the original uint16. I have been using itoa to print the results of the 10bit internal ADC, but it is killing me that something this simple is taking me such a long time.

I really appreciate your time.

Was it helpful?

Solution

If you have sprintf

char outString[10];
sprintf(outString, "%hu", ADC_result);

Or, if your ints are 16 bits

sprintf(outString, "%u", ADC_result);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top