Basic kw41z ADC Concept - Beginner

cancel
Showing results for 
Search instead for 
Did you mean: 

Basic kw41z ADC Concept - Beginner

437 Views
b_manojkumar521
Contributor II

Hello Community,

I hope everyone was safe in this difficult situation.

I'm a beginner and new to this microcontroller (KW41z).

I was struggling with the ADC interrupt concept. 

I tried with an example program in the SDK and explore the reference manual.

Help me to interface the analog sensor with the ADC concept?

Thanks in advance.

3 Replies

302 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Manoj Kumar,

   Thanks for your concern, hopes you are well, please take care of yourself.

    You can run the ADC code in the SDK:

   SDK_2.2.1_FRDM-KW41Z\boards\frdmkw41z\driver_examples\adc16

   Each project contains the readme.txt, 

   In the board, you need to connect the your external Analog signal to:

3.  Connect external signal source to user-defined channel, defined by "DEMO_ADC16_USER_CHANNEL" in source code

  At first, I suggest you connect the DEMO_ADC16_USER_CHANNEL to the fixed 0, VDD or other VDD divider anlog signal, to readout the ADC value. After it works, you can connect the channel to your own analog channel.

   About the ADC register, you can refer to the KW41Z reference manual ADC chapater.

Wish it helps you!

If you still have questions about it, please kindly let me know.

Wish it helps you!

Kerry

 

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos

302 Views
b_manojkumar521
Contributor II

Thanks for your valuable content sir, Kerry Zhou 

Can you please explain me, where my mistake in below code sir

#include "fsl_debug_console.h"
#include "board.h"
#include "fsl_adc16.h"

#include "pin_mux.h"
#include "clock_config.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define DEMO_ADC16_BASE ADC0
#define DEMO_ADC16_CHANNEL_GROUP 0U
#define DEMO_ADC16_USER_CHANNEL 0 /* PTB18, ADC0_SE4 */

/*******************************************************************************
* Prototypes
******************************************************************************/

/*******************************************************************************
* Variables
******************************************************************************/

/*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief Main function
*/
int main(void)
{
adc16_config_t adc16ConfigStruct;
adc16_channel_config_t adc16ChannelConfigStruct;

BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();

PRINTF("\r\nADC16 polling Example.\r\n");

/*
* adc16ConfigStruct.referenceVoltageSource = kADC16_ReferenceVoltageSourceVref;
* adc16ConfigStruct.clockSource = kADC16_ClockSourceAsynchronousClock;
* adc16ConfigStruct.enableAsynchronousClock = true;
* adc16ConfigStruct.clockDivider = kADC16_ClockDivider8;
* adc16ConfigStruct.resolution = kADC16_ResolutionSE12Bit;
* adc16ConfigStruct.longSampleMode = kADC16_LongSampleDisabled;
* adc16ConfigStruct.enableHighSpeed = false;
* adc16ConfigStruct.enableLowPower = false;
* adc16ConfigStruct.enableContinuousConversion = false;
*/

ADC16_GetDefaultConfig(&adc16ConfigStruct);
#ifdef BOARD_ADC_USE_ALT_VREF
adc16ConfigStruct.referenceVoltageSource = kADC16_ReferenceVoltageSourceValt;
#endif
ADC16_Init(DEMO_ADC16_BASE, &adc16ConfigStruct);
ADC16_EnableHardwareTrigger(DEMO_ADC16_BASE, false); /* Make sure the software trigger is used. */
#if defined(FSL_FEATURE_ADC16_HAS_CALIBRATION) && FSL_FEATURE_ADC16_HAS_CALIBRATION
if (kStatus_Success == ADC16_DoAutoCalibration(DEMO_ADC16_BASE))
{
PRINTF("ADC16_DoAutoCalibration() Done.\r\n");
PRINTF("ADC Value: %d\r\n", ADC16_GetChannelConversionValue(DEMO_ADC16_BASE, DEMO_ADC16_CHANNEL_GROUP));
}
else
{
PRINTF("ADC16_DoAutoCalibration() Failed.\r\n");
}
#endif /* FSL_FEATURE_ADC16_HAS_CALIBRATION */
PRINTF("Press any key to get user channel's ADC value ...\r\n");

adc16ChannelConfigStruct.channelNumber = DEMO_ADC16_USER_CHANNEL;
adc16ChannelConfigStruct.enableInterruptOnConversionCompleted = false;
#if defined(FSL_FEATURE_ADC16_HAS_DIFF_MODE) && FSL_FEATURE_ADC16_HAS_DIFF_MODE
adc16ChannelConfigStruct.enableDifferentialConversion = false;
#endif /* FSL_FEATURE_ADC16_HAS_DIFF_MODE */

while (1)
{
GETCHAR();
/*
When in software trigger mode, each conversion would be launched once calling the "ADC16_ChannelConfigure()"
function, which works like writing a conversion command and executing it. For another channel's conversion,
just to change the "channelNumber" field in channel's configuration structure, and call the
"ADC16_ChannelConfigure() again.
*/
ADC16_SetChannelConfig(DEMO_ADC16_BASE, DEMO_ADC16_CHANNEL_GROUP, &adc16ChannelConfigStruct);
while (0U == (kADC16_ChannelConversionDoneFlag &
ADC16_GetChannelStatusFlags(DEMO_ADC16_BASE, DEMO_ADC16_CHANNEL_GROUP)))
{
}
PRINTF("ADC Value: %d\r\n", ADC16_GetChannelConversionValue(DEMO_ADC16_BASE, DEMO_ADC16_CHANNEL_GROUP));
}
}

When I use the step over, it stuck in the while(1) loop itself.

Where would be the problem sir

Thanks in advance.

0 Kudos

302 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hello Manoj Kumar,

  I have a question, do you test the SDK ADC code on your side?

  That code has been tested.

  Which detail ADC pin you want to use? 

  I find you configure 

   #define DEMO_ADC16_USER_CHANNEL 0 

  Channel is the differential channel.

You can select a single channel to test it at first.

Your code seems the same as the SDK ADC polling code, just modify the ADC channel. I suggest you use the SDK code directly, then input the ADC voltage in PTB18 which is the ADC channel 4.

pastedImage_3.png

If you want to change another ADC channel, you also need to modify the pinmux.c

  PORT_SetPinMux(PORTB, PIN18_IDX, kPORT_PinDisabledOrAnalog); /* PORTB18 (pin 23) is configured as ADC0_SE4 */

Just change the related port as the ADC pin.

Wish it helps you!

Kerry

 

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------