View previous topic :: View next topic |
Author |
Message |
koenbielen
Joined: 23 Apr 2009 Posts: 42
|
PIC18F46K80 ADC problem 12BIT |
Posted: Tue Mar 25, 2014 10:07 am |
|
|
Hi,
I have a analog input 12Bit using a potentiometer of 10Kohm tied to gnd and +5V.
Processor is also connected to +5V.
The value at +5V = 4095 --> OK
The value at 0V = 65535 --> Bad (1111 1111 1111 1111)
Did anyone have the same problem and how did you solve it ?
Code: |
#include <18F46K80.h>
#device adc=16
#priority INT_RDA,INT_TIMER1
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES PLLEN //High speed osc with HW enabled 4X PLL
#FUSES INTRC_IO //Internal RC Osc
#FUSES NOPROTECT //Code not protected from readingdef
#FUSES NOIESO //Internal External Switch Over mode disabled
#use delay(int=64000000)
#use rs232(baud=1000000,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,DISABLE_INTS,STREAM=NETWERK)
#byte ADCON2 = getenv("sfr:ADCON2")
void main()
{
setup_oscillator (OSC_16MHZ|OSC_PLL_ON);
//-----------------------------------------------------------------------------------------------------------------------------------------------------
set_tris_a (0xCF);//
set_tris_b (0xCF);//
set_tris_c (0x82);//
set_tris_d (0x80);//
set_tris_e (0XF8);//
//-----------------------------------------------------------------------------------------------------------------------------------------------------
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
setup_timer_2(T2_DIV_BY_1,49,8); // 20 = 0,8 volt 30 =1volt 325 is 10 volt
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_PWM);
setup_ccp3(CCP_OFF);
setup_ccp4(CCP_OFF);
setup_ccp5(CCP_PWM);
set_pwm2_duty(0);
set_pwm5_duty(0);
port_b_pullups(0b00001110);
setup_adc_ports(sAN0 | sAN1 | sAN2 | sAN3);
setup_adc(ADC_CLOCK_DIV_8|ADC_TAD_MUL_8);
ADCON2|= 0x80;
while (1)
{
for(lX=0;lX<=3;lX++)
{
set_adc_channel(lX);
delay_ms(10);
nADCVal = read_adc();
printf("<II nADCVal %ld:%Lu>\n\r",lX,nADCVal);
}
delay_ms(1000);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 25, 2014 7:34 pm |
|
|
Read the ADC errata for the 18F46K80:
http://ww1.microchip.com/downloads/en/DeviceDoc/80000519F.pdf
There are silicon bugs with the 12-bit ADC. It has an offset error,
which you are seeing. They want you to test, and record the offset
error for each PIC that you are using. Put the correction factor into
eeprom in each PIC, or some other way. You will have to decide if
this is too annoying to use the 12-bit ADC in a product. If it's just a
"one off" hobby project, then it's not so bad. |
|
|
koenbielen
Joined: 23 Apr 2009 Posts: 42
|
|
Posted: Wed Mar 26, 2014 2:26 am |
|
|
Thank you for reply
I will remove the processor ... time to change
Microchip needs to deliver good products. |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Wed Mar 26, 2014 7:01 am |
|
|
It is returning a signed value of -1. As stated there can be an offset error and this also depends on the silicon revision. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Wed Mar 26, 2014 7:21 am |
|
|
Another thing to consider is the actual need for 12bits! Anything over 10 bits does require PROPER PCB design and layout. Also critical is the power supply which obviously must NOT have ANY noise(ripple,EMI,ect.) Then there's the wiring to the sensor as well as the sensor. You need properly shielded wiring,RF bypass caps,maybe inductors, etc. to get a useful 12bits of analog data.
hth
jay |
|
|
koenbielen
Joined: 23 Apr 2009 Posts: 42
|
|
Posted: Wed Apr 02, 2014 2:08 am |
|
|
Hi Jay
interesting thing you mention.
The 12 bit is actually not really needed. So YES a 10 bit would be an option.
I will check if i can replace this processor type with the same pinout to a 10 bit part.
Thanks
byby |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Wed Apr 02, 2014 5:22 am |
|
|
Everyone here has their favorite PIC.
I've settled on using the 18F46K22 for 99% of my projects. This 'Swiss Army Knife' of PICs has the most 'bang for the buck'. Lots of memory,peripherals and comes in a 40 pin DIP that I can still see, most of the time.
While 'overkill' for most projects, by standardizing on it, I have ONE common PCB that I KNOW works,a library of subroutines,er 'functions', that I KNOW work, and test programs that work as well.
I've learned over the past 2 decades of using PICs it's better to spend time cutting specific code for projects rather than learning the 'quirks' of every new 'better' PIC that comes out.PICs are cheap..R&D time isn't! So it's better to spend an extra buck or two on a more powerful PIC upfront than waste several days on 'quirky' code that worked on old PIC 'A' but doesn't on new PIC 'B' .
just food for thought.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19550
|
|
Posted: Wed Apr 02, 2014 6:32 am |
|
|
The limitation is lack of CanBus. The family the poster was choosing, is about the only one offering CanBus, and dual UART's. I had a similar decision (a few months ago, so there may be newer models now), and ended up adding an I2C UART, and using a single UART PIC, to avoid these chips....
Best Wishes |
|
|
|