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

A/D from 16F873

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







A/D from 16F873
PostPosted: Tue Feb 10, 2004 12:27 am     Reply with quote

Question
Hi all, is very strange for me, we try to read 4 ports analogs from 16F873, but the readings not are corrects.
We try to enter differents voltages, here a sample:
0,0 V..0000 bits
0,5 V..0131 bits
1,0 V..0240 bits
2,0 V..0174 bits
3,0 V..0130 bits
3,5 v..0229 bits
4,0 v..0060 bits
4,5 v..0129 bits
5,0 v..1023 BITS

I include an small part of my code to try found the error.
Please, help me..

#include <16f873.h>
#device ADC=10
#fuses xt,nowdt,put,nolvp
#use delay(clock=4000000)
#use RS232(BAUD=9600,BITS=8,XMIT=PIN_C6,RCV=PIN_C7)
#use standard_io ( a )
#use standard_io ( b )
LONG T,K1,K2,K3,K4,K;
main()
{
while(1)
{
SET_TRIS_A(0x27);
//SETUP_ADC(ADC_CLOCK_DIV_8);
SETUP_ADC(ADC_CLOCK_INTERNAL);
SET_ADC_CHANNEL(0);
delay_ms(10);
T=READ_ADC();
K1=T/64; //0 to 1023
delay_ms(50);

SET_ADC_CHANNEL(1);
delay_ms(10);
T=READ_ADC();
K2=T/64;
delay_ms(50);

SET_ADC_CHANNEL(2);
delay_ms(10);
T=READ_ADC();
K3=T/64;
delay_ms(50);

SET_ADC_CHANNEL(5);
delay_ms(10);
T=READ_ADC();
K4=T/64;
delay_ms(50);

display();
}
}
KerryW
Guest







PostPosted: Tue Feb 10, 2004 1:25 am     Reply with quote

Add this line

setup_port_a( ALL_ANALOG );

Also, you should take these lines

SET_TRIS_A(0x27);
//SETUP_ADC(ADC_CLOCK_DIV_8);
SETUP_ADC(ADC_CLOCK_INTERNAL);

out of the while loop.

It should look like this

main()
{
SET_TRIS_A(0x27);
//SETUP_ADC(ADC_CLOCK_DIV_8);
SETUP_ADC(ADC_CLOCK_INTERNAL);
setup_port_a( ALL_ANALOG );
while(1)
{
SET_ADC_CHANNEL(0);
delay_ms(10);
T=READ_ADC();
K1=T/64; //0 to 1023
delay_ms(50);
.
.
.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 10, 2004 1:30 am     Reply with quote

Here are some additional comments on your code.

------------
I noticed that you have this line of code:

SET_TRIS_A(0x27);

You are setting pins A3 and A4 as outputs. If I look at the
chart in the 16F873 data sheet, of permissible Analog and
Digital pin combinations on Port A, I don't see a setting
that would allow you to do this. You should look at that
chart, and also look at the constants (such as A_ANALOG)
given in the 16F873.H file. The constants are used with
setup_adc_ports(), and correspond to the settings shown
in the data sheet.

---------------------
After you choose the proper setting for your Port A
configurations (between Analog and Digital pins), you
should change your SET_TRIS_A() function so it fits
that configuration.

---------------------
I also noticed that you are dividing the ADC result by 64,
to compensate for the result being left-justified in a 16-bit word.

You don't have to do that. The PIC has a bit which allows
you to set the ADC result to be right-justified. See this
post which shows how to do it:

http://www.ccsinfo.com/forum/viewtopic.php?t=2771&highlight=adfm_bit
wedilo



Joined: 16 Sep 2003
Posts: 71
Location: Moers, Germany

View user's profile Send private message

PostPosted: Tue Feb 10, 2004 1:30 am     Reply with quote

Hello Francesc,
Why does you devides the value from ADC by 64?
I think READ_ADC() returns an 10bit value (0...1023)

But that is not the problem, I mean you have forgotten the 'setup_adc_ports' command

Code:
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(ALL_ANALOG);
set_adc_channel(0);

Try it out...

Hope to help you
73 Sven
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