This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Need help for Converting string to Hex on ADC

FormerMember
FormerMember

Hello!

I'm trying to use the nRF51422 internal ADC to send the value over BLE then monitoring the value received on the nRF Master control panel on Android phone but I see that the received value is "(0x) 3D,"=" for ADC value, which the second string value "=" is equivalent to 0x3D as shown on asciitable.com. I want to let it shows as (0x) 3D3D

Below is my code for ADC_IRQHandler :

 void ADC_IRQHandler(void)
{
	while(!NRF_ADC->EVENTS_END&&NRF_ADC->BUSY){};
	
	/* Clear dataready event */
  NRF_ADC->EVENTS_END = 0;	
	
//  adc_result = nrf_adc_result_get();
		
	
		uint8_t adc_result[2];
		adc_result[0] = NRF_ADC->RESULT;
		adc_result[1] = NRF_ADC->RESULT >> 8;
		
		ble_adc(&m_adc, adc_result);
		
		
	//Use the STOP task to save current.
  NRF_ADC->TASKS_STOP = 1;
	
	//Release the external crystal
		sd_clock_hfclk_release();
}	

I use the ble_adc command to send the data over BLE as 8 bit unsign integer which from ADC as uint8_t.

And this is the ble_adc function code:

uint32_t ble_adc(ble_adc_t * p_adc, uint8_t *adc_level)
{
    uint32_t err_code = NRF_SUCCESS;
    ble_gatts_value_t gatts_value;

        // Initialize value struct.
        memset(&gatts_value, 0, sizeof(gatts_value));

        gatts_value.len     = sizeof(uint8_t);
        gatts_value.offset  = 0;
        gatts_value.p_value = adc_level;

        // Save new battery value.
        p_adc->adc_level_last = *adc_level;

        // Update database.
        err_code = sd_ble_gatts_value_set(p_adc->conn_handle,
                                          p_adc->adc_level_handles.value_handle,
                                          &gatts_value);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }

        // Send value if connected and notifying.
        if ((p_adc->conn_handle != BLE_CONN_HANDLE_INVALID) && p_adc->is_notification_supported)
        {
            ble_gatts_hvx_params_t hvx_params;

            memset(&hvx_params, 0, sizeof(hvx_params));

            hvx_params.handle = p_adc->adc_level_handles.value_handle;
            hvx_params.p_data = gatts_value.p_value;
			hvx_params.p_len  = &gatts_value.len;
			hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
  
            err_code = sd_ble_gatts_hvx(p_adc->conn_handle, &hvx_params);
        }
        else
        {
            err_code = NRF_ERROR_INVALID_STATE;
        }
    return err_code;
}

However, what I received from the nRF Master control panel is "(0X) 3D,"=" which the “second” received "adc_result" value is "=" in Hex value it should be "0x3D". I've tried a lot methods to solve the problem, but not of them is working. This is the screenshot of what I'm getting on the Phone.

How to convert string to HEX?

If anyone know the solution for how to convert this string as the HEX value, Please help me out.

Thank you!

Parents
  • Update: I thought you were using Nordic UART service, but it turns out you are using a custom one. Sorry about that misunderstanding. Disregard my original answer then.

    In that case, it is as @RK answered. The app offers to interpret the value as a character. But what the app received is exactly one byte, whose value is exactly 0x3D. It is doing exactly as you wish now.

    To make it clearer, here is how I interpret it:

    • What the app display is: (0x) 3D, "=".
    • What it meant to say:
    1. (0x) 3D --- The value of this characteristic in hex is 3D.
    2. = --- The value of this characteristic in ASCII is "="

    Anyhow, side note, remember that ADC value could be as large as 0x03FF. You should store it in an uint16 and send it in 2 bytes.


    Original answer (Should be disregarded):

    Nordic UART service will interpret the bytes you send over as ASCII characters. The byte adc_value whose value is 0x3D would be interpreted as a '=' as you have figured out.

    To get what you want, you need to convert 0x3D into an array of ASCII characters. I enjoy the solution found here for that. After you got the array of ASCII characters, send that array, not the raw ADC value.

    Another thing to note, ADC value's maximum value is 0x03FF so it should be stored with uint16_t and translated into 3 ASCII characters.

  • FormerMember
    0 FormerMember in reply to Hieu

    Hello! Thanks for your post. But your solution doesn't work for me. Is anyone know what's the solution to convert the ASCII Character to Hex Value??

Reply Children
No Data
Related