int16 to uint32

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

I have a sine table that is in signed 16 bit 2's compliment format. I need to convert that to an unsigned 32 bit number, left justified and shifted to be an unsigned number, i.e. 00000000 to ffff0000. I hope I made that clear. Any cool ways of doing this?

 

Thanks!

 

Tim Ressel
Portland, OR
timr@earthling.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You must have some fantastic DAC if you need 32 bits. wink I have used only 8 bits tables.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

24 bit codec that tales a 32 bit word.

 

Turns out I don't need it after all. The codec takes signed values. I don't know why I thought otherwise.

 

Thanks for the help anyway!

Tim Ressel
Portland, OR
timr@earthling.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:
You must have some fantastic DAC if you need 32 bits.

What I want to here about is this "left justified".  Let's take a couple values,  say 1 and 256.  With the resulting 0x80000000 loaded left-justified into the unsigned 32-bit slot, how does the receiver distinguish between the two?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If I am understanding your question correctly, the receiver (or codec) takes the first 24 bits and ignores the last 8. My codec is the WM8731. You can look up the datasheet and see how it works.

Tim Ressel
Portland, OR
timr@earthling.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

madhun wrote:
If I am understanding your question correctly,

Is that addressed to me?  If so, my "question" dealt with

madhun wrote:
I need to convert that to an unsigned 32 bit number, left justified ...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you familar with unions to interpet the same data in different manners?

I haven't thought this through completely, but how close do you get with a union of an uint32_t and a struct (or array) of 2 int16_t ?

union {
    uint32_t big_num;
    struct {
        int16_t small_num;
        int16_t dummmy_num;
    }
    struct {
        uint8_t bytes [4];          // Edit: Added bytes.
    }
} Custom_Number;


void main( void) {
    
    CustomNumber num1;

    num1.small_num = -23;
    printf( "%d\n", num1.big_num);      // I believe "%d" does not handle 32 bi int's...
}

Code is untried, and probably has some typo's.

You also might / will have some endian issues.

 

Oops, doesn't work and/or needs fiddling.

2's complement notation of -1 is 0xFFFF, which interpreded as an unsigned number becomes a very big number instead of biased halfway.

So the union trick handles the bit shifting with (almost?) no overhead, but the signs have to be changed manually by adding some offset and flipping bits.

 

Edit:

You could also add another structure into the union if you want to work directly with bytes without relying on compiler optimisation for shifts of 8 bits. Just manipulate the byte you want to modify from the array.

 

Edit:

Why would you need a "cool" way to do this? How often is this calculation performed?

Why not simply convert the table once with a simple / naive algorithm and get on with more interesting stuff?

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com