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

what's wrong with this simple A/D code?

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







what's wrong with this simple A/D code?
PostPosted: Thu Feb 16, 2006 7:22 am     Reply with quote

Hi to everyone,
I'm trying to simply convert an analog input using the ad module of the PIC18F4431.
The analog input is on the AN0 pin, the range of the voltage is 0-5V (Vdd-Vss).
The problem is that whatever I put on the AN0 pin (0V, 1V, 3V...5V) I always read adc3=1023.
Trying to visualize adresh and adresl I obtained that adresh=3 and adresl=255.
I'v also tried setting manually each of the AD registers, but it was the same. Always 1023.

What's wrong?
The code is the following:

#include <18F4431.h>
#device adc=10
#use delay(clock=20000000)
#fuses HS, NOPROTECT, NOBROWNOUT, NOWDT, NOLVP, PUT, NOSTVREN, NOFCMEN, NOIESO, MCLR
#use rs232(baud=19200,xmit=PIN_C6,rcv=PIN_C7,parity=N,bits=8)

#define led_status PIN_D7


void main()
{ unsigned int16 adc3=0;

output_bit(led_status,1);
delay_ms(2000);
output_bit(led_status,0);
printf("\n\r adc alive");

setup_adc_ports(sAN0|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);

while (1)
{
set_adc_channel(0);
delay_us(20);
adc3=read_adc( );

printf("\n\r adc=%lu",adc3);
delay_ms(1000);
}
}

Thank you.
Eli
dr
Guest







ADC problem
PostPosted: Thu Feb 16, 2006 7:57 am     Reply with quote

setup_adc_ports(sAN0|VSS_VDD);

I think your syntax is wrong here. In my usage, setup_adc_ports( ALL_ANALOG) always works. I don't know how that would pan out with the
18F4431 though.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Feb 16, 2006 9:38 am     Reply with quote

Quote:
#device adc=10


Change this to:

Quote:
#device adc=16


Plus, your result will be a 10 bit value and uses two full Bytes. This leaves six bits unused. The register bit ADFM controls which 'side' of these two bytes will be stuffed with zero's. ACCON2 contains bit ADFM. ADFM defaults to a value of '0'. This will Left justify the result, meaning the lower six bits will be zero's. Setting ADFM to a '1' will Right justify the result and, maybe, give you the result you are looking for.

Ronald
eliberg
Guest







PostPosted: Thu Feb 16, 2006 11:05 am     Reply with quote

Thanks for the replays.
First of all I need just one AN pin, so I cannot use ALL_ANALOG, also because the other possible AN pins are used as digital I/O, in my project.
In the 18f4431.h "setup_adc_ports(sAN0|VSS_VDD)" is the right syntax to set AN0 as analog, from 0 to 5V.

Second, I'm not sure I can use #device adc=16, because the AD module is a 10bit AD converter, in the 18F4431, so I can set adc=8 or adc=10.
Anyway I will try, thanks for the idea.

In the meanwhile I think I solved the problem by myself. Smile Inserting "set_tris_a(0b00011001)" now the adc works. The only strange thing is that the digital value becomes 255 when the input voltage is 4V, not 5V.

Any idea for this new problem?
Thank you very much!
Eli
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Thu Feb 16, 2006 11:19 am     Reply with quote

Quote:

setup_adc_ports(sAN0|VSS_VDD)

Is right.

If you use #device adc=10 the output range is 0 - 03FF
If you use #device adc=16 the output range is 0 - FFC0

Quote:

The only strange thing is that the digital value becomes 255 when the input voltage is 4V, not 5V.


Nop. It would be:
Code:

     255 = 1.25V
     512 = 2.50V
    1024 = 5.00V


Unles you are looking only the less significant register of 0x03FF


Humberto
eliberg
Guest







PostPosted: Mon Feb 20, 2006 7:34 am     Reply with quote

Ok, with #device=10 I obtain 1023 at 4V, instead of 5V.
The same problem with #device=8 (255 at 4V).
I don't know where is the problem!
eli
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Mon Feb 20, 2006 8:16 am     Reply with quote

Do you know for a fact that your supply rail is 5V?

If it has drooped to 4V, then you will get the full reading at the lower voltage.
_________________
The difference between genius and stupidity is that genius has its limits...
eliberg
Guest







PostPosted: Fri Feb 24, 2006 6:56 am     Reply with quote

Yes, I'm sure my voltage is 5V, but if I set my Vdd, for example, to 5.5V, the ADC gets 1023 to 4.5V.
Do you think it cuold be a "broken" PIC?
Thanks
eli
Ttelmah
Guest







PostPosted: Fri Feb 24, 2006 7:41 am     Reply with quote

Double check that the supply is actually 'right' at pin11. This is the pin used for the analog reference.
Yes, it could be a faulty PIC, possibly in the MOSFET used to connect the reference to the ADC.

Best Wishes
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