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 CCS Technical Support

16f877A Port A (ADC)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sysysy



Joined: 17 Nov 2010
Posts: 38
Location: 121

View user's profile Send private message

16f877A Port A (ADC)
PostPosted: Sat Dec 04, 2010 7:46 am     Reply with quote

Hi,

May I know if it is allowed to use only 1 of the Port A pin (channel 0 to read the adc), then the remaining Port A pins to control my motor?

Problem Encountered:

When I only use PIN_A0 to interface with the output of LM35, my LCD show me valid temperature result. (29 Celcius or 61 Resolution number.)

But when I enable my motor control pins that also use Port A, then the temperature result becomes not correct. I wonder why this could happen.

when i use multimeter to measure the pin, the voltage is about 0.29v (29celcius), but the lcd display never 29 but 33 Celcius++ something. and it is not stable, sometimes will exceed 40 and 50 Celcius.

Following is my setup:
Code:

   set_tris_A(1) ;                 //only pin_a0 to be an input
   setup_adc(ADC_CLOCK_INTERNAL); //use internal clock
   setup_adc_ports(0xE);          //use only pin_a0 as analog input
   set_adc_channel(0);            //set pin_a0 as adc channel

Anyone can point out my setup problem?

Thanks,

sysysy
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sat Dec 04, 2010 9:59 am     Reply with quote

What chip?.

It is probably not a 'setup problem'.
There is a fault in the setup, if your CPU clock is higher than 1MHz. _Read the adc section of the data sheet_. Look at what clock is recommended for your CPU clock rate, and read the note about ADC_CLOCK_INTERNAL.
Then use the defines for selecting the adc_port, don't use your own values for this.

Two more things:
1) Unless you are using 'fast_io, don't bother with the TRIS. The compiler will do this for you, _but_ is you are using a byte wide port output (output_a), this _will_ interfere with your ADC, unless you switch to using fast_io....
2) Your problem has all the symptoms of possibly being noise. When you start controlling things like motors, _careful_ electrical design is needed....

Best Wishes
sysysy



Joined: 17 Nov 2010
Posts: 38
Location: 121

View user's profile Send private message

PostPosted: Sat Dec 04, 2010 11:18 am     Reply with quote

Quote:
There is a fault in the setup, if your CPU clock is higher than 1MHz. _Read the adc section of the data sheet_.


Thanks for the reply.

I just do an experiment just now, I use another PIC to repeat the test.
(Pre-condition: only 1 pin use in the whole A port). In the result, I can get precise value that display on my LCD. So, I really suspect is the noise in my previous problem. (Since 10mV already affect 1 celcius).

But one thing not clear here, I use 4 MHz clock for my PIC. But the adc section of the 16f877a datasheet really not talk much about how to choose the clock rate. It just show how to set the register according to what clock rate we want?

May I know, how really this take effect in the outcome? Why is it so crucial? Can you give me some example?

Thanks.

regards,

sysysy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Dec 04, 2010 11:18 pm     Reply with quote

Actually, the data sheet says exactly what is needed:
Quote:

11.2 Selecting the A/D Conversion
Clock
The A/D conversion time per bit is defined as TAD. The
A/D conversion requires a minimum 12TAD per 10-bit
conversion. The source of the A/D conversion clock is
software selected. The four possible options for TAD
are:
• 2TOSC
• 8TOSC
• 32TOSC
• Internal A/D module RC oscillator (2-6 µs)
For correct A/D conversions, the A/D conversion clock
(TAD) must be selected to ensure a minimum TAD time
of 1.6 µs.


With a 4 MHz oscillator, the oscillator period is 250 ns.
The clock divisor values above give the following Tad times:
Quote:

2TOSC = 500 ns
8TOSC = 2 us
32TOSC = 8 us
Internal = 2 to 6 us (Only use if PIC is running at less than 1 MHz).

A divisor of 8 gives a TAD time of 2us. This divisor allows the fastest
conversion time which still satisfies the minimum TAD time of 1.6 us.
So your setup_adc() statement should look like this:
Code:

setup_adc(ADC_CLOCK_DIV_8);
sysysy



Joined: 17 Nov 2010
Posts: 38
Location: 121

View user's profile Send private message

PostPosted: Sun Dec 05, 2010 12:09 am     Reply with quote

Thanks for the explanation.

Anyway, I am very honestly say sorry here, because I re-check the datasheet again and found that the bottom section for the choosing clock rate explanation after the set register part.

This is the very serious mistake that I made. Sorry for never scroll down and assume it finish after reading the how to set register part.

Again, thanks for the help,
going to digest all of them now.

Thanks.

regards,

sysysy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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