News:

Let's find out together what makes a PIC Tick!

Main Menu

Using LM35 Temperature

Started by M3NDex, Today at 03:04 PM

Previous topic - Next topic

M3NDex


I tried to make a temperature control with lm35 and pic877a, but the button and control functions of lm 35 don't work well, considering I'm not very skilled, I hope the members here can help me to find the problem, here is my code below :
'******************************************************************************
'* Name    : LM35_Pro_Controller.BAS                                           *
'* Device  : PIC16F877A                                                        *
'* Crystal : 4 MHz                                                             *
'*******************************************************************************

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ***********************************************
; Use the Fuse Configurator plug-in to change these settings

Device = 16F877A

Config FOSC_HS, WDTE_OFF, PWRTE_OFF, BOREN_ON, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, CP_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
'----------------------Declare Xtal 4 Mhz --------------------------------------
Declare Xtal = 4
'-------------------------------------------------------------------------------

'-------------- LCD Diplay pins ------------------------------------------------
Declare LCD_DTPin PORTB.4
Declare LCD_ENPin PORTB.3
Declare LCD_RSPin PORTB.2
Declare LCD_Interface 4
Declare LCD_Lines 2
Declare LCD_Type = 0 
'--------------------------------------------------------------------------------           

'------------- ADC Config -------------------------------------------------------
Declare Adin_Res 10
Declare Adin_Tad FRC
Declare Adin_Stime 50
'---------------------------------------------------------------------------------
'----------PWM Pin output --------------------------------------------------------
Declare HPWM1_Pin = PORTC.0             ' PWM Chn PORTC.0
Declare HPWM2_Pin = PORTC.1             ' PWM Chn PORTC.1
'---------------------------------------------------------------------------------

'---------- State button pins -----------------------------------------------------
Symbol BTN_UP = PORTD.0
Symbol BTN_SET = PORTD.1
Symbol BTN_DN = PORTD.2
'---------------------------------------------------------------------------------

'---------- PORT_PIN_DIRECTION----------------------------------------------------
TRISA = %00000001    'PORTA.0 Input For LM35
TRISB = %00000000    'All PORTB are output
TRISC = %00000000    'All PORTC are output
TRISD = %00000111    'Make PORT RD0, RD1, RD2 as input
'----------------------------------------------------------------------------------

'----------------------IO Analog---------------------------------------------------
ADCON1 = %10000000   ' $80 it will make the I/O analog and right justify the result
'-----------------------------------------------------------------------------------

'------------------------Declare Variables -----------------------------------------
Dim RawValue As Word
Dim TempC As Float
Dim SetPoint As Byte
Dim DutyCycle As Byte
Dim EditMode As Bit   
Dim lastUp As Byte
'------------------------------------------------------------------------------------
'------------------Print First Message ----------------------------------------------
Print At 1, 1, "AUDIO AMPLIFIER "
   DelayMS 500
EditMode = 0                                           'Enter button
SetPoint = ERead 0                                     'Read EEprom 0
    If SetPoint < 10 Or SetPoint > 80 Then SetPoint = 30 'If no number value then put 30
 
Main:

'----------- Read ADC PORTA Pin 0-----------------------------------------------------
RawValue = ADIn 0
TempC = RawValue * 0.488
'--------------------------------------------------------------------------------------

'------------------Handle Button-------------------------------------------------------   
 
    If BTN_SET = 0 Then                                '0
        DelayMS 50                                     ' Debounce
        EditMode = ~EditMode                           ' Toggle mode
        If EditMode = 0 Then EWrite 0, [SetPoint]      'Save when exiting edit
        While BTN_SET = 0 : Wend                                      'Wait for release
    EndIf

   If EditMode = 1 Then                                'Press button Edit Mode status
       If BTN_UP = 0 Then
           SetPoint =SetPoint +1                        'Inc SetPoint 1
          DelayMS 150                                  'Debounce
       EndIf
       
        If BTN_DN = 0 Then
            SetPoint = SetPoint - 1                    'Dec SetPoint 1
            DelayMS 150
        EndIf
        EndIf
'-----------------------Control Fan For Temperature ------------------------------------
    If TempC < SetPoint Then
        DutyCycle = 0
    ElseIf TempC >= SetPoint And TempC < (SetPoint + 5) Then
        DutyCycle = (TempC - SetPoint) * 51
    Else
        DutyCycle = 255
    EndIf
   
    HPWM 1, DutyCycle, 2000                             'Turn 2 KHz PWM
   
'------------------------LCD Display -----------------------------------------------------
    If EditMode = 1 Then
        Print At 1, 1, "SET TARGET TEMP"
        Print At 2, 1, "Target: ", Dec SetPoint, 223, "C  " 'Symbol degree 223 & C Temperature
    Else
        Print At 1, 1, "Temp: ", Dec1 TempC, 223, "C   "
        Print At 2, 1, "Fan: ", Dec (DutyCycle / 2.55), "% Set:", Dec SetPoint
    EndIf
       DelayMS 100                                      'Debounce
GoTo Main