This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Uint16 to int(signed) conversion

Other Parts Discussed in Thread: TMS320F28335

I am programming into a TMS320F28335 controlcard. I want to convert variable in into varialbe in2.  in is declared as Uint and in2 is declared as int. Uint holds a 12 bit adc conversion number between 0x0FFF (3.3V adc (4095)) and 0x0000 (0V adc (0)). I want to convert in into in2 so that in2 holds a 12 bit signed number between 2047 and -2048 without overflow.

  • Amit Aryal said:
    Uint holds a 12 bit adc conversion number between 0x0FFF (3.3V adc (4095)) and 0x0000 (0V adc (0)). I want to convert in into in2 so that in2 holds a 12 bit signed number between 2047 and -2048 without overflow.

    That is a semantic contradiction.

  • sorry i mean in not Uint rephrasing,

    in is declared as Uint and in2 is declared as int. Variable in holds a 12 bit adc conversion number between 0x0FFF (3.3V adc (4095)) and 0x0000 (0V adc (0)). I want to convert in into in2 so that in2 holds a 12 bit signed integer between 2047 and -2048 without overflow.

  • Hi!

    Conversion, actually math, you re looking for:

    in2 = (int)in - 2048;

    Note that "blind" typecasting of uint in to int in is safe in your case because max value it holds is <= 4095. It will overflow if value is larger than 32767.

  • Apart from the simple type cast and the "shift" (subtraction of 2048) - having values from -2048...+2047 representing all-positive ADC values and voltages is a bit dubious. That would fit to truly differential ADCs. However, you need to keep track of your ideas, and be able to read and understand your code later on.

  • f. m. said:
    having values from -2048...+2047 representing all-positive ADC values and voltages is a bit dubious

    Depends on ground reference point. What if it is VCC/2? Actually "VCC/2 virtual ground" is how most low-end/low-speed DSP circuits do - use single-rail power supply, single-supply opamps and single-ended ADC/DAC, just lift the ground reference for signals. Note that TMS320 is DSP btw

  • Do you mean I can get between -2048 and +2047 with manipulating the hardware?I don't quite understand what you mean?

  • You are right of course. Only the O.P. did not present any hint that the analogue input originates from a differential signal.

    My point is - one should keep some consistency with a design, else the next one (or even oneself, a few month later) will be quite confused.

  • No, check the datasheet.
    The ADC allows no negative input voltage, you need to expand/compress any input range to that one specified in the datasheet. And the output will be 0 ... 4095.
    BTW, did you notice that this is not quite the proper forum ?

**Attention** This is a public forum