View previous topic :: View next topic |
Author |
Message |
amjad_alahdal
Joined: 19 Feb 2013 Posts: 50
|
Reading Analog Input |
Posted: Sun Feb 24, 2013 7:26 am |
|
|
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: 9226 Location: Greensville,Ontario
|
|
Posted: Sun Feb 24, 2013 7:47 am |
|
|
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
|
|
Posted: Sun Feb 24, 2013 7:53 am |
|
|
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: 19513
|
|
Posted: Sun Feb 24, 2013 8:52 am |
|
|
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
|
|
Posted: Sun Feb 24, 2013 11:54 am |
|
|
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
|
|
Posted: Sun Feb 24, 2013 12:05 pm |
|
|
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
|
|
Posted: Sun Feb 24, 2013 12:11 pm |
|
|
Furthermore, I connected the Sharp IR output to A0 Directly |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sun Feb 24, 2013 12:55 pm |
|
|
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
|
|
Posted: Sun Feb 24, 2013 2:03 pm |
|
|
This smells like a duplicate of thread you've already got running.
Please clarify.
Mike |
|
|
|