I would have provided the code, but I don't know to add it. So, I pasted main and support functions.
Bruce
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "board.h"
#include "peripherals.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "MKL16Z4.h"
#include "fsl_debug_console.h"
/* TODO: insert other include files here. */
void FloatToStringNew(char *str, float f, char size);
/* TODO: insert other definitions and declarations here. */
// Table 3-35. ADC0 channel assignment" of KL26P121M48SF4RM
#define ADC16_CHANNEL_BATT_VOLT 0x11 // ADC0_SE11 pin 45
//#define ADC16_CHANNEL_BATT_TEMP 0x06 // ADC0_SE6b pin 62
#define ADC16_CHANNEL_GROUP 0
/* BATT_VOLT
4.2V full charge
3.4V low, must charge
3.3V protection kicks in
Vin 4.2V (Full Charge)
|
10k
|---- Vout (BATT_VOLT)
4.7k
|
GND
Vout = (Vin * R2) / (R1 + R2)
Vout = (Vin * 4700) / (10000 + 4700)
= 19740 / 14700
Max BATT_VOLT = 1.34V
*/
#define VOLTS_PER_BIT (3.3f / 4096.0f) // Vref / 12 full scale 0x0FFF
float ADC_ValueToVoltage(uint16_t iAdcValue)
{
float fVolts = VOLTS_PER_BIT * (float)iAdcValue;
return fVolts;
}
/*
* @brief Application entry point.
*/
int main(void)
{
adc16_config_t adc16ConfigStruct;
adc16_channel_config_t adc16ChannelConfigStruct;
/* Init board hardware. */
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
/* Init FSL debug console. */
BOARD_InitDebugConsole();
PRINTF("Hello World\n");
ADC16_GetDefaultConfig(&adc16ConfigStruct);
ADC16_Init(ADC0, &adc16ConfigStruct);
PRINTF(" referenceVoltageSource: %d\n", adc16ConfigStruct.referenceVoltageSource);
PRINTF(" clockSource: %d\n", adc16ConfigStruct.clockSource);
PRINTF(" enableAsynchronousClock: %d\n", adc16ConfigStruct.enableAsynchronousClock);
PRINTF(" clockDivider: %d\n", adc16ConfigStruct.clockDivider);
PRINTF(" resolution: %d\n", adc16ConfigStruct.resolution);
PRINTF(" longSampleMode: %d\n", adc16ConfigStruct.longSampleMode);
PRINTF(" enableHighSpeed: %d\n", adc16ConfigStruct.enableHighSpeed);
PRINTF(" enableLowPower: %d\n", adc16ConfigStruct.enableLowPower);
PRINTF("enableContinuousConversion: %d\n", adc16ConfigStruct.enableContinuousConversion);
ADC16_EnableHardwareTrigger(ADC0, false);
#if defined(FSL_FEATURE_ADC16_HAS_CALIBRATION) && FSL_FEATURE_ADC16_HAS_CALIBRATION
if (kStatus_Success == ADC16_DoAutoCalibration(ADC0))
{
PRINTF("ADC16_DoAutoCalibration() Done.\r\n");
}
else
{
PRINTF("ADC16_DoAutoCalibration() Failed.\r\n");
}
#endif // FSL_FEATURE_ADC16_HAS_CALIBRATION
adc16ChannelConfigStruct.channelNumber = ADC16_CHANNEL_BATT_VOLT;
adc16ChannelConfigStruct.enableInterruptOnConversionCompleted = false;// Disable the interrupt.
#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)
{
//PRINTF("Press any key to get user channel's ADC value ...\r\n");
//GETCHAR(); // Input any key in terminal console.
ADC16_SetChannelConfig(ADC0, ADC16_CHANNEL_GROUP, &adc16ChannelConfigStruct);
while (kADC16_ChannelConversionDoneFlag != ADC16_GetChannelStatusFlags(ADC0, ADC16_CHANNEL_GROUP))
{
}
uint16_t iValueAdc = ADC16_GetChannelConversionValue(ADC0, ADC16_CHANNEL_GROUP);
float fBatteryVoltage = ADC_ValueToVoltage(iValueAdc);
#if 1
char szBatteryVoltage[32];
FloatToStringNew(szBatteryVoltage, fBatteryVoltage, 6);
PRINTF("ADC Value: 0x%04X %s Volts\r\n", iValueAdc, szBatteryVoltage);
#else
PRINTF("ADC Value: 0x%04X %f Volts\r\n", iValueAdc, fBatteryVoltage);
#endif
}
}
#if 1
// convert float to string one decimal digit at a time
// assumes float is < 65536 and ARRAYSIZE is big enough
// problem: it truncates numbers at size without rounding
// str is a char array to hold the result, float is the number to convert
// size is the number of decimal digits you want
void FloatToStringNew(char *str, float f, char size)
{
int pos; // position in string
char len; // length of decimal part of result
int value; // decimal digit(s) to convert
pos = 0; // initialize pos, just to be sure
value = (int)f; // truncate the floating point number
itoa(value, str, 10); // this is kinda dangerous depending on the length of str
// now str array has the digits before the decimal
strcat(str, ".");
if (f < 0 ) // handle negative numbers
{
f *= -1;
value *= -1;
}
len = strlen(str); // find out how big the integer part was
pos = len; // position the pointer to the end of the integer part
while(pos < size) // process remaining digits
{
f = f - (float)value; // hack off the whole part of the number
f *= 10; // move next digit over
value = (int)f; // get next digit
itoa(value, &str[pos], 10); // convert digit to string
pos++;
}
str[pos] = 0;
}
#endif