Quantcast
Channel: MEL PICBASIC Forum
Viewing all articles
Browse latest Browse all 4787

Simple READ/WRITE EEPROM is driving me nuts! pls help

$
0
0
This is a stripped down part of a larger program. Eventually I need to collect several temperatures over a period of time, and read them back later, after power-cycling the pic. I simply can't figure out what is going wrong. I have commented the listing at several points to explain what I am doing in this very simple routine. I have used extensive serout2 commands to see exactly what is going on - but they do not help my simple mind at this point. As I have commented in the listing, I can write, and then read back immediately to the on-board EEPROM, and everything works in two identical routines. However, trying the same code another time does not work. I'm sure there are probably postings here that will help, I just have not been able to find any yet. I noticed on this listing that the first read of TempF shows TEMPF as all caps , however that is not the way the actual listing is.

I am using PBP version 2.6a, and a 18f2520. The defines, etc are required for the complete program.

any and all comments will be most appreciated

Ken

Code:

'****************************************************************
'*  Name    : RD / WRT TEST.BAS                                *
'*  Author  : KRS                                              *
'*  Date    : 7/27/2012                                        *
'*  Version : 1.0                                              *
'*  Notes  :  USING PBP VERSION 2.60A    PIC IS 18F2520        *
'*          :  I AM USING WEB4ROBOTS SERIAL KEYPAD AND LCD    *
'*          :  REQUIRES DIFFERENT SEROUT2 COMMANDS              *
'****************************************************************
 
'
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

OSCTUNE.6 = 1
 define OSC 4
 OSCCON = %01101110       
ADCON1 = %10001011          ' RIGHT JUSTIFY, PORTB ALL DIGITAL

TRISC =  %10111000
TRISA =  %11111111
TRISB =  %00000011
             
                       
MXSO            VAR  PORTC.0    ' SERIAL IN
MXCS            VAR  PORTC.1    ' CHIP SELECT
MXSCLK          VAR  PORTC.2    ' CLOCK                             
 
INT            VAR  PORTC.5    'Keyboard INT used to signal a key entry
TX              var  PORTC.6
Rx              var  PORTC.7

 
 'Allocate MAX6875 Variables
MXTemp      var    word    'raw data from 6675/type K sensor
TempF      var    word      'Actual Farenheit  temp
TempC      var    word

Y          VAR    BYTE
x          var byte
Z  VAR BYTE
TEMPF1      VAR    WORD
TEMPF2      VAR WORD


'***************************************************************
'      web4robots must be set-up first ! ! !
'****************************************************************
serout2 TX,84,[$FE,$02,3]      'SET BAUD RATE AT 9600
PAUSE  50
SEROUT2 TX,84, [$FE, $0A]  'Ensures that the display is on
pause  150                'Eliminate double key entry
SEROUT2 TX, 84, [$FE, $14]    'Ensures that the screen is cleared
PAUSE 100
SEROUT2 TX, 84, [$FE, $03, 20] 'Sets the brightness (0-250) 
PAUSE 100
SEROUT2 TX, 84, [$FE, $04, 10]  'sets contrast (Low number = Hi Contrast)
PAUSE  100
serout2 TX,84,[$FE,$14] 'clear the screen
'
'*******************************************************

START:
    PAUSE  2000   
    Y = 0 
    for x = 1 to 5
 
WRITE_temp:      'READ THE THERMOCOUPLE AND SAVE IT
      MXCS = 0  'Chip select low
      shiftin MXSO, MXSCLK, 0, [MXTemp\16]  'read the data to MXTemp
      MXCS = 1    'Chip select high
      TempC = MXtemp >> 5   
      TempF = (TempC*18)/10+32
      pause  30   
    '      READ THIS TEMPERATURE
      SEROUT2 TX,84,[$FE,$0C,0,0,"TEMP= ",dec TEMPF]
      PAUSE 3000
      SEROUT2 TX,84,[$FE,$14] ' clears the display
      PAUSE 50
'
'      NOW WRITE IT TO EEPROM LOCATION IN VARIABLE Y
       
      WRITE Y,WORD TEMPF
'
'  IMMEDIATELY READ IT BACK  - - THIS VALUE IS READ CORRECTLY ALL 5 TIMES
'
      READ Y, WORD TEMPF1
      SEROUT2 TX,84,[$FE,$0C,1,0,"READ TEMP= ",DEC TEMPF]     
      PAUSE  30     
      PAUSE 3000
      SEROUT2 TX,84,[$FE,$14] ' clears the display
      PAUSE    3
'******************************   
'    NOW READ IT BACK AND WRITE TO A DIFFERENT VARIABLE ( TEMPF1)
'  THIS READ ALSO WORKS CORRECTLY ALL 5 TIMES
'***************************** 
      READ Y, WORD TEMPF1
      PAUSE 50
      SEROUT2 TX,84,[$FE,$0C,0,0,"READ1 TEMPF1= ",DEC TEMPF1]
      PAUSE 30
      SEROUT2 TX,84,[$FE,$0C,1,0,"X = ",dec x]
      PAUSE    20
      SEROUT2 TX,84,[$FE,$0C,2,0,"Y = ",dec Y]
      PAUSE 3000
      SEROUT2 TX,84,[$FE,$14] ' clears the display
      pause 30     
      Y = Y + 1
      next x     
     
      PAUSE  3000

'**********************************
' NOW TRY TO READ IT AGAIN, USING ANY VARIABLE DOES NOT MATTER , TEMPF1, ETC
'    THE ONLY CORRECT READ HERE IS THE LAST ONE, (Y = 4)
'ALL OTHERS ARE INCORRECT - - VERY HIGH VALUES - - 23375, 22860, ETC
'*****************************************************************
      Y = 0        'RESET THE EEPROM ADDRESS POINTER TO 0
      FOR X = 1 TO 5
      READ Y, WORD TEMPF2  ' USING VARIABLE TEMPF2 HERE, ANY OTHER VARIABLES
                            ' DO NOT WORK EITHER
      PAUSE 50
      SEROUT2 TX,84,[$FE,$0C,0,0,"READ2 TEMPF2= ",DEC TEMPF2]
      PAUSE 30
      SEROUT2 TX,84,[$FE,$0C,1,0,"X = ",dec x]
      PAUSE    30
      SEROUT2 TX,84,[$FE,$0C,2,0,"Y = ",dec Y]
      PAUSE 2000
      SEROUT2 TX,84,[$FE,$14] ' clears the display
      pause 30       
      Y = Y + 1
      next x     
           
 DONE:
      SEROUT2 TX,84,[$FE,$14] ' clears the display
      PAUSE 50
      SEROUT2 TX,84,[$FE,$0C,2,0,"DONE"]
      pause  1000   
      GOTO  DONE


Viewing all articles
Browse latest Browse all 4787

Trending Articles