Hello Everyone,

I can't seem to get my 18f452 to save variables to the eeprom, I've posted this here because I'm thinking this has something to do with the pickit 2 set-up and not the picbasic pro code. I've used these same routines before to save to the eeprom and it has worked good in the past. I have the "Program Memory" and the "EEprom Data" boxes selected for hex only but have tried other settings as well with no success. Can anyone here in the know suggest what settings need to be looked at in the pickit 2 in order to get this code to work properly? Any help will be greatly appreciated.

Thanks
jessey



Code:
'*******************************************************************************
'* Name     : Car_Thermostat.bas                                               *
'* Author   : Jessey Montgomery                                                *
'* Includes : STRINGS.PBP By Darrel Taylor                                     *
'* Notice   : Copyright (c) 2004                                               *
'*          : All Rights Reserved                                              *
'* Date     : 22/10/2005                                                       *
'* Version  : Using PicBasic Pro Ver 2.50, MPASM Ver 5.20, Using MicroCode     *
'*          : Studio 3.0.0.5, PICkit 2 programmer - Application Version        *
'*          : 2.50.02 - Device File Version 1.5100 - OS Firmware Version       *
'*          : 2.30.01. Using a PIC18f452-20/P                                  *
'*******************************************************************************

'                                --------------
'                                |   18F452   |
'               MCLR/Vpp ------> |1         40| <-----> RB7/PGD 
'                RA0/AN0 <-----> |2         39| <-----> RB6/PGC 
'                RA1/AN1 <-----> |3         38| <-----> RB5/PGM 
'          RA2/AN2/Vref- <-----> |4         37| <-----> RB4 
'          RA3/AN3/Vref+ <-----> |5         36| <-----> RB3/CCP2* 
'              RA4/TOCK1 <-----> |6         35| <-----> RB2/INT2 
'       RA5/AN5/SS/LVDIN <-----> |7         34| <-----> RB1/INT1 
'             RE0/RD/AN5 <-----> |8         33| <-----> RB0/INT0
'             RE1/WR/AN6 <-----> |9         32| <-----  Vdd 
'             RE2/CS/AN7 <-----> |10        31| <-----  Vss 
'                    Vdd  -----> |11        30| <-----> RD7/PSP7 
'                    VSS  -----> |12        29| <-----> RD6/PSP6 
'              OSC1/CLK1  -----> |13        28| <-----> RD5/PSP5 
'          OSC2/CLKO/RA6 <------ |14        27| <-----> RD4/PSP4 
'        RC0/T1OSO/TICK1 <-----> |15        26| <-----> RC7/RX/DT 
'        RC1/T1OSI/CCP2* <-----> |16        25| <-----> RC6/TX/CK 
'               RC2/CPP1 <-----> |17        24| <-----> RC5/SDO 
'            RC3/SCK/SCL <-----> |18        23| <-----> RC4/SD1/SDA 
'               RD0/PSP0 <-----> |19        22| <-----> RD3/PSP3  
'               RD1/PSP1 <-----> |20        21| <-----> RD2/PSP2       
'                                --------------
                 '

'                               ADC pins and ports
'                               ==================
'                              AN0  (pin 2)   PORTA.0
'                              AN1  (pin 3)   PORTA.1
'                              AN2  (pin 4)   PORTA.2
'                              AN3  (pin 5)   PORTA.3
'                              AN4  (pin 7)   PORTA.4 
'                              AN5  (pin 8)   PORTE.0
'                              AN6  (pin 9)   PORTE.1
'                              AN7  (pin 10)  PORTE.2 
 
'------------------------------------------------------------------------------'
'-- Clear All The Variables And Each Port Before Setting The TRIS Registers ---'
'------------------------------------------------------------------------------'
 
Clear    'Set all ram registers to zero
PORTA = 0
PORTB = 0                           
PORTC = 0   
PORTD = 0
PORTE = 0
 
'--------------------------- VSS VDD MCLR Etc. Pins ---------------------------'
 
'MCLR        (Pin 1)
'VDD         (Pin 11)
'VSS         (Pin 12)
'OSC/CLKIN   (Pin 13)
'OSC2/CLKOUT (Pin 14)
'VSS         (Pin 31)
'Vdd         (Pin 32)
 
'((OPTION_REG=127 ' ENABLE WEAK PULL UP RESISTORS ON PORTB))
 
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
'XXXXXXXXXXXXXXXXXXXXXXXXXX Set The TRIS In/Out Pins XXXXXXXXXXXXXXXXXXXXXXXXXX'
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX BELOW XXX'
 
'-------------------- PORTA PINS --------------------'
TRISA.0 = 1  '(pin 2) ..........Adjust_Hi_Temp_Push_Button VAR PORTA.0   ' pin 2  
TRISA.1 = 1  '(pin 3) ..........Adjust_Low_Temp_Push_Button VAR PORTA.1  ' pin 3  
TRISA.2 = 1  '(pin 4) ................Decrease_Push_Button VAR PORTA.2   ' pin 4  
TRISA.3 = 1  '(pin 5) .................Increase_Push_Button VAR PORTA.3  ' pin 5   
TRISA.4 = 1  '(pin 6) NOT USED  
TRISA.5 = 1  '(pin 7) NOT USED 
 
'-------------------- PORTB PINS --------------------'
TRISB.0 = 1  '(pin 33) NOT USED VAR PORTB.0 'pin 33  
TRISB.1 = 1  '(pin 34) NOT USED VAR PORTB.1 'pin 34 
TRISB.2 = 1  '(pin 35) NOT USED VAR PORTB.2 'pin 35    
TRISB.3 = 1  '(pin 36) NOT USED VAR PORTB.3 'pin 36  
TRISB.4 = 1  '(pin 37) NOT USED VAR PORTB.4 'pin 37 
TRISB.5 = 1  '(pin 38) NOT USED VAR PORTB.5 'pin 38 
TRISB.6 = 1  '(pin 39) NOT USED VAR PORTB.6 'pin 39 
TRISB.7 = 1  '(pin 40) NOT USED VAR PORTB.7 'pin 40 
 
'-------------------- PORTC PINS --------------------'
TRISC.0 = 1  '(pin 15) ................................DS1820 Temperature Sensor  
TRISC.1 = 1  '(pin 16) NOT USED VAR PORTC.1 'pin 16 
TRISC.2 = 1  '(pin 17) NOT USED VAR PORTC.2 'pin 17  
TRISC.3 = 1  '(pin 18) NOT USED VAR PORTC.3 'pin 18 
TRISC.4 = 1  '(pin 23) NOT USED VAR PORTC.4 'pin 23  
TRISC.5 = 1  '(pin 24) NOT USED VAR PORTC.5 'pin 24 
TRISC.6 = 1  '(pin 25) NOT USED VAR PORTC.6 'pin 25 
TRISC.7 = 1  '(pin 26) NOT USED VAR PORTC.7 'pin 26 
 
'-------------------- PORTD PINS --------------------'
TRISD.0 = 1 '(pin 19) NOT USED VAR PORTD.0 'pin 19 
TRISD.1 = 0 '(pin 20) .............................Buzzer VAR PORTD.1  '(pin 15)  
TRISD.2 = 0 '(pin 21) ...............................The_Fan VAR PORTD.2 'pin 21 
TRISD.3 = 1 '(pin 22) NOT USED VAR PORTD.3 'pin 22  
TRISD.4 = 0 '(pin 27) ...........................Lcd out for Data bit 4                                  
TRISD.5 = 0 '(pin 28) ...........................Lcd out for Data bit 5                                     
TRISD.6 = 0 '(pin 29) ...........................Lcd out for Data bit 6                                     
TRISD.7 = 0 '(pin 30) ...........................Lcd out for Data bit 7 

'-------------------- PORTE PINS --------------------'
TRISE.0 = 0 '(pin 8)  ...........................LCD FOR Register/Select                                     
TRISE.1 = 0 '(pin 9)  ...........................LCD Enable
TRISE.2 = 1 '(pin 10) NOT USED PORTE.2 


'-----------------------------------------------------------------------------'
'--------------------- Lcd Set Up For Parallel 4 Bit Mode --------------------'
'----------------------- Using Melanie's Cool Blue Lcd -----------------------'
'-----------------------------------------------------------------------------'
'  LCD       Wire                                                 
'Pin No.   Color         Symbol    Function          Connections To 18F452
'------   -------        ------    --------          ---------------------
'  A     Red / Black     A        Back Light +       To +5V through 1.5K res
'  K     Black /White    K        Back Light -       To -5V 
'  1     Black           Vss      Negative 5V        To -5V 
'  2     Red             Vdd      Positive 5V        To +5V 
'  3     White / Black   Vo       LCD cont adj.      To -5V through 1.5K res
'  4     Orange/Black    RS       Register select    Connect to pin 8 RE0
'  5     Red / White     R/W      Read / write       Connect to -5V 
'  6     Blue / White    E        Enable             Connect to pin 9 RE1
'  7    (not used)       DB0      Data bit 0                                  
'  8    (not used)       DB1      Data bit 1                          
'  9    (not used)       DB2      Data bit 2                                     
'  10   (not used)       DB3      Data bit 3                                    
'  11    Blue            DB4      Data bit 4         Connect to pin 27 RD4 
'  12    White           DB5      Data bit 5         Connect to pin 28 RD5 
'  13    Green           DB6      Data bit 6         Connect to pin 29 RD6 
'  14    Orange          DB7      Data bit 7         Connect to pin 30 RD7  

;==========================================================================
;@ __CONFIG _CONFIG1H, _OSCS_ON_1H & _XT_OSC_1H ' use this one for 4 mhz and 
                                                ' also declare the osc as 4mhz
@ __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HSPLL_OSC_1H ; use this one for 16 mhz
@ __CONFIG _CONFIG2L, _BOR_OFF_2L & _BORV_20_2L & _PWRT_ON_2L
@ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
@ __CONFIG _CONFIG3H, _CCP2MX_OFF_3H
@ __CONFIG _CONFIG4L, _STVR_OFF_4L & _LVP_OFF_4L & _DEBUG_OFF_4L
@ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L
@ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L
@ __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_ON_6H
@ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
@ __CONFIG _CONFIG7H, _EBTRB_OFF_7H 


 DEFINE OSC 16     ' using HSPLL X 4 gives us OSC 16
@ errorlevel -306 ; turn off the Crossing page boundary warnings 

 Define LCD_DREG   PORTD
 Define LCD_DBIT   4
 Define LCD_RSREG  PORTE
 Define LCD_RSBIT  0
 Define LCD_EREG   PORTE
 Define LCD_EBIT   1
                      
'-----------------------------------------------------------'
'--------------------- Configure Pins ----------------------'
'-----------------------------------------------------------' 

ADCON1 = 7  ' Set to Digital
                                           
'Pushbuttons Variables '
'===================== '
 Adjust_Hi_Temp_Push_Button VAR PORTA.0   ' pin 2  
 Adjust_Low_Temp_Push_Button VAR PORTA.1  ' pin 3  
 Decrease_Push_Button VAR PORTA.2         ' pin 4  
 Increase_Push_Button VAR PORTA.3         ' pin 5  
 
'Port pins Variables '
'=================== ' 
 Buzzer VAR PORTD.1         ' pin 20 
 The_Fan VAR PORTD.2        ' pin 21
 
 ' Allocate variables '
 ' ================== '
 a var byte                 ' Enables if..then statements to execute without
 a = 0                      ' using GOTO's, a always equals 0
 b Var Word
 c Var byte                 ' used to select a print to show to the Lcd 
 D Var Byte                 ' used to speed up var changes using push buttons
 Var_Pause Var Byte         ' used to extend the time in for next loops 

Variable_Pause Var Byte     ' used to speed up var changes using push buttons
Pause_Counter Var Word      ' used to speed up var changes using push buttons 

'used for temperature'
'--------------------'
temp VAR	WORD           ' Temperature storage
count_remain VAR BYTE   ' Count remaining
count_per_c VAR BYTE    ' Count per degree C
Sign  VAR  BIT          ' bit determines id - sign is needed 
DQ VAR PORTC.0          ' One-wire data pin (pin 15) 

'--------------------------------------------------------------
EEPROMData91 var byte              'Define EEPROM Data 
Hi_Temp_Setpoint VAR EEPROMData91  'alias the Hi_Temp_Setpoint
Read 91, EEPROMData91              'Read into Hi_Temp_Setpoint
'To save the var write'
'---------------------'
'EEPROMData91 = Hi_Temp_Setpoint   'save our Hi_Temp_Setpoint adjustment
'Write 91, EEPROMData91 
 
'--------------------------------------------------------------
EEPROMData92 var byte                 'Define EEPROM Data 
Low_Temp_Setpoint VAR EEPROMData92    'alias the Low_Temp_Setpoint
Read 92, EEPROMData92                 'Read into Low_Temp_Setpoint
'To save the var write'
'---------------------'
'EEPROMData92 = Low_Temp_Setpoint     'save our Low_Temp_Setpoint adjustment
'Write 92, EEPROMData92

 Is_On CON 1
 Is_Off CON 0
 Is_Pressed CON 0  
 Is_Not_Pressed CON 1

 LCDOUT  254,64,14,10,14,0,0,0,0,0 ' Custom Char #0 - degree sign 

 Pause 1000' wait for Lcd to initialize

    LCDOut $fe, 1, "Taurus Controller"
  LCDOut $fe, $c0, "By JJ Montgomery" 
  PAUSE 2000

   GOSUB Check_Temperature
   If a = 0 Then mainloop' Skip subroutines


Sound_Buzzer:
            PWM  Buzzer,250,15
RETURN



Save_The_Hi_Temp_SetPoint:
       EEPROMData91 = Hi_Temp_Setpoint ' save our Hi_Temp_Setpoint adjustment
       Write 91, EEPROMData91
Return



Save_The_Low_Temp_SetPoint:
       EEPROMData92 = Low_Temp_Setpoint'save our Low_Temp_Setpoint adjustment
       Write 92, EEPROMData92
Return


Print_Release_Button:
   LCDOut $fe, 1, " Release Button "
 LCDOut $fe, $c0, "   To Return    "
Return

     
Show_Print_To_Lcd:    
  If c = 0 then
       LCDOut $fe, 1, "Adj The Hi Temp "
  Else ' if c = 1     
       LCDOut $fe, 1, "Adj The Low Temp"
  Endif
     LCDOut $fe, $c0, "Release Button  "
Return 


'This speeds up the var change when either of these two push buttons are held down  
_Pause:
     IF Increase_Push_Button = Is_Not_Pressed THEN
      If D = 0 then
        Pause_Counter = 0
      Endif
     Endif
 
     IF Decrease_Push_Button = Is_Not_Pressed THEN
      If D = 1 then
        Pause_Counter = 0
      Endif
     Endif

     If Pause_Counter >= 63000 Then Pause_Counter = 50

     Pause_Counter = Pause_Counter + 1
   
     If Pause_Counter <= 9 Then Variable_Pause = 100
     If Pause_Counter = 10 Then Variable_Pause = 75
     If Pause_Counter = 20 Then Variable_Pause = 50
     If Pause_Counter = 30 Then Variable_Pause = 25
     If Pause_Counter >= 40 Then Variable_Pause = 5

     Pause Variable_Pause
Return



Adjust_Hi_Temp_Set_Point:
  GOSUB Sound_Buzzer : c = 0 : Variable_Pause = 100          
  WHILE Adjust_Hi_Temp_Push_Button = Is_Pressed
     Gosub Show_Print_To_Lcd
     Pause 100
  WEND
  Pause 200 ' Button Debounce 
   Var_Pause = 100 ' using 100, this loop times out in 11 seconds & returns     
  Loop_1:
     Var_Pause = Var_Pause - 1
    'Increase the Hi_Temp_Setpoint'
    '=========================='
     IF Increase_Push_Button = Is_Pressed THEN
       GOSUB Sound_Buzzer' button press indicator 
       Var_Pause = 100 ' allow more time in this loop when button is pressed 
       Hi_Temp_Setpoint = Hi_Temp_Setpoint + 1
       Gosub Save_The_Hi_Temp_SetPoint
       D = 0
     ENDIF 
    
    'Decrease the Hi_Temp_Setpoint'
    '=========================='
     IF Decrease_Push_Button = Is_Pressed THEN
       GOSUB Sound_Buzzer' button press indicator 
       Var_Pause = 100 ' allow more time in this loop when button is pressed  
       Hi_Temp_Setpoint = Hi_Temp_Setpoint - 1
       Gosub Save_The_Hi_Temp_SetPoint
       D = 1
     ENDIF

         'Show the Hi_Temp_Setpoint increment & decrement'
         '================================================='
           LCDOut $fe, 1, DEC temp / 100,0," Hi Turn On "
         LCDOut $fe, $c0, " Fan On @ ", dec Hi_Temp_Setpoint,0,"F "
   
     If Adjust_Hi_Temp_Push_Button = Is_Pressed Then 
       GOSUB Sound_Buzzer
       Var_Pause = 100          
       WHILE Adjust_Hi_Temp_Push_Button = Is_Pressed
          Gosub Print_Release_Button
           Pause 100
       WEND
       Pause 200 ' Button Debounce 
       Return
     Endif

     Gosub _Pause ' pause changes (speeds up) when button is held down 
              
    If Var_Pause <= 1 Then 
      GOSUB Sound_Buzzer
      Return
    Endif
  If a = 0 Then Loop_1
RETURN ' auto return when the Var_Pause = 1





Adjust_Low_Temp_Set_Point:
   GOSUB Sound_Buzzer : c = 1 : Variable_Pause = 100           
   WHILE Adjust_Low_Temp_Push_Button = Is_Pressed
     Gosub Show_Print_To_Lcd
     Pause 100
   WEND
   Var_Pause = 100 ' allow more time in this loop when button is pressed  
   Pause 200 ' Button Debounce    
  Loop_2:
     Var_Pause = Var_Pause - 1 
    'Increase the Low_Temp_Setpoint'
    '=============================='
     IF Increase_Push_Button = Is_Pressed THEN
       GOSUB Sound_Buzzer' button press indicator 
       Var_Pause = 100 ' allow more time in this loop when button is pressed 
       Low_Temp_Setpoint = Low_Temp_Setpoint + 1
       Gosub Save_The_Low_Temp_SetPoint
       D = 0 
     ENDIF 
    
    'Decrease the Low_Temp_Setpoint'
    '=============================='
     IF Decrease_Push_Button = Is_Pressed THEN
       GOSUB Sound_Buzzer' button press indicator
       Var_Pause = 100 ' allow more time in this loop when button is pressed 
       Low_Temp_Setpoint = Low_Temp_Setpoint - 1
       Gosub Save_The_Low_Temp_SetPoint
       D = 1
     ENDIF
                                                     
        'Show the Low_Temp_Setpoint increment & decrement'
        '================================================'
           LCDOut $fe, 1, DEC temp / 100,0," Lo Turn Off" 
         LCDOut $fe, $c0, "Stop Fan @ ", dec Low_Temp_Setpoint,0,"F "
   
     If Adjust_Low_Temp_Push_Button = Is_Pressed Then 
       GOSUB Sound_Buzzer          
       WHILE Adjust_Low_Temp_Push_Button = Is_Pressed
          Gosub Print_Release_Button
          Pause 100
       WEND
       Pause 200 ' Button Debounce 
       Return
     Endif

     Gosub _Pause ' pause changes (speeds up) when button is held down 

    If Var_Pause <= 1 Then 
      GOSUB Sound_Buzzer
      Return
    Endif
  If a = 0 Then Loop_2
RETURN ' auto return when the Var_Pause = 1





Check_Temperature:
  OWOut DQ, 1, [$CC, $44]    ' Start Temp conversion
 waitloop:
   If count_remain = 0 Then b = b + 1     '
   If b >= 25000 then                     '
    Loop_4:                               '
        LCDOut $fe, 1, "Temp Sensor Has " '
      LCDOut $fe, $c0, "Malfunctioned!2!" ' this detects a sensor malfunction
      PWM  Buzzer,255,100                 '
      The_Fan = Is_On                     '
      Pause 100                           '
    If a = 0 then Loop_4                  '
   Endif                                  ' 
  OWIn DQ, 4, [count_remain]	' Check for still busy converting
	IF count_remain = 0 THEN waitloop

	OWOut DQ, 1, [$CC, $BE]		  ' Read the Temp
  OWIn DQ, 0, [temp.LOWBYTE, temp.HIGHBYTE, _
                        Skip 4, count_remain, count_per_c]
    ' Calculate temp in degrees Celsius to 2 decimal places 
     Sign = temp.15
     temp = ((ABS(temp) >> 1) * 100)
     if Sign then temp = -temp
     temp = temp - 25 _
                     + (((count_per_c - count_remain) * 100) / count_per_c)

    ' Convert Celsius to Fahrenheit before returning   
     Sign = temp.15
     temp = (ABS(temp) */ 461)
     if Sign then temp = -temp
     temp = temp + 3200
RETURN


 mainloop:

  b = 0 ' reset var to prevent the alarm from going off 

  GOSUB Check_Temperature  

 If Temp / 100 <= 31 Then                '
   Loop_3:                               '
       LCDOut $fe, 1, "Temp Sensor Has " '
     LCDOut $fe, $c0, "Malfunctioned!1!" ' this detects a sensor malfunction
     PWM  Buzzer,255,100                 '
     The_Fan = Is_On                     '
     Pause 100                           '
   If a = 0 then Loop_3                  '
 Else                                    '
'  LCDOut $fe, 1,"Engine H2o Temp "
   LCDOut $fe, 1,"Monitor Collant "
   LCDOut $fe, $c0
   LCDOut "  Temp = "
   if temp.15 = 1 then LCDOut "-"
   temp = ABS(temp)
   LCDOut DEC temp / 100,0
   LCDOut "F "
 Endif                                   '

 If Temp / 100 >= Hi_Temp_Setpoint then The_Fan = Is_On
 If Temp / 100 <= Low_Temp_Setpoint then The_Fan = Is_Off  

 If Adjust_Hi_Temp_Push_Button = Is_Pressed Then Gosub Adjust_Hi_Temp_Set_Point 
 If Adjust_Low_Temp_Push_Button = Is_Pressed Then Gosub Adjust_Low_Temp_Set_Point 

 Pause 100  

 If a = 0 Then mainloop                                          
 
    End