View previous topic :: View next topic |
Author |
Message |
moryoav
Joined: 23 Feb 2014 Posts: 35
|
sampling 3 analog inputs |
Posted: Thu Mar 06, 2014 6:37 am |
|
|
hey, im having a little problem understanding how to sample 3 analog inputs from the accelerometer ADXL335. im using pic16f877A.
i've managed to sample one analog input [Y] with this code:
Code: | #include <16f877a.h>
#fuses NOWDT, NOPROTECT, HS
#DEVICE ADC=10
#use DELAY (crystal=20MHz)
#use rs232 (xmit=pin_C6, rcv=pin_C7, baud=38400)
void main(void)
{
int16 res_Y;
setup_ADC_ports(AN0);
set_adc_channel(0);
setup_adc(ADC_CLOCK_DIV_32);
while(TRUE)
{
delay_us(10);
res_Y=read_adc();
printf("ADC Y=%lu\n\r", res_Y);
}
} |
how do i tell the uC what pins to sample from? is it possible to sample all 3 axis alltogether?
the only codes i found online are using adcon\chs\etc and i've absolutely no idea what's going on there |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Thu Mar 06, 2014 6:54 am |
|
|
Set_ADC_channel(channel no) for the other two analog pins, wait a couple of cycles before reading to allow SH to settle.
Regards |
|
|
moryoav
Joined: 23 Feb 2014 Posts: 35
|
|
Posted: Thu Mar 06, 2014 7:06 am |
|
|
something like this?
Code: | setup_ADC_ports(AN0_AN1_AN3);
set_adc_channel(0);
set_adc_channel(1);
set_adc_channel(2);
setup_adc(ADC_CLOCK_DIV_32); |
if so, how do i "switch" between the channels?
if not, please explain.
also, is there suppose to be any change in the while loop? any commands to add\change? |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Mar 06, 2014 7:19 am |
|
|
moryoav wrote: | something like this?
Code: | setup_ADC_ports(AN0_AN1_AN3);
set_adc_channel(0);
set_adc_channel(1);
set_adc_channel(2);
setup_adc(ADC_CLOCK_DIV_32); |
if so, how do i "switch" between the channels?
if not, please explain.
|
No, something like this:
Code: |
int16 Value_0, Value_1, Value_3;
void main()
{
...
setup_ADC_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_DIV_32);
...
while (TRUE)
{
set_adc_channel(0); // Select AN0
delay_us(12); // Wait for Taq, typically about this long; depends on the PIC. Read the datasheet to get the correct time.
Value_0 = read_adc(); // Convert and read the ADC result. This waits while the ADC converts, but it is possible to start a conversion, do other stuff and then go back to read the result.
...
set_adc_channel(1); // Switch to AN1
delay_us(12); // Do the same as before
Value_1 = read_adc(); // This time we convert and read AN1
...
set_adc_channel(3); /// and repeat for AN3
delay_us(12);
value_3 = read_adc();
...
}
}
|
There are many other ways of doing it, and doing it more efficiently, but this is the basic way. |
|
|
moryoav
Joined: 23 Feb 2014 Posts: 35
|
|
Posted: Thu Mar 06, 2014 7:22 am |
|
|
ahhh so set_adc_channel is a way of saying "focus on this pin and sample it's value, then focus on the other one and sample it's value"?
if i understood correctly then that answered my question and many many thanks :> |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Mar 06, 2014 7:31 am |
|
|
moryoav wrote: | ahhh so set_adc_channel is a way of saying "focus on this pin and sample it's value, then focus on the other one and sample it's value"? |
Yes, there's just one ADC, but its got a switch in front of it to select which analogue input to sample. Every time you change the switch with set_adc_channel, you have to wait long enough for the ADC to "catch up" and sample the new channel correctly. You should also wait about the same time between successive readings of the same channel.
Also, as there's only one ADC, it cannot read more than one input channel at a time, so there's no way to simultaneously sample two or more channels. |
|
|
|