NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » unsigned int will not return 16 bit int

April 11, 2010
by sask55
sask55's Avatar

I cannot determine what it is I am doing wrong. I am trying to initiate a array integer that can hold up to 200 - 16 bit integers. I have been using
uint16_t x[200];

appears to work. but when a value set into this array is above 32768 it return a negative number. It appears to me that I am getting a 16 bit signed int array. some of the values I need to record will be over 50000. is this not what unsigned int are for.

I am new at this what am I doing wrong?

April 12, 2010
by bretm
bretm's Avatar

When you say it "returns a negative number" what exactly do you mean? Arrays just hold data, they don't have functions that return anything. You just read values from them. If you read a value into a signed integer variable you could see the behavior you describe:

int temp = x[42];

Then "temp" would be signed because you'd be implicitly casting from uint16_t to int.

If you convert the value to a string for display it might also implicitly convert it to a signed value--it depends on the string function you're using.

April 12, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi sask55,

I think the question bretm asked is right on the nose, what do you mean returns a negative number. You are likely seeing this problem because of the way you are formatting the string to be displayed. If you are printing the value out to the lcd or over the serial port to your computer you are probably using a printf format string. In this case you want to use %u for unsigned integers instead of %d for signed integers. Take a look at our printf and scanf tutorial for more in string formatting.

Humberto

April 12, 2010
by sask55
sask55's Avatar

I think that I understand what you are getting at., perhaps the 16 bit unsigned integers are being cast as sighed when I go to look at them. I am very new to this so I am sure there is something that I am missing. I am sending along a bit of code can you tell me how I could send to the serial and or the lcd as 16 bin unsigned int. in this code they both appear as signed intigers that is not much good to me.

    uint16_t x[200];
    int c = 1;

    x[0]= 40000;

   uart_init();
   FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
   stdin = stdout = &uart_stream;

   lcd_init();
   FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);

  lcd_line_two();
  fprintf_P(&lcd_stream, PSTR("a[0]= %d"),x[0]);

  printf_P(PSTR("a[0]= %d"),x[0]);

  delay_ms(5000);

Thanks I really appreciate any help that u may be able to give me

April 12, 2010
by bretm
bretm's Avatar

Yup, you're using %d in two places, which tells it to format it as a signed integer. Change those two spots to %u for unsigned.

printf_P(PSTR("a[0]= %d"),x[0]);
                      ^
April 13, 2010
by sask55
sask55's Avatar

Hi Yes that was the problem. Although I normally do not directly looking at the values in my 16bit unsigned integer array I have been temperately placing a few lines of code in as a means of tracing what was going on in the module as it executed. Anyway thanks a lot I have made the very simple correction. I am now seeing what I was expecting to see all along as well the pc is receiving the data as expected.

Thanks again for the great support

sask 55

Post a Reply

Please log in to post a reply.

Did you know that NerdKits has been featured on Slashdot, Hack A Day, Hacked Gadgets, the MAKE blog, and other DIY-oriented websites? Learn more...