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.

Advanced Bit Operation

Other Parts Discussed in Thread: TMS320F28335, DAC8568

Hi! 
I am using the SPI interface of my TMS320F28335 for use the external DAC8568. 
The package is 32 bit length:

For send data I have to write the 16 bit register SpiaRegs.SPITXBUF for for two times consequentially.

As in the screen shot, the format of the package is:


'xxxxxxxxxxxxYYYYYYYYYYYYYYYYxxxx'

So this package is splitted in two, the 16 bit registers format are:

'xxxxxxxxxxxxYYYY' 'YYYYYYYYYYYYxxxx'

where the 'x' mean command/control bit and the 'Y' mean the output value of the DAC: all 0 mean 0V and all 1 mean 5V.

I would like a smart way to change the 'Y' bit only, for change directly the output voltage.

Thank you very much!

Alessandro

  • Alessandro,

    If you are looking to only modify the 'Y' bits of the message but leave the 'x' bits as they are, you can "mask" the Y bits with a temporary variable.

    So let's just use your first 16 bits for example.  Since the last four bits are Y, we will create a mask which sets these four bits to 0 by performing a bitwise AND.  Then, you can just do a bitwise OR of the data with the new Y values.  In the example below, let's say we wanted to make the last four Y bits equal to 0xA.

    Uint16 data1_mask = 0xFFF0;     // Set all bits that you do not want to be changed to '1' in the mask, in this case, all the x's

    send_data1 &= data1_mask;     // perform a bitwise & with the mask, therefore setting all the values subject to change to 0 and leaving the others intact

    send_data1 |= 0xA;      // Do a bitwise OR with the new Y values

    It's a few extra steps, but this way you do not have to worry about setting the X values again.  Is this what you were looking to do?

    Kris

  • Yes, this method is good, thanks!! Is correct this method that start from the value of the voltage that I want to send to the DAC and prepare the two register to be send?

    Uint16 mask_MSB = 0b1111000000000000;

    Uint16 mask_LSB = 0b0000111111111111;

    Uint16 voltage = 0b1010101010101010;   //value of voltage that I want at the output of the DAC

    Uint16 voltage_copy = voltage;

    voltage &= mask_MSB; // xxxx0000000000000000

    voltage = voltage >> 12; //000000000000xxxx

    voltage_copy&= mask_LSB; //0000xxxxxxxxxxxx

    voltage_copy = voltage_copy << 4 //xxxxxxxxxxxx0000

    send_data1 |= voltage ;  

    send_data2 |= voltage_copy ;  


    Is it efficient?