1
\$\begingroup\$

I'm working with Pic18f6622 and migrating my program into XC8 compiler. Before I'm using the C18 compiler ADC conversion was working just fine, but in new compiler ADC returns me 11 bits value and the expected value is far more different. Maybe the timing is not enough to allow ADC to work healthy, but I couldn't be sure. EDIT #I'm using external 10Mhz crystal#

The "analogval" value returns as 077C, but should be 01DB or close to this. By the way I'm reading PT100 value. I'm sharing the snippet below:

#include <xc.h>
#pragma config OSC = HSPLL, WDT = OFF, WDTPS = 512, BOREN = SBORDIS, BORV = 2, PWRT = ON, LVP = OFF,  MCLRE = OFF

unsigned int analogval;

void AnalogReader(void) {
  while( ADCON0bits.GO != 0 ) continue;

  analogval = ADRESH;
  analogval <<= 8;
  analogval += ADRESL;

  ADCON0 = 0x10; // select first AN0 to AN4
  ADCON0bits.ADON = 1;
  ADCON0bits.GO = 1;
}


void InitDevice(void) {
  TRISA = 0b00111111;
  TRISB = 0b00000000;
  TRISD = 0b00000000;
  TRISF = 0b00111111; // F0~F3 analog input


  // an0 an1 an2 *an3-NC* an4 an5 an6 an7 an8 analog, internal vref+ vref-
  ADCON1 = 0b00000110;
  ADCON2 = 0b10110010; // right justified, 16 tad acquisition, fosc/32
  ADCON0 = 0b00000001; // enable adc


  INTCON2 = 0b11111000; /// pull-up disabled

  RCONbits.IPEN = 0;  // interrupt priority not used
  INTCONbits.INT0IE = 1; // int0 interrupt enabled
  INTCONbits.PEIE = 1; // peripheral interrupts enabled
  INTCONbits.GIE = 0; // interrupts disabled
}

void InitTimeBase(void) {
  T3CON = 0x00;
  T1CON = 0x00;
  CCP1CON = 0b00001011; // Compare mode(RC2 OUTPUT): reset TMR1, set CCP1IF

  CCPR1H = 0x03;
  CCPR1L = 0xE8; // 1000 increments(=instructions)
  // Instruction cycle: 1/(40Mhz/4) = 0.1us
  // 1000 instructions = 0.1ms = 10KHz

  TMR1H = 0x00;
  TMR1L = 0x00;

  T1CONbits.TMR1ON = 1; //start timer

  PIE1bits.CCP1IE = 1; // TMR1 interrupt enable bit
  INTCONbits.PEIE = 1; // Overflow interrupt enable bit
  INTCONbits.GIE = 0; //Global interrupt enable bit
}

void main(void) {
  InitDevice();
  InitTimeBase();


  delay_ms(2000);  // custom delay


  do {
    if (PIR1bits.CCP1IF) { //10kHz  --> 1000 instruction.
      PIR1bits.CCP1IF = 0;

      /*
       * Bunch of operations
      */


      if (slot == 0)
        AnalogReader();
      if (slot == 1)
        // OP
      if (slot == 2)
        // OP
      if (slot == 3)
        //OP

      slot++;
      if (slot == 100) // slot 0..99 : Enter a slot each 10ms.
        slot = 0;

      //-----------------------------
      counter++;
      if (counter == 10000) {
        counter = 0; // 1 sec
      }

      if (counter > 5000) { //0.5 sec
        PORTBbits.LED1 = 1;
      } else { //0.5 sec
        PORTBbits.LED1 = 0;
      }
    }
  } while (1);
}

I appreciate any help.

\$\endgroup\$
  • \$\begingroup\$ Have you checked the analogue voltage on the the ADC pin is what you think it should be? Have you modified any of the code at all since switching from the old compiler? \$\endgroup\$ – DiBosco Feb 23 '18 at 9:17
  • 1
    \$\begingroup\$ Your result seems to be 4 times the expected result, as though the result were shifted left twice. But I don't see a cause. \$\endgroup\$ – gbarry Feb 23 '18 at 9:20
  • \$\begingroup\$ @DiBosco I've checked and it should be 01DB (2.44V), but as gbarry stated , the value is 4 times this one. \$\endgroup\$ – TheSoulkiller Feb 23 '18 at 10:52
  • \$\begingroup\$ Sorry it should be 2.31V \$\endgroup\$ – TheSoulkiller Feb 23 '18 at 11:06
  • 1
    \$\begingroup\$ ... or are you actually using a ..23 - which would perfectly explain the apparently 12-bit result you're seeing? \$\endgroup\$ – brhans Feb 23 '18 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.