Menu

Working on an lm 35 temp sensor

Judah Ben
2019-11-19
2019-12-06
1 2 3 > >> (Page 1 of 3)
  • Judah Ben

    Judah Ben - 2019-11-19

    Hello! I need urgent help.
    I'm new here. I just began working on a temperature controlled system using Pic 16f877a and an lm 35 sensor.
    I'm simulating the results of it in Proteus. I have been able to get the pic to display the startup text only.
    I get stuck at writing the code to make the pic keep displaying the current temperature until it detects a change.
    I'm also stuck at doing the right Adc conversions for the sensor.
    I've gone through all the related threads here. I haven't obtained any help yet.
    I'd really appreciate a quick response.
    Thanks.
    Judah.

     
  • Anobium

    Anobium - 2019-11-19

    Hello!

    We need to see how far you have got.

    Post your program as an attachment.

     
  • Judah Ben

    Judah Ben - 2019-11-19

    Alright, Ill do just that

     
  • Judah Ben

    Judah Ben - 2019-11-19

    I attached the gcbasic ide and code and the proteus simulation snapshots

     
  • Judah Ben

    Judah Ben - 2019-11-19

    I included the ds header file cos from what i saw in the gcbasic documentation, i thought it contained temperature settings and might work on the lm 35 sensor also.
    Uhmm...also, this is the first part of the project. I intend to use switches(push buttons) later on to be able to set reference temperature and save it to maybe EEPROM (I guess).

     
  • Anobium

    Anobium - 2019-11-19

    These are pictures.... always good to attach the source GCB file.

     
  • Judah Ben

    Judah Ben - 2019-11-19

    Alright, let me try that

     
  • Judah Ben

    Judah Ben - 2019-11-19

    I attache the text file, assembly file, and hex file.

     
  • Anobium

    Anobium - 2019-11-19

    OK. Rather than have me fix the code - I am going to use the experiencial method. I hope this works for you.

    Have a look at http://gcbasic.sourceforge.net/help/__option_explicit.html

    Then, when you have resolve the three errors you get, and, you understand why the errors are happening, and, you have the correct type of variables... I want you to consinder using scale, see Scale in the Help rather than using your existing code temp=ReadAD10(AN4)*5000/1024

    PS A good post regarding your project. The GCB enabled me to review your work, the ASM let me check you source code and the HEX is not required. :-)

     

    Last edit: Anobium 2019-11-19
  • Judah Ben

    Judah Ben - 2019-11-19

    Alright....thanks a lot.
    Pls if I'm not able to understand or make good use of the references, I might have to return here for help.

     
  • Anobium

    Anobium - 2019-11-19

    Of course you can, and you should. Lots of folks here will help.

     
  • Judah Ben

    Judah Ben - 2019-11-19

    Uhmm....I have gone through both links but I could not understand them. I think it might help me out to fix the code and I'll fiddle with it a lot after implementing to really understand what it actually does.

     
  • Anobium

    Anobium - 2019-11-19

    OK.

    You have not define the variables.

    So, add #option explicit this will force you to define the variables.

    Your ReadAD10 returns a WORD, s, temp should be a WORD
    but, I think this will work better as this will read 0-1023 range and set temp1 to 0 to 499

    dim temp1 as word
    temp1=scale(ReadAD10(AN4),0,1023,0,499)
    
     
  • Judah Ben

    Judah Ben - 2019-11-19

    Okay let me do this....thanks

     
  • Judah Ben

    Judah Ben - 2019-11-19

    I added it to the code. The value displayed on the lcd began changing as the temperature was changing in accordance with the lm35 even though it was lower by one degree. Also, I havent been able to make the pic display the startup text only on startup while looping the rest of the code.
    I have attached the gcb file and hex.
    thanks a lot

     
  • David Stephenson

    Couple of observations in your code TEMP will always equal TEMP so I'm surprised that you are getting output to the screen.
    Second the output of the LM35 gives out an absolute voltage (independent of the supply) so the reading from the ADC will depend on the voltage you are supplying to the circuit (and how stable it is). Using a voltage reference (Vref+) for the ADC may be a better way to go.

     
    • Judah Ben

      Judah Ben - 2019-11-19

      Thanks a lot. I tried using "when temp=!temp at first to detect when the temperature changes to then obtain anothe reading but it didnt work out. Then I tried doing the reverse. Still didnt work.
      Thanks a lot. I'm using a 5 volts supply for the sensor. But I have to ask, how do I do the referencing. I came across sth like that while searching but haven't understood it yet, same way I'm still yet to understand about ADC conversions.

       
  • Anobium

    Anobium - 2019-11-19

    Many little errors.

    As David said, but, here is working code.
    * I renamed your variables. They are now meaningful.
    * The if-endif is meaningful now
    * The test is now valid. But, note - you have to initialise tempold with a value that cause the test to work first time.
    * The print has a CLS screen. (remove it and discover what happens).
    * Note sure what #include <ds18b20.h> is intended to do.

    Enjoy

    #chip 16f877a, 4
    #include <ds18b20.h>
    #option explicit
    
    #config osc=RC
    #config WDT = OFF
    #define sensor PORTA.5
    
    ;lcd connection settings
    #define LCD_Speed medium
    #define LCD_IO 4
    #define LCD_NO_RW
    
    #define LCD_RS PORTD.2
    #define LCD_Enable PORTD.3
    #define LCD_DB4 PORTD.4
    #define LCD_DB5 PORTD.5
    #define LCD_DB6 PORTD.6
    #define LCD_DB7 PORTD.7
    
    dir sensor in
    dim tempnow as Word
    dim ccount as Word
    dim tempold as word
    
    ;variables used
    ccount=0
    print "System On."
    wait 3 s
    cls
    Print "Obtaining temp."
    wait 2 s
    locate 1,0
    print "Please wait..."
    wait 2 s
    cls
    
    'init tempold so it not equalt tempnow, if can never read 65535
    tempold = 0xffff
    
    main:
    do forever
    
        tempnow=scale(ReadAD10(AN4),0,1023,0,499)
    
        if tempold <> tempnow then
            cls
            print tempnow
            print "degree C"
            tempold=tempnow
            ccount++
        end if
    
        wait 10 ms
    
    loop
    
     
    • Judah Ben

      Judah Ben - 2019-11-19

      Thank you.
      The code worked exactly as said.
      When I took out the cls, it simply kept writing each new temperature on the screen continously. It has become clear its use.
      I'd appreciate if you could help me explain the line "tempold=0xffff" and the comment before it.

       
      • Judah Ben

        Judah Ben - 2019-11-19

        I included the ds18b20 file cos I thought it would contain some needed stuff. It doesnt, for this project I see. I'd take it out.

         
      • Anobium

        Anobium - 2019-11-19

        Setting tempold to a very big number ensures the if -then test works on the first run through. If tempold was equal to zero and the returned value from the readadc was also 0 then the if-then would fail and your LCD would not get written to. The big number ensures the first read updates the LCD.

        My comment.. oh I left it in.

         
        • Judah Ben

          Judah Ben - 2019-11-19

          I appreciate the help so far. I'm now proceeding to add a menu to set a reference temperature and save that temperature using push buttons for menu control.

           
  • Judah Ben

    Judah Ben - 2019-11-20

    Hello!
    After looking for ways I could put in a menu to the system, I saw that using interrupts was the most viable way, if not the only way.
    Here's what I have in mind for the project I'm on.
    I intend for the lcd to continue diplaying the regular temperature until whenever the "ok" switch is pressed. It should then enter a menu where it shows the current reference temperature (as last saved in the EEPROM) and gives you space to either increase or decrease this temperature by pressing either the down or the up button (using subroutines). After adjusting this temperature to what you want, you could then save it (still with the ok button but under a new subroutine called "save"). It then returns to the normal display where it shows the regular temperature.

    Firstly, I'm not sure if this thread should fall under interrupts but I figured since it was the same project, and for ease, I should drop it here.
    Also, I've gone through some of the other threads on interrupts and I must say that they are really getting me confused as the documentation just has very few things to say about interrupts.

    I've seen how people post images of their code here but since I'm not sure yet how to do that and my code refused to compile, I might just have to attach the gcb file and asm, if I find it.
    Many thanks.

     
    • Judah Ben

      Judah Ben - 2019-11-20

      I thought to attach the circuit also

       
  • Anobium

    Anobium - 2019-11-20

    Had a scan - but, I am very confused.

    set IOCB.0 save
    set IOCB.1 down
    set IOCB.2 up
    

    Where save, down and up are subroutines - these therefore are not values that will set the bits you are setting, and, you have the same defined as CONSTANTS that map to Ports.

    I think to write out your overall approach may be of benefit.

     
1 2 3 > >> (Page 1 of 3)

Log in to post a comment.