View previous topic :: View next topic |
Author |
Message |
robertop Guest
|
CAD 3 channels |
Posted: Thu May 06, 2004 6:29 pm |
|
|
hi people:
I have a problem, I need use 3 channel but I can't did that. But I can used one channel, I Have pic 16f877 : but I need your help: my code is:
#if defined(__PCM__)
#include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#byte puertab=0x06
void main() { //
int value0,value1,value2;
set_tris_b(0x00);
setup_adc_ports(ALL_ANALOG);
setup_adc( ADC_CLOCK_INTERNAL );
// set_adc_channel( 1 );
do {
set_adc_channel(0);
value0 = Read_ADC();
puertab=value0;
delay_ms(500);
set_adc_channel(1);
value1 = Read_ADC();
puertab=value1;
delay_ms(500);
set_adc_channel(2);
value2 = Read_ADC();
puertab=value2;
delay_ms(500);
} while (TRUE);
}
please helpme !!
bye and thank u. |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Thu May 06, 2004 6:39 pm |
|
|
Two things:
1. From PIC16F877 datasheet: "When the device frequencies are greater than 1 MHz, the internal RC A/D conversion clock source is only recommended for SLEEP operation.". So for a 4MHz crystal it is recommended to use setup_adc(ADC_CLOCK_DIV_8).
2. From the CCS help file on set_adc_channel(): "Be aware that you must wait a short time after changing the channel before you can get a valid read. The time varies depending on the impedance of the input source. In general 10us is good for most applications.". You need to add that delay everytime you change the ADC channel, something like this:
Code: | do {
set_adc_channel(0);
delay_us(10); //Add this delay here
value0 = Read_ADC();
puertab=value0;
delay_ms(500);
...
... |
|
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Thu May 06, 2004 7:11 pm |
|
|
Another "good" way to delay is to make a dummy read of the new channel.
Code: |
#define COWS_NOT_HOME 1
while(COWS_NOT_HOME)
{
for(i=0;i<3;i++)
{
set_adc_channel(i);
value[i] = Read_ADC();
value[i] = Read_ADC();
}
/* now do something wonderful with your new data */
}
|
This can possibly make your code smaller if you haven't already used the delay_us() function anywhere else. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
|