View previous topic :: View next topic |
Author |
Message |
crushneck
Joined: 27 Nov 2005 Posts: 29
|
2 analog inputs |
Posted: Fri Feb 10, 2006 2:25 am |
|
|
Is it possible to get two analog input from 1 PIC? Im using RA0 and RA1 for my PIC18F452. If its possible, what must i change to my source codes? |
|
|
rberek
Joined: 10 Jan 2005 Posts: 207 Location: Ottawa, Canada
|
|
Posted: Fri Feb 10, 2006 7:14 am |
|
|
You use the set_adc_channel() command to select the input you wish to sample. _________________ The difference between genius and stupidity is that genius has its limits... |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri Feb 10, 2006 7:37 am |
|
|
Opening the 18F452.H file in the Devices folder, you have the constant definitions used
to select the different analog setup input configuration:
// Constants used in SETUP_ADC_PORTS() are:
#define NO_ANALOGS 7 // None
#define ALL_ANALOG 0 // A0 A1 A2 A3 A5 E0 E1 E2
#define AN0_AN1_AN2_AN4_AN5_AN6_AN7_VSS_VREF 1 // A0 A1 A2 A5 E0 E1 E2 VRefh=A3
#define AN0_AN1_AN2_AN3_AN4 2 // A0 A1 A2 A3 A5
#define AN0_AN1_AN2_AN4_VSS_VREF 3 // A0 A1 A2 A5 VRefh=A3
#define AN0_AN1_AN3 4 // A0 A1 A3
#define AN0_AN1_VSS_VREF 5 // A0 A1 VRefh=A3
#define AN0_AN1_AN4_AN5_AN6_AN7_VREF_VREF 0x08 // A0 A1 A5 E0 E1 E2 VRefh=A3 VRefl=A2
#define AN0_AN1_AN2_AN3_AN4_AN5 0x09 // A0 A1 A2 A3 A5 E0
#define AN0_AN1_AN2_AN4_AN5_VSS_VREF 0x0A // A0 A1 A2 A5 E0 VRefh=A3
#define AN0_AN1_AN4_AN5_VREF_VREF 0x0B // A0 A1 A5 E0 VRefh=A3 VRefl=A2
#define AN0_AN1_AN4_VREF_VREF 0x0C // A0 A1 A5 VRefh=A3 VRefl=A2
#define AN0_AN1_VREF_VREF 0x0D // A0 A1 VRefh=A3 VRefl=A2
#define AN0 0x0E // A0
#define AN0_VREF_VREF 0x0F // A0 VRefh=A3 VRefl=A2
#define ANALOG_RA3_REF 0x1 //!old only provided for compatibility
#define A_ANALOG 0x2 //!old only provided for compatibility
#define A_ANALOG_RA3_REF 0x3 //!old only provided for compatibility
#define RA0_RA1_RA3_ANALOG 0x4 //!old only provided for compatibility
#define RA0_RA1_ANALOG_RA3_REF 0x5 //!old only provided for compatibility
#define ANALOG_RA3_RA2_REF 0x8 //!old only provided for compatibility
#define ANALOG_NOT_RE1_RE2 0x9 //!old only provided for compatibility
#define ANALOG_NOT_RE1_RE2_REF_RA3 0xA //!old only provided for compatibility
#define ANALOG_NOT_RE1_RE2_REF_RA3_RA2 0xB //!old only provided for compatibility
#define A_ANALOG_RA3_RA2_REF 0xC //!old only provided for compatibility
#define RA0_RA1_ANALOG_RA3_RA2_REF 0xD //!old only provided for compatibility
#define RA0_ANALOG 0xE //!old only provided for compatibility
#define RA0_ANALOG_RA3_RA2_REF 0xF //!old only provided for compatibility
You will need to select the BOLD option:
#define AN0_AN1_VSS_VREF 5 // A0 A1 VRefh=A3
SETUP_ADC_PORTS(AN0_AN1_VSS_VREF);
Define A0 and A1 as analog inputs with A3 (VRefh) tied to a voltage reference equal
to the maximum expected input voltage.
Read the datasheet for details.
Humberto |
|
|
LUKS Guest
|
|
Posted: Wed Nov 05, 2008 10:00 am |
|
|
Hi! I have a similar problem with the PIC 18F458, I want to measure 2 independent signals, here is the code
Code: |
#include <18F458.h>
#device adc=10
void main()
{
long ADC_VALUE_0;
long ADC_VALUE_1;
setup_adc_ports(AN0_AN1_VSS_VREF);
setup_adc(ADC_CLOCK_DIV_8);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
disable_interrupts(GLOBAL);
setup_low_volt_detect(FALSE);
set_tris_a (0x07);
set_tris_e (0x00);
while(1)
{
set_adc_channel(0);
ADC_VALUE_0 = Read_ADC();
if(ADC_VALUE_0 >= 512)
{
output_high(PIN_E1);
}
else
{
output_low(PIN_E1);
}
set_adc_channel(1);
ADC_VALUE_1 = Read_ADC();
if(ADC_VALUE_1 >= 256)
{
output_high(PIN_E2);
}
else
{
output_low(PIN_E2);
}
}
|
at the end if I set a Vref=5v, I get an output high on E1=2.5v and E2=1.2v but i only have A0 or A1 conected to the source.
What should I do to get E1 high as a result of A0>=2.5v and E2=high as a result of A1>=1.2v |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 05, 2008 12:44 pm |
|
|
Quote: | #include <18F458.h>
#device adc=10
void main()
{ |
That's not a complete program. Post the #fuses statement and the
#use delay() statement.
Quote: | setup_adc_ports(AN0_AN1_VSS_VREF); |
Don't use Vref initially. Use the internal Vdd as the Vref. It's easier.
Change to this, as shown in bold:
Quote: | setup_adc_ports(AN0_AN1_AN3); |
Quote: | set_adc_channel(0);
ADC_VALUE_0 = Read_ADC();
set_adc_channel(1);
ADC_VALUE_1 = Read_ADC(); |
You need a delay after you change the A/D channel. This is in
the 18F458 data sheet. Add the lines shown in bold below:
Quote: |
set_adc_channel(0);
delay_us(20);
ADC_VALUE_0 = Read_ADC();
set_adc_channel(1);
delay_us(20);
ADC_VALUE_1 = Read_ADC(); |
|
|
|
|