cancel
Showing results for 
Search instead for 
Did you mean: 

What is the issue when I try to divide an integer value in order to scale it and send it to a DAC ?

JMorl.1
Associate

Hello,

I have an issue with my DAC. I am using a Nucleo STM32L476RG and my project consist of acquiring data from a mems microphone over i2S using the SAI and then send it to the DAC in order to view it with an oscilloscope.

First I initialize the DAC, SAI and a UART. During the while loop I acquire 6 words of 8 bits from the MEMS and use the first three in order to get my microphone value. I need to offset this data in order to put it in the range of the DAC and it worked too. The issue arrise when I try to divide the peak to peak amplitude by a factor of 10 for example. When not dividing I see my signal clear on the oscilloscope but when I try to divide the signal is unreadable .

So far, i have tried to cast as much as possible the type of my variables, change the DAC alignement but nothing works.

Help appreciated,

Sincerely

2 REPLIES 2
Paul1
Lead

// Need data in left 12 bits of 32bit liData

if (HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_L, liData) != HAL_OK)

//This seems to create 24bit data? Is it signed or unsigned?

    //Create an integer from the 3 words of the microphone

   liData = tuiBuffer[0] << 8;

   liData = (((liData | tuiBuffer[1]) << 8) | tuiBuffer[2]);

// Need data in 32bit values before shifting, else upper bits discarded during shift,

// and we want in top of 32bit:

lidata = ((uint32_t)tuiBuffer[0]) << 24 + ((uint32_t)tuiBuffer[1]) << 16 + ((uint32_t)tuiBuffer[0]) <<8;

//This looks like a shift to centre of 16bit(65536/2=32768), not 32bit, and not a divide

   //I am trying to divide here

   liData = 32765 + liData;

//Maybe

  liData = liData + 0x7FFFFFFF; //Centre of 32bit

Paul

Use a PC with example data-sets to test/analyze algorithms

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