Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing decimal values #158

Open
hakimoune opened this issue Apr 21, 2024 · 6 comments
Open

Parsing decimal values #158

hakimoune opened this issue Apr 21, 2024 · 6 comments

Comments

@hakimoune
Copy link

hakimoune commented Apr 21, 2024

Hello,
I am having a problem sending decimal value like 2.8 to an STM32 DAC, it worked for me only with integer values. for a DAC in 12 bits mode for example, accepted values are only from 0 to 4096. maybe i made a mistake in conversion.
Thank you

@j123b567
Copy link
Owner

j123b567 commented May 7, 2024

Hard to say what are you trying to do without example code

@hakimoune
Copy link
Author

hakimoune commented May 7, 2024

Hello Jan,
for example for this code

static scpi_result_t DAC_MeasureVoltage(scpi_t * context) {
	uint32_t param1;  // Channel number
	double output1 ;

	if (!SCPI_ParamUInt32(context, &param1, TRUE)) {
				return SCPI_RES_ERR;
			}

	if (param1 <= 0 ) {
		return SCPI_ERROR_ILLEGAL_PARAMETER_VALUE;
	} else if (param1 > 2) {
		return SCPI_ERROR_ILLEGAL_PARAMETER_VALUE;
	} else {

		if (param1 == 1) {   // Channel number
			//Channel 1 : PA4
			HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
			output1 = HAL_DAC_GetValue(&hdac, DAC_CHANNEL_1);
		} else {
		//Do something else
		}
	}

	//SCPI_ResultDouble(context, 3.3*output/4095);
	SCPI_ResultDouble(context, output1);
	   return SCPI_RES_OK;
}

it work for integer values from 0 to 4096 but the instruction
SCPI_ResultDouble(context, 3.3*output/4095);
want result in decimal values like 2.3

Thank you

@jancumps
Copy link

jancumps commented May 10, 2024

@hakimoune , have you tried to put 3.3*output/4095 in a variable of type double, and see what its value is?

suggestion to see what's wrong:

double dacval = 3.3 * output1 / 4095; // set breakpoint for value inspection with debugger
SCPI_ResultDouble(context, dacval);
return SCPI_RES_OK;

edit: your code has output in the calculation, instead of output1

@j123b567
Copy link
Owner

I can't se anything wrong except the typo output vs output1 as pointed out by jancumps

@hakimoune
Copy link
Author

hakimoune commented May 12, 2024

It didn't work even with double.
in the following the code with some corrections
I have a 12 bits DAC in a microcontroller and want to send value like : "CONFigure:DAC Channel ,value ", for example :
CONFigure:DAC 1, 2.3 to get 2.3 volts from the DAC

scpi_result_t DAC_ConfigureVoltage(scpi_t * context) {
uint32_t param1; // Channel number
uint32_t param2; // Value 4095 max in binary

double var = param2*4095/3.3;

/* read first parameter if present */
if (!SCPI_ParamUInt32(context, &param1, TRUE)) {
	return SCPI_RES_ERR;
}

/* read second parameter if present */
    if (!SCPI_ParamUInt32(context, &param2, TRUE)) {
        return SCPI_RES_ERR;
    }

    if (param1 <= 0 ) {
          return SCPI_ERROR_ILLEGAL_PARAMETER_VALUE;
        } else if (param1 > 2) {
          return SCPI_ERROR_ILLEGAL_PARAMETER_VALUE;
        } else {

        	if (param1 == 1) {   // Channel number

        		
        		HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, var);
        		HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
        		
			} else {

				//.........
			}

        }
return SCPI_RES_OK;

@hinxx
Copy link

hinxx commented Aug 15, 2024

@hakimoune the DAC_ConfigureVoltage you posted has several issues. Here is a version of it that will set the var variable to a correct uint32_t value (this is what HAL_DAC_SetValue() expects).

static scpi_result_t DAC_ConfigureVoltage(scpi_t * context) {
    uint32_t param1; // Channel number
    double param2; // Value 3.3 max

    /* read first parameter if present */
    if (!SCPI_ParamUInt32(context, &param1, TRUE)) {
        return SCPI_RES_ERR;
    }

    /* read second parameter if present */
    if (!SCPI_ParamDouble(context, &param2, TRUE)) {
        return SCPI_RES_ERR;
    }

    uint32_t var = param2*4095/3.3;

    if (param1 <= 0 ) {
          return SCPI_RES_ERR;
        } else if (param1 > 2) {
          return SCPI_RES_ERR;
        } else {

        	if (param1 == 1) {   // Channel number

        		
        		HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, var);
        		HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
        		
			} else {

				//.........
			}

        }

    return SCPI_RES_OK;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants