Menu

write the value 1023 on a memory 24c32

Help
Romtek
2023-04-27
2023-12-19
  • Romtek

    Romtek - 2023-04-27

    Hi, I have to write 5 ADconverter values in memory 24c32 and then read the values again. Basically I have to make a datalogger.
    I could not understand in the example (see DEMO folder) "Write-Read_Arrays+Strings_to_EEProm_Mega328p"
    how to write a value 1023 in memory, in a precise memory cell.
    I would be glad if someone can explain me how to do it, thanks

     
    • Romtek

      Romtek - 2023-04-29

      avoid answering me if you haven't tested the code (because I don't know much more I also have to solve the code you put me) or if the answers are not relevant to my questions thanks

       
  • Ccin E Crout

    Ccin E Crout - 2023-04-27

    The first thing to remember is that EeProm can only store Byte values. To store a Word value it needs splitting into two bytes.

    I use the following routines for storing and retrieving word values into and out of EeProm.
    Each word will take up two Eeprom storage locations. Your first AD value might be in EeProm location 0, the next would need to go into location 2, then 4, 6, 8 and so on.

    Dim THi As Byte
    Dim TLo As Byte
    Dim WordVariable As Word
    #Define EePromStartAddress 0' This holds a word variable across two byte locations - next free EeProm location is 2
    
    'Other code here...
    
    GetWEeProm(EePromStartAddress,WordVariable)
    
    'More code...
    
    PutWEeProm(EePromStartAddress,WordVariable)
    
    'Yet more code...
    
    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
    
     
  • Ccin E Crout

    Ccin E Crout - 2023-04-27

    Although I've just realised you are saving your values in external EeProm...

    That will require a slightly different approach.

    You will need to set up the I2C:

    #include <I2CEEPROM.h>
    ' Define I2C settings
    #define I2C_MODE Master
    #define I2C_DATA PORTC.1   'Dat Pin 9
    #define I2C_CLOCK PORTC.2  'Clk Pin 8
    
    #define I2C_DISABLE_INTERRUPTS ON
    
    ' Default I2C Address
    #define EEpromDevice 0xA0 'Default I2C address
    

    0xA0 being the I2C address of the 24C32 device. {May need changing to suit your device and your address.}

    In my earlier post, I used 'EpRead' and 'EpWrite' , for your 24C32 those commands would be:

    eeprom_wr_byte (EEpromDevice, HiAddr, THi)
    eeprom_wr_byte (EEpromDevice, HiAddr+1, TLo)
    
    eeprom_rd_byte (EEpromDevice, HiAddr+1, EePromVal)
    eeprom_rd_byte (EEpromDevice, HiAddr, EePromVal_H)
    

    Hopefully that will give you a start at least.

     

    Last edit: Ccin E Crout 2023-04-27
    • Romtek

      Romtek - 2023-04-28

      1) THE eeprom_rd_byte COMMAND WHERE DID YOU FIND IT? I looked in the help but couldn't figure out how to use it.

      2) I'd like to know if the libraries are described in the help with examples (see DS1307 and other libraries)

      3) the example I found in the Demo folder I'd like to understand the commands
      eeprom_rd_byte
      eeprom_rd_word
      eeprom_wr_byte
      eeprom_wr_word


      I figured out that a value of 1023 takes up 2 memory cells.
      How can I write a value 1023 that I want to store in memory cells (8 and 9)
      and to be able to read it again and print?

      Thank you.

       
  • Ccin E Crout

    Ccin E Crout - 2023-04-28

    I found the (rd_byte) commands in the online help, having searched for I2C.
    If there are commands for reading and writing to words, then using those would be easier still.

    On the assumption that the word commands are included in the I2CEEPROM.h file I'd start with something similar to:

    #include <I2CEEPROM.h>
    ' Define I2C settings
    #define I2C_MODE Master
    #define I2C_DATA PORTC.1   'Dat Pin 9
    #define I2C_CLOCK PORTC.2  'Clk Pin 8
    
    #define I2C_DISABLE_INTERRUPTS ON
    
    ' Default I2C Address
    #define EEpromDevice 0xA0 'Default I2C address
    Dim WordVariableToBeStored As Word
    Dim WordVariableToBeRead   As Word
    Dim HiAddr                 As Byte
    
    Let HiAddr = 8 'location in EeProm
    Let WordValueToBeStored = 1023
    
    eeprom_wr_word(EEpromDevice, HiAddr, WordValueToBeStored)
    eeprom_wr_word(EEpromDevice, HiAddr, WordValueToBeRead)
    Print WordVariableToBeRead
    'Note that print may need an LCD or other output device to be connected!
    

    However, in my I2CEEPROM.h file, those commands do not exist. So I'd use something similar my earlier example program.

    #include <I2CEEPROM.h>
    ' Define I2C settings
    #define I2C_MODE Master
    #define I2C_DATA PORTC.1   'Dat Pin 9
    #define I2C_CLOCK PORTC.2  'Clk Pin 8
    
    #define I2C_DISABLE_INTERRUPTS ON
    
    ' Default I2C Address
    #define EEpromDevice 0xA0 'Default I2C address
    Dim WordVariableToBeStored As Word
    Dim WordVariableToBeRead   As Word
    Dim EePromStartAddress     As Byte
    
    Let EePromStartAddress = 8 'upper byte location in EeProm
    Let WordValueToBeStored = 1023
    
    PutWEeProm(EePromStartAddress,WordValueToBeStored)
    GetWEeProm(EePromStartAddress,WordValueToBeRead)
    Print WordVariableToBeRead
    'Note that print may need an LCD or other output device to be connected!
    
    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)
    
        eeprom_wr_byte(EEpromDevice, HiAddr,   THi)
        eeprom_wr_byte(EEpromDevice, 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.
        eeprom_rd_byte(EEpromDevice, HiAddr+1, EePromWVal)
        eeprom_rd_byte(EEpromDevice, HiAddr,   EePromWVal_H)
    
    End Sub
    

    *Programs untested as I don't have a 24C32 to hand.

     
  • Ccin E Crout

    Ccin E Crout - 2023-04-28

    Why not get it working using the 'On Chip' EeProm to start with and then add the I2C EeProm later?

     
    • Romtek

      Romtek - 2023-04-28

      Hi, I have to write 5 ADconverter values in memory 24c32 and then read the values again. Basically I have to make a datalogger.
      1) It doesn't work, it gives me errors when copile
      2) You didn't answer me about eeprom_rd_byte and eeprom_rd_word, how to use them and where can I find the manual. Where did you find the information on "eeprom_rd_byte"???

       
      • Ccin E Crout

        Ccin E Crout - 2023-04-29

        Sorry, I thought I did answer your second question about eeprom_rd_byte. I found the answer in the online help, having searched for I2C.

        The program I posted should only be missing the #Chip definition and I'm surprised it doesn't compile.

        Sorry I am unable to help further as I don't have a working program for the device you are using. I have a complete program for EeRam, but that isn't the same as your 24C32. I have programs that use the internal EeProm, but sadly they won't work with your 24C32 either.

         
  • mmotte

    mmotte - 2023-04-29

    Romtek

    The i2ceeprom.h library file is at "C:\GCstudio\GreatCowBASIC\include"

    There is methods for writing and reading bytes or strings and for small eeproms with byte addresses and larger eeproms with word addresses. Yours is 32K so you need word addr.

    The difference between strings and arrays is the string has the first location , mystring(0), as the length of the string. So by convention mystring (1) is the first byte that you can use.
    When treating it as an array you can write to mystring(0). But the I2C routine starts at mystring(1).

    The coding approach should be to DIMension the variable (word, integer, or long) first, then create the byte aliases:
    
        Dim my_variable as LONG
        Dim ByteOne   as Byte alias my_variable_E
        Dim ByteTwo   as Byte alias my_variable_U
        Dim ByteThree as Byte alias my_variable_H
        Dim ByteFour  as Byte alias my_variable
    

    As pointed out above by Ccin, Word variables can be broken into bytes by unscore extensions.
    Dim myVar as word

    If you want to write or read the hi byte just use myVar_H
    Writing a byte to the lo byte myVar = myByte
    Reading myByte = myVar

    In your case it would be more efficient to write all 10 bytes at once. This would involve using the write string to eeprom.
    Dim inBuf (10)
    inBuf(1) = myA2D1_H
    inBuf(2) = myA2D1
    inBuf(3) = myA2D2_H
    inBuf(4) = myA2D2
    inBuf(5) = myA2D3_H
    inBuf(6) = myA2D3
    inBuf(7) = myA2D4_H
    inBuf(8) = myA2D4
    inBuf(9) = myA2D5_H
    inBuf(10) = myA2D5
    Then send the array to eeprom

    Nope this is not working code but it is a method to send you 5 words.

    GL
    Mike

     
  • Anobium

    Anobium - 2023-04-30

    There are seven demos that use the eeprom methods defined the the I2C library.

    The demos cover the usage and how to use bytes, arrays and other structures. The choice of chip is not important, the setup to I2C is.

    These are the best resources for the eeprom library.

    Evan

    ..demos\I2C_Solutions\I2C_EEProm_Solutions\Write-Read_Arrays+Strings_to_EEProm_Mega328p.gcb
      112,9:         eeprom_rd_array( eepDev, eepAddr , datarray(), 10 )
      159,11:           eeprom_rd_byte( eepDev, eepAddr , datastring(eepAddr) )
    
    ..demos\I2C_Solutions\I2C_EEProm_Solutions\Write-Read_Arrays+Strings_to_EEProm_with16bytePage_16F1939.gcb
      108,9:         eeprom_rd_array( eepDev, eepAddr , datarray(), 10 )
      155,11:           eeprom_rd_byte( eepDev, eepAddr , datastring(eepAddr) )
    
    ..demos\I2C_Solutions\I2C_EEProm_Solutions\Write-Read_Arrays+Strings_to_EEProm_with64bytePage_16F1939.gcb
      108,9:         eeprom_rd_array( eepDev, eepAddr , datarray(), 10 )
      155,11:           eeprom_rd_byte( eepDev, eepAddr , datastring(eepAddr) )
    
    ..demos\Real_Time_Clock_Solutions\i2c_DS3231_16F1937_LCD.gcb
      324,5:     eeprom_rd_byte ( AT24C32 , EEpromAddress,  eepromval )
    
    ..demos\Real_Time_Clock_Solutions\i2c_DS3231_16F1937_Serial.gcb
      329,5:     eeprom_rd_byte ( AT24C32 , EEpromAddress,  eepromval )
    
    ..demos\Real_Time_Clock_Solutions\i2c_DS3231_mega328p_LCD.gcb
      309,5:     eeprom_rd_byte ( AT24C32 , EEpromAddress,  eepromval )
    
    ..demos\Real_Time_Clock_Solutions\i2c_DS3231_mega328p_Serial.gcb
      318,5:     eeprom_rd_byte ( AT24C32 , EEpromAddress,  eepromval )
    
     
  • Anobium

    Anobium - 2023-04-30

    Using GC Code to search demo folder.

    Open the demo folder, using 'Open Folder', then select 'Edit\Find in Files' and then enter the search criteria. I used 'eeprom_w' this shows the seven demos.

    Hope this helps.

     

    Last edit: Anobium 2023-04-30
  • Romtek

    Romtek - 2023-12-19

    **I finally managed to solve it after a long time.
    Hi, I would like to be of help by bringing my example (certainly improvable) for all those people like me who need to memorize and re-read on an external eeprom, (24lc32) a value of a 10bit ADC converter (word) in a specific memory position (data logger type).
    When the program is turned on, it displays the name of the program on the display and prints it on the serial line, and shows the value previously saved in the memory before turning it off.
    Then it begins to read the analog channel A1, until the button for storing the ADC value is pressed (occupies 2 memory cells of 2 bytes) the value of the ADC converter).
    By pressing the button again, it reads the previously stored value into the memory.
    The next time you press the button it returns to the beginning of the program.
    With this example I hope to do something pleasant, greetings to everyone. (The diagram is attached)

    warning: indentation if you copy the code**

    `'ROMTEK working program that writes a word and rereads it in eeprom 24lc32a
    'Data 17/12/23 ver.0.00
    'micro:16F876A
    'GCBASIC (1.01.00 2023-11-23 (Windows 64 bit) : Build 1308)
    'description: analogue reading on channel A1 and storage on 24lc32 with button and 4 bit display
    'IMPORTANT!!!!!!!!!!!!!!!!!!!! with the prefix _H the high byte is determined !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!
    'references https://gcbasic.sourceforge.io/help/_using_variables.html
    'Change the microcontroller, frequency and config to suit your needs.
    'setup:
    #chip 16F876A, 4 'tipe microcontroller e frequency
    #option explicit 'force declaration of variables
    '#config MCLRE_ON
    'Required Library to read and write to an EEPROM
    #include <i2ceeprom.h></i2ceeprom.h>

    ;Defines display lcd 
    #define LCD_IO 4
    #define LCD_NO_RW
    #define LCD_Speed SLOW '!!!!!!!!!!!!! set for lcd lenses 'FAST IS OK ON 16f877a !!!!!!!!!!!!!!!
    #define LCD_RS PORTB.2
    #define LCD_Enable PORTB.3
    #define LCD_DB4 PORTB.4
    #define LCD_DB5 PORTB.5
    #define LCD_DB6 PORTB.6
    #define LCD_DB7 PORTB.7
    

    '---------------------------------------------------------------------------------------------------------------------
    ' ----- Define Hardware settings
    ' Define I2C settings - CHANGE PORTS
    #define I2C_MODE Master
    #define I2C_DATA PORTC.4 'pin 25
    #define I2C_CLOCK PORTC.3 'pin 24
    #define I2C_DISABLE_INTERRUPTS ON
    'Optionally, you can reduce the I2C timings.
    ' #define I2C_BIT_DELAY 0 us
    ' #define I2C_CLOCK_DELAY 1 us
    '---------------------------------------------------------------------------------------------------------------------
    ' THIS CONFIG OF THE SERIAL PORT WORKS WITH max232 THEN TO PC
    ' USART settings
    #define USART_BAUD_RATE 9600
    Dir PORTc.6 Out
    Dir PORTc.7 In
    #define USART_DELAY OFF
    #define USART_TX_BLOCKING
    wait 500 ms
    '---------------------------------------------------------------------------------------------------------------------
    ' Read ONE byte from the EEPROM
    DIM DeviceID as byte
    DIM EepromAddress, syscounter as word
    #define EEpromDevice 0xA0 'address divice 24lc32
    EepromAddress=2000 'address of the external memory cell
    / address can be chosen from 0 to 64000(approximately) but it depends on the size of the external memory used.
    remember that the analog value (max1023) occupies 2 memory cells and consequently to store more analog channels you must increase EepromAddress by 2 cells,
    e.g. store in cell 0 and after EepromAddress= EepromAddress + 2
    after store the second analog channel (i.e. increment by 2 every time you write and read)
    /
    '---------------------------------------------------------------------------------------------------------------------
    'Define our array
    Dim outarray(64)
    DIM inarray(64)
    Dim bHi As Byte 'byte high
    Dim bLo As Byte 'byte low
    'button
    #define p1 PORTc.0 'button
    Dir p1 In 'button

    'Define variable per read adconverter
    Dim ch as Word
    Dir PORTA.1 In
    ch=0                    'reset variable
    

    '*****************
    Do 'main cycle
    main: 'do not write here in the label but go to the beginning
    CLS
    PRINT "ROMTEK"
    locate 1,0
    PRINT "i2cmem-logger"
    HSerPrintCRLF 1
    HSerPrint "i2cmem-logger" 'version and title
    wait 2000 ms
    readmem 'I read the storage before turning it off
    wait 2000 ms
    rdch: 'do not write here in the label but go to the beginning
    ch=ReadAD10(AN1) 'read port analog
    wait 50 ms 'delay
    CLS
    PRINT "ch "
    PRINT ch
    HSerPrintCRLF 1 'line at the end
    HSerPrint "ch " 'I send the adc value to the serial line
    HSerPrint ch
    wait 100 ms
    if p1=1 Then 'if the button is pressed
    goto wrmem 'I jump to store the value
    Else
    goto rdch
    end if
    /

    */
    wrmem: 'don't write here in the label but go to the beginning
    writemem 'call sub routine write memory
    wait 100 ms
    ptest: 'don't write here in the label but go to the beginning
    if p1=1 then 'if the button is pressed
    wait 100 ms
    goto rdmem 'jump to read stored value
    end if
    goto ptest

    rdmem: 'don't write here in the label but go to the beginning
    ch=0 'reset variable
    readmem 'call sub routine read memory
    wait 100 ms
    ptest2: 'don't write here in the label but go to the beginning
    if p1=1 then 'if the button is pressed
    goto main 'go to main
    end if
    goto ptest2

    Loop
    'end '****end prog****************

    '---------------------------------------------------------------------------------------------------------------------------------------

    sub writemem
    'IMPORTANT!!!!!!!!!!!!!!!!!!!! with the suffix _H the high byte is determined !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!
    CLS
    PRINT "write mem"
    locate 1,0
    PRINT "ch "
    PRINT ch
    wait 100 ms 'delay
    HSerPrintCRLF 1
    HSerPrint "write ch "
    HSerPrint ch
    bLO=ch 'get byte low
    bHi = ch_H 'get byte high with suffix _H
    outarray(1)=bLo
    outarray(2)=bHi
    'write(device,memory page,address,array name,2 bytes used)
    eeprom_wr_array(EEpromDevice, 64, EepromAddress, outarray(), 2)

    end Sub

    sub readmem
    'IMPORTANT!!!!!!!!!!!!!!!!!!!! with the suffix _H the high byte is determined !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!

    EepromAddress=2000  'address of the external memory cell
     'read (device, cell address, array name, 2 bytes used)   
    eeprom_rd_array(EEpromDevice, EepromAddress, inarray(),2)
    ch_H=inarray(2)                     'I take the high bytes with suffix _H 
    ch=ch + inarray(1)                  'I take the low bytes and add them to the high bytes
    wait  100 ms   
    CLS
    PRINT "read mem"
    locate 1,0
    PRINT "ch "
    PRINT ch
    PRINT " "
    wait 100 ms          'delay 
    HSerPrintCRLF 1
    HSerPrint "read mem   ch "
    HSerPrint  ch
    wait  100 ms
    

    end sub

    `

     

Log in to post a comment.