CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC18F26J11 ADC conversion error

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Doane



Joined: 29 Sep 2009
Posts: 2
Location: New England

View user's profile Send private message

PIC18F26J11 ADC conversion error
PostPosted: Tue Sep 29, 2009 8:33 am     Reply with quote

Recently upgraded to PIC18F26J11 from PIC18F25FJ10 (need xtra UART). New product uses AN4 and AN12 for ADC alarm inputs (same as 18F25J10 design). AN12 consistently reads higher than AN4 by 26 counts on average. Have tried tying pins together and grounding at Vss, AN4 reads from 0 to 3 counts, AN12 reads 26 to 28 counts. Error scales with input voltage compared to AN4. Also, AN8 and AN10 show same count error of +25 counts compared to AN4 (AN8, AN10 and AN12 all are on PortB).

Has anyone used these inputs to measure DC voltages? Again, PIC18F25J10 works flawlessly on same PCB.

Thanks in advance.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 29, 2009 11:27 am     Reply with quote

See the errata sheet for the 18F26J11:
http://ww1.microchip.com/downloads/en/DeviceDoc/80435d.pdf
There are two problems with the A/D module.

You didn't post a test program that shows the problem, so I don't know
if these erratas affect your code. But just the fact that the A/D module
has these two problems, makes me think that it could have more.

If you can't make it work, post a short, compilable test program and give
a description of the external circuit that drives the A/D pins.
Example of an A/D test program:
http://www.ccsinfo.com/forum/viewtopic.php?t=40279&start=3
Guest








Thanks PCM programmer!
PostPosted: Wed Sep 30, 2009 11:49 am     Reply with quote

I followed your advice and tried a simplified ADC test program:

Code:
#include <18F26J11.h>
                                 
#device adc=10
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //External HS Clock
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES XINST                    //Extended set extension and Indexed Addressing mode enabled
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NOPROTECT                //Code not protected from reading
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES PRIMARY                  //Primary clock is system clock when scs=00
#FUSES RESERVED                 //Used to set the reserved FUSE bits

#byte ANCON0 =  0xf48
#byte ANCON1 =  0xf49
#byte ADCON1 =  0xfc1
#bit  ADFM   =  0xfc1.7 // bit7 = 1 right justified, = 0 left justified

#pin_select U2TX=PIN_B3
#pin_select U2RX=PIN_B4

#use delay(clock=16000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,STREAM=COM1)
#use rs232(baud=9600,parity=N,xmit=PIN_B3,rcv=PIN_B4,bits=8,STREAM=COM2)

//=================================
void main()
{
   int16 result;
   int8  i;
   
   setup_adc_ports(ALL_ANALOG|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_16);
   ADFM = 1;
   set_adc_channel(12);
   delay_us(15);

   while(1)
     { 

      result=0;
     
      for (i=0;i<16;i++)
      {
         result += read_adc (ADC_START_AND_READ);     
      }
     
      result = result >> 4;  // average 16 readings
     
      fprintf(COM2,"AN12 %LX \n\r", result);
      delay_ms(500);
     
      set_adc_channel(4);
     
      result=0;
     
      for (i=0;i<16;i++)
      {
         result += read_adc (ADC_START_AND_READ);     
      }
     
      result = result >> 4;  // average 16 readings
     
      fprintf(COM2,"AN4 %LX \n\r", result);
     
      set_adc_channel(12);
      delay_ms(500);
     
     }
   
}

AN4 and AN12 are physically tied together and driven from a power supply. Now, when I run the program, the 2 inputs track within a few counts of each other. Previously, AN12 would be 26 counts greater than AN4. My original code must do something to PortB to cause the count error.

Again, thanks for the suggestion. I now know that the problem is my code and not the device.

I will have to back track and determine the setup error.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 30, 2009 11:52 am     Reply with quote

Quote:
#FUSES XINST

This fuse may be the cause of all your problems. CCS doesn't support
the Extended instruction set. Enabling it will cause erratic program
operation. Change the fuse to NOXINST. Do this in all your programs.
I didn't even look at the rest of your code. I'll wait until you fix this.
Doane



Joined: 29 Sep 2009
Posts: 2
Location: New England

View user's profile Send private message

PostPosted: Wed Sep 30, 2009 12:37 pm     Reply with quote

I found the error in my code:

port_b_pullups(TRUE);

I enabled the weak pull-ups in a previous version of my code (PIC18F25J10). Strangely, the older code running on the '25J10 does not exhibit the same ADC count error on PortB. Anyways, removing the pull-ups enable line from my code fixed the problem.

Thank you for your help!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group