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!

Related