12F675 compare voltage help.


Closed Thread
Results 1 to 20 of 20
  1. #1
    Join Date
    Aug 2006
    Posts
    91

    Default 12F675 compare voltage help.

    I am trying to figure out how to use a 12F675 to compare voltage.
    This will eventually be used to meaure a current load.

    I am sending the output to serial and checking it via hyperterm just so I can see whats going on.

    I have the chip connected like this.
    1 + 3
    2 NC
    3 NC
    4 NC
    5 NC
    6 pin2 serial.
    7 testing connected to ground/pos/nothing.
    8 ground and serial ground.

    Here is my code most copied off this forum.
    Code:
    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
        ' Internal Oscillator
        ' Enable watch dog timer
        ' Enable power up timer
        ' Disable MCLR pin
        ' Enable brown out detect
    
    DEFINE OSCCAL_1K 1
    INCLUDE "modedefs.bas"
    CMCON = 7 ' turn off analog comparator
    DEFINE OSC          4
    DEFINE ADC_BITS     10  '10 BIT A/D CONVERSION RESULT
    DEFINE ADC_CLOCK    3   'INTERNAL A/D RC CLOCK
    DEFINE ADC_SAMPLEUS 50  'SET SAMPLE TIME IN MICROSECONDS
    adval0  VAR WORD		'Create adval0 to store result
    
    TRISIO = %000001        ' Set GSIO 0 & 3 TO INPUTS, others to OUTPUT
    ANSEL.0 = 1             ' Set GSIO 0 to ANALOG
    ADCON0.7 = 1	        ' Right Justify for 10-bit
    I var byte
    
    
    
    
    
    
    
    Pause 2000 
    
    
    loop:
        ADCIN 0, ADVAL0         ' Read A/D channel 0            
        SerOut GPIO.1,N2400,["Result ",#adval0,13,10]
    
    Pause 200
    GoTo loop
    When I have pin 7 connected to ground.
    I get all 0's
    When I have pin 7 connected to +
    I get Result 1020
    When I disconnect 7 from + I get Result 600 and it slowly goes down to 113 to 140.

    Can someone explain to me what this is telling me?

  2. #2
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    The ADC is 10-bits so the highest possible reading is 1023. Your 1020 indicates 3V (if that's your Vcc).

    The 600 that fades indicates the ADC input is floating.

    The 0 reading indicates the 0V.

  3. #3
    Join Date
    Aug 2006
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    Ok thanks that helps but what does it mean its floating?

    That mean the VCC is floating or when the ADC is connected to nothing its floating?

  4. #4
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    The ADC input is floating and the reading is meaningless if it has no connection.

  5. #5
    Join Date
    Aug 2006
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    Thank you very much.

  6. #6
    Join Date
    Aug 2006
    Posts
    91


    Did you find this post helpful? Yes | No

    Default Little more help

    I am stuck again.

    I multiply by 1000 to shift where the decimal will be.
    The problem is my first calculation is
    1022*1000 comes out 38960 instead of 1022000 How do I keep my whole number?


    I tried this as well
    dummy VAR WORD
    adval0=1019
    dummy = adval0*1000*3
    adval0 = div32 1023


    Gives me 111... Should be giving me 2988 or something...
    Last edited by geckogrotto; - 1st February 2007 at 17:03. Reason: Div32

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Try this...
    Code:
        adval0 = adval0*3000
        adval0 = div32 1023
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    Join Date
    Aug 2006
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    Thank you much Mister_e!

  9. #9
    Join Date
    Aug 2006
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    Stuck again. I'm just not understanding this math

    adval0 = 2904
    result = adval0 / 15100
    remainder = adval0 // 15100
    result = 0
    remainder is 2904
    How do I get the acutal number it should be 0.192xxx?


    I'm looking at dig but not sure if thats the right thing to use because it will not always be 2 digits...

    adval 0 = 2904*10
    result = adval/151
    192
    then use dig to move the places... and do something like
    if > 9999
    if > 999
    to figure out where the . should be... Is this the best way to do it?
    Last edited by geckogrotto; - 2nd February 2007 at 00:20.

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by geckogrotto View Post
    Stuck again. I'm just not understanding this math

    adval0 = 2904
    result = adval0 / 15100
    remainder = adval0 // 15100
    result = 0
    remainder is 2904
    How do I get the acutal number it should be 0.192xxx?
    You aren't going to get .192xxx.
    PBP only deals with integer math, as stated in the PBP manual.
    You'll have to write your own routines to get anything past the decimal point.

  11. #11
    Join Date
    Aug 2006
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    Ok how do I do that. I had edited my post while you were posting.

  12. #12
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by geckogrotto View Post
    Ok how do I do that. I had edited my post while you were posting.
    I see the edit, and you're on the right track, pretty much the only track.

  13. #13
    Join Date
    Aug 2006
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    Ok thanks,
    I just didn't want to go down that road if there was a better way,

  14. #14
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by geckogrotto View Post
    Ok thanks,
    I just didn't want to go down that road if there was a better way,
    Well, you could use the floating point math routines on melabs website, microchip's site, etc.etc. or write your own.

    The trick to doing it the way you've got set up above is to make sure that no individual variables overflow or underflow, no matter what values end up going into them. It just takes a bit of thinking thru it all...

  15. #15
    Join Date
    Aug 2006
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Well, you could use the floating point math routines on melabs website, microchip's site, etc.etc. or write your own.

    The trick to doing it the way you've got set up above is to make sure that no individual variables overflow or underflow, no matter what values end up going into them. It just takes a bit of thinking thru it all...


    I would like to not use those as I want to learn how to do this, but I sure could use some help i'm a bit lost here. Maybe just working on it too long.

    The number I expect is 1-3 digits. Example

    If the number is 123 it would be 1.23
    If its 12 .12
    If its just 1 then would be .01

    I just can't seem to wrap my mind around how to actually accomplish this.

  16. #16
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by geckogrotto View Post
    I would like to not use those as I want to learn how to do this, but I sure could use some help i'm a bit lost here. Maybe just working on it too long.

    The number I expect is 1-3 digits. Example

    If the number is 123 it would be 1.23
    If its 12 .12
    If its just 1 then would be .01

    I just can't seem to wrap my mind around how to actually accomplish this.

    In post #9 and post #15, you've practically got the answer for yourself.
    Cook on it for a bit, roll it around in your brain, see what you come up with.

  17. #17
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    You should have a look at DEC and DIG modifier and keep your maths as is.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  18. #18
    Join Date
    Aug 2006
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    Ok guys thanks for the input I think im going to call it a night and take a fresh look at it again tomorrow.

    Hopefully it will all fall into place

  19. #19
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Try this;
    Code:
    X VAR WORD
    Y VAR WORD
    
    AD_10BIT:
        FOR X = 0 TO 1023  ' Simulate 0 TO 5V
        Y = X */ 1251      ' For 10-bit A/D (5V/1023)*256 = 1.251
        SEROUT GPIO.1,N2400, ["X = ",DEC X," : = ",DEC Y DIG 3,".",DEC3 Y,"V",13,10]
        NEXT
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  20. #20
    Join Date
    Aug 2006
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    This is what I came up with.
    Code:
    adval0 = 4828
    adval0 =adval0/151
    'should give me 31 in this example.
    
    if adval0 > 99 then
                   'if adval was say 131 result would be 1. and remainder should calculate to 3*10+1 being 31. total of 1.31
    
    result= adval0 dig 2
    remainder= adval0 dig 1*10
    remainder= remainder + adval0 dig 0
    else
    if adval0 < 99 then
    result= 0
    remainder= adval0
    endif
    endif

    Look about right?

Similar Threads

  1. high voltage high frequency H-Bridge
    By George in forum Off Topic
    Replies: 6
    Last Post: - 27th April 2009, 11:50
  2. Expanded Scale Voltmeter
    By Pic_User in forum Schematics
    Replies: 6
    Last Post: - 8th February 2008, 20:32
  3. make a low voltage output from a PIC pin
    By emptyset in forum General
    Replies: 1
    Last Post: - 8th February 2008, 19:20
  4. Help with final project
    By OvERKiLL in forum General
    Replies: 4
    Last Post: - 15th December 2006, 20:35
  5. Low Voltage Detection
    By Srigopal007 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th January 2005, 18:29

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