Analog to Digital on 16F648A


Closed Thread
Results 1 to 22 of 22
  1. #1
    ghoot's Avatar
    ghoot Guest

    Default Analog to Digital on 16F648A

    Hello all,
    Is it possible to do this on this chip? I want to read the resistance on a cds Photo cell to tell when it's day or night. I tried using ADCON1 to set the PORTA registers, but it doesn't like it. After looking at the data sheet, it looks like CMCON is what is needed?, but the compiler doesn't like that either. Any tips?
    Thanks in advance,
    ghoot

  2. #2
    Keith55555's Avatar
    Keith55555 Guest


    Did you find this post helpful? Yes | No

    Default

    The cmcon tells it whether to turn on or off the comparators on chip.Which must be done on the channel you are using for your A/D conversion(turn it off that is)so include that in your code up top(find the register and the code for the channel(s) you're using,in the data sheet).You could also use a comparator on the chip for your function,but it is not as resolved as the A/D but in this case I think you could find a place that could suffice for 'tripping' the comparator between day and night.
    When you say "it doesn't like it" you mean you get a 'syntax' error?

  3. #3
    ghoot's Avatar
    ghoot Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Keith,
    Yes it gives an error saying something like "unrecognized command". I'll have to check for sure when I get home from work this evening, but it acts like that command is no good, but it is in the data sheet. I'm wondering if the ADCIN command can be used with this chip since ADCON isn't anywhere in the data sheet?
    Thanks,
    ghoot

  4. #4
    Keith55555's Avatar
    Keith55555 Guest


    Did you find this post helpful? Yes | No

    Default

    Well,I'll check the chip but in some pics there is an adcon0 or adcon1.If there's an A/D on chip ,there's an Adcon somethin'. Rest assured what you want to do, can be done.Why not post what you have concerning that section of code.It sounds to me like you might be missing the several things necessary to get things rolling.Don't feel bad,there is a very quick and exponential learning curve to use the I/O as analog intputs.
    If you want to tack on just a tiny few more components to the front end of what you already need for your photo cell(there are some very cute sot-23-5 comparators from digi-key that several manufacturers make or you could 'brew' your own with a mosfet transistor and voltage divider using your photo cell),you could use the PIC strictly digitally and use a less expensive pic.Way overboard,speaking,there are complete ,3 pin,brightness control chips by panasonic but stupidly expensive for your needs.

  5. #5
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Ghoot!

    Ghoot>>Yes it gives an error saying something like "unrecognized command". I'll have to check for sure when I get home from work this evening, but it acts like that command is no good, but it is in the data sheet. I'm wondering if the ADCIN command can be used with this chip since ADCON isn't anywhere in the data sheet?<<

    A couple of things....

    1. Make sure your chip supports and has a AD.
    2. Make sure your compiler is set to the correct chip!!!!<g>
    I have been caught on this a few times...<g>


    Dwayne


    PS just took a look at your Chip.. the 648a does NOT have a A/D on it. It has comparitors, thus you could use those, and possibly use the 5 volts off the chip as a reference. But I do not know the Voltage output of a photocell. (I am thinking 1.2 to 1.5 volts)...But don't take me on this!... Thus you would have to do a "Test" on how much light you will allow as being "Daylight".
    Last edited by Dwayne; - 5th May 2004 at 20:06.
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  6. #6
    ghoot's Avatar
    ghoot Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Dwayne,It says "2 Analog (comparators)", but doesn't say "ADC" like I've seen on some chips...is that the problem?
    Thanks for your reply,
    ghoot

  7. #7
    Keith55555's Avatar
    Keith55555 Guest


    Did you find this post helpful? Yes | No

    Default

    Did you mean 16F684?You wrote 16F648.I am actually about to use this 14 pin chip and it does indeed have an A/D on it.What Dwayne suggests has been a reality for me as well : don't for get to include in your compiling somewhere the option -p16F684 to distinguish this chip from the default.I thought I had the PDF on that.Will get it cause I need it anyway and find which adcon you have

  8. #8
    Keith55555's Avatar
    Keith55555 Guest


    Did you find this post helpful? Yes | No

    Default

    Uh...found it.No A/D.Use the comparator(if it indeed has one) as your sensor interface...I'm downloading that sheet now should

  9. #9
    ghoot's Avatar
    ghoot Guest


    Did you find this post helpful? Yes | No

    Default

    Wonder if the 16F88 would work as a replacement. I started with a 16f628 (doesn't look like it supports AD either) and ran out of space, so went to the 16f648 without having to change any code. If the 16F88 will work with only code changes and no circuit changes, then that may be the way to go. Sure wish there was a side by side comparison of compatabilities for the different chips somewhere. Some areas of the data sheets don't make enough sense to me yet to know what the differences are.

  10. #10
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Ghoot,

    Ghoot>>Hi Dwayne,It says "2 Analog (comparators)", but doesn't say "ADC" like I've seen on some chips...is that the problem?
    Thanks for your reply,<<

    ADCON is the A/D converters on the chip...Unfortuantely do you do not have *any* AD's on the 648. the 648 does have 2 comparators...

    CMCOM=7 will make 648A Digital instead of analog. Turning on those little Comparitors. <g>

    Sorry I can't be of more help right now.

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  11. #11
    Keith55555's Avatar
    Keith55555 Guest


    Did you find this post helpful? Yes | No

    Default

    Cmcon=7 turns the comparators off in most.I'm checking that register once I've the data sheet downloaded and show you what numbers work turning them on individually or both at the same time.So if you're going to use the comparators for the input,you want them on,not off which is the all digital mode.

  12. #12
    Keith55555's Avatar
    Keith55555 Guest


    Did you find this post helpful? Yes | No

    Default

    Okay,I read the comparator section.It's a nice setup if you are indeed comparator intensive .Cmcon =7 does indeed what it does in other pics with comparators ; turns 'em off.So other wise there are seven other modes(besides off )with different configurations.One thing comes to mind is the neat fact that there are two, making it easy to implement a 'window comparator'(something in between two trip points sets the 'action).Anyway,now decide if that's how you want to go(using a comparator) and your interface.Pick a comparator and we can figure out which configuration you need.You need to choose things like whether or not your reference is going to be external or use the internal one(the internal is not like using a voltage divider,it has discreet voltage levels and for your app,(and I am assuming you are not trying to precisely match an azimuth of the sun to the horizon but rather more of "yeah,I guess it's light out now")I think the internal reference will do.

  13. #13
    ghoot's Avatar
    ghoot Guest


    Did you find this post helpful? Yes | No

    Default

    Keith,
    I really don't know which way to go. I cand more easily understand how to get it going if I use the 16f88 since it has ADC, but I may run into bigger problems if I make the switch. I think i'll get one and try it.

    The comparrators may be the way to go, I'm just ignorant about how to make them work. From what I get so far from the data sheet, portA.0-4 can be inputs for the comparators and A3 and A4 are the outputs? All of these pins are also used to drive an LCD, but the LCD will be off while checking for daylight, so I don't think that will be a problem. Also, it will only be checking every 10 minutes or so. I noticed in the dta sheet where it's talking about the comarator output, that it says it may use more power in sleep mode, this may be a problem if it's very much. (A few uA wont be a problem though).

    In the future, I would like to add a low battery indicator so that should be considered as well on choosing the right chip for the job since the project is still in the design phases. From what I've read there though, is that the voltage can only be 5 volts, and the circuit uses a 9Volt battery, so there is a need for some extra circuitry there too it appears. Since I want to show the battery as "low" when it gets to about 6 volts.
    ghoot

  14. #14
    Keith55555's Avatar
    Keith55555 Guest


    Did you find this post helpful? Yes | No

    Default

    Hmmm,not sure if that will be a prob or not(I think essentially the LCD will look like some capacitance but may not be a critical problem in circuit.Again,I am assuming this read of when it is daylight,is a sloppy one but probably not a good idea if the ouput is pinging the LCD).I also don't know the particulars of what you're doing with the comparator outputs(They don't have to go out ,they can be read internally and make another pin do something as well)Hey if it's no problem for you to jump to a chip with A/D and dedicate a pin,then by all means.But make sure you have laid out what you want to do and what resources you need before you get yourself in the same position again."Once is philosophy,twice is perversion"-Goethe

  15. #15
    d1camero's Avatar
    d1camero Guest


    Did you find this post helpful? Yes | No

    Default

    ghoot, a completely different approach:

    If you want to measure resistance of a photoresistor, why not use an RC circuit? No A/D or fancy chips required. Check out page 141 of the "Basic Analog and Digital" basic stamp document here:

    http://www.parallax.com/dl/docs/books/edu/baad.pdf

    It has exactly what you would like to do. Just change the syntax a touch to work with PBP.

    Don

  16. #16
    anj's Avatar
    anj Guest


    Did you find this post helpful? Yes | No

    Default

    Gday Ghoot
    If you want to compare chips / functions, then the microchip site has a nice parametric search engine
    http://www.microchip.com/stellent/id...PAGE&nodeId=74
    From here you can search by various means, very usefull.

    As to the 16F88, its almost pin compatible with the 16F84 but has multiple ADCs and PWM and and and... Very nice chip.
    You could use ADC for yr light sensor, and via a simple voltage divider you can calibrate yr battery warning as well.
    Only one warning, the 16F88 uses different registers for controlling the ADC. Ie it uses ANSEL/ADCON0 and ADCON1. Its simple but different to prev chips. As such ADCIN wont work in PBP and you will need to do the ADC manually. Very easy though, and MEL supply some example files to cover this.
    You will also need the 16F88 library patches, as the 88 is not in the std PBP.
    Andrew

  17. #17
    ghoot's Avatar
    ghoot Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks to all for the info...lots of good info here. Should get me going.

  18. #18
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Hi ghoot,
    Didn't see it mentioned here... How about using the "POT" command?
    Works OK for me in the same application (detecting night/day).
    Does require some calibration of the "scale" value.

    Archilochus

  19. #19
    ghoot's Avatar
    ghoot Guest


    Did you find this post helpful? Yes | No

    Default

    Hello Arch!,
    I was considering the POT command, but the way the info reads is that you would have to calibrate it for each setup to allow for differences in caps and resistors. Is that correct? Since I'll be making several boards, I would have to allow for that so that all components would be the same. Haven't played with the POT command yet, so I don't know how "sensitive" it is, but maybe the thing to do would be to change it until the maximum resistance reads about 125? Then if you have a cap that is off by a large margin, it would still work? Say if a value of 50 or so was enough to know it's dark, then I could just say anything greater than 50 is dark. Does this make sense?

  20. #20
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Hi ghoot,
    Yup - the POT command requires some calibration. If you use tight tolerance caps, the real problem will be the CDS cells - which vary pretty wildly (at least all the ones I've tried) and are going to need some 'tweaking' to get the Day/Night switching right in any circuit.
    Ther used to be an "auto-calibrate" routine for the POT command in the PBP manual - but it may have not been included in the new manual.

    QUOTE:
    "Say if a value of 50 or so was enough to know it's dark, then I could just say anything greater than 50 is dark. Does this make sense?"
    EndQUOTE

    That's about what I did. The neat thing about the POT command is you can tell relative light intensity without using an ADC. Kind of interesting for logging info like "overcast", "bright night", etc.

    BTW - when calibrating, you'd want the maximum resistance (CDS totally dark) to end up with a count at or near 255.

  21. #21
    ghoot's Avatar
    ghoot Guest


    Did you find this post helpful? Yes | No

    Default

    Hello,
    I was playing with the POT command and couldn't get it to work. I noticed that RCTime looked very similar so I tried it and with a little tweeking it works great! It doesn't work like I assumed it would, but it works. The book shows this:

    'RCTIME Pin,State,Var

    Low PORTB.3 ' Discharge cap to start
    Pause 10 ' Discharge for 10ms
    RCTIME PORTB.3,0,W0 ' Read potentiometer on Pin3

    I assumed that the "State" value could be 0 or 1, but if I used this:

    High PORTB.3 ' Discharge cap to start
    Pause 10 ' Discharge for 10ms
    RCTIME PORTB.3,1,W0 ' Read potentiometer on Pin3

    Then it returned a 1 or 0 depending with the POT at min and max positions.

    I can't remember exactly (late night), but I think this is what I ended up with:

    High PORTB.3 ' Discharge cap to start
    Pause 10 ' Discharge for 10ms
    RCTIME PORTB.3,0,W0 ' Read potentiometer on Pin3

    This is probably basic, but I didn't know that having the CAP connected to +5V and the POT connected to GND used more power than having the CAP to GND and the POT to +5V. It doesn't seem to use any more power this way than it did without the CAP and POT.

  22. #22
    Join Date
    Jun 2004
    Posts
    18


    Did you find this post helpful? Yes | No

    Default

    Howdy fellas,

    Just a note on this subject. I have played with this extensively too. It works outstanding for a one time setup where you calibrate the CDS cell for that application but when I started using multiple setups everyone of them was different. I had to calibrate them all. Variance in the CDS cell alone would drive me crazy. Some would not work at all out of the same batch.

    If you do figure this out remember to set the port low after the POT command so you don't draw any more excessive current when you are not reading the port.

Similar Threads

  1. pic18f analog comparator problem
    By david.silaghi in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 6th May 2009, 09:38
  2. analog and digital
    By lerameur in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 10th June 2008, 02:40
  3. PortA analog and digital IO
    By krohtech in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th November 2007, 02:25
  4. Replies: 2
    Last Post: - 14th April 2006, 09:42
  5. Analog pins for digital input
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 9th September 2005, 00:32

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts