Society of Robots - Robot Forum

Software => Software => Topic started by: airman00 on August 22, 2007, 03:48:29 PM

Title: Reading analog signal on PIC Micro
Post by: airman00 on August 22, 2007, 03:48:29 PM
What is the assembly language code for reading an analog signal on a PIC microcontroller? I have a PIC 16F627 and I need it to read the analog signal from a Maxsonar EZ1(connected to port a.0) and then react by lighting an LED connected to port A.1 whenever the value becomes too low.

Please help, I was searching online for hours already..  :(

,Eric
Title: Re: Reading analog signal on PIC Micro
Post by: snow on August 23, 2007, 02:20:09 AM
http://www.microchip.com/ParamChartSearch/chart.aspx?branchID=1002&mid=10&lang=en&pageId=74 (http://www.microchip.com/ParamChartSearch/chart.aspx?branchID=1002&mid=10&lang=en&pageId=74) - 16f627 doesnt have A/D.
Title: Re: Reading analog signal on PIC Micro
Post by: airman00 on August 23, 2007, 05:20:05 AM
OK thank you.
and what is the code for reading the analog signal? (in assembly language please)
Thank you so much,
Eric
Title: Re: Reading analog signal on PIC Micro
Post by: JonHylands on August 23, 2007, 06:28:01 AM
The face that it has no A/D channels indicates that you can't read analog signals with it. The hardware to support that just isn't available on that specific model.

- Jon
Title: Re: Reading analog signal on PIC Micro
Post by: airman00 on August 23, 2007, 02:54:10 PM
I have selected the PIC16F685. Now how can I use that chip to read an analog signal? I know how to read digital signals but not analog.
please help me. :P
Title: Re: Reading analog signal on PIC Micro
Post by: paulstreats on August 23, 2007, 07:19:30 PM
There are lots of examples to use the a/d using c rather than assembly, maybe you could compile one of the c examples into assembly using picclite and then find out that way.
Title: Re: Reading analog signal on PIC Micro
Post by: snow on August 24, 2007, 05:26:53 AM
Read datasheet?

It has topic 9.2.6 A/D CONVERSION PROCEDURE and example in assembly.
Title: Re: Reading analog signal on PIC Micro
Post by: paulstreats on August 24, 2007, 05:34:42 AM
here is an example i just found for the 16f877a usin a/d ports

http://www.piclist.com/techref/microchip/16f877adsamp.htm (http://www.piclist.com/techref/microchip/16f877adsamp.htm)
Title: Re: Reading analog signal on PIC Micro
Post by: Robotboy86 on August 25, 2007, 05:50:56 PM
You can also buy an external A/D IC.
Title: Re: Reading analog signal on PIC Micro
Post by: paulstreats on September 30, 2007, 04:49:23 PM
for 8 bit a/d pics you need to:

set the a/d ports to inputs i.e.                TRISA = 0b11111111
configure the a/d converter ie                 ADCON1 = 0b10000000
Select which port to read from ie             ADCON0 = 0b00000001
mask the interrupt                                 ADIE = 0
reset the interrupt flag                           ADIF = 0
reset the read adress value ie                  ADRES = 0
start the conversion ie                            ADGO = 1

wait for the conversion to happen
once the conversion is finished the ADIF bit should read as 1 so you can implement some code to wait until ADIF goes high.

once ADIF = 1 the 8 bit result will be waiting in ADRES (either a binary or hex value i.e. Result1 = ADRESH : Result1 = 0b00111011 )

for 10 bit results , the low bits are stored in ADRESL and the 2 high bits in ADRESH

THE ABOVE SAMPLES PORT A0 ONLY
Title: Re: Reading analog signal on PIC Micro
Post by: yesus on February 21, 2009, 04:48:49 PM
would it be possible to convert analog signals to digital siganls BEFORE connecting the device to the input of the PIC?
just like here:http://users.tpg.com.au/users/talking/a_to_d.html (http://users.tpg.com.au/users/talking/a_to_d.html)
Title: Re: Reading analog signal on PIC Micro
Post by: paulstreats on February 21, 2009, 08:19:26 PM
The tutorial link that you provided was to turn an analog signal into a boolean signal or i/o signal. This is completely against the principal of an a/d port. of course you can convert a signal like in that tutorial but you can only record 2 states either state 1 or state 0. an a/d port woulkd allow you to convert the voltage into a number. 5v would produce the number 255 and 0 volts would produce the number 0 where any voltage in between would produce a linear example. so 1.25v would produce a reading of 63 in software.

If you wanted to boost a very low voltage signal in order to register than you should look into using a boosting transistor.

So the example you provided is easy to do with a microcontroller you can just connect that up to a normal digital port but you limit you reading to just reading 1 for a high sweep and 0 for a low sweep. If however you amplify your signal correctly you can get a linear number reading from an analog to digital port
Title: Re: Reading analog signal on PIC Micro
Post by: yesus on February 22, 2009, 04:30:07 AM
i'm totally new to this but could u use that converter to create simple action as:
if there is high signal on the input THEN do some action(like when light in the room darkens,turn the other lights on)
i'm asking that because that's something i'm working on myself
Title: Re: Reading analog signal on PIC Micro
Post by: paulstreats on February 22, 2009, 10:00:16 AM
yes but you dont even need a converter. just connect the high/low signal to an i/o pin and read it.
Title: Re: Reading analog signal on PIC Micro
Post by: yesus on February 22, 2009, 02:09:30 PM
ok,but when input isn't really high,like say 0.5-1V ie.when you use photoresistor and you want to use that change in light intensity(which is followed but change in resistance and voltage drop) as an input and trigger some action through it ,can you connect the output from that photoresistor to IC input port directly and read from it??
Title: Re: Reading analog signal on PIC Micro
Post by: paulstreats on February 22, 2009, 02:18:11 PM
Sort of, you will need a voltage divider circuit. which is basically 2 resistors. If you look at the $50 robot tutorial on this site. It uses 2 photoresistors to read light intensities. You can see a step by step guide how to do it. The only difference is that tutorial doesnt use a PIC but the principles are the same