Author Topic: Bit shifting a defined value  (Read 1486 times)

0 Members and 1 Guest are viewing this topic.

Offline PerArduaTopic starter

  • Regular Contributor
  • *
  • Posts: 58
  • Country: gb
Bit shifting a defined value
« on: July 07, 2022, 04:37:27 pm »
I'm in the process of writing a C library for communicating to a DAC60501 using SPI (datasheet: https://www.ti.com/lit/ds/symlink/dac80501.pdf). I'm writing this in MplabX using the XC16 compiler.

The DAC needs at least 24 falling edges of SCLK (pg 24), so I have used the MCC generated library for a 32 bit transfer on my PIC24FJ256GA406.

The actual 'data' to send consists of 20 bits, itself 4 bits to indicate the register being written to, and 16 bits of 'data' (pg 29).

In my .h file (attached), I have defined values for these registers, as per table 4 on page 28 using #define x y.

In my .c file (attached), I attempt to construct a 32 bit variable to send, by first shifting register data by 16 places (line 42), then OR'ing with the value I wish to output on the DAC (line 43). I then pass this 32 bit value to a function (line 44), where I shift it by 8 bits to move the register and data to the MSB of the 32 bits, and then send using an MCC function (line 11).  However, I do not see any output from my DAC.

I've used the debugger (excuse the Nop() on line 10), and have discovered that the register value (DAC_DATA) is not being shifted in the data_out variable on line 42. Is it not possible to use #defines in this way? Or do I need to be more explicit in my declarations?

I now realise that this will affect the init function, but hopefully by learning how I have incorrectly implemented this portion of the code, I will be able to rewrite the init to proper functionality.

The code as attached is a bit messy due to my debugging attempts, but I would also welcome suggestions to improve/standardise my code - I'm still new to programming and very much learning the basics.
« Last Edit: July 07, 2022, 04:43:52 pm by PerArdua »
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3926
  • Country: it
Re: Bit shifting a defined value
« Reply #1 on: July 07, 2022, 04:47:03 pm »
problem 1) you have to remember that the sizeof(int) is 2 in XC16, this is reflected in the preprocessor as well, so if you want to do any 32bit you have to add the L/UL suffix in preprocessor constants and cast to int32_t/uint32_t  in macros
I believe the compiler should warn you about a shift greater than the bit size of the value being shifted though
problem 2) keep data_to_send right aligned
 
The following users thanked this post: PerArdua

Offline PerArduaTopic starter

  • Regular Contributor
  • *
  • Posts: 58
  • Country: gb
Re: Bit shifting a defined value
« Reply #2 on: July 07, 2022, 08:28:46 pm »
Thanks both JPortici and evb149.

JPortici, thanks for your advice re. the preprocessor as well as catching the need to keep the data left aligned - is that from the DB23 on SCLK 1 on page 24 - so you'd want the 32 bits to be xxx... b1, b0 - without left or right justification?

evb149, thank you for your suggestion to use structures - I hadn't come across them yet in my basic knowledge. Would it be 'better' to create the struct (presumably one for each register) in the header, or keep it as a local variable within the init function?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->