CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

issue with ADC
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sliders_alpha



Joined: 03 Mar 2008
Posts: 55

View user's profile Send private message

issue with ADC
PostPosted: Fri Apr 04, 2008 10:02 am     Reply with quote

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







PostPosted: Fri Apr 04, 2008 10:52 am     Reply with quote

Code:

set_tris_A(0x03);

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

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 11:04 am     Reply with quote

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







PostPosted: Fri Apr 04, 2008 11:12 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 11:24 am     Reply with quote

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







PostPosted: Fri Apr 04, 2008 11:34 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 11:44 am     Reply with quote

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

View user's profile Send private message Visit poster's website

Looks to be same issue addressed in the post listed below
PostPosted: Fri Apr 04, 2008 3:01 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=34287

tom
Ttelmah
Guest







PostPosted: Fri Apr 04, 2008 3:36 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 4:17 pm     Reply with quote

@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







PostPosted: Sat Apr 05, 2008 4:10 am     Reply with quote

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







PostPosted: Sat Apr 05, 2008 4:56 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 07, 2008 12:55 pm     Reply with quote

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







PostPosted: Mon Apr 07, 2008 1:01 pm     Reply with quote

#use fast_io(port_a)
is an example for using fast_io for the port A (and only this one).
Matro.
Matro
Guest







PostPosted: Mon Apr 07, 2008 1:05 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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