NHS3152 - Measure resitance correct param ADC-I2D-DAC

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

NHS3152 - Measure resitance correct param ADC-I2D-DAC

Jump to solution
2,188 Views
leo94
Contributor IV

Hello, 

I am trying to create code following @driesmoors ' solution to Measure resitance: (NHS3152)

I have modified code from the example: mainndeft2t.c. 

The program follows the following logic:

    0. Wake up when NFC field is detected  

(from dries's answer)

   1. Set the configuration of the analog pins to IOCON_FUNC_

   2. Connect the DAC to ANA1, set a continuous conversion of 1.25V (used: adcdac_nss_example_1)

   3. Connect the I2D to ANA4, configure correctly and start a conversion. Only when         a conversion is started, current will start flowing over your resistor. The current             will continue to flow when the conversion ends. -> a (used: i2d_nss_example_1)

   4. Connect the ADC to ANA4, and measure the voltage. -> v4 (used: adcdac_nss_example_3)

   5. Connect the ADC to ANA1, and measure the voltage. -> v1

(New insturcitons)

   6. R= V(V1-V2)/(I_pico * 10^-6)

   7. Display resitance value on screen phone

 

I connected a 100KOHM resitor between ana1 and ana4. The code runs but i am not getting a close resistor value. I get the following results: 

r=100[Kohm]

adc_1 = 1.34 [V]

adc_4 = 1.04 [V]

current_picoampere = 5*10^5 [pA]

which gives a low res = 0.3/0.5= 0.6 [Ohm]

I have the following questions: 

  1. How do i pass 1.25V ana1? (formula to convert the bits to ana value)
  2. Why am i getting current_picoampere = 5*10^5? is this too high?  
  3. What gain to i pass I2D for 100kOHM, where can i find a table/formula to calculate this based on resistance ? 
  4. What errors are in the code that are giving me such a low res output ? 

 

Here is the code: 

 

/*
FROM https://community.nxp.com/t5/NFC/Measure-resistance-NHS3152/m-p/831045

1. Set the configuration of the analog pins to IOCON_FUNC_1
2. Connect the DAC to ANA1, set a continuous conversion of 1.25V.
3. Connect the I2D to ANA4, configure correctly and start a conversion. Only when a conversion is started, current will start flowing over your resistor. The current will continue to flow when the conversion ends. -> a
4. Connect the ADC to ANA4, and measure the voltage. -> v4
5. Connect the ADC to ANA1, and measure the voltage. -> v1

*/



/* ------------------------------------------------------------------------- */

#include <string.h>
#include <stdlib.h>
#include <stdio.h> // contains sprintf function (needs to be included to avoid : https://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function
#include "board.h"
#include "ndeft2t/ndeft2t.h"

/* ------------------------------------------------------------------------- */

#define LOCALE "en" /**< Language used when creating TEXT records. */
#define MIME "nhs31xx/example.ndef" /**< Mime type used when creating MIME records. */

/** The URL will be used in a single-record NDEF message. */
#define MAX_URI_PAYLOAD (254 - NDEFT2T_MSG_OVERHEAD(true, NDEFT2T_URI_RECORD_OVERHEAD(true)))
static const uint8_t sUrl[MAX_URI_PAYLOAD + 1 /* NUL */] = "nxp.com/NTAGSMARTSENSOR";

/**
 * The text and the MIME data are always presented together, in a dual-record NDEF message.
 * Payload length is split evenly between TEXT and MIME.
 */
#define MAX_TEXT_PAYLOAD (254 - (NDEFT2T_MSG_OVERHEAD(true, \
        NDEFT2T_TEXT_RECORD_OVERHEAD(true, sizeof(LOCALE) - 1) + \
        NDEFT2T_MIME_RECORD_OVERHEAD(true, sizeof(MIME) - 1)) / 2))
static uint8_t sText[MAX_TEXT_PAYLOAD] = "10 V1-V4";

/** @copydoc MAX_TEXT_PAYLOAD */
#define MAX_MIME_PAYLOAD MAX_TEXT_PAYLOAD
static uint8_t sBytes[MAX_MIME_PAYLOAD] = {0, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE};

/* ------------------------------------------------------------------------- */

/**
 * Used to determine which NDEF message must be generated.
 * - @c true: generate a single-record NDEF message containing a URL.
 * - @c false: generate a dual-record NDEF message containing a TEXT and a MIME record.
 */
static bool sState;

static volatile bool sButtonPressed = false; /** @c true when the WAKEUP button is pressed on the Demo PCB */
static volatile bool sMsgAvailable = false; /** @c true when a new NDEF message has been written by the tag reader. */
static volatile bool sFieldPresent = false; /** @c true when an NFC field is detected and the tag is selected. */

static void GenerateNdef_Url(void);
static void GenerateNdef_TextMime(void);
static void ParseNdef(void);

/* ------------------------------------------------------------------------- */

//Store adc_1, adc_4, current and resistance value
static volatile float adcInput_4 = 5.0;  // 4. store V input 4
static volatile float adcInput_1 = 5.0; //  5. store V input 1
static volatile int current_native; // 3. store I2D ana 4
static volatile int current_picoampere; // 3. store I2D ana4
static volatile double resval = -1; // store resistance calculated:  (dcInput_1-adcInput_4)/Current_picoampere*10e-6

static void adcdac_nss_example_1(void); //continuous DAC ana1
static void i2d_nss_example_1(void); //I2D ana4
static void adcdac_nss_example_3(void); //NEW single shot get ADC value ana4
static void adcdac_nss_example_3_2(void); //NEW single shot get ADC value ana1


/**
 * Handler for PIO0_0 / WAKEUP pin.
 * Overrides the WEAK function in the startup module.
 */
void PIO0_IRQHandler(void)
{
    Chip_GPIO_ClearInts(NSS_GPIO, 0, 1);
    sButtonPressed = true; /* Handled in main loop */
}

/**
 * Called under interrupt.
 * @see NDEFT2T_FIELD_STATUS_CB
 * @see pNdeft2t_FieldStatus_Cb_t
 */
void App_FieldStatusCb(bool status)
{
    if (status) {
        LED_On(LED_RED);
    }
    else {
        LED_Off(LED_RED);
    }
    sFieldPresent = status; /* Handled in main loop */
}

/**
 * Called under interrupt.
 * @see NDEFT2T_MSG_AVAILABLE_CB
 * @see pNdeft2t_MsgAvailable_Cb_t
 */
void App_MsgAvailableCb(void)
{
    sMsgAvailable = true; /* Handled in main loop */
}

/* ------------------------------------------------------------------------- */

/** Generates a single-record NDEF message containing a URL, and copies it to the NFC shared memory. */
static void GenerateNdef_Url(void)
{
    uint8_t instance[NDEFT2T_INSTANCE_SIZE];
    uint8_t buffer[NFC_SHARED_MEM_BYTE_SIZE];
    NDEFT2T_CREATE_RECORD_INFO_T recordInfo = {.pString = NULL /* don't care */,
                                               .shortRecord = true,
                                               .uriCode = 0x01 /* "http://www." */};
    NDEFT2T_CreateMessage(instance, buffer, NFC_SHARED_MEM_BYTE_SIZE, true);
    if (NDEFT2T_CreateUriRecord(instance, &recordInfo)) {
        if (NDEFT2T_WriteRecordPayload(instance, sUrl, (int)strlen((char *)sUrl))) {
            NDEFT2T_CommitRecord(instance);
        }
    }
    NDEFT2T_CommitMessage(instance); /* Copies the generated message to NFC shared memory. */
}

/** Generates a dual-record NDEF message containing a TEXT and a MIME record, and copies it to the NFC shared memory. */
static void GenerateNdef_TextMime(void)
{
    uint8_t instance[NDEFT2T_INSTANCE_SIZE];
    uint8_t buffer[NFC_SHARED_MEM_BYTE_SIZE];
    NDEFT2T_CREATE_RECORD_INFO_T textRecordInfo = {.pString = (uint8_t *)"en" /* language code */,
                                                   .shortRecord = true,
                                                   .uriCode = 0 /* don't care */};
    NDEFT2T_CREATE_RECORD_INFO_T mimeRecordInfo = {.pString = (uint8_t *)MIME /* mime type */,
                                                   .shortRecord = true,
                                                   .uriCode = 0 /* don't care */};
    NDEFT2T_CreateMessage(instance, buffer, NFC_SHARED_MEM_BYTE_SIZE, true);
    if (NDEFT2T_CreateTextRecord(instance, &textRecordInfo)) {
        if (NDEFT2T_WriteRecordPayload(instance, sText, sizeof(sText) - 1 /* exclude NUL char */)) {
            NDEFT2T_CommitRecord(instance);
        }
    }
    if (NDEFT2T_CreateMimeRecord(instance, &mimeRecordInfo)) {
        if (NDEFT2T_WriteRecordPayload(instance, sBytes, sizeof(sBytes))) {
            NDEFT2T_CommitRecord(instance);
        }
    }
    NDEFT2T_CommitMessage(instance); /* Copies the generated message to NFC shared memory. */
}

/** Parses the NDEF message in the NFC shared memory, and copies the TEXT and MIME payloads. */
static void ParseNdef(void)
{
    uint8_t instance[NDEFT2T_INSTANCE_SIZE];
    uint8_t buffer[NFC_SHARED_MEM_BYTE_SIZE];
    NDEFT2T_PARSE_RECORD_INFO_T recordInfo;
    int len = 0;
    uint8_t *pData = NULL;

    if (NDEFT2T_GetMessage(instance, buffer, NFC_SHARED_MEM_BYTE_SIZE)) {
        while (NDEFT2T_GetNextRecord(instance, &recordInfo) != false) {
            pData = (uint8_t *)NDEFT2T_GetRecordPayload(instance, &len);
            switch (recordInfo.type) {
                case NDEFT2T_RECORD_TYPE_TEXT:
                    if ((size_t)len <= MAX_TEXT_PAYLOAD) {
                        memcpy(sText, pData, (size_t)len);
                        memset(sText + len, 0, MAX_TEXT_PAYLOAD - (size_t)len);
                    }
                    break;
                case NDEFT2T_RECORD_TYPE_MIME:
                    if ((size_t)len <= MAX_MIME_PAYLOAD) {
                        memcpy(sBytes, pData, (size_t)len);
                        memset(sBytes + len, 0, MAX_MIME_PAYLOAD - (size_t)len);
                    }
                    break;
                default:
                    /* ignore */
                    break;
            }
        }
    }
}


// 1. Set the configuration of the analog pins to IOCON_FUNC_1
// 2. Connect the DAC to ANA1, set a continuous conversion of 1.25V.

void adcdac_nss_example_1(void) // Modified IOCON_ANA0_0 --> IOCON_ANA0_1 & ADCDAC_IO_ANA0_0 --> ADCDAC_IO_ANA0_1
{
//! [adcdac_nss_example_1]
    Chip_IOCON_SetPinConfig(NSS_IOCON, IOCON_ANA0_1, IOCON_FUNC_1); // IOCON_FUN_1
    Chip_ADCDAC_Init(NSS_ADCDAC0);
    Chip_ADCDAC_SetMuxDAC(NSS_ADCDAC0, ADCDAC_IO_ANA0_1);
    Chip_ADCDAC_SetModeDAC(NSS_ADCDAC0, ADCDAC_CONTINUOUS);
    Chip_ADCDAC_WriteOutputDAC(NSS_ADCDAC0, 3000);
//! [adcdac_nss_example_1]
}


/* ------------------------------------------------------------------------- */
// 3. Connect the I2D to ANA4, configure correctly and start a conversion. Only when a conversion is started, current will start flowing over your resistor. The current will continue to flow when the conversion ends. -> a

// FROM i2d_nss_example_1


void i2d_nss_example_1(void)
{
//! [i2d_nss_example_1]
    int i2dValue;
    int i2dNativeValue;
    Chip_IOCON_SetPinConfig(NSS_IOCON, IOCON_ANA0_4, IOCON_FUNC_1); // IOCON_FUN_1
    Chip_I2D_Init(NSS_I2D);
    Chip_I2D_Setup(NSS_I2D, I2D_SINGLE_SHOT, I2D_SCALER_GAIN_10_1, I2D_CONVERTER_GAIN_HIGH, 100);
    Chip_I2D_SetMuxInput(NSS_I2D, I2D_INPUT_ANA0_4);
    Chip_I2D_Start(NSS_I2D);
    while (!(Chip_I2D_ReadStatus(NSS_I2D) & I2D_STATUS_CONVERSION_DONE)) {
        ; /* wait */
    }
    i2dNativeValue = Chip_I2D_GetValue(NSS_I2D);
    i2dValue = Chip_I2D_NativeToPicoAmpere(i2dNativeValue, I2D_SCALER_GAIN_10_1, I2D_CONVERTER_GAIN_HIGH, 100);
    Chip_I2D_DeInit(NSS_I2D);
//! [i2d_nss_example_1]

    current_native = i2dNativeValue;
    current_picoampere = i2dValue;
}

// 4. Connect the ADC to ANA4, and measure the voltage. -> v4

//FROM Example 3 - Single-shot Analog-to-Digital Conversion without IRQ

// The conversion: adc_input_voltage = (native_value * 1.2V) / 2825 + 0.09V
void adcdac_nss_example_3(void) // Modifications:  IOCON_ANA0_5--> IOCON_ANA0_4 & converted adcinput_4= (adcInput_4 * 1.2) / 2825 + 0.09
{
//! [adcdac_nss_example_3]
    Chip_IOCON_SetPinConfig(NSS_IOCON, IOCON_ANA0_4, IOCON_FUNC_1);
    //Chip_ADCDAC_Init(NSS_ADCDAC0);
    Chip_ADCDAC_SetMuxADC(NSS_ADCDAC0, ADCDAC_IO_ANA0_4);
    Chip_ADCDAC_SetInputRangeADC(NSS_ADCDAC0, ADCDAC_INPUTRANGE_WIDE);
    Chip_ADCDAC_SetModeADC(NSS_ADCDAC0, ADCDAC_SINGLE_SHOT);
    Chip_ADCDAC_StartADC(NSS_ADCDAC0);

    while (!(Chip_ADCDAC_ReadStatus(NSS_ADCDAC0) & ADCDAC_STATUS_ADC_DONE)) {
        ; /* Wait until measurement completes. For single-shot mode only! */
    }

    adcInput_4 = Chip_ADCDAC_GetValueADC(NSS_ADCDAC0);
    adcInput_4= (adcInput_4 * 1.2) / 2825 + 0.09; // CONVERSION added line

//! [adcdac_nss_example_3]
}


void adcdac_nss_example_3_2(void) // Modifications:  IOCON_ANA0_5--> IOCON_ANA0_1 & converted adcinput_1= (adcInput_1 * 1.2) / 2825 + 0.09
{
//! [adcdac_nss_example_3]
    Chip_IOCON_SetPinConfig(NSS_IOCON, IOCON_ANA0_1, IOCON_FUNC_1);
    //Chip_ADCDAC_Init(NSS_ADCDAC0);
    Chip_ADCDAC_SetMuxADC(NSS_ADCDAC0, ADCDAC_IO_ANA0_1);
    Chip_ADCDAC_SetInputRangeADC(NSS_ADCDAC0, ADCDAC_INPUTRANGE_WIDE);
    Chip_ADCDAC_SetModeADC(NSS_ADCDAC0, ADCDAC_SINGLE_SHOT);
    Chip_ADCDAC_StartADC(NSS_ADCDAC0);

    while (!(Chip_ADCDAC_ReadStatus(NSS_ADCDAC0) & ADCDAC_STATUS_ADC_DONE)) {
        ; /* Wait until measurement completes. For single-shot mode only! */
    }

    adcInput_1 = Chip_ADCDAC_GetValueADC(NSS_ADCDAC0);
    adcInput_1= (adcInput_1 * 1.2) / 2825 + 0.09; // CONVERSION added line

//! [adcdac_nss_example_3]
}





// MAIN
int main(void)
{
	Board_Init();
	NDEFT2T_Init();
    NVIC_EnableIRQ(PIO0_IRQn); /* PIO0_IRQHandler is called when this interrupt fires. */
    Chip_GPIO_EnableInt(NSS_GPIO, 0, 1);


    // 	for ever,  WHILE field is detected: 2. Connect the DAC to ANA1, set a continuous conversion of 1.25V.
    for (;;) {
    while (sFieldPresent) {

    // 2. Connect the DAC to ANA1  -output Voltage
    	adcdac_nss_example_1();
    // 3. Connect the I2D to ANA4, - read current ana4 through resistor
    	i2d_nss_example_1();
    // 4. Connect the ADC to ANA4, and measure the voltage. ->v4
    	adcdac_nss_example_3();
    // 5. Connect the ADC to ANA1, and measure the voltage. -> v1
    	adcdac_nss_example_3_2();
    // generate the message only once per field???
    	GenerateNdef_TextMime();
    	resval = (adcInput_1-adcInput_4)/(current_picoampere * 10e-5); // (v1-v4)/(I4pico * 10^-6) V/I=R [V/A =ohm]
    	sprintf((char *)sText, "adc_1: %4.2f adc_4: %4.2f pA: %4.d res: %4.4f, nat: %4.d", adcInput_1, adcInput_4,current_picoampere, resval,current_native );//commented in from Thesis, changes payloadText to sText


    // attach message to be read
        if (sMsgAvailable) {
              sMsgAvailable = false;
              ParseNdef();
          }
    }

    Chip_PMU_PowerMode_EnterDeepSleep();
    }
    return 0;

}



 

Thanks, 

Leo

 

P.S. since the update to the community page with the subsequent series of emails for badges, all your emails have gone to spam - maybe this is happening to other clients of yours.

 

0 Kudos
1 Solution
1,767 Views
l95kr
Contributor II

Hi @leo94 

I found the error in you code:

when you convert to pA in line 226 you used the Parameter: I2D_CONVERTER_GAIN_HIGH

while having the I2D configuered with the gain low ( Line 219)

I tried it with:

i2dValue = Chip_I2D_NativeToPicoAmpere(i2dNativeValue, I2D_SCALER_GAIN_10_1, I2D_CONVERTER_GAIN_LOW, 100);

and got a resistor Value of rougly 97K using a 100k resistor

Best wishes,

Lennard

View solution in original post

6 Replies
1,699 Views
leo94
Contributor IV

@l95kr, thanks! 

I just verified it. 

0 Kudos
2,096 Views
leo94
Contributor IV

Hi, is there no solution to this problem, or is it out of the scope of this community? 

 

0 Kudos
2,169 Views
leo94
Contributor IV

Hi Dries, 

Thanks for your help,

I updated Chip_I2D_Setup as suggested:

225 Chip_I2D_Setup(NSS_I2D, I2D_SINGLE_SHOT, I2D_SCALER_GAIN_10_1, I2D_CONVERTER_GAIN_LOW, 100);

and converted pico (this was emabrassing) 

306 resval = (adcInput_1 - adcInput_4) / (current_picoampere * 1e-12)

i also changed output code: 

307 sprintf((char *)sText, "adc_1: %6.2f adc_4: %6.2f current_picoampere: %8.d resistance: %e current_native: %6.d", adcInput_1, adcInput_4,current_picoampere, resval,current_native );

I get the following output: 

  1. adc_1:1.34 adc_4:0.58 current_picoampere:178980 resistance:4.241142e+06 current_native:8949 
  2. adc_1:1.34 adc_4:0.56 current_picoampere:179140 resistance:4.303748e+06 current_native: 8957
  3. adc_1:1.34 adc_4:0.58 current_picoampere: 179220 resistance:4.249684e+06 current_native:8961 

 

Possible things that could be affecting results: 

I am running Debug--> resume connect via LPC-link 2. maybe running the code this way affects outcome? 

I haven't connected anything to the GND pin of NHS3152 - only connection is ana1-resistor[100kOhm]-ana4

Possible conversion errors for V1, V4, I2D

Thanks, 

leo

FULL NEW CODE:

/*
FROM https://community.nxp.com/t5/NFC/Measure-resistance-NHS3152/m-p/831045

1. Set the configuration of the analog pins to IOCON_FUNC_1
2. Connect the DAC to ANA1, set a continuous conversion of 1.25V.
3. Connect the I2D to ANA4, configure correctly and start a conversion. Only when a conversion is started, current will start flowing over your resistor. The current will continue to flow when the conversion ends. -> a
4. Connect the ADC to ANA4, and measure the voltage. -> v4
5. Connect the ADC to ANA1, and measure the voltage. -> v1

*/




/* ------------------------------------------------------------------------- */

#include <string.h>
#include <stdlib.h>
#include <stdio.h> // contains sprintf function (needs to be included to avoid : https://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function
#include "board.h"
#include "ndeft2t/ndeft2t.h"

/* ------------------------------------------------------------------------- */

#define LOCALE "en" /**< Language used when creating TEXT records. */
#define MIME "nhs31xx/example.ndef" /**< Mime type used when creating MIME records. */

/** The URL will be used in a single-record NDEF message. */
#define MAX_URI_PAYLOAD (254 - NDEFT2T_MSG_OVERHEAD(true, NDEFT2T_URI_RECORD_OVERHEAD(true)))
static const uint8_t sUrl[MAX_URI_PAYLOAD + 1 /* NUL */] = "nxp.com/NTAGSMARTSENSOR";

/**
* The text and the MIME data are always presented together, in a dual-record NDEF message.
* Payload length is split evenly between TEXT and MIME.
*/
#define MAX_TEXT_PAYLOAD (254 - (NDEFT2T_MSG_OVERHEAD(true, \
NDEFT2T_TEXT_RECORD_OVERHEAD(true, sizeof(LOCALE) - 1) + \
NDEFT2T_MIME_RECORD_OVERHEAD(true, sizeof(MIME) - 1)) / 2))
static uint8_t sText[MAX_TEXT_PAYLOAD] = "10 V1-V4";

/** @copydoc MAX_TEXT_PAYLOAD */
#define MAX_MIME_PAYLOAD MAX_TEXT_PAYLOAD
static uint8_t sBytes[MAX_MIME_PAYLOAD] = {0, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE};

/* ------------------------------------------------------------------------- */

/**
* Used to determine which NDEF message must be generated.
* - @c true: generate a single-record NDEF message containing a URL.
* - @c false: generate a dual-record NDEF message containing a TEXT and a MIME record.
*/
static bool sState;

static volatile bool sButtonPressed = false; /** @c true when the WAKEUP button is pressed on the Demo PCB */
static volatile bool sMsgAvailable = false; /** @c true when a new NDEF message has been written by the tag reader. */
static volatile bool sFieldPresent = false; /** @c true when an NFC field is detected and the tag is selected. */

static void GenerateNdef_Url(void);
static void GenerateNdef_TextMime(void);
static void ParseNdef(void);

/* ------------------------------------------------------------------------- */

//Store adc_1, adc_4, current and resistance value
static volatile float adcInput_4 = 5.0; // 4. store V input 4
static volatile float adcInput_1 = 5.0; // 5. store V input 1
static volatile int current_native; // 3. store I2D ana 4
static volatile int current_picoampere; // 3. store I2D ana4
static volatile double resval = -1; // store resistance calculated: (dcInput_1-adcInput_4)/Current_picoampere*10e-6

static void adcdac_nss_example_1(void); //continuous DAC ana1
static void i2d_nss_example_1(void); //I2D ana4
static void adcdac_nss_example_3(void); //NEW single shot get ADC value ana4
static void adcdac_nss_example_3_2(void); //NEW single shot get ADC value ana1


/**
* Handler for PIO0_0 / WAKEUP pin.
* Overrides the WEAK function in the startup module.
*/
void PIO0_IRQHandler(void)
{
Chip_GPIO_ClearInts(NSS_GPIO, 0, 1);
sButtonPressed = true; /* Handled in main loop */
}

/**
* Called under interrupt.
* @see NDEFT2T_FIELD_STATUS_CB
* @see pNdeft2t_FieldStatus_Cb_t
*/
void App_FieldStatusCb(bool status)
{
if (status) {
LED_On(LED_RED);
}
else {
LED_Off(LED_RED);
}
sFieldPresent = status; /* Handled in main loop */
}

/**
* Called under interrupt.
* @see NDEFT2T_MSG_AVAILABLE_CB
* @see pNdeft2t_MsgAvailable_Cb_t
*/
void App_MsgAvailableCb(void)
{
sMsgAvailable = true; /* Handled in main loop */
}

/* ------------------------------------------------------------------------- */

/** Generates a single-record NDEF message containing a URL, and copies it to the NFC shared memory. */
static void GenerateNdef_Url(void)
{
uint8_t instance[NDEFT2T_INSTANCE_SIZE];
uint8_t buffer[NFC_SHARED_MEM_BYTE_SIZE];
NDEFT2T_CREATE_RECORD_INFO_T recordInfo = {.pString = NULL /* don't care */,
.shortRecord = true,
.uriCode = 0x01 /* "http://www." */};
NDEFT2T_CreateMessage(instance, buffer, NFC_SHARED_MEM_BYTE_SIZE, true);
if (NDEFT2T_CreateUriRecord(instance, &recordInfo)) {
if (NDEFT2T_WriteRecordPayload(instance, sUrl, (int)strlen((char *)sUrl))) {
NDEFT2T_CommitRecord(instance);
}
}
NDEFT2T_CommitMessage(instance); /* Copies the generated message to NFC shared memory. */
}

/** Generates a dual-record NDEF message containing a TEXT and a MIME record, and copies it to the NFC shared memory. */
static void GenerateNdef_TextMime(void)
{
uint8_t instance[NDEFT2T_INSTANCE_SIZE];
uint8_t buffer[NFC_SHARED_MEM_BYTE_SIZE];
NDEFT2T_CREATE_RECORD_INFO_T textRecordInfo = {.pString = (uint8_t *)"en" /* language code */,
.shortRecord = true,
.uriCode = 0 /* don't care */};
NDEFT2T_CREATE_RECORD_INFO_T mimeRecordInfo = {.pString = (uint8_t *)MIME /* mime type */,
.shortRecord = true,
.uriCode = 0 /* don't care */};
NDEFT2T_CreateMessage(instance, buffer, NFC_SHARED_MEM_BYTE_SIZE, true);
if (NDEFT2T_CreateTextRecord(instance, &textRecordInfo)) {
if (NDEFT2T_WriteRecordPayload(instance, sText, sizeof(sText) - 1 /* exclude NUL char */)) {
NDEFT2T_CommitRecord(instance);
}
}
if (NDEFT2T_CreateMimeRecord(instance, &mimeRecordInfo)) {
if (NDEFT2T_WriteRecordPayload(instance, sBytes, sizeof(sBytes))) {
NDEFT2T_CommitRecord(instance);
}
}
NDEFT2T_CommitMessage(instance); /* Copies the generated message to NFC shared memory. */
}

/** Parses the NDEF message in the NFC shared memory, and copies the TEXT and MIME payloads. */
static void ParseNdef(void)
{
uint8_t instance[NDEFT2T_INSTANCE_SIZE];
uint8_t buffer[NFC_SHARED_MEM_BYTE_SIZE];
NDEFT2T_PARSE_RECORD_INFO_T recordInfo;
int len = 0;
uint8_t *pData = NULL;

if (NDEFT2T_GetMessage(instance, buffer, NFC_SHARED_MEM_BYTE_SIZE)) {
while (NDEFT2T_GetNextRecord(instance, &recordInfo) != false) {
pData = (uint8_t *)NDEFT2T_GetRecordPayload(instance, &len);
switch (recordInfo.type) {
case NDEFT2T_RECORD_TYPE_TEXT:
if ((size_t)len <= MAX_TEXT_PAYLOAD) {
memcpy(sText, pData, (size_t)len);
memset(sText + len, 0, MAX_TEXT_PAYLOAD - (size_t)len);
}
break;
case NDEFT2T_RECORD_TYPE_MIME:
if ((size_t)len <= MAX_MIME_PAYLOAD) {
memcpy(sBytes, pData, (size_t)len);
memset(sBytes + len, 0, MAX_MIME_PAYLOAD - (size_t)len);
}
break;
default:
/* ignore */
break;
}
}
}
}


// 1. Set the configuration of the analog pins to IOCON_FUNC_1
// 2. Connect the DAC to ANA1, set a continuous conversion of 1.25V.

void adcdac_nss_example_1(void) // Modified IOCON_ANA0_0 --> IOCON_ANA0_1 & ADCDAC_IO_ANA0_0 --> ADCDAC_IO_ANA0_1
{
//! [adcdac_nss_example_1]
Chip_IOCON_SetPinConfig(NSS_IOCON, IOCON_ANA0_1, IOCON_FUNC_1); // IOCON_FUN_1
Chip_ADCDAC_Init(NSS_ADCDAC0);
Chip_ADCDAC_SetMuxDAC(NSS_ADCDAC0, ADCDAC_IO_ANA0_1);
Chip_ADCDAC_SetModeDAC(NSS_ADCDAC0, ADCDAC_CONTINUOUS);
Chip_ADCDAC_WriteOutputDAC(NSS_ADCDAC0, 3000);
//! [adcdac_nss_example_1]
}


/* ------------------------------------------------------------------------- */
// 3. Connect the I2D to ANA4, configure correctly and start a conversion. Only when a conversion is started, current will start flowing over your resistor. The current will continue to flow when the conversion ends. -> a

// FROM i2d_nss_example_1


void i2d_nss_example_1(void)
{
//! [i2d_nss_example_1]
int i2dValue;
int i2dNativeValue;
Chip_IOCON_SetPinConfig(NSS_IOCON, IOCON_ANA0_4, IOCON_FUNC_1); // IOCON_FUN_1
Chip_I2D_Init(NSS_I2D);
Chip_I2D_Setup(NSS_I2D, I2D_SINGLE_SHOT, I2D_SCALER_GAIN_10_1, I2D_CONVERTER_GAIN_LOW, 100);
Chip_I2D_SetMuxInput(NSS_I2D, I2D_INPUT_ANA0_4);
Chip_I2D_Start(NSS_I2D);
while (!(Chip_I2D_ReadStatus(NSS_I2D) & I2D_STATUS_CONVERSION_DONE)) {
; /* wait */
}
i2dNativeValue = Chip_I2D_GetValue(NSS_I2D);
i2dValue = Chip_I2D_NativeToPicoAmpere(i2dNativeValue, I2D_SCALER_GAIN_10_1, I2D_CONVERTER_GAIN_HIGH, 100);
Chip_I2D_DeInit(NSS_I2D);
//! [i2d_nss_example_1]

current_native = i2dNativeValue;
current_picoampere = i2dValue;
}

// 4. Connect the ADC to ANA4, and measure the voltage. -> v4

//FROM Example 3 - Single-shot Analog-to-Digital Conversion without IRQ

// The conversion: adc_input_voltage = (native_value * 1.2V) / 2825 + 0.09V
void adcdac_nss_example_3(void) // Modifications: IOCON_ANA0_5--> IOCON_ANA0_4 & converted adcinput_4= (adcInput_4 * 1.2) / 2825 + 0.09
{
//! [adcdac_nss_example_3]
Chip_IOCON_SetPinConfig(NSS_IOCON, IOCON_ANA0_4, IOCON_FUNC_1);
//Chip_ADCDAC_Init(NSS_ADCDAC0);
Chip_ADCDAC_SetMuxADC(NSS_ADCDAC0, ADCDAC_IO_ANA0_4);
Chip_ADCDAC_SetInputRangeADC(NSS_ADCDAC0, ADCDAC_INPUTRANGE_WIDE);
Chip_ADCDAC_SetModeADC(NSS_ADCDAC0, ADCDAC_SINGLE_SHOT);
Chip_ADCDAC_StartADC(NSS_ADCDAC0);

while (!(Chip_ADCDAC_ReadStatus(NSS_ADCDAC0) & ADCDAC_STATUS_ADC_DONE)) {
; /* Wait until measurement completes. For single-shot mode only! */
}

adcInput_4 = Chip_ADCDAC_GetValueADC(NSS_ADCDAC0);
adcInput_4= (adcInput_4 * 1.2) / 2825 + 0.09; // CONVERSION added line

//! [adcdac_nss_example_3]
}


void adcdac_nss_example_3_2(void) // Modifications: IOCON_ANA0_5--> IOCON_ANA0_1 & converted adcinput_1= (adcInput_1 * 1.2) / 2825 + 0.09
{
//! [adcdac_nss_example_3]
Chip_IOCON_SetPinConfig(NSS_IOCON, IOCON_ANA0_1, IOCON_FUNC_1);
//Chip_ADCDAC_Init(NSS_ADCDAC0);
Chip_ADCDAC_SetMuxADC(NSS_ADCDAC0, ADCDAC_IO_ANA0_1);
Chip_ADCDAC_SetInputRangeADC(NSS_ADCDAC0, ADCDAC_INPUTRANGE_WIDE);
Chip_ADCDAC_SetModeADC(NSS_ADCDAC0, ADCDAC_SINGLE_SHOT);
Chip_ADCDAC_StartADC(NSS_ADCDAC0);

while (!(Chip_ADCDAC_ReadStatus(NSS_ADCDAC0) & ADCDAC_STATUS_ADC_DONE)) {
; /* Wait until measurement completes. For single-shot mode only! */
}

adcInput_1 = Chip_ADCDAC_GetValueADC(NSS_ADCDAC0);
adcInput_1= (adcInput_1 * 1.2) / 2825 + 0.09; // CONVERSION added line

//! [adcdac_nss_example_3]
}







// MAIN
int main(void)
{
Board_Init();
NDEFT2T_Init();
NVIC_EnableIRQ(PIO0_IRQn); /* PIO0_IRQHandler is called when this interrupt fires. */
Chip_GPIO_EnableInt(NSS_GPIO, 0, 1);


// for ever, WHILE field is detected: 2. Connect the DAC to ANA1, set a continuous conversion of 1.25V.
for (;;) {
while (sFieldPresent) {

// 2. Connect the DAC to ANA1 -output Voltage
adcdac_nss_example_1();
// 3. Connect the I2D to ANA4, - read current ana4 through resistor
i2d_nss_example_1();
// 4. Connect the ADC to ANA4, and measure the voltage. ->v4
adcdac_nss_example_3();
// 5. Connect the ADC to ANA1, and measure the voltage. -> v1
adcdac_nss_example_3_2();
// generate the message only once per field???
GenerateNdef_TextMime();
resval = (adcInput_1-adcInput_4)/(current_picoampere * 1e-12); // (v1-v4)/(I4pico * 1e-12) V/I=R [V/A =ohm]
sprintf((char *)sText, "adc_1: %6.2f adc_4: %6.2f current_picoampere: %8.d resistance: %e current_native: %6.d", adcInput_1, adcInput_4,current_picoampere, resval,current_native );//commented in from Thesis, changes payloadText to sText


// attach message to be read
if (sMsgAvailable) {
sMsgAvailable = false;
ParseNdef();
}
}

Chip_PMU_PowerMode_EnterDeepSleep();
}
return 0;

}


 

0 Kudos
2,171 Views
driesmoors
NXP Employee
NXP Employee

Hello Leo,

 

Can you adjust the parameters in Chip_I2D_Setup? Given your expected current at 100kOhm, you'll need to use the 25uA range, which corresponds to these parameters: 

Chip_I2D_Setup(NSS_I2D, I2D_SINGLE_SHOT, I2D_SCALER_GAIN_10_1, I2D_CONVERTER_GAIN_LOW, 100);

See also Chapter 8 Current-to-Digital converter (I2D) > Specifications in the datasheet.

The conversion from van pico Ampère to Ohm is then calculated as: resval = (adcInput_1 - adcInput_4) / (current_picoampere * 1e-12)

 

Let me know your results!

Best,
Dries.

 

0 Kudos
2,143 Views
leo94
Contributor IV

Hi @driesmoors

 

is there any update on why i am getting erroneous values for Resistance? 

I have tested for different resistances (1MOhm, 2Mohm, 3Mohm) and resistance measured is scaling correclty with resitance_real, but still not the correct value. 

 

Best, 

Leo

0 Kudos
1,768 Views
l95kr
Contributor II

Hi @leo94 

I found the error in you code:

when you convert to pA in line 226 you used the Parameter: I2D_CONVERTER_GAIN_HIGH

while having the I2D configuered with the gain low ( Line 219)

I tried it with:

i2dValue = Chip_I2D_NativeToPicoAmpere(i2dNativeValue, I2D_SCALER_GAIN_10_1, I2D_CONVERTER_GAIN_LOW, 100);

and got a resistor Value of rougly 97K using a 100k resistor

Best wishes,

Lennard