Need help with LM34 and 12F675


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    To start with, your PIC does not have a ADCON1
    It works more like this.
    http://www.picbasic.co.uk/forum/showthread.php?t=11947
    Dave
    Always wear safety glasses while programming.

  2. #2
    Join Date
    Nov 2009
    Location
    Wagoner, USA
    Posts
    52


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    To start with, your PIC does not have a ADCON1
    It works more like this.
    http://www.picbasic.co.uk/forum/showthread.php?t=11947
    Still not sure I understand. My brain hurts at this point.
    Ok Should be ADCON0=%00000101?
    I may need to sleep on this and try again tomorrow.
    The data sheet has me confused.

  3. #3
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Cancelled

    Al.
    All progress began with an idea

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Maybe this will get you going with the ADC on this chip. Seems like MicroChip likes to keep folks guessing by changing some things around for different chips.

    This will display the 8 bit ADC reading on a terminal.
    Code:
    <font color="#000080"><i>'12F675 ADC TEST FOR CHANNEL 0
        </i></font><font color="#FF0000"><b>DEFINE </b></font><font color="#0000FF"><b>OSC </b></font><font color="#800000"><b>4
       </b></font>@ <font color="#0000FF"><b>__config _INTRC_OSC_NOCLKOUT </b></font>&amp; <font color="#0000FF"><b>_WDT_ON </b></font>&amp; <font color="#0000FF"><b>_MCLRE_OFF </b></font>&amp; <font color="#0000FF"><b>_CP_OFF
        ANSEL</b></font>=<font color="#800000"><b>%00000001 </b></font>:<font color="#0000FF"><b>CMCON</b></font>=<font color="#800000"><b>7 </b></font>:<font color="#0000FF"><b>TRISIO </b></font>= <font color="#800000"><b>%001001
        </b></font><font color="#0000FF"><b>RESULT   </b></font><font color="#FF0000"><b>VAR BYTE</b></font>	<font color="#000080"><i>'A/D CONVERSION RESULT STORAGE BYTE
    
        </i></font><font color="#0000FF"><b>START</b></font>:  <font color="#000080"><i>'A HEART BEAT
        </i></font><font color="#0000FF"><b>GPIO </b></font>= <font color="#800000"><b>%010000</b></font>:<font color="#FF0000"><b>PAUSE </b></font><font color="#800000"><b>500</b></font>:<font color="#0000FF"><b>GPIO </b></font>= <font color="#800000"><b>%000000</b></font>:<font color="#FF0000"><b>PAUSE </b></font><font color="#800000"><b>500
        </b></font><font color="#FF0000"><b>SEROUT2 </b></font><font color="#0000FF"><b>GPIO</b></font>.<font color="#800000"><b>5</b></font>, <font color="#800000"><b>16780</b></font>, [<b><i>&quot;RESULT &quot;</i></b>,<font color="#FF0000"><b>DEC </b></font><font color="#0000FF"><b>RESULT</b></font>,<font color="#800000"><b>$a</b></font>,<font color="#800000"><b>$d</b></font>]
        <font color="#FF0000"><b>PAUSE </b></font><font color="#800000"><b>50
        </b></font><font color="#FF0000"><b>GOSUB </b></font><font color="#0000FF"><b>ADC_0      </b></font><font color="#000080"><i>'STARTS THE READING OF ADC CHANNEL 0
        </i></font><font color="#FF0000"><b>GOTO </b></font><font color="#0000FF"><b>START
        
        ADC_0</b></font>:       <font color="#000080"><i>'READ AN0
        </i></font><font color="#0000FF"><b>ADCON0 </b></font>= <font color="#800000"><b>%00000001      </b></font><font color="#000080"><i>'TURNS ADC ON
        </i></font><font color="#FF0000"><b>GOSUB   </b></font><font color="#0000FF"><b>READ_AD
        RESULT </b></font>= <font color="#0000FF"><b>ADRESH    </b></font><font color="#000080"><i>'PLACES THE ADC VALUE INTO VAR
        </i></font><font color="#FF0000"><b>RETURN
        
        </b></font><font color="#0000FF"><b>READ_AD</b></font>:   <font color="#000080"><i>'DOES THE ADC CONVERSION
        </i></font><font color="#FF0000"><b>PAUSE   </b></font><font color="#800000"><b>50
        </b></font><font color="#0000FF"><b>ADCON0</b></font>.<font color="#800000"><b>1</b></font>=<font color="#800000"><b>1
        </b></font><font color="#FF0000"><b>WHILE </b></font><font color="#0000FF"><b>ADCON0</b></font>.<font color="#800000"><b>1</b></font>=<font color="#800000"><b>1</b></font>:<font color="#FF0000"><b>WEND
        RETURN  
    </b></font>
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Nov 2009
    Location
    Wagoner, USA
    Posts
    52


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    Maybe this will get you going with the ADC on this chip. Seems like MicroChip likes to keep folks guessing by changing some things around for different chips.
    This will display the 8 bit ADC reading on a terminal.
    Thanks I'll try it tomorrow. I just needed to get away from it for awhile.
    I started over from scratch this morning and finally got it working. Only thing is that it reads exactly half the temperature (input voltage from the LM34). I multiplied it by 2 in the software to fix the problem but I don't know why it does that. Is there a calibration for the A/D?
    Anyway here is my final working code for anyone interested. It could probably be optimized but it ain't broke so I ain't gonna fix it.

    I built this to control a solar heater (see it on my web site). It basically reads the voltage from the LM34 on GPIO.0 10mv per deg F and
    turns on a fan relay on GPIO.1 when the temperature goes above the preset temp (110F) and outputs a PWM signal on GPIO.2 to open a damper door.
    Below the preset low temperature (90F) it turns off the fan and again sends out a PWM signal to close the damper. I still need to tweak pulse widths for the servo. I just set them up to make sure the PWM works.
    It also sends out the current temperature and preset info on GPIO.4 to a PC terminal if needed using the debug command.
    Hope someone else can use the info and thanks to Bruce for making the basics available.

    Code:
    'Modified from file found at Renolds Electronics
    'http://www.rentron.com/PIC12C67XAD.htm
    'LM34 F temperature sensor
    'PIN  NAME     Purpose
    ' 1    VDD      +5v
    ' 2   Output    10mv/degF, +5° to +300°F = 50mv-3000mv
    ' 3    VSS      GND      
    ' 
    'PIC12F675 
    ' PIN  NAME     Purpose
    '  1    VDD      +5v
    '  8    VSS      GND
    '  3   GPIO.4   Serial out for debug
    '  5   GPIO.2   PWM out to servo
    '  6   GPIO.1   Output to NPN transistor sinking relay.
    '  7   GPIO.0   A/D input from center leg of LM34
    '********************************* Fuses ******************************
    @ device pic12F675, intrc_osc_noclkout, wdt_off, pwrt_on, mclr_off, bod_off, protect_off
    '********************************* Setup *******************************
    DEFINE debug_mode 1     '0=True, 1=Inverted serial data
    DEFINE debug_bit 4      'sends serial data out GPIO.4 at N,8,1
    DEFINE debug_baud 2400  'Default baud rate = 2400
    DEFINE osc 4            'We're using a 4 MHz oscillator
    Define OSCCAL_1K 1      'Set OSCCAL for 1K device
    DEFINE ADC_BITS 8       'Set A/D for 8-bit operation
    DEFINE ADC_SAMPLEUS 50  'Set A/D sampling time @ 50 uS
    CMCON=7                 '(%00000111) Turn off analog comparators
    TRISIO=%001001          'Port direction register. GPIO.0 & GPIO.3 =inputs (.3 is always input)
    ADCON0=%00000000        'Left justified,Vref = Vdd,GP.0=ANALOG INPUT
    ANSEL=%01010001         'FOSC=16=101,GPIO.0=analog
    Samples VAR WORD        ' Multiple A/D sample accumulator
    Sample  VAR BYTE        ' Holds number of Samples to take
    Temp    VAR BYTE        ' Temperature storage
    PWMPin  var GPIO.2      'Variable for PWM output
    Relay   var GPIO.1      'Variable for relay output
    'LowTemp var byte
    'HighTemp var byte
    LowTemp con 90          'Turn off relay
    HighTemp con 110        'Turn on relay
    Samples = 0             ' Clear Samples accumulator on power-up
    PAUSE 500               ' Wait .5 second
    '********************************* Main ********************************
    loop:
        FOR sample = 1 TO 20    ' Take 20 Samples
            ADCIN 0, temp       ' Read channel 0 into temp variable
            Samples = Samples + temp ' Accumulate 20 Samples
            PAUSE 250           ' Wait approximately 1/4 seconds per loop
        NEXT sample
            temp = Samples/20*2
            if temp >lowtemp and temp>hightemp then gosub onrelay
            if temp <=lowtemp then gosub offrelay
    DEBUG "Current Temp: ",DEC temp," Low Turn Off: ",dec lowtemp," High Turn On: ",dec hightemp,10,13
               Samples = 0         ' Clear old sample accumulator
        GOTO loop               ' Do it forever
        END
    '******************************* Output **********************************                                                                 
    OnRelay:
            high relay           'Turns on GPIO.1
            PWM PWMPin, 127, 500 '50% duty cycle for 500 cycles on GPIO.2
    return
    OffRelay:
            low relay            'Turns off GPIO.1
            PWM PWMPin, 64, 500 '25% duty cycle for 500 cycles on GPIO.2
    return

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    That is the way the LM34 is when running on 5 volts. Figure the steps in 8 bit and all that.

    If you want a 1 degree resolution set up the VREF and have the reference voltage at 3.3 volts.

    Then in place of multiplying by 2
    you would use
    ADC_VAR * 4 / 3

    At least that works for me

    Glad you got it going.
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Nov 2009
    Location
    Wagoner, USA
    Posts
    52


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    That is the way the LM34 is when running on 5 volts. Figure the steps in 8 bit and all that.
    If you want a 1 degree resolution set up the VREF and have the reference voltage at 3.3 volts.
    Then in place of multiplying by 2
    you would use
    ADC_VAR * 4 / 3
    At least that works for me
    Glad you got it going.
    Thanks for the tip. I'll have to try to find a 3.3v reference tomorrow. I have some 1.25's but that's a little shy.
    The LM34 is doing what it's supposed to. It is putting out .700v at 70 degrees but the pic reads it as .350v.
    Thanks..

Similar Threads

  1. 12f675 sensing temperature...
    By rossfree in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 6th April 2007, 01:11

Members who have read this thread : 0

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