cancel
Showing results for 
Search instead for 
Did you mean: 

How to write to the 12 data bits in mcp4821 DAC's register using 8bit data size Spi ?.

As.1
Associate

I'm using stm32 MCU to interface with mcp4821 D A C . The MCU's spi data size is set to 8 bit mode.The D A C has a 16 bit register and (Starting from 0) bit 12 to 15 are configuration bits and the remaining bits are data bits . My problem is ...how should I enter the full scale value by using 8 bit mode spi and how should I separate the data and store it in the first 8 bit register and the other in bit 9 to 12 . I'm new to MCUs and I'm just getting started with programming ... so could anyone please help me with this problem.

3 REPLIES 3
Nikita91
Lead II

As the DAC a use a 16 bits register you should use a 16 bits word (uint16_t).

Put the DAC value in the lower bits and the command in the upper bits.

Then send the high byte then the low byte.

Something like this pseudo code:

uint16_t  word ;
 
word = 0x800u ;      // The dac value, mid range
word |= 0x3000u ;  // Add the command bits
spiCs (0) ;         // Set SPI CS low
spiSendByte (word >> 8) ;     // Send high byte
spiSendByte (word & 0x00FFu) ;  // Send low byte
spiCs (1) ;       // Set SPI CS high

Which STM32, the range is quite extensive, with assorted functionality.

Perhaps as a pair of 6-bit transactions?​

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal

maybe read 16 bit modifyand write. this way you know what is happening.