ADC interrupt problem

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>
#include "header.h"
int counter=0;
void interrupt GetValue()
{
    if(ADIF==1)
    {
        LATB = ADRESL; // read converted value to PORTC
        ADON = 0;
    }
    ADIF = 0;
}

void main()
{
    TRISB = 0;
    TRISAbits.TRISA0 = 1;
    OSCCON = 0x7F;
    ADCON0 = 0x01; // channel 0 selected
    ADCON1 = 0x0E; // references selected
    ADCON2 = 0x89;
    ADIF = 0;
    ADIE = 1;
    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.
 
As you're not using interrupt priorities you need to set PEIE to 1 to get peripheral interrupts.

Mike.
 
The conventional way of writing your interrupt handler would be -
Code:
if (ADIE && ADIF){
    LATB = ADRESL;
}

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.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…