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

pic18f23k22 and PORTB ADC not working

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



Joined: 08 Mar 2008
Posts: 6

View user's profile Send private message

pic18f23k22 and PORTB ADC not working
PostPosted: Wed Jun 15, 2016 6:49 am     Reply with quote

Hi peeps, I am trying to read ADC on RA0 (AN0) (works well) and RB5 (AN13) (doesn't do anything, just seems to take an initial reading but doesn't change afterwards). I have read some posts and have enabled PBADEN in the fuse settings, disabled pullup resistors on portb, set the tristate register to input for RB5 and disabled CCP3. I am not exactly sure what else I am doing wrong. Could anyone make a suggestion as to what I am missing ?

Compiler Version PCWHD 5.045

kind regards
Code:

#include <18F23K22.h>

#device ADC=10

#fuses NOWDT, NOPROTECT, NOLVP, NOPUT, NOWRT, NODEBUG, NOMCLR, NOBROWNOUT, NOPROTECT,NOCPD, NOWRT, NODEBUG, PLLEN  , HSM, PBADEN
                                                                                                            // PBADEN = Enable ADCs on PortB                 
#use delay(CLOCK=40MHz)

#define Trigger_VR    PIN_B5
#define Duration_VR   PIN_A0

unsigned int16 Freq_VR_RAW;
unsigned int16 Duration_VR_RAW;

void main()
{
  setup_adc(ADC_CLOCK_DIV_8 | ADC_CLOCK_INTERNAL | VSS_VDD );
 
  setup_adc_ports(sAN0 | sAN13);   
 
  port_b_pullups(FALSE);   // disable pullup resistors
 
  SETUP_CCP3(CCP_OFF);
   
   while(1)
      {     
         set_adc_channel(0);
         delay_ms(20);
         Duration_VR_RAW =  read_adc();// measure 0-5V Analog voltage and convert it to 1024 value
         delay_us(20);     
         // display Duration VR Value       
         
         set_adc_channel(13);
         delay_ms(20);
         Freq_VR_RAW = read_adc();  // measure 0-5V Analog voltage and convert it to 1024 value
         delay_us(20);     
         // display Freq VR Value         
               
         
       }//end While
 
}
 

Embarassed Embarassed Embarassed Embarassed Embarassed Embarassed Embarassed Embarassed Embarassed Embarassed Embarassed
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Wed Jun 15, 2016 7:28 am     Reply with quote

Your ADC clock settings are _wrong_.
ADC_CLOCK_INTERNAL, means use the internal RC clock. Not recommended above 1MHz, and doesn't use a divider. The divider is too low for your clock rate anyway. You need /64. for 40MHz.

Then the VSS_VDD setting goes on the setup_adc_ports command, not with the clock.

This ADC also allows you to program an automatic acquisition delay (so no need for delay between selecting and sampling). Have set it to 6.4uSec.

Also, you don't need PBADEN. What this does is set the whole of port B, to _default_ to being set for ADC. You can use it if you want, but not necessary.

Every peripheral 'above' the analog, has priority over it. Both CCP2, and CCP3 default to being on this pin.

Code:

#include <18F23K22.h>

#device ADC=10

#fuses NOWDT, NOPROTECT, NOLVP, NOPUT, NOWRT, NODEBUG, NOMCLR, NOBROWNOUT, NOPROTECT,NOCPD, NOWRT, NODEBUG, PLLEN  , HSM
                                                                                                               #use delay(CLOCK=40MHz)

#define Trigger_VR    PIN_B5
#define Duration_VR   PIN_A0

unsigned int16 Freq_VR_RAW;
unsigned int16 Duration_VR_RAW;

void main(void)
{
       
  setup_adc(ADC_CLOCK_DIV_64 | ADC_TAD_MUL_4);
  //The VSS to VDD setting does not go here
 
  setup_adc_ports(sAN0 | sAN13, VSS_VDD);   
 
  port_b_pullups(FALSE);   // disable pullup resistors
 
  SETUP_CCP3(CCP_OFF);
  SETUP_CCP2(CCP_OFF);
     
  while(TRUE)
  {     
      set_adc_channel(0);
      Duration_VR_RAW =  read_adc();// measure 0-5V Analog voltage and convert it to 1024 value
 
      // display Duration VR Value       
         
         
      set_adc_channel(13);
      Freq_VR_RAW = read_adc();  // measure 0-5V Analog voltage and convert it to 1024 value
   
      // display Freq VR Value         
   }//end While
}
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