|
|
View previous topic :: View next topic |
Author |
Message |
Francesc Guest
|
A/D from 16F873 |
Posted: Tue Feb 10, 2004 12:27 am |
|
|
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
|
|
Posted: Tue Feb 10, 2004 1:25 am |
|
|
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
|
|
Posted: Tue Feb 10, 2004 1:30 am |
|
|
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
|
|
Posted: Tue Feb 10, 2004 1:30 am |
|
|
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 |
|
|
|
|
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
|