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

PIC24FJ64GA004 using AN4 as an analog

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



Joined: 08 Nov 2013
Posts: 11
Location: Istanbul

View user's profile Send private message

PIC24FJ64GA004 using AN4 as an analog
PostPosted: Tue Jul 08, 2014 10:38 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 8:01 am     Reply with quote

Page 219 of the datasheet explains how to configure the ADC.
_________________
David
fuxafak



Joined: 08 Nov 2013
Posts: 11
Location: Istanbul

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 8:19 am     Reply with quote

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 Smile I am so confused
_________________
-ufuk safak-
Istanbul Technical University
fuxafak



Joined: 08 Nov 2013
Posts: 11
Location: Istanbul

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 8:22 am     Reply with quote

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: 19546

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 8:30 am     Reply with quote

AN0 = adc channel 0

AN4 = adc channel 4 (not 5).....
fuxafak



Joined: 08 Nov 2013
Posts: 11
Location: Istanbul

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 8:44 am     Reply with quote

daf--k Smile 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: 19546

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 9:03 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 5:47 pm     Reply with quote

Yea i understand that Smile sorry for foolish question :p
_________________
-ufuk safak-
Istanbul Technical University
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