PIC24FJ256GB106 Auto Sampling


	//----- SETUP THE A TO D PINS -----
	//AN0, AN1, AN11 analog
	//Auto scan of inputs
	AD1PCFGL = 0xF7FC;		//Set the analog pins (0 = analog)
	AD1PCFGH = 0xFFFF;
	AD1CON2 = 0x0408;
	AD1CON3 = 0x1F05;
	AD1CHS = 0x0000;
	AD1CSSL = 0x0803;		//Set the inputs to scan
	AD1CON1 = 0x80E4;		//AD1CON2 SMPI (interrupts after # conversions) must >= the number of inputs being scanned otherwise the higher inputs will not be sampled (if >  the number of inputs being scanned then the scannign will repeat fillign up the buffer before the interrupt flag is set, se you can set to 2x the number of inputs being scanned to get 2 samples of all the inputs before an interrupt etc).
	IFS0bits.AD1IF = 0;		//Clear the interrput flag

	
	//----- READ THE A TO D PINS -----
	if (IFS0bits.AD1IF)
	{
		//Interrupt - the number of samples specified by AD1CON2 SMPI has occured.  ADC1BUF# will have been filled up by the number of samples with the specified inputs to scan in lowest to highest order and repeating if number of samples is > the number of inputs to scan specified
		atod_0 = ADC1BUF0;
		atod_1 = ADC1BUF1;
		atod_2 = ADC1BUF2;		
		IFS0bits.AD1IF = 0;		//Reset the irq
	}

 

PIC24FJ256GB106 Manual Sampling


	//----- SETUP THE A TO D PINS -----
	AD1PCFGH = 0b1111111111111111;	//Select analog pins (1 = digital, 0 = analog)
	AD1PCFGL = 0b1111111111100000;	//Select analog pins (1 = digital, 0 = analog)
	AD1CON1 = 0b0000000011100000;	//Internal counter ends sampling and starts conversion, manual sampling trigger
	AD1CON2 = 0b0000000000000000;	//ALTS = 0 = Always uses MUX A input multiplexer settings
	AD1CON3 = 0b0001111100000010;	//ADCS = 4 (Tad must be 75nS min for PIC24FJ256GB110 family)
	AD1CHS0 = 0;					//AtoD input select

	AD1CON1bits.ADON = 1;

	AD1CHS0 = 0;						//Select analog input to sample
	AD1CON1bits.SAMP = 1;				//Start sampling





//****************************************
//****************************************
//********** READ ANALOG INPUTS **********
//****************************************
//****************************************
void read_pic_analog_inputs (void)
{
	BYTE doing_input;
	static BYTE sample_count = 0;
	static DWORD psu_voltage_atod_buffer;
	static DWORD vref_atod_buffer;



	//----- WAIT FOR LAST CONVERSION TO COMPLETE -----
	if (!update_atod)						//We only check every 1mS as we have no need for the PIC's very high AtoD sampling speed
		return;
		
	if (AD1CON1bits.SAMP || !AD1CON1bits.DONE)
		return;

	doing_input = AD1CHS0 & 0x000f;
	update_atod = 0;

	switch (doing_input)
	{
	case 0:
		//----- DONE AN0 - PSU VOLTAGE -----
		psu_voltage_atod_buffer += ADC1BUF0;

		AD1CHS0 = 1;						//Select analog input to sample
		AD1CON1bits.SAMP = 1;				//Start sampling
		break;
		
	case 1:
		//----- DONE AN1 - Vref -----
		vref_atod_buffer += ADC1BUF0;

		AD1CHS0 = 0;						//Select analog input to sample
		AD1CON1bits.SAMP = 1;				//Start sampling
		
		//-------------------------------------
		//---- END OF SAMPLING ALL INPUTS -----
		//-------------------------------------
		sample_count++;
		if (sample_count >= 16)
		{
			sample_count = 0;

			psu_voltage_atod_value = (WORD)(psu_voltage_atod_buffer >> 4);
			vref_atod_value = (WORD)(vref_atod_buffer >> 4);
			
			psu_voltage_atod_buffer = 0;
			vref_atod_buffer = 0;
			
			process_atod_inputs();
		}
		
		
		break;
		
	default:
		//Error - shouldn't be here
		while (1)
			;		
		
		break;
	}

}
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 *