1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Measuring negative voltages with a PIC

Discussion in 'Microcontrollers' started by bigal_scorpio, Jul 3, 2012.

  1. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Al,

    Try this original circuit [ with the 5.6Vz added]

    E.
     
  2. bigal_scorpio

    bigal_scorpio Active Member

    Joined:
    Oct 6, 2007
    Messages:
    1,072
    Likes:
    4
    Location:
    Rotherham South Yorkshire England
    Hi Eric,

    Just hoping your journeys were trouble free and you saw your brother.

    Al
     
  3. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Al,
    Did you try setting up the current sensor with the modification.?

    Eric.
    Sent you a PM
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. bigal_scorpio

    bigal_scorpio Active Member

    Joined:
    Oct 6, 2007
    Messages:
    1,072
    Likes:
    4
    Location:
    Rotherham South Yorkshire England

    Hi Eric,

    Yes mate, that seems to work. But I do have some conversion questions!

    I don't understand the .4v offset thing. I have set the circuit to give 2.48v at 1A but then the LCD shows garbage at 0A, I assume its going less than 0.

    I remade the OPA circuit to do both current measurements on the same OPA and all seems to work ok apart from the above problem anyway and the fact that I have not found a .5A load yet to set up the variable voltage current sense. I used a car sidelight bulb for the 1A which strangely is almost exactly a 1A draw at 5v. A happy coincidence!

    Also I am having a problem with adding! Back to school I think! Seriously though I can't understand how PicBasic uses the DEC function as you will see in my latest thread. Can you simulate this function in Oshonsoft?

    It is a real pain burning and swapping Pics just to find its still wrong. Poor manual for PBP in some aspects.

    Al
     
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    Hi Al,
    The 0.4V offset showed up on the Sim, so if you actually get zero ADC counts at 0mA thats fine, forget the 0.4.

    At 1Amp, at 2.44V, with the ADC count set to 500, multiply the 500 by 2 to give 1000 counts equal to 1000mA

    The rubbish at 0V could be noise or your sums.
    At what voltage above zero amps does the reading become steady.???

    E.
     
  7. bigal_scorpio

    bigal_scorpio Active Member

    Joined:
    Oct 6, 2007
    Messages:
    1,072
    Likes:
    4
    Location:
    Rotherham South Yorkshire England
    Hi Eric,

    The problem may be something different as when I set the circuit to 2.44 then I have to multiply by 4 to get 1000 as the ADC count is only 250ish. But doing that means its fine at 1A but reads 56 for 16mA.

    I must be doing something wrong? But what?

    Al
     
  8. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Al,
    You have an error in yours sums.

    The 2.44Volts with a 5Vref will give 500 counts in the adc value, you multiply this value by 2. So 500 adc count value times 2 =1000. equivalent to 1000mA

    This will enable a mA resolution of 2mA per adc count, which means the next lower current value displayed will be 998 counts * 2= 998 which is 0.998 amps [ its going down in 2mA steps]

    So each count of the ADC value = 2mA, if you have a 16mA actual current this means the adc count should be 8..

    Working the sums back:
    Assume the adc count is 8, then *2 = a value of 16 , which is 16mA or 0.016Amp

    If the count increased by 1, to say 9 counts, then *2 = 18 value = 0.018A

    If the adc count was only 1 say, then *2 = 2 = 2mA or 0.002A.

    If you dont follow, just ask, its no problem.;)
    E


    Dont multiply the count by 4
     
    Last edited: Jul 16, 2012
  9. bigal_scorpio

    bigal_scorpio Active Member

    Joined:
    Oct 6, 2007
    Messages:
    1,072
    Likes:
    4
    Location:
    Rotherham South Yorkshire England
    Hi Eric,

    If I set the OPA to 2.44v out at 1A and then use the ADC on it it shows 500 when multiplied by 2. So do I adjust the OPA to output enough to make the ADC 500 or what else? Really puzzled at this. But thats normal at least. hehehe

    EDIT. Is this line doing the damage?
    Code (text):

    adval = (adval */ 500)>>2        ' Equates to: (adval * 500)/1024
    Al
     
    Last edited: Jul 16, 2012
  10. bigal_scorpio

    bigal_scorpio Active Member

    Joined:
    Oct 6, 2007
    Messages:
    1,072
    Likes:
    4
    Location:
    Rotherham South Yorkshire England
    Hi Eric,

    In truth I don't know why that line is in at all, since I dont understand what it does. Can you explain it?

    I picked it up from the PBP examples I think and thought I needed it as they seemed to use it often.

    Told you I was getting dimmer!

    Al
     
  11. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Al,
    Is adval what you are reading from the PIC's adc .???

    If yes, then adval = adval *2 = 1000

    That means if adval was 500 say, it will be 1000 after being multiplied by 2, which is 1000mA.
    Do you have a command in ProBasic, to send the adval to the display.???

    In Oshonsoft I would use LCDOUT = #adval, and that would display 1000

    I dont use ProBasic, I will look for free download of their ProBasic commands



    E
     
    Last edited: Jul 16, 2012
  12. bigal_scorpio

    bigal_scorpio Active Member

    Joined:
    Oct 6, 2007
    Messages:
    1,072
    Likes:
    4
    Location:
    Rotherham South Yorkshire England
    Hi Eric,

    Yes the ADC value is read into adval.

    I have just removed the offending line and now things seem better. Thats why all along I have had half ( or roughly) of the numbers I should have. So what is the line for?

    I still have a problem with the DEC command I think. My readings fluctuate in value by tens without changing unit values so I must be getting the DEC command wrong somehow.

    Al
     
    Last edited: Jul 16, 2012
  13. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    Have you tried LCDOUT #adval without the DEC.

    The way I read the manual is that DEC is used to Format the display data. According to the manual the LCDOUT #adval will display the actual count value.

    That should give you a direct reading of the milli-amps 0 thru 1000mA.
    If that is steady it will prove that the ADC and display is working OK and the jumping problem is caused by the way you are using the DEC statement.

    I am trying to get a PICBasic tutorial download.

    E.

    EDIT:

    Look at the LCD section of this tutorial
    http://melabs.com/resources/pbpmanual/5_32-5_35.htm
     
    Last edited: Jul 16, 2012
  14. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    Morning Al,
    I have converted your posted PBP program to Oshonsoft Basic so that I can follow what you are doing.

    This is what I have so far, no formatting at the moment.
    Will post post more as I progress.

    E.

    EDIT:
    Deleted , incorrect wiring on my part !! duh!!

    mainloop:
    Gosub getposval 'Get + value
    Gosub getnegval 'Get - value
    Gosub getfive 'Get 5v rail amps
    Gosub getvaramp 'Get variable amps

    Case 2
    Goto amps


    amps:
    If five > 1023 Then five = 0
    If five < 0 Then five = 0
    Lcdcmdout LcdLine1Home
    Lcdout "+5 Rail = ", #five ''dec(five * 4 - 40), "mA" 'Display the decimal value
    Lcdcmdout LcdLine2Home
    Lcdout "Var Rail = ", #five ''dec(varamp * 4 - 40), "mA" 'Display the decimal value
    WaitMs 200 ''pause 200
    Goto mainloop
     
    Last edited: Jul 17, 2012
  15. bigal_scorpio

    bigal_scorpio Active Member

    Joined:
    Oct 6, 2007
    Messages:
    1,072
    Likes:
    4
    Location:
    Rotherham South Yorkshire England
    Hi Eric,

    I have sorted the problem with the displays jumping in tens by using dec3 instead of dec2 after the decimal point.

    Now even the maths add up. Though I have done this I still do not understand what the dec function does! Nowhere does it explain what the number ater dec does. Now I have the correct readings but I also have 3 digits after the decimal point instead of 2 and the 3rd one is such a small value that it fluctuates all the time.

    I didn't need the 3 places but it seems that I can't have the correct results without it!

    Also at the moment I have got a problem with reading the var rail. It has no volts coming out of the OPA ciruit and I don't know why! I have looked at it half a dozen times now and see nothing amiss. May be better to start again with the OPA circuit board? Do OPAs fail on one side very often?

    Al
     
  16. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    Hi Al,
    Chan 4 is not set as ADC its Digital!.

    Look at this image.

    E.
     
  17. bigal_scorpio

    bigal_scorpio Active Member

    Joined:
    Oct 6, 2007
    Messages:
    1,072
    Likes:
    4
    Location:
    Rotherham South Yorkshire England
    Hi Eric,

    I followed the datasheet for the 872 and it does say that the first 4 "As are 0,1,2,3 channels then the last AD channel is "A5 missing out RA4.

    I think I got the hex numbers right s0 ADC1 = $41, 1 = $49, 2 = $51, 3 = $59 and 4 = $61 though I wasn't sure if the last one should be $69 even though that would be RA5 I understood it that they didn't count RA4 at all?

    How do you read it?

    BTW I have got the thermal sensor wired as you showed with the linearisation and the output is definately better but my reading fluctuates wildly. It is roughly right sometimes maybe showing 22 when it should be 21 but flicks from 22 to 16 to 4 etc, the only way of actually telling is that the 22 is more prevellant.

    Al
     
    Last edited: Jul 17, 2012
  18. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Al,
    Look at this modification to the program

    Dim temp As Word
    TRISC = %00001111
    TRISA = %00111111

    ADCON1 = %10000000 'Set PortA to A/D inputs


    I am trying to get the ADC's working just to display the actual ADC count, it seems to be working OK.

    How far have you got.?

    E.

    EDIT:
    Image of Therm, no formatting, yet
     
    Last edited: Jul 17, 2012
  19. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,180
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE

    hi Al,
    These are the ADC addresses [ dont mult the adcval * 2 in the getad subr]

    I am assuming that:
    posval is 4.88V at 15V out
    negval is 4.88V at -15V out

    getfive is 2.44V at 1Amp out
    getvaramp is 2.44V at 1Amp out

    Dont know your temperature volts or range.??

    Can you confirm.?

    I will explain the 'DEC' formatting when we sort out the ADC's
    E.

    Code (text):

    'Subroutine to get pot x value
    getposval:
    ADCON0 = 41h  'Set A/D to Fosc/8, AN0, On
    Gosub getad
    posval = adval  
    Return                                            

    'Subroutine to get pot negval value
    getnegval:
    ADCON0 = 49h  'Set A/D to Fosc/8, AN1, On
    Gosub getad
    negval = adval  
    Return                                            

    'Subroutine to get 5v current value
    getfive:
    ADCON0 = 51h  'Set A/D to Fosc/8, AN2, On
    Gosub getad
    five = adval
    Return                                            

    'Subroutine to get variable amps value
    getvaramp:
    ADCON0 = 59h  ''61h  'Set A/D to Fosc/8, AN3, On
    Gosub getad
    varamp = adval
    Return                                            

    'Subroutine to get temperature value
    gettherm:
    ADCON0 = 61h  'Set A/D to Fosc/8, AN4, On
    Gosub getad
    therm = adval
    Return                                            

    getad:
    WaitUs 50  ''pauseus 50                      ' Wait for A/D channel acquisition time
    ADCON0.2 = 1  'Start conversion
    While ADCON0.2
    Wend  'Wait for it to complete
    adval.HB = ADRESH  'Write conversion result
    adval.LB = ADRESL
    '''adval = adval * 2

    WaitMs 2  ''pause 2
    Return  
     
  20. bigal_scorpio

    bigal_scorpio Active Member

    Joined:
    Oct 6, 2007
    Messages:
    1,072
    Likes:
    4
    Location:
    Rotherham South Yorkshire England
    Hi Eric,

    Yes all your assumptions are correct except that the varamp would be 2.44 at 500mA since the PSU is only rated at 500mA for that rail. But it is a moot point at the moment since I can't get the varamp side of the OPA to do anything at the moment.

    My temp sensor is set up as per the diagram you posted with +5v to sensor pos and a 10k from sensor neg to gnd. there is a 56k resistor in parallel with the sensor and the output if from the junction of the 10k and sensor neg.

    With that setup I get an ADC of 354 at 0c and 933 at 100c.

    Now the maths, I took 354 from 933 to give a 100 degree scale of 579 so 1 degree is 579/100 = 5.79?

    Then I take the ADC and subtract 354 and multiply the remainder by 5.79 well 579 and divide by 1000 to get result?

    Am I on the right track?

    Al
     
  21. bigal_scorpio

    bigal_scorpio Active Member

    Joined:
    Oct 6, 2007
    Messages:
    1,072
    Likes:
    4
    Location:
    Rotherham South Yorkshire England
    Hang on I got confused. I meant get ADC value and subtract 354 then divide by 5.79, I think!

    But it still gives weird results, certainly not the results I get by using the calculator.

    Al
     

Share This Page