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

Reading Analog Input

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



Joined: 19 Feb 2013
Posts: 50

View user's profile Send private message Yahoo Messenger MSN Messenger

Reading Analog Input
PostPosted: Sun Feb 24, 2013 7:26 am     Reply with quote

I'm using PIC 18F2455 ,,
I wonder if the following is the right code to read analog input:
Code:


#include<18F2455.h>
#device adc = 10

#fuses HS,NOPROTECT,NOWDT,NOLVP
#use delay(clock = 8000000)

void main()
{
   unsigned int16  value;
   setup_adc(ADC_CLOCK_DIV_32);
   setup_adc_ports(AN0_TO_AN2|vss_vdd );
   set_adc_channel(0);\\ The pic will read from A0 ?
   while(true)
   {
   delay_ms(100);
   value=read_adc();
     printf("%Lu",value);
      delay_ms(500);

   }
}
temtronic



Joined: 01 Jul 2010
Posts: 9247
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Feb 24, 2013 7:47 am     Reply with quote

There is no better teacher than 'hands on' experience.
That being said, I suggest you...
1) cut code for a 'blinking LED' program. This would confirm that your harware is wired correctly,fuses are right,etc.
2)Next, cut code for the 'Hello PC' test.This will confirm your commincations to a PC via the UART.
3)Then rewire to allow access to the ADC section of the PIC.

Doing small steps, you can get a 'feel' for how both hardware and software work.

hth
Jay
amjad_alahdal



Joined: 19 Feb 2013
Posts: 50

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Sun Feb 24, 2013 7:53 am     Reply with quote

The problem is when I try to read analog signal from SHARP IR , The signal is supposed to be legal. What happens is all the time , the output is wrong. I thought that the part of reading the analog signal is wrong.
Speaking of the Steps that I should follow.
I do exactly what you have mentioned above. Because , I always don't expect a long program will be working well from the first time.

Thank you
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Sun Feb 24, 2013 8:52 am     Reply with quote

What you post should work.

Remember though that you are using the supply at the PIC as your 'reference'. Unless this is very nice and smooth, this will lead to less than perfect results.

Also, what is the output impedance of the sensor?. The PIC requires a relatively low impedance to drive the ADC. Do a test with something like a pot, and prove that the ADC is working as expected.

and, 'yes' as written, the ADC will read from A0.

Best Wishes
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sun Feb 24, 2013 11:54 am     Reply with quote

You say the result is "wrong". What sort of "wrong" is it? What are you expecting? Is the output close? Is it always high or always low? Is it too noisy? Can you give us some sample output?
_________________
The search for better is endless. Instead simply find very good and get the job done.
amjad_alahdal



Joined: 19 Feb 2013
Posts: 50

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Sun Feb 24, 2013 12:05 pm     Reply with quote

The entire code is supposed to read analog signal from Sharp IR. Then I multiply the result coming by a factor. This factor is written down in the Sharp IR. The result of the multiplication is supposed to give me the correct distance between the IR and the object.
If I go to what my program is supposed to do , it'll be as the following.
1- Set A0 to read Analog
2- set channel 0
3- read result from A0 put it in a variable x
4- multiply x by factor
5- print out the result
----------------------------------
amjad_alahdal



Joined: 19 Feb 2013
Posts: 50

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Sun Feb 24, 2013 12:11 pm     Reply with quote

Furthermore, I connected the Sharp IR output to A0 Directly
temtronic



Joined: 01 Jul 2010
Posts: 9247
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Feb 24, 2013 12:55 pm     Reply with quote

Get rid of the sensor and put a pot in FIRST !

Run a test program to read and display the voltage on the wiper of the pot which is connected to, say analog input #1.

Measure the wiper's voltage and compare the displayed value. If Vref is Vdd (+5), then an input of 2.55 volts should be 512 bits (1/2 of 1024).

Once you've confirmed this is so THEN rewire the Sharp sensor as the input. Be sure to read the datasheet! You might need an op amp buffer.

hth
jay
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Feb 24, 2013 2:03 pm     Reply with quote

This smells like a duplicate of thread you've already got running.

Please clarify.

Mike
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