View previous topic :: View next topic |
Author |
Message |
sliders_alpha
Joined: 03 Mar 2008 Posts: 55
|
issue with ADC |
Posted: Fri Apr 04, 2008 10:02 am |
|
|
plop,
i've got troube with the ADC.
i need :
1 ADC input
1 digital input
2 digital output
1 digital open-drain output.
so i wanted to do it like this :
Code: | #include <16F876.h>
#device ADC = 10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 4000000)
void main(){
set_tris_A(0x03);
output_A(0x00);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(RA0_ANALOG);
short adcvalue;
while(1){
my code here
}
} |
with this, only PIN_A4,PIN_A1 and pin_A2 are working, I can't put anything on PIN_A3 and my ADC input isn't working.
do you know why?
ps : i'm reading the ADC like this : value = readadc();
thanks _________________ yup, i know, i don't speak english very well
CCS V4.057 |
|
|
Matro Guest
|
|
Posted: Fri Apr 04, 2008 10:52 am |
|
|
This line is unuseful since you don't use fast_io or fixed_io
With the piece of code that you posted, there is no use of ADC so hard to say what is bad...
Matro. |
|
|
sliders_alpha
Joined: 03 Mar 2008 Posts: 55
|
|
Posted: Fri Apr 04, 2008 11:04 am |
|
|
well if i've correctly understand.
i need to put :
#use fast_io
in my code to make it work, also, you want to see the full code :
Quote: | #include <16F876.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
void main() {
long adcvalue;
short read;
set_tris_A(0x03);
output_A(0x00);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(RA0_ANALOG);
set_adc_channel(0);
while(1){
adcvalue = read_adc();
if (adcvalue < 799){
output_BIT(PIN_A4, 1);
delay_us(800);
output_BIT(PIN_A4, 0);
delay_us(200);
output_BIT(PIN_A5, 1);
}
else {
output_BIT(PIN_A4, 0);
output_BIT(PIN_A5, 0);
delay_ms(1000);
}
read = input(PIN_A1);
if (read == 1){
output_BIT(PIN_A2, 1);
delay_ms(1000);
output_BIT(PIN_A2, 0);
output_BIT(PIN_A3, 1);
}
}
} |
_________________ yup, i know, i don't speak english very well
CCS V4.057
Last edited by sliders_alpha on Fri Apr 04, 2008 11:45 am; edited 1 time in total |
|
|
Matro Guest
|
|
Posted: Fri Apr 04, 2008 11:12 am |
|
|
Simpler than using fast_io, as a first step you can just remove these 2 lines :
Code: |
set_tris_B(0x03);
output_B(0x00);
|
So now the code is here, what is exactly the problem?
Does the code is locked in the "read_adc()"?
Or the read value isn't the one you are waiting for?
Matro. |
|
|
sliders_alpha
Joined: 03 Mar 2008 Posts: 55
|
|
Posted: Fri Apr 04, 2008 11:24 am |
|
|
nope
read_adc always return a value<799 wich is not normal.
and pin A3 is always at low state.
if i'm usign A0 as an ADC and port B for my digital input/output everything works. but i can only have an open-drain output on port A _________________ yup, i know, i don't speak english very well
CCS V4.057 |
|
|
Matro Guest
|
|
Posted: Fri Apr 04, 2008 11:34 am |
|
|
In your 2nd post, all possible analog inputs are selected as analog when in your 1st post only AN0 is selected.
Which of both are you using?
Matro |
|
|
sliders_alpha
Joined: 03 Mar 2008 Posts: 55
|
|
Posted: Fri Apr 04, 2008 11:44 am |
|
|
sorry,
setup_adc_ports(RA0_ANALOG);
(edited) _________________ yup, i know, i don't speak english very well
CCS V4.057 |
|
|
w2drz
Joined: 27 Dec 2006 Posts: 55 Location: Western New York - USA
|
|
|
Ttelmah Guest
|
|
Posted: Fri Apr 04, 2008 3:36 pm |
|
|
Note also (not mentioned yet), that a 'short', cannot hold a 10bit adc value. The register used to receive the ADC value, _must_ be declared as a 'long'.
Best Wishes |
|
|
sliders_alpha
Joined: 03 Mar 2008 Posts: 55
|
|
Posted: Fri Apr 04, 2008 4:17 pm |
|
|
@tetelma, well, i'm using a long type.
and i find a solution, if I replace :
Quote: | set_tris_A(0x03);
output_A(0x00); |
by
Quote: | output_float(PIN_A1);
output_drive(PIN_A2);
output_drive(PIN_A3);
output_drive(PIN_A4);
output_drive(PIN_A5); |
but why? i mean, i don't see any error in my first code _________________ yup, i know, i don't speak english very well
CCS V4.057 |
|
|
Matro Guest
|
|
Posted: Sat Apr 05, 2008 4:10 am |
|
|
Because you don't use fast_io or fixed _io the compiler will automatically set the tris for you and so all port A pins will be outputs. What isn't compatible with ADC.
That's why I asked that you remove them. ;-)
Matro. |
|
|
Ttelmah Guest
|
|
Posted: Sat Apr 05, 2008 4:56 am |
|
|
The 12th line in your original posted code (which can't work, because type declarations in C, _must_ be in front of code in any section), says 'short adcvalue;', which is what worried me...
Best Wishes |
|
|
sliders_alpha
Joined: 03 Mar 2008 Posts: 55
|
|
Posted: Mon Apr 07, 2008 12:55 pm |
|
|
ha ok, but i don't understand how to use fast_io or fixed_io.
i juste need to write #use fast_io in my code and then i'll be able to use :
set_tris_A(0x03);
output_A(0x00);
?
@ Ttelmah : my bad ;) _________________ yup, i know, i don't speak english very well
CCS V4.057 |
|
|
Matro Guest
|
|
Posted: Mon Apr 07, 2008 1:01 pm |
|
|
#use fast_io(port_a)
is an example for using fast_io for the port A (and only this one).
Matro. |
|
|
Matro Guest
|
|
Posted: Mon Apr 07, 2008 1:05 pm |
|
|
Have a look at the reference manual or in PCW help (if you own it).
ccsinfo . com / downloads / CReferenceManual . pdf
-remove the spaces in the link. ;-)
Matro |
|
|
|