In an ADC PICkle

Thread Starter

kutalinelucas

Joined Nov 20, 2007
98
Hello...

Was wondering if I could get a little direction...I'm trying to read the voltage on two ADC pins of a PIC18F4550, but not concurrently. The readings are taken on demand within a switch statement, and if the ADCON0 register (which chooses the channel) is set in a function outside the switch statement, I have no problems in taking readings by setting the ADCON0.GoDone bit within the switch statement.

I'm just trying to find a way to set-up the adc register, and then select which adc channel to sample within the switch statement...the code below is just a small excerpt

Rich (BB code):
void main(void)
{
    InitialiseSystem();
		adc_init();

    while(1)
    {	
        USBDeviceTasks();
        ProcessIO();        
    }
}

void adc_init(void)
{
	ADCON1 = 0b00001101;//VSS,VDD ref. AN0 and AN1 analog only
	ADCON0 = 0x00;//clear ADCON0 to select channel 0 (AN0)
	ADCON2 = 0b00001000;//ADCON2 setup: Left justified, Tacq=2Tad, Tad=2*Tosc (or Fosc/2)
	ADCON0bits.ADON = 0x01;//Enable A/D module
}

void selectchannelan1(void)
{
	ADCON0 = 0x00;
}
				
void selectchannelan2(void)
{
	ADCON0 = 0x04;
}		

void ProcessIO(void)
{   
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
   
		if(!HIDRxHandleBusy(USBOutHandle))									
			{   
				switch(ReceivedDataBuffer[0])
			{

	case 0x04:						
		
			    selectchannelan1();
					DelayUs(10);
						ADCON0bits.GO_DONE = 1;//Start A/D Conversion
		
					while(ADCON0bits.GO_DONE != 0);
							ADC_VALUE_ARID = ADRESH;

			ToSendDataBuffer[0] = 0x04;
				ToSendDataBuffer[1] = ADC_VALUE_ARID; 

				
                if(!HIDTxHandleBusy(USBInHandle))
				{
					USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
				}
				
			DelayMs(5);
			
break;
thanks for looking
 

Thread Starter

kutalinelucas

Joined Nov 20, 2007
98
it's ok...you just need to disable the adc before selecting the channel...like this

Rich (BB code):
void selectchannelan1(void)
{
	ADCON0bits.ADON = 0x00;
	DelayUs(5);
	ADCON0 = 0x00;
	DelayUs(5);
	ADCON0bits.ADON = 0x01;
}
				
void selectchannelan2(void)
{
	ADCON0bits.ADON = 0x00;
	DelayUs(5);
	ADCON0 = 0x04;
	DelayUs(5);
	ADCON0bits.ADON = 0x01;
}
 
Top