ADC Settings

FRC

Does your uC have a FRC to use as the clock source? Nice and simple if it does.

FVR

Does your uC have a FVR to use as the reference voltage? Useful if you want an accurate reference, but not useful it you want your analog readings referenced to the uC power rail of course.

You need to add it as a separate peripheral and select its multiplier to give the reference voltage you want

Using in your code

#include "mcc_generated_files/adc.h"

//INITIALISE
	//Start AtoD Conversion
	ADC_SelectChannel(channel_AN1);
	ADC_StartConversion();


//PROCESS ATOD

	//Exit if last conversion is not complete
	if (!ADC_IsConversionDone())
		return;

	//Get result of last conversion
	atod_result = (uint16_t)ADC_GetConversionResult();

	//Start next measurement
	ADC_SelectChannel(channel_AN1);
	ADC_StartConversion();

An example function to handle AtoD measurements


//************************************
//************************************
//********** PROCESS A TO D **********
//************************************
//************************************
void ProcessAtoD (void)
{
	uint16_t AtodResult;
	uint8_t u8Ttemp;

	//Exit if last conversion is not complete
	if (!ADC_IsConversionDone())
		return;

	//Get result of last conversion
	AtodResult = (uint16_t)ADC_GetConversionResult();

	//0-5V = 0-1023
	//Arduino is 0-3.3V
	//1.65V = 337
	
	u8Ttemp = (uint8_t)((ADCON0 >> 2) & 0x0f);
	switch (u8Ttemp)
	{
	case 0:
		//--------------------
		//----- AN0 DONE -----
		//--------------------
		//if (AtodResult > 562)

		ADC_SelectChannel(channel_AN1);
		break;
		
	case 1:
		//--------------------
		//----- AN1 DONE -----
		//--------------------
		//if (AtodResult > 562)

		ADC_SelectChannel(channel_AN2);
		break;

	case 2:
		//--------------------
		//----- AN2 DONE -----
		//--------------------
		//AtodResult

		ADC_SelectChannel(channel_AN0);
		break;


	default:
		ADC_SelectChannel(channel_AN0);
		break;
	} //switch (u8Ttemp)

	//----- START NEXT CONVERSION -----
	ADC_StartConversion();
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *