Status
Not open for further replies.

#### Cantafford

##### Member
Hello,

I'm trying to read the ADC of PIC18f4550 and put that value on PORTB. I'm using ADC interrupt so I want to read the value when the ADC triggers an interrupt(conversion is complete).

I wrote the following code:

Code:
#include <stdio.h>
#include <stdlib.h>
int counter=0;
void interrupt GetValue()
{
{
}
}

void main()
{
TRISB = 0;
TRISAbits.TRISA0 = 1;
OSCCON = 0x7F;
ADCON0 = 0x01; // channel 0 selected
ADCON1 = 0x0E; // references selected
GIE = 1;

while(1)
{
GODONE = 1; // start conversion
}
}
However I cannot see any change on the leds. They won't light up at all. Please help me correct this issue. I have no ideea what I'm doing wrong. Thanks.

#### Pommie

##### Well-Known Member
As you're not using interrupt priorities you need to set PEIE to 1 to get peripheral interrupts.

Mike.

Thank you!

#### Jon Wilder

##### Active Member
The conventional way of writing your interrupt handler would be -
Code:
if (ADIE && ADIF){
}
This ensures it will only invoke the ADC handler if the ADC interrupt is enabled should you decide to expand on your code and add other interrupt sources.

Also, disabling the ADC in the handler will only invoke a sample once instead of providing real time updates. I'd add a 1/4 second delay to the while loop as you don't need to sample every microsecond.

I would also keep the result left justified and read ADRESH only unless you need full 10-bit resolution.

Status
Not open for further replies.