Menu

Store Integer into EPPROM question

Help
Keith
2023-02-13
2023-02-17
  • Keith

    Keith - 2023-02-13

    I have it in the back of mind that I can only store a numeric value to a maximum of 255 onto an EEPROM Y/N

    I want to be able to store a numeric value from between 4000 of up to 6525. The Dark Arts folks (Arduino) can do but I can't follow how it works. is it possible? if so how do I do it?

     
  • Anobium

    Anobium - 2023-02-14

    In the Help.

    https://gcbasic.sourceforge.net/help/_lookup_tables.html

    Great Cow BASIC will try automatically detect the type of the table depending on the values in it. Great Cow BASIC can be explicitly instructed to cast the table to a variable type, as follows:

    Table TestDataSource as [Byte | Word | Integer | Long ]
        12
        24
        36
        48
        60
        72
    End Table
    

    Addresssing the Table Data
    Item 0 of a lookup table stores the size of the table. If the ReadTable command attempts to read beyond the end (number of data items) of the table, the value 0 will be returned. For tables with more than 255 elements it is mandatory to use a WORD variable to read the size of the table.

     
  • Ccin E Crout

    Ccin E Crout - 2023-02-14

    If you are trying to store a 'word' into Eeprom you could try this:

    Sub PutWEeprom (HiAddr As Byte, EWord As Word)
    'Places a Word into two bytes of
    'Eeprom, HiAddr is the first Byte
    'location in Eeprom, the lower Byte
    'is placed into the adjacent byte
    '
        Let THi = EWord / 256
        Let TLo = EWord - (THi * 256)
    
        EpWrite (HiAddr,  THi)
        EpWrite (HiAddr+1,TLo)
    
    End Sub
    
    Sub GetWEeprom (HiAddr As Byte, Out EePromWVal As Word)
    'Takes the address of the High Byte and returns
    'a Word made from the HiAddr and the adjacent
    'byte in Eeprom
    '
       'The sequence of this 'Rebuild' appears important.
       'The order of Low byte - High must be maintained.
        EpRead (HiAddr+1, EePromWVal)
        EpRead (HiAddr,   EePromWVal_H)
    
    End Sub
    

    These are called like this:

    Dim THi As Byte
    Dim TLo As Byte
    Dim WordVariable As Word
    #Define EePromStartAddress 0' This stores a word variable across two byte locations - next free EeProm location is 2
    

    Other code here...

    GetWEeProm(EePromStartAddress,WordVariable)
    

    And:

    PutWEeProm(EePromStartAddress,WordVariable)
    
     

    Last edit: Ccin E Crout 2023-02-14
    • Anobium

      Anobium - 2023-02-14

      @cc

      Very nice. May I lift and put into Help? I will attribute the knowledge to yourself.

       
      • Ccin E Crout

        Ccin E Crout - 2023-02-15

        By all means.

        If you prefer to use Williams, I won't cry!

         
  • William Roth

    William Roth - 2023-02-15

    I knocked these up for Terry Platt in another thread a couple a weeks ago. Seem to work ok

    Sub EPWriteWord(MemLoc, WordVar as WORD)
         EPWRITE MemLoc,WordVar
         EPWRITE (MemLoc+1), WordVar_H
     End Sub
    
    Sub EPReadWord(MemLoc,WordVar as Word)
        EPREAD(MemLoc,WordVar)
        EPREAD(MemLoc+1),WordVar_H
    End Sub
    
     
  • William Roth

    William Roth - 2023-02-15

    Cleaned up and added Integers to support negative numbers

    Sub EPWriteInteger(IN epMemLoc, IN epINTVar as INTEGER)
       EPWRITE epMemLoc, epINTVar
       EPWRITE (epMemLoc+1), epINTVar_H
    End Sub
    
    Sub EPReadInteger(IN epMemLoc, OUT epINTVar as INTEGER)
        EPREAD(epMemLoc, epINTVar)
        EPREAD(epMemLoc+1), epINTVar_H
    End sub
    
    
    Sub EPWriteWord(IN epMemLoc, IN epWordVar as WORD)
       EPWRITE epMemLoc, epWordVar
       EPWRITE (epMemLoc+1), epWordVar_H
    End Sub
    
    Sub EPReadWord(IN epMemLoc, OUT epWordVar as Word)
        EPREAD(epMemLoc, epWordVar)
        EPREAD(epMemLoc+1), epWordVar_H
    End sub
    
     
    • Anobium

      Anobium - 2023-02-15

      A thought, that would need investigation.. or may be you have already, but using the EEAddress rather than epMemLoc could save a variable. Check this idea in the ASM... the compiler may already do the optimisation.

      :-|

       
      • William Roth

        William Roth - 2023-02-15

        I was more focused on how it could be easily done than saving a single byte of memory. But I do think that using "EEAddress" would save that byte.

         
  • William Roth

    William Roth - 2023-02-15

    Here they all are . Used "EEAddress" to save a variable and a few instructions

    // Words
    Sub EPWriteWord(IN EEAddress, IN epWordVar as WORD)
       EPWRITE EEAddress, epWordVar
       EPWRITE (EEAddress+1), epWordVar_H
    End Sub
    
    Sub EPReadWord(IN EEAddress, OUT epWordVar as Word)
        EPREAD(EEAddress, epWordVar)
        EPREAD(EEAddress+1), epWordVar_H
    End sub
    
     // Integers
    Sub EPWriteInteger(IN EEAddress, IN epINTVar as INTEGER)
       EPWRITE EEAddress, epINTVar
       EPWRITE (EEAddress+1), epINTVar_H
    End Sub
    
    Sub EPReadInteger(IN EEAddress, OUT epINTVar as INTEGER)
        EPREAD(EEAddress, epINTVar)
        EPREAD(EEAddress+1), epINTVar_H
    End sub
    
      // Longs 
    Sub EPWriteLong(IN EEAddress, IN epLongVar as Long)
       EPWRITE(EEAddress, epLongVar)
       EPWRITE(EEAddress+1), epLongVar_H
       EPWRITE(EEAddress+2), epLongVar_U
       EPWRITE(EEAddress+3), epLongVar_E
    End Sub
    
    Sub EPReadLong(IN EEAddress, OUT epLongVar as Long)
       EPREAD(EEAddress, epLongVar)
       EPREAD(EEAddress+1), epLongVar_H
       EPREAD(EEAddress+2), epLongVar_U
       EPREAD(EEAddress+3), epLongVar_E
    End Sub
    

    Please ....someone test these routines

    Bill

     

    Last edit: William Roth 2023-02-15
    • Ccin E Crout

      Ccin E Crout - 2023-02-15

      I'm struggling to see how this saves a Byte. Is 'EEAdress' a system variable?

       
      • William Roth

        William Roth - 2023-02-15

        Variable "EEAddress" is used with both EPRead and EPWrite, so might as well reuse it here. And a few instructions are saved by not moving data from variable to variable.

        The only gotcha that I can see is if code was shuffling data around in EEPROM, like a in nested loop of Reads and Writes. In that rare case it may have been better to have different address variables for reads and writes

         
        👍
        1
  • William Roth

    William Roth - 2023-02-15

    Heads Up!

    I forgot that many Chips have more than 256 Bytes of EEPROM. In these cases "EEAddress" must be a Word Variable

    William

     
    • Anobium

      Anobium - 2023-02-16

      Use an overloaded Sub(). That will resolve.

       
      • William Roth

        William Roth - 2023-02-16

        That implies that the code was written with the intention of placing it in a library where 2 subs with the same name (overloaded) makes sense. These were posted with the intention of a user ( Keith) grabbing one or 2 and placing it in his source code.

         
        • Anobium

          Anobium - 2023-02-16

          OK.

           
  • Keith

    Keith - 2023-02-16

    I have tried using William's initial example but I cannot get it compile

    If ENCODER_SW = 0 Then
    Wait until ENCODER_SW = 1

        CLS
        ;
       EPWriteWord(eepAddr_0, value as WORD)
       EPWRITE eepAddr_0,value
       EPWRITE (eepAddr_0+1), value_H
    
           Goto Main
      End if
    

    it tells me :-

    Errors have been found:

    My Spindle Motor Temp Controller.gcb (270): Error: Array/Function
    EPWRITEWORD has not been declared
    My Spindle Motor Temp Controller.gcb (270): Error: Syntax Error

    putting the EPWRITEWORD in the DIM statements as a Word does nothing for it.

     
  • William Roth

    William Roth - 2023-02-17

    That is not my code! Not even close. It won't compile because you changed it . I think you may be misunderstanding that I gave you new subroutines to use. Use them exactly as I wrote them (as subroutines).

    EPWriteWord is a subroutine that I created (you must copy it into your code). Then you call that subroutine when you want to write a Word to EEPROM. Same goes for all of the others.

    Note that each WORD written to EEPROM takes up 2 memory locations. IN the example below the value of 4127 uses Locations 10 and 11.

      ' ----------- Main Code Starts Here ---------
    
    Dim TestWord as WORD
    
    EPWriteWord 10, 4127    '// Write Val of 4127 to EEPROM location 10  
    EPReadWord 10, TestWord '// Read it back into a variable
    
    Print TestWord  '// Show the Variable on LCD
    
    End
    
    
     '------------ Sub Routines Start Here ------------
     ' ----   DO NOT CHANGE OR MODIFY THESE !  -------
    
    Sub EPWriteWord(IN EEAddress, IN epWordVar as WORD)
        EPWRITE EEAddress, epWordVar
        EPWRITE (EEAddress+1), epWordVar_H
    End Sub
    
    Sub EPReadWord(IN EEAddress, OUT epWordVar as Word)
        EPREAD(EEAddress, epWordVar)
        EPREAD(EEAddress+1), epWordVar_H
    End sub
    
     

    Last edit: William Roth 2023-02-17
  • Keith

    Keith - 2023-02-17

    Thank you so much William, That has settled my threshold temperature issue, I'm not sure where it is being stored in either the 16F18326 EEPROM or the 256k EEPROM on the i2c Bus.

    I have seen a small i2c program which gives me a dump of the EEPROM contents but I don't seem to be able to find it.

     

Log in to post a comment.