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 |
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 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 |
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) |
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 |
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.
Thanks I really appreciate any help that u may be able to give me |
April 12, 2010 by bretm |
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.
|
April 13, 2010 by sask55 |
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 |
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...
|