<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/12658/how-to-read-16-bit-adc-value-on-nrf-master-app-not-8-bit</link><description>Hello! 
 I&amp;#39;m trying to read 16 bit ADC Value on nRF Master app instead of only 8 bit, and I know that there is the example code on github which is only send me the 8 bit &amp;quot;uint8_t adc_result&amp;quot; data to nRF mater app 
 /* Interrupt handler for ADC data</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sat, 19 Mar 2016 06:35:45 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/12658/how-to-read-16-bit-adc-value-on-nrf-master-app-not-8-bit" /><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48109?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 06:35:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2b5b38fc-c349-4dd2-8638-42dfb8223972</guid><dc:creator>FormerMember</dc:creator><description>&lt;p&gt;RK, I really appreciated your kindly help. I decied to place the code only inside of the &amp;quot;ADC_IRQHandler&amp;quot;  for now. Could you please help me check it?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/* Interrupt handler for ADC data ready event */
void ADC_IRQHandler(void)
{
   static uint16_t adc_result;
   static uint16_t adc_len = sizeof( adc_result);

    /* Clear dataready event */
  NRF_ADC-&amp;gt;EVENTS_END = 0;  

  /* Write ADC result both to the UART and over BLE */
    adc_result = NRF_ADC-&amp;gt;RESULT;

            ble_gatts_hvx_params_t hvx_params;

            memset(&amp;amp;hvx_params, 0, sizeof(hvx_params));

            hvx_params.p_data = (uint8_t*)&amp;amp;adc_result;
	        hvx_params.p_len  = &amp;amp;adc_len;
            hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;



    //Use the STOP task to save current. Workaround for PAN_028 rev1.5 anomaly 1.
  NRF_ADC-&amp;gt;TASKS_STOP = 1;

    //Release the external crystal
    sd_clock_hfclk_release();
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48108?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 06:15:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6ff3a3a3-c901-43ad-bb76-9b606c8b5ca9</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;either way - put the code in the ble_nus_send_string function (although it&amp;#39;s no-longer NUS and no-longer sending a string so .. not really named correctly) or put it right in the ADC handler, it doesn&amp;#39;t matter either way. I&amp;#39;d have a function called send_adc_data( uint16_t adc_data ) which would do it just for clean code but it doesn&amp;#39;t really matter.&lt;/p&gt;
&lt;p&gt;The whole point is you put the value in a static uint16_t somewhere, and you put the length of the uint16_t (which is 2) in another uint16_t somewhere and you set the p_data of the hvx_params to a pointer to the uint16_t, properly cast as a byte-pointer, and a pointer to the length in the p_len and the softdevice will then send 2 bytes starting at the first byte of the reading.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48107?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 06:08:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:48472739-85bc-4fd9-9aea-b17397fcd0f6</guid><dc:creator>FormerMember</dc:creator><description>&lt;p&gt;ok,I&amp;#39;m sorry. I was a little bit of confuse. so do you mean that your code above:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static uint16_t adc_value;
static uint16_t adc_len = sizeof( adc_value);

.... // get the value into the adc_value handle in any way you like

hvx_params.handle = p_nus-&amp;gt;rx_handles.value_handle;
hvx_params.p_data = (uint8_t*)&amp;amp;adc_value;
hvx_params.p_len  = &amp;amp;adc_len;
hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;

return sd_ble_gatts_hvx(p_nus-&amp;gt;conn_handle, &amp;amp;hvx_params);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;is all in the ADC_IRQHandler() ? and we don&amp;#39;t need to worry about the ble_nus_send_string command?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48106?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 06:03:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2715ae91-158f-42e0-9f19-153479ee7165</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;No that&amp;#39;s complete rubbish. You already passed the address of the data into your function, then you&amp;#39;re taking the address of it again and sending that. Similarly adc_len is the size of &amp;#39;string&amp;#39; and string is a pointer to uint16_t so that&amp;#39;s 4. So you are now sending 4 bytes which represent the address of the static variable adc_result, which will always be the same.&lt;/p&gt;
&lt;p&gt;sorry but this is basic C. Instead of just guessing around trying random things you need to understand what a pointer is, what a length is and how to convert from one to another.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48105?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 05:57:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6f96817e-e97b-4abe-ad86-8de6c0240834</guid><dc:creator>FormerMember</dc:creator><description>&lt;p&gt;ADC_IRQHandler :&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/* Interrupt handler for ADC data ready event */
void ADC_IRQHandler(void)
{
   static uint16_t adc_result;

    /* Clear dataready event */
  NRF_ADC-&amp;gt;EVENTS_END = 0;  

  /* Write ADC result both to the UART and over BLE */
    adc_result = NRF_ADC-&amp;gt;RESULT;
    app_uart_put(adc_result);
    ble_nus_send_string(&amp;amp;m_nus, &amp;amp;adc_result);
    nrf_gpio_pin_toggle(LED_3);        //indicate on LED that the ADC interrupt handler is executing

    //Use the STOP task to save current. Workaround for PAN_028 rev1.5 anomaly 1.
  NRF_ADC-&amp;gt;TASKS_STOP = 1;

    //Release the external crystal
    sd_clock_hfclk_release();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;ble_nus_send_string :&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;uint32_t ble_nus_send_string(ble_nus_t * p_nus, uint16_t * string)
    {
        ble_gatts_hvx_params_t hvx_params;
    
        if (p_nus == NULL)
        {
            return NRF_ERROR_NULL;
        }
    
        if ((p_nus-&amp;gt;conn_handle == BLE_CONN_HANDLE_INVALID) || (!p_nus-&amp;gt;is_notification_enabled))
        {
            return NRF_ERROR_INVALID_STATE;
        }
    
        if (length &amp;gt; BLE_NUS_MAX_DATA_LEN)
        {
            return NRF_ERROR_INVALID_PARAM;
        }
    
static uint16_t adc_len = sizeof(string);

        memset(&amp;amp;hvx_params, 0, sizeof(hvx_params));
    
        hvx_params.handle = p_nus-&amp;gt;rx_handles.value_handle;
        hvx_params.p_data = (uint8_t*)&amp;amp;string;
        hvx_params.p_len  = &amp;amp;adc_len;
        hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
    
        return sd_ble_gatts_hvx(p_nus-&amp;gt;conn_handle, &amp;amp;hvx_params);
    }
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48104?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 05:56:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8ac3174e-787c-4f2e-b3db-4f9331413fde</guid><dc:creator>FormerMember</dc:creator><description>&lt;p&gt;Could you please help me check following of my code?&lt;code&gt;enter code here&lt;/code&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48103?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 05:41:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a554c118-ef37-437a-9b6b-fb36f6a44aab</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;well you are doing something wrong then, go check your code. Use something other than the MCP, like Lightblue, see what the actual data sent it, Lightblue doesn&amp;#39;t try to interpret it. If you are only getting one byte, you are only &lt;em&gt;sending&lt;/em&gt; one byte. adc_len must be 2 to send 2 bytes. What is it? Try debugging.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48102?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 05:35:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b423d94-3adb-4c86-a81a-47382bbb075a</guid><dc:creator>FormerMember</dc:creator><description>&lt;p&gt;But, on the MCP it only received the ADC value as 0xDF (as 8 bit) and it stay unchanged.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48101?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 05:26:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:af40cace-ac0d-4ad0-a30e-0426a7d50d56</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;What? It&amp;#39;s a standard C cast from a uint16_t pointer to a uint8_t pointer so you can put the pointer in a structure which wants a uint8_t pointer without the compiler complaining.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48100?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 05:21:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c4cf8121-bf3e-4d01-9223-0279dbadb523</guid><dc:creator>FormerMember</dc:creator><description>&lt;p&gt;could you please tell me what this code does? hvx_params.p_data = (uint8_t*)&amp;amp;adc_value;&lt;/p&gt;
&lt;p&gt;Does it send the ADC value as 16 bit by using this code, and what the uint8_t doing in here?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48099?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 05:13:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:205fcdeb-0a4a-4b0f-80a2-5d3cbdcebf36</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;I just edited it to put the pointer to length instead of the length in the structure, that&amp;#39;s what I get for typing code before drinking coffee, is that where you were seeing the compile error, because otherwise it makes no sense, that&amp;#39;s a standard cast from a uint16_t* to a uint8_t*&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48098?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 05:01:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:50da8596-54c8-47c7-bdcb-8fa24aa67f08</guid><dc:creator>FormerMember</dc:creator><description>&lt;p&gt;Hi! RK, your solution above doesn&amp;#39;t work for me. I still got the error when compiler it on keil. &amp;quot; &amp;#39;uint8_t &lt;em&gt;&amp;#39; (aka &amp;#39;unsigned char&amp;#39;&lt;/em&gt;&amp;#39;) from smaller integer type &amp;#39;uint16_t&amp;#39; (aka &amp;#39;unsigned short&amp;#39;) Also on MCP it still display the 8 bit 0xDF. Do u have any other better solution?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to Read 16 bit ADC Value on nRF Master app? not 8 bit!</title><link>https://devzone.nordicsemi.com/thread/48097?ContentTypeID=1</link><pubDate>Sat, 19 Mar 2016 00:50:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:28634826-cb42-4d55-a9fa-47a112b43855</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;This seems to be much the same as your last question.&lt;/p&gt;
&lt;p&gt;For a start &amp;quot;it will show me endless ERROR&amp;quot;. What will show you endless ERROR, compilation, logging from the device, the master app? Where are you seeing this ERROR?&lt;/p&gt;
&lt;p&gt;Your code won&amp;#39;t work anyway because the value you&amp;#39;re reading you&amp;#39;re putting into a stack variable &lt;code&gt;adc_result&lt;/code&gt; which will be gone by the time the softdevice sends it out, you need to put it into something static.&lt;/p&gt;
&lt;p&gt;As I tried to explain to you before, data is just data, all you want to do is send out two bytes of information. If you are seeing ERROR on the receiving end it&amp;#39;s not because there&amp;#39;s an error, it&amp;#39;s because it&amp;#39;s trying to interpret the data in some way in order to show it to you and is unable to do so, possibly because it&amp;#39;s expecting ascii strings or something. Try using something like Lightblue which shows you the raw bytes and doesn&amp;#39;t try to interpret them. Since in the end you&amp;#39;re not going to be using the MCP to receive your data, but an app of some sort, getting hung up on how it&amp;#39;s displaying or not displaying two random bytes doesn&amp;#39;t help you.&lt;/p&gt;
&lt;p&gt;To send a uint16_t is very simple,&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static uint16_t adc_value;
static uint16_t adc_len = sizeof( adc_value)

.... // get the value into the adc_value handle in any way you like

hvx_params.handle = p_nus-&amp;gt;rx_handles.value_handle;
hvx_params.p_data = (uint8_t*)&amp;amp;adc_value;
hvx_params.p_len  = &amp;amp;adc_len;
hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;

return sd_ble_gatts_hvx(p_nus-&amp;gt;conn_handle, &amp;amp;hvx_params);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That&amp;#39;s it - your value will now go out as two bytes representing the uint16_t in little endian format. That is everything you need to do.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>