This blog gives an overview on implementing 12-bit ADC by polling method and displaying the digital values over hyper terminal. Corresponding to the digital values the Red Green and Blue LED’s are indicated.
For 12-bit resolution ADC the digital value ranges from 0 to 4095 (0 - 2^12), if the value is in between 0 to 1200 RED led is switched on and if the value is in between 1200 and 2500 Green led is switched on, if the value is in between 2500 and 4095 Blue led is switched on.
ADC port is assigned to ADC0-SE23 -> PTE-30 which is located on jumper J2-11as shown below:
The potentiometer connection is done as shown below
RGB led’s are connected as shown below
Software code for this project is given below:
Add below lines in main.c file
#include"fsl_debug_console.h" #include "board.h" #include "fsl_adc16.h" #include "pin_mux.h" #include "clock_config.h" #include "fsl_port.h" #include "fsl_gpio.h" #include "fsl_common.h" #include "pin_mux.h" #include "clock_config.h" #define DEMO_ADC16_BASE ADC0 #define DEMO_ADC16_CHANNEL_GROUP 0U #define DEMO_ADC16_USER_CHANNEL 23U /* PTE30, ADC0_SE23 */ int main(void) { adc16_config_t adc16ConfigStruct; adc16_channel_config_t adc16ChannelConfigStruct; BOARD_InitPins(); BOARD_BootClockRUN(); BOARD_InitDebugConsole(); CLOCK_EnableClock(kCLOCK_PortE); CLOCK_EnableClock(kCLOCK_PortC); PORT_SetPinMux(PORTE, 29U, kPORT_MuxAsGpio); //RED PORT_SetPinMux(PORTC, 4U, kPORT_MuxAsGpio); //Green PORT_SetPinMux(PORTE, 31U, kPORT_MuxAsGpio); // Blue LED_RED_INIT(LOGIC_LED_OFF); LED_GREEN_INIT(LOGIC_LED_OFF); LED_BLUE_INIT(LOGIC_LED_OFF); int val,i; PRINTF("\r\nADC16 polling Example.\r\n"); ADC16_GetDefaultConfig(&adc16ConfigStruct); 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"); } else { PRINTF("ADC16_DoAutoCalibration() Failed.\r\n"); } #endif /* FSL_FEATURE_ADC16_HAS_CALIBRATION */ 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(); for(i=0;i<=1000000;i++); ADC16_SetChannelConfig(DEMO_ADC16_BASE, DEMO_ADC16_CHANNEL_GROUP, &adc16ChannelConfigStruct); while (0U == (kADC16_ChannelConversionDoneFlag & ADC16_GetChannelStatusFlags(DEMO_ADC16_BASE, DEMO_ADC16_CHANNEL_GROUP))) { } val = ADC16_GetChannelConversionValue(DEMO_ADC16_BASE, DEMO_ADC16_CHANNEL_GROUP); if((val >= 0) && (val <= 1200)) { LED_RED_ON(); LED_GREEN_OFF(); LED_BLUE_OFF(); } if((val >= 1200) && (val <= 2500)) { LED_GREEN_ON(); LED_RED_OFF(); LED_BLUE_OFF(); } if((val >= 2500) && (val <= 4095)) { LED_BLUE_ON(); LED_GREEN_OFF(); LED_RED_OFF(); } PRINTF("ADC Value: %d\r", val); } }
The project has been implemented using KDS+KSDK
Code Explanation:
For ADC part:
Structure adc16_config_t has the parameters like reference voltage, input clock source, divider of input clock source, sample resolution mode, long sample mode, high-speed mode, low power and continuous conversion mode.
Resolution is defined by the variable:
config->resolution = kADC16_ResolutionSE12Bit;
Channel configuration is done by the structure adc16_channel_config_t
Below are the configuration done for this project:
ADC16_GetDefaultConfig(&adc16ConfigStruct);
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;
Interrupts are disabled by below function
adc16ChannelConfigStruct.enableInterruptOnConversionCompleted = false;
ADC channel number is located at
adc16ChannelConfigStruct.channelNumber = DEMO_ADC16_USER_CHANNEL;
(In my case it is define as DEMO_ADC16_USER_CHANNEL = 23U)
- i.e ADC channel-0, ADC0_SE23
Differential conversion is disabled
adc16ChannelConfigStruct.enableDifferentialConversion = false;
ADC16_SetChannelConfig will start the ADC to perform its task
ADC16_GetChannelConversionValue will get the converted value
Port pin settings
PORT_SetPinMux(PORTE, 29U, kPORT_MuxAsGpio); //RED
The above function set the portE-29 pin as output and similar with PortE-31 and PortC-4
LED_RED_INIT(LOGIC_LED_OFF);
The above function initialises the red led in off condition
Similar the case with Green and Blue led initialisation
The digital value is stored in variable ‘val’ and compared as below
If the value is in between 0 to 1200 RED led is switched on
if((val >= 0) && (val <= 1200)) { LED_RED_ON(); LED_GREEN_OFF(); LED_BLUE_OFF(); }
If the value is in between 1200 and 2500 Green led is switched on
if((val >= 1200) && (val <= 2500)) { LED_GREEN_ON(); LED_RED_OFF(); LED_BLUE_OFF(); }
If the value is in between 2500 and 4095 Blue led is switched on
if((val >= 2500) && (val <= 4095)) { LED_BLUE_ON(); LED_GREEN_OFF(); LED_RED_OFF(); }
The video output execution of this project is shown below:
The project folder is attached here for quick evaluation and reference.