Found the problem!
I was using Fixed internal voltage reference, but was not enabling it at all.
added
FVRCON=%11011111
and now it works OK!
Found the problem!
I was using Fixed internal voltage reference, but was not enabling it at all.
added
FVRCON=%11011111
and now it works OK!
Well, troubles continue.
I've built a following circuitry. 10K resistor goes to LATA.2 pin and goes then to gnd.
100k variable pot is connected one end to VCC, another to LATA.2
LED is connected to GND via 1k resistor from LATA.1
Wiring is 100% checked and corrected for shorts, misswire, etc.
The code is as follows:
This code should do a simple thing - vary LED brightness according to pot setting. And sure it does, but the problem is that it works in very narrow range, because port sinks huge amount of current - up to 25mA via that pin. So say to achieve 2.5v on the pin, the pot resistance should be set somewhere to 40 ohms. I think this is not correct, right?Code:OSCCON = %11110000 'SET INTOSC TO 32MHZ TRISA=%00110000 'set PORt as input 'ANSELA=%00010101 'set PORTA.4 as analog ADCON0=%00001111 'ENABLE AND CONFIGURE ADC ADCON1=%11000011 'JUSTIFY FVRCON=%11011111 'VREF ENABLE & set at 4.096v WPUA=%00000000 'pull up disable 'CM1CON0=%00000000 'DISABLE COMPARATORS DEFINE OSC 32 DEFINE ADC_BITS 10 DEFINE ADC_SAMPLEUS 50 DEFINE ADC_CLOCK 3 VOLT VAR WORD LED VAR LATA.1 greater: adcin 2,volt high led pause 1 low led pause volt/10 goto greater
Also I found a strange thing - if I set TRISA=%00110010, which means to set LATA.2 as input, the code does not works at all, but it also stops consuming current on that pin. What can be the reason?
Hi,
The input (basically) only sinks current when it's configured as an output and set low. When being used as an input for the ADC it obviously needs to be set to input - just as Mark has pointed out. Then it "sinks" current only when the input is connected to the S/H circuit which leads us to the next issue: Your 100k pot viloates the maximum recommended input impedance by an order of magnitude.
/Henrik.
I have created a voltage divider, 100k and 10k resistors, ADC input connected in their middle point. The problem is that PIC shorts the input to GND, but still works as ADC. This has no relation to input impedance, it feels like same pin is both digital out and analog in at same time. Is this possible?
Hi,
In your original code ( TRISA=%00110000 ) leaves PortA.2 as an output which is wrong.
In your post you contiune to say TRISA=%00110010, to set LATA.2 as input which again, as mark_s points out, is wrong, the above will STILL leave PortA.2 as an OUTPUT.
Your setting of ANSELA in the code is commented out which doesn't really have any effect on the ADC input since it defaults to analog but it WILL have an effect on the pins you want to use as digital.
The ADC DOES put a load on the node when it samples the input that's where the max input impedance comes from. Too high impedance and too short sampling time makes the voltage at the node droop when it's sampled.
/Henrik.
HenrikOlsson,
If you use 10K and 100K for divider, impedance that PIC input see is 9,09K.
Bookmarks