View previous topic :: View next topic |
Author |
Message |
fuxafak
Joined: 08 Nov 2013 Posts: 11 Location: Istanbul
|
PIC24FJ64GA004 using AN4 as an analog |
Posted: Tue Jul 08, 2014 10:38 am |
|
|
Hi,
I want to use 23rd pin as an analog input.
I did use analog input with AN0 before. But this pin always return zero analog value.
I think it is about I2C.
Do you have any idea about it.
Datasheet says it has both Analog level input/output and I2C/SMBUS input buffer. In my opinion, i couldn't change this pin to analog.
Thanks _________________ -ufuk safak-
Istanbul Technical University |
|
|
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Wed Jul 09, 2014 8:01 am |
|
|
Page 219 of the datasheet explains how to configure the ADC. _________________ David |
|
|
fuxafak
Joined: 08 Nov 2013 Posts: 11 Location: Istanbul
|
|
Posted: Wed Jul 09, 2014 8:19 am |
|
|
Here is my config code:
Code: | //! setup_comparator(0x0003);
//! setup_vref(false);
setup_comparator(NC_NC_NC_NC);
//! setup_adc_ports(sAN4, VSS_VDD);
//! setup_adc(ADC_CLOCK_INTERNAL | ADC_TAD_MUL_31);
setup_adc_ports(sAN4, VSS_VDD);
setup_adc(ADC_CLOCK | ADC_TAD_MUL_16);
set_adc_channel(5);
//delay_us(10); |
And adc sampling code:
Code: | if(adc_flag)
{
read_adc(ADC_START_ONLY);
printf("started");
int1 done = adc_done();
while(!done) {
done = adc_done();
}
printf("done");
x = read_adc(ADC_READ_ONLY);
printf("\n adc: %u",x);
adc_flag=0;
} |
There could be problem about comparator module. AN4 is also C1IN- pin and when i use AN5 (which is C1IN+ pin) it works.
Code: | .................... //! setup_comparator(0x0003);
.................... //! setup_vref(false);
.................... setup_comparator(NC_NC_NC_NC);
1D6A: CLR 630 |
Here asm code. When i use setup_comparator for all not connected, it clear CMCON register. When all bits of CMCON goes zero,
bit 1 C1NEG: Comparator 1 Negative Input Configure bit
1 = Input is connected to VIN+
0 = Input is connected to VIN-
Figure 22-1 for the Comparator modes.
bit 0 C1POS: Comparator 1 Positive Input Configure bit
1 = Input is connected to VIN+
0 = Input is connected to CVREF
See Figure 22-1 for the Comparator modes.
C1- connects to VIN- and C1+ connects to CVref. When C1EN is low, comparator should be off, but is it can be problem about C1- pin configuration.
Please help I am so confused _________________ -ufuk safak-
Istanbul Technical University |
|
|
fuxafak
Joined: 08 Nov 2013 Posts: 11 Location: Istanbul
|
|
Posted: Wed Jul 09, 2014 8:22 am |
|
|
By the way, AN4 is sda and AN5 is scl for i2c module. Is it relevant ? _________________ -ufuk safak-
Istanbul Technical University |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Wed Jul 09, 2014 8:30 am |
|
|
AN0 = adc channel 0
AN4 = adc channel 4 (not 5)..... |
|
|
fuxafak
Joined: 08 Nov 2013 Posts: 11 Location: Istanbul
|
|
Posted: Wed Jul 09, 2014 8:44 am |
|
|
daf--k setup_adc_ports() sets only pin's analog feauture and set_adc_channel() sets the sampling channel. I don't know that i have to use set_adc_channel(), i suppose that it is like STM32 devices. They have a few pin to set ADC but have lots of channel.
Hmm, thanks a lot. Do i have to set channel same as port, right? _________________ -ufuk safak-
Istanbul Technical University |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Wed Jul 09, 2014 9:03 am |
|
|
Yes.
When you configure the ADC, you are saying what AN channels are physically connected to the multiplexer. Then to read a channel, you have to select a connected channel. You are connecting AN4 to the multiplexer, then trying to read AN5, which is not connected. Result nothing.... |
|
|
fuxafak
Joined: 08 Nov 2013 Posts: 11 Location: Istanbul
|
|
Posted: Wed Jul 09, 2014 5:47 pm |
|
|
Yea i understand that sorry for foolish question :p _________________ -ufuk safak-
Istanbul Technical University |
|
|
|