FSL's design are always full of complexity and powerful. But unfortunately, the MCUxpressoSDK document really sucks.
Here is my experience&code based on KSDK2.1, MK66F18, I believe the IP core are the mostly similar to KL series. And I've tested the code on my smart car while participating the competition
Just like STM32 or other ARM-based MCU, you should firstly set up the pinmux like:
PORT_SetPinMux(PORTC, 9u, kPORT_PinDisabledOrAnalog);
following the datasheet, we could see PTC9 could be mux to ADC1_SE5b, which also means ADC 1, Group B, Channel 5.
And then, to finish setting up ADC:
adc16_config_t ADC1_config; // defines ADC configure structure
ADC16_GetDefaultConfig(&ADC1_config); // ??ADC????
ADC1_config.resolution = kADC16_ResolutionSE12Bit; // Set ADC1 working on 12-bit resolution mode
ADC16_Init(ADC1, &ADC1_config);
ADC16_EnableHardwareTrigger(ADC1, true); // ??ADC????(Auto Calibration??)
ADC16_DoAutoCalibration(ADC1); // u should always finish this step if firstly use ADC after reset.
ADC16_EnableHardwareTrigger(ADC1, false); // ??ADC????
ADC16_SetHardwareAverage(ADC1, kADC16_HardwareAverageDisabled); // ??ADC1??0?????
ADC16_SetChannelMuxMode(ADC1, kADC16_ChannelMuxB);
After finishing initializing Pins and ADCs, u could package a single conversion function like this:
uint32_t Motor_GetADCVal(_Motor_t MOTORn)
{
static adc16_channel_config_t ADC1_SExb_config;
ADC1_SExb_config.channelNumber = MOTORn; // 4 ~ ADC1 SE4a(b), 4 ~ ADC1 SE5a(b)
ADC1_SExb_config.enableDifferentialConversion = false; // ?????????
ADC1_SExb_config.enableInterruptOnConversionCompleted = false; // ?????????
ADC16_SetChannelConfig(ADC1, 0, &ADC1_SExb_config); // This trigs ADC start working
while(kADC16_ChannelConversionDoneFlag != ADC16_GetChannelStatusFlags(ADC1, 0));
return ADC16_GetChannelConversionValue(ADC1, 0); // ???????????
}
And finially congratulations! This is a single-end and single conversion sample, and these codes should make ADCs work successfully.
ADCs on Kinetis are really flexible, pins and channels can be mux and reused in so many ways. Each ADC has several Channels that could be linked to the main converter. There are 2 Groups in each ADC, and each Group contains dozens of pins(7?). These two Group use some same channel numbers which leads to ADC part-time using. U can change register settings to define which Group are u used right now in the ADC. After setting this, if u let ADC working in Single Conversion Mode but NOT Continuous Mode, u could start a definite ADC conversion through a named channel linked to a named pin, and u can check the register flags to find out whether the conversion has finished or not.
PS: some comments are wrote in Chinese and it seems our forum dosen't support Chinese Encoding Charsets so well