|
|
View previous topic :: View next topic |
Author |
Message |
microJuan
Joined: 08 Mar 2008 Posts: 6
|
pic18f23k22 and PORTB ADC not working |
Posted: Wed Jun 15, 2016 6:49 am |
|
|
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
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Wed Jun 15, 2016 7:28 am |
|
|
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
}
|
|
|
|
|
|
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
|