ADC_Read() out of specification

Post your requests and ideas on the future development of mikroC PRO for PIC.
Post Reply
Author
Message
ocayaro
Posts: 2
Joined: 16 Jul 2012 15:31

ADC_Read() out of specification

#1 Post by ocayaro » 16 Jul 2012 16:15

Thanks for the brilliant mikroC pro compiler. :D

I have only one issue :). I have noticed that the ADC_Read() output is badly out of specification on the PIC18f2x80 series (and many others as well, I am sure). Upon reading their Microchip datasheets I found two likely reasons. Firstly, the ADC_Read() function presupposes only ADCON0 and ADCON1, and of course an RC-clock for the ADC. The PIC18f series uses ADCON0, ADCON1 and ADCON2 for ADC operation. The bits in the named registers are not necessarily directly compatible. For example, what PIC16f does with a given bit in a given register is not necessarily the same on a PIC18f. Secondly, on the PIC18f additional parameters are needed to specify TAD (acquisition time per bit), TACQ, and so on. Therefore your present function ADC_Read() is underspecified.

I got around the problem by writing my own function, myADC_Read(char channel, char TAD, char TACQ, char CLKtype) and immediately noticed very accurate conversions. I hope that you will make a similar addition to your otherwise beautiful compiler.

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: ADC_Read() out of specification

#2 Post by janko.kaljevic » 17 Jul 2012 15:24

Hello,

Our libraries are generalized and if you notice they are different for PIC16F and PIC18F.
Also ADC library for this controller sets ADCON2 register properly prior ADC conversion.

Thanks for the kind words and comments regarding compiler.

Also it is expected to get better results if you set ADC parameters according to your application and device.

Best regards.

ocayaro
Posts: 2
Joined: 16 Jul 2012 15:31

Re: ADC_Read() out of specification

#3 Post by ocayaro » 17 Jul 2012 15:28

:D Will do, thanks :D

phingoctu
Posts: 2
Joined: 27 Oct 2012 16:59

Re: ADC_Read() out of specification

#4 Post by phingoctu » 31 Mar 2013 08:50

hi, i have trouble with my code Read_ADC. can you help me,please .
here is my code. i'm using pic18f452 crystal 8M Hz

Code: Select all

unsigned adc;
unsigned int ADC_VALUE;


char txt[13];

char mes[]="NHIETDO";


// LCD module connections
sbit LCD_RS at RB5_bit;
sbit LCD_EN at RB4_bit;
sbit LCD_D4 at RB3_bit;
sbit LCD_D5 at RB2_bit;
sbit LCD_D6 at RB1_bit;
sbit LCD_D7 at RB0_bit;

sbit LCD_RS_Direction at TRISB5_bit;
sbit LCD_EN_Direction at TRISB4_bit;
sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB2_bit;
sbit LCD_D6_Direction at TRISB1_bit;
sbit LCD_D7_Direction at TRISB0_bit;
// End LCD module connections


unsigned adc_read2(char channel)
{

   switch (channel) {
    case 0: {
        ADCON0.CHS0 = 0;
        ADCON0.CHS1 = 0;
        ADCON0.CHS2 = 0;
    }
    case 1: {
        ADCON0.CHS0 = 1;
        ADCON0.CHS1 = 0;
        ADCON0.CHS2 = 0;
    }
    case 2: {
        ADCON0.CHS0 = 0;
        ADCON0.CHS1 = 1;
        ADCON0.CHS2 = 0;
    }
    case 3: {
        ADCON0.CHS0 = 1;
        ADCON0.CHS1 = 1;
        ADCON0.CHS2 = 0;
    }
    case 4: {
        ADCON0.CHS0 = 0;
        ADCON0.CHS1 = 0;
        ADCON0.CHS2 = 1;
    }


   }

   delay_us(20);
   ADRESL        = 0;                                                  /* Resetting the ADRES value register */
   ADRESH        = 0;
   ADCON0.B2 = 1; // start conversion
   
   while (ADCON0.B2)

        ADC_VALUE        =         ADRESL;                                                                                /* Getting HSB of CCP1 */
        ADC_VALUE        += (ADRESH << 8);                                                   /* Getting LSB of CCP1 */

  return (ADC_VALUE);     /* Return the value of the ADC process */
}
void adcinit()
     {
              ADCON1.ADCS2=0;  //Fosc/16
              ADCON0.ADCS1=1;
              ADCON0.ADCS0=0;

              ADCON0.B2=0; //ADC not process


              ADCON1.ADFM=1;//Right justified
              ADCON1.B3=0;// select analog or digital I/O
              ADCON1.B2=0;
              ADCON1.B1=0;
              ADCON1.B0=0;
              ADCON0.B0=1; //ADC enable
     }

void main(){
TrisB=0x00;
TrisC=0x04;
TrisA=0xFF;
adcinit();
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);               // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
Lcd_Out(1,1,mes);
while(1){
adc=adc_read2(1);

temp=adc*(500/1023);
wordTostr(temp,txt);
Lcd_Out(2,1,txt);
Delay_ms(200);

}
}

Post Reply

Return to “mikroC PRO for PIC Wish List”