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 process_atod (void)
{
	uint16_t atod_result;
	BYTE b_temp;

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

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

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

		ADCON0 = 0x01 | (2 << 2);	//Set AN# input
		break;

	case 2:
		//--------------------
		//----- AN2 DONE -----
		//--------------------
		//atod_result

		ADCON0 = 0x01 | (3 << 2);	//Set AN# input
		break;

	case 3:
		//--------------------
		//----- AN3 DONE -----
		//--------------------
		//atod_result
		
		ADCON0 = 0x01 | (4 << 2);	//Set AN# input
		break;

	case 4:
		//--------------------
		//----- AN4 DONE -----
		//--------------------
		//atod_result

		ADCON0 = 0x01 | (1 << 2);	//Set AN# input
		break;


	default:
		ADCON0 = 0x01 | (1 << 2);	//Set AN# input
		break;
	} //switch (b_temp)

	//----- START NEXT CONVERSION -----
	ADCON0bits.GO = 1;				//Start a conversion
}
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 *