PIC18F252 ADC issue

Thread Starter

Damian P

Joined Mar 23, 2020
10
Hi everyone,

I'm trying stimulate the ADC on the XC8 compiler and I'm not sure about couple of things.

1) Once I turn the CONVERSION ON ( ADCON0bits.GO = 1; ) my program jumps to the interrupt and stays there FOR ETERNITY.

2) What I have realized is that my ADRESH is 51 decimal, always.

I was going to use the simulator, to shoot 2v and then I was gonna check the value of ADRESH; but it is never changing.

C:
void __interrupt() HighInt(void){

if(PIE1bits.ADIE && PIR1bits.ADIF){ //Test for Flag & Timer 2 interrupt enable, because external interrupts might trigger it even when not set
  
PIR1bits.ADIF = 0; //reset ADC interrupt flag

ADCON0bits.GO = 1; //start the conversion !!!!!
}

   return;
}
//Main Function
void main(void){
setup();
aDc_Setup();
ADCON0bits.GO = 1; //start the conversion !!!!!
while(1)
{

  
    }
}

void setup(void) {

    // Port configuration
    TRISA = 0b00011111;
    TRISB = 0b00000000;
    OUTPUT_PIN = 0;

}

void aDc_Setup(void) {
// ADC configuration
    ADCON0bits.CHS = 0b000; // AN0 as the analogue source
    ADCON1bits.PCFG = 0b0010; //Configures bit 0 - 4 as Analogue and bit 5 - 7 as Digital
    ADCON1bits.ADFM = 0; // left justify for 8 bits (256) )
    ADCON0bits.ADCS0 = 0; //
    ADCON0bits.ADCS1 = 1; // Fosc/64
    ADCON1bits.ADCS2 = 1; //
    ADCON0bits.ADON = 1; // Turn ON the module

    // Resolution = |(5v pos reference) - (0v neg reference)| / |steps 256 or 1024|
    // (5v - 0v)/1024 = 0.00488v per each step
  
//INTERRUPT SETUP
  
    INTCONbits.GIE = 1; // Global interrupt
    INTCONbits.PEIE = 1; // Enables all unmasked peripheral interrupts
     PIE1bits.ADIE = 1; // Enable Analogue to Digital conversion
   
     ADRESH=0b00000000;
   }
Moderators note: Please use code tags for pieces of code
 
Last edited by a moderator:
Top