12F675 and LM35 coding issue


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Mar 2010
    Posts
    52

    Default 12F675 and LM35 coding issue

    Hi All,

    I'm a bit of a n00b when it comes to the PIC world, :-).

    I have developed previously in C, Cobol, VB6 (and .net) and C++, however I have decided to use PIC Basic for my embedded projects. I started with the simple blink (with and without button input) with a few LEDs hanging off the various ports, then worked up to rotating beacons (using PWM).

    My next mini learning project is to connect an LM35 to a 12F675. Along with the LM35 im connecting 3 LEDs, one white, one blue and one red simply to indicate a range of temps per color (i.e. if Temp < 30 then light the white LED etc) - Something I have got working through my Arduino and want to move to a more cost effective platform, :-).

    I have spent a few evenings mulling over it and changing the code, flashing and testing with not much success. I think my issues are stemming from my lack of understanding of the various registers used i.e. ANSEL, TRISIO etc, although I think I have read the datasheet correctly its still not giving me the desired result.

    Below is the code I have so far, please could you look and advise where I am going wrong?

    Code:
    'Using PIC 12F675 with White LED connected to Pin 7, Blue LED on Pin 6, 
    'Red LED on pin 7. Vdd is supplied as a regulated 5v to pin 1, Ground to pin 8.
    'LM 35 connected to Vdd and Ground + Centre pin connected to Pin 3 (AN3)
    'Pin 4 disconnected - Have tried connecting to Vdd with 10K res and turning MCLR_on with no sucess 
                                                   
    @ device pic12F675,INTRC_OSC,wdt_off,pwrt_on,protect_off,MCLR_off
    DEFINE CHAR_PACING 1000
    DEFINE OSC 4                                      'Set internal osc. To 4MHz
    
    '---------Define ADCin Parameters   
    DEFINE ADC_BITS 10 'set number of bits in result
    DEFINE ADC_CLOCK 3 'set clock source (3=rc)
    DEFINE ADC_SAMPLEUS 50 'Set sampling time in uS
    
    '---------Define Vairables         
    samples VAR WORD        ' Multiple A/D sample accumulator
    sample  VAR BYTE        ' Holds number of samples to take
    temp    VAR BYTE        ' Temperature storage
               ' clear Samples
    
    LED0	Con	0	' GPIO.0 = White LED
    LED1    CON 1   ' GPIO.1 = Blue LED              
    LED2    CON 2   ' GPIO.2 = Red LED   
                   
    '--------Configuring I/O PORTS
    TRISIO = %010000   'set GPIO.0, GPIO.1, GPIO.2 as output (LEDs) & GPIO.4 as input
    ANSEL = %00111000 ' Set ADC clock = Frc, GPIO.4, 1 = A/D 
    ADCON0 = %10000000 ' Right justify, use Vdd as Vref 
    CMCON = 7
    
    '---------Main Program 
    'Flash White LED on Start
    HIGH LED0
    Pause 500
    LOW LED0       
                  
    TEMP_READ:                                      'Subroutine to measure temp
                temp = 0                            'Clear temp register
                samples = 0                         'Clear samples register 
                FOR sample = 1 TO 20                'Take 20 samples
                    ADCIN 3, temp                   ' Read AN3 into temp variable
                    samples = samples + temp      
                    PAUSE 250                       ' Wait 1/4 seconds per reading
                NEXT sample                         
                    temp = samples/20               'Average over 20 samples (Every 5 seconds)
                    temp=(temp* 10)/2               
                                                     
    if TEMP <20 then      'Less than 20deg - White LED
        HIGH LED0
        LOW LED1
        LOW LED2
        GOTO             TEMP_READ              
    ENDIF
    
    IF TEMP >20 AND temp <30 then       'Between 20deg and 30deg - Blue
        LOW LED0
        HIGH LED1
        LOW LED2
        GOTO             TEMP_READ              
    ENDIF
    
    IF TEMP >30 then                    'Greater than 30deg - Red
        HIGH LED2
        LOW LED1
        LOW LED0
        GOTO             TEMP_READ              
    endif
    
    END

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,605


    Did you find this post helpful? Yes | No

    Default

    Hi,
    You're not saying what the problem is, just that it doesn't do what you expect. As it looks quite alright to me I'm guessing you have problem with actually getting a reading from the ADC.

    ADCON0.0 turns on/off the ADC and you have it set to '0' = OFF and I'm not sure if the ADCIN command handles that bit automatically or not. Try setting it to '1' and see what happens.

    If your problem isn't related to the actual ADC then please elaborate a bit on what the problem is.

    /Henrik.

  3. #3
    Join Date
    Mar 2010
    Posts
    52


    Did you find this post helpful? Yes | No

    Default

    Sorry, :-) you are right. Basically, its not doing anything currently. I have however checked the Data sheet and you are right, so I have changed the ADCON0 as follows: ADCON0 = %10001101, this should give me

    Bit - Setting
    0 - 1 = A/D Operating
    1 - A/D conversion Bit (I assume ADCIN would set this to 1 when running)
    2-3 - 11 = Channel 3 (AN3 / GPIO4)
    4-5 - Not Used
    6 - Vref, 0 = Vdd
    7 - 1= Right Justify

    However, even after this change and running it in the simulator (I don't have my breadboard etc here at work) it still does not even light up the <20 LED, :-(

    Kind Regards
    Rob
    Last edited by Bobbo_ZA; - 5th March 2010 at 11:08. Reason: Layout change

  4. #4
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Try this...

    Code:
    'Using PIC 12F675 with White LED connected to Pin 7, Blue LED on Pin 6, 
    'Red LED on pin 7. Vdd is supplied as a regulated 5v to pin 1, Ground to pin 8.
    'LM 35 connected to Vdd and Ground + Centre pin connected to Pin 3 (AN3)
    'Pin 4 disconnected - Have tried connecting to Vdd with 10K res and turning MCLR_on with no sucess 
                                                   
    @ device pic12F675,INTRC_OSC,wdt_off,pwrt_on,protect_off,MCLR_off
    DEFINE CHAR_PACING 1000
    DEFINE OSC 4                                      'Set internal osc. To 4MHz
    
    '---------Define ADCin Parameters   
    DEFINE ADC_BITS 10 'set number of bits in result
    DEFINE ADC_CLOCK 3 'set clock source (3=rc)
    DEFINE ADC_SAMPLEUS 50 'Set sampling time in uS
    
    '---------Define Vairables         
    samples VAR WORD        ' Multiple A/D sample accumulator
    sample  VAR BYTE        ' Holds number of samples to take
    temp    VAR BYTE        ' Temperature storage
               ' clear Samples
    
    LED0	Con	0	' GPIO.0 = White LED
    LED1    CON 1   ' GPIO.1 = Blue LED              
    LED2    CON 2   ' GPIO.2 = Red LED   
                   
    '--------Configuring I/O PORTS
    TRISIO = %010000   'set GPIO.0, GPIO.1, GPIO.2 as output (LEDs) & GPIO.4 as input
    ANSEL = %00111000 ' Set ADC clock = Frc, GPIO.4, 1 = A/D 
    ADCON0 = %10000000 ' Right justify, use Vdd as Vref 
    CMCON = 7
    
    '---------Main Program 
    'Flash White LED on Start
    HIGH LED0
    Pause 500
    LOW LED0       
                  
    TEMP_READ:                                      'Subroutine to measure temp
                temp = 0                             'Clear temp register
                samples = 0                         'Clear samples register 
                FOR sample = 1 TO 20                'Take 20 samples
                    ADCIN 3, temp                   ' Read AN3 into temp variable
                    samples = samples + temp      
                    PAUSE 250                       ' Wait 1/4 seconds per reading
                NEXT sample                         
                    temp = samples/20               'Average over 20 samples (Every 5 seconds)
                    temp=(temp* 10)/2               
                                                     
    if TEMP <20 then      'Less than 20deg - White LED
        HIGH LED0
        LOW LED1
        LOW LED2
    '    GOTO             TEMP_READ              
    ENDIF
    
    IF TEMP >20 AND temp <30 then       'Between 20deg and 30deg - Blue
        LOW LED0
        HIGH LED1
        LOW LED2
    '    GOTO             TEMP_READ              
    ENDIF
    
    IF TEMP >30 then                    'Greater than 30deg - Red
        HIGH LED2
        LOW LED1
        LOW LED0
     '   GOTO             TEMP_READ              
    endif
    
    goto temp_read
    
    END

  5. #5
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    It loloks to me that you are taking the 10-bit A/D samples and putting them in BYTE variable called TEMP. Changing TEMP to a word should certainly get you one more step.
    Charles Linquist

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink

    Quote Originally Posted by Charles Linquis View Post
    It loloks to me that you are taking the 10-bit A/D samples and putting them in BYTE variable called TEMP. Changing TEMP to a word should certainly get you one more step.
    Hi, Charles

    The point is interesting as the maximum ADC value WITH THIS SENSOR ... will be ~ 256, and always far from the actual use of the sensor.

    so, we could use ADRESH as an open line indicator ....

    Alain
    Last edited by Acetronics2; - 5th March 2010 at 15:10.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink

    Hi, Bobbo

    Your main problem was scaling temps before displaying ...

    try this one :

    Code:
    '****************************************************************
     '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2010 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 05/03/2010                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    '*  Name    : Temp 675'Using PIC 12F675 with White LED connected to Pin 7, Blue LED on Pin 6, 
    'Red LED on pin 7. Vdd is supplied as a regulated 5v to pin 1, Ground to pin 8.
    'LM 35 connected to Vdd and Ground + Centre pin connected to Pin 3 (AN3)
    'Pin 4 disconnected - Have tried connecting to Vdd with 10K res and turning MCLR_on with no sucess 
                                                   
    @ device pic12F675,INTRC_OSC,wdt_on,pwrt_on,protect_off,MCLR_off
    
    'DEFINE CHAR_PACING 1000
    DEFINE OSC 4                                      'Set internal osc. To 4MHz
    
    '---------Define ADCin Parameters   
    DEFINE ADC_BITS 10 'set number of bits in result
    DEFINE ADC_CLOCK 3 'set clock source (3=rc)
    DEFINE ADC_SAMPLEUS 50 'Set sampling time in uS
    
    ANSEL  = %00111000 ' Set ADC clock = Frc, GPIO.4, 1 = A/D 
    ADCON0 = %10001100 ' Right justify, use Vdd as Vref          ' Select channel 3 !!! 
                       
    CMCON = 7
    
    
    '---------Define Vairables         
    samples VAR WORD        ' Multiple A/D sample accumulator
    dummy   var word        ' DIV32 intermediate value
    sample  VAR BYTE        ' Holds number of samples to take
    temp    VAR BYTE        ' Temperature storage  ... might be a WORD !!! ( 0 - 1023 !!! )
    
    CLEAR           ' clear Variables
    
    LED0	Con	0	' GPIO.0 = White LED
    LED1    CON 1   ' GPIO.1 = Blue LED              
    LED2    CON 2   ' GPIO.2 = Red LED   
                   
    '--------Configuring I/O PORTS
    GPIO = 0
    TRISIO = %010000   'set GPIO.0, GPIO.1, GPIO.2 as output (LEDs) & GPIO.4 as input
    
    '---------Main Program 
    'Flash White LED on Start
    
    HIGH LED0
    Pause 500
    LOW LED0       
                  
    While 1
    
     TEMP_READ:                                      'Subroutine to measure temp   input = 10 mv/°C ~250 mv 
    
                temp = 0                            'Clear temp register
                samples = 0                         'Clear samples register
                 
                FOR sample = 1 TO 20                'Take 20 samples
                    ADCIN 3, temp                   ' Read AN3 into temp variable    result is ~ 50  ( .25 / 5 * 1024 )
                    samples = samples + temp      
                    PAUSE 12                       ' Wait 1/4 seconds per display ... (20 x 12 ms )
                NEXT sample
                                         
                    temp = samples/20               'Average over 20 samples (Every 5 seconds)   result is ~ 50
    '                temp=(temp* 10)/2               ' Result is 250 ... may overflow !!! so, the mult is no use !!!
    
    '*******************************************************************************
    ' Trim to scale
    '********************************************************************************
    
      Dummy = 500 * temp                            ' 500 = 5000 mv full scale  / 10 mv per degree
      temp = DIV32 1024                               ' Number is Deg C Now ...
    
    '********************************************************************************
    
     SELECT CASE temp
    
        CASE is < 20    'Less than 20deg - White LED                                             
      
        HIGH LED0
        LOW LED1
        LOW LED2
     
    
        CASE is < 31        'Between 20deg and 30deg - Blue
        
        LOW LED0
        HIGH LED1
        LOW LED2
      
    
        CASE is < 45         'Greater than 30deg - but less than 45 deg Red
        
        HIGH LED2
        LOW LED1
        LOW LED0
        
        CASE Else              ' Too cold !!! or dangerously hot ... let's blink everyting !!!
        
        HIGH LED0
        HIGH LED1
        HIGH LED2
        
        PAUSE 250
        
        LOW LED0
        LOW LED1
        LOW LED2
        
     END SELECT
     
    wend
    
    END
    Interesting parts have been explained ...

    now, have a good bath ... you've sweat a lot on this one ... ROFL.
    Last edited by Acetronics2; - 5th March 2010 at 14:13.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Similar Threads

  1. 12F675 cant serout to PC
    By ruijc in forum General
    Replies: 9
    Last Post: - 3rd December 2007, 00: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