word variable to 25lc640


Closed Thread
Results 1 to 6 of 6
  1. #1
    TONIGALEA's Avatar
    TONIGALEA Guest

    Default word variable to 25lc640

    Is it possible to store a word variable to this eeprom? as highbyte and low byte?


    Toni

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Yes, but usually you'd store Lowbyte first followed by Highbyte - especially if you're loading/unloading from word arrays. Why? Consider an array...

    Myword var word [8]

    Bit zero as in Myword.0(0) is bit zero of MyWord(0)
    Bit 127 as in Myword.0(127) is bit 15 of MyWord(7)

    All the 128 bits(0-127) of the entire word (or byte) array are sequentially available to you in that order. Therefore, correspondingly, so are the bytes...

    Therefore storing in Lowbyte/Highbyte order makes assembling words and word arrays particularly simple.

  3. #3
    TONIGALEA's Avatar
    TONIGALEA Guest


    Did you find this post helpful? Yes | No

    Default

    Thank you very much Mel
    but i still seem to be having problems as i am trying to store the following word data format
    0 to addr 0 to 1
    1000 to addrs 2 to 3
    2000 to addrs 4 to 5

    but i seems to be getting the following instead

    Eeprom Address = 1 Eeprom Value = 0
    Eeprom Address = 4 Eeprom Value = 1000
    Eeprom Address = 7 Eeprom Value = 2000
    Eeprom Address = 10 Eeprom Value = 3000
    Eeprom Address = 13 Eeprom Value = 4000
    Eeprom Address = 16 Eeprom Value = 5000
    Eeprom Address = 19 Eeprom Value = 6000
    Eeprom Address = 22 Eeprom Value = 7000
    Eeprom Address = 25 Eeprom Value = 8000

    i can't lay my finger on whwere i am going wrong so i put my code below

    Your help is appreciated
    Toni


    DEFINE LOADER_USED 1 ' uses a bootloader
    Include "Modedefs.Bas"


    Define OSC 20
    clear

    ' Setup Hardware for uart
    DEFINE HSER_BAUD 19200
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 24h
    DEFINE HSER_CLROERR 1


    DEFINE LCD_DREG PORTD ' Use Portd for LCD Data
    DEFINE LCD_DBIT 4 ' Use Upper(4) 4 bits of Port
    ' PORTD-4 thru PORTD-7 connects to
    ' LCD DB4 thru LCD DB-7 respectively
    DEFINE LCD_RSREG PORTE ' PORTE for RegisterSelect (RS) bit
    DEFINE LCD_RSBIT 0 ' PORTE-0 pin for LCD's RS line
    DEFINE LCD_EREG PORTE ' PORTE for Enable (E) bit
    DEFINE LCD_EBIT 1 ' PORTE-1 pin for LCD's E line

    DEFINE LCD_BITS 4 ' Using 4-bit bus
    DEFINE LCD_LINES 2 ' Using 2 line Display
    DEFINE LCD_COMMANDUS 2000 ' Command Delay (uS)
    DEFINE LCD_DATAUS 50 ' Data Delay (uS)

    ' ** Define LCD Control Constants **

    I CON 254 ' Control Byte
    Clr CON 1 ' Clear the display
    Line1 CON 128 ' Point to beginning of line 1
    Line2 CON 192 ' Point to beginning of line 2


    ' Software Defines
    ' ----------------
    ' ** Declare the Variables **
    Temp VAR word ' General Purpose
    Temp2 VAR word ' General Purpose
    Data_Out VAR BYTE ' Data read from the Eeprom
    Data_In VAR BYTE ' Data written to the Eeprom

    ' ** Define the Pin assignments **
    CS Var PortC.4 ' Chip select pin
    SCK Var PortC.3 ' Clock pin
    SI Var PortC.5 ' Data in pin
    SO Var PortC.5 ' Data out pin

    ' ** Declare the Variables **
    Addr var Word ' Memory address within the eeprom (0-511)


    ' ** Define the eeprom's op-codes **
    WRSR Con 1 ' Write to STATUS REGISTER op-code
    EWR Con 2 ' WRITE op-code
    ERD Con 3 ' READ op-code
    WRDI Con 4 ' DISABLE WRITES op-code
    RDSR Con 5 ' Read the STATUS REGISTER op-code
    EWEN Con 6 ' ENABLE WRITES op-code

    '
    ' Initialise Hardware
    ' -------------------
    ADCON1=7
    TRISA=%00000000
    TRISB=%00000000
    TRISC=%10000000
    TRISD=%00000000


    Pause 1000
    lcdout I,Clr:Pause 30
    lcdout I,Line1," 25LC640 TEST "
    lcdout I,Line2+2,"..Power On.. !!"
    Pause 1000 '
    Data_Out=0: Data_In =0: Temp =0

    'WRITE SUB
    '=======
    For Addr = 0 TO 100 STEP 2 ' Loop 50 times
    Data_In =Temp.LowByte ' Data_In is data TO EPPROM
    Gosub EWrite
    Addr=Addr+1
    Data_In =Temp.HighByte ' Data_In is data TO EPPROM
    Gosub EWrite
    Temp =Temp + 1000 ' Add 1000 to the previous value
    Next Addr

    loop:


    Again:
    lcdout I,Clr ' Clear the LCD

    For Addr=0 To 100 STEP 2 ' Create a loop of 50 WORD VALUES
    Gosub ERead ' Read the byte from the Eeprom
    Temp2.LowByte= Data_Out
    AddR=AddR+1
    Gosub ERead
    Temp2.HighByte = Data_Out

    lcdout I,Line1," Eprom ADDR:" ,DEC AddR
    lcdout I,Line2+2,DEC Temp2
    HSerout ["Eeprom Address = ",DEC AddR," Eeprom Value = ",DEC Temp2,10,13]
    ' Display the WORD values from the eeprom
    Pause 1000
    Next ' Close the loop
    Goto Again ' Do it forever
    End

    ' Read a single byte from the Eeprom
    ' The address is held in the variable "ADDR"
    ' The byte read is returned in the variable "Data_Out"
    Eread: Low CS ' Enable the eeprom
    Shiftout SI,SCK,MSBFIRST,[ERD,Addr.highbyte,Addr.lowbyte]' Send READ COMMAND and address
    Shiftin SO,SCK,MSBPRE,[Data_Out] ' Read data
    High CS ' Disable the eeprom
    Return

    ' Write a single byte to the Eeprom
    ' The address is held in the variable "ADDR"
    ' The byte to be written is held in the variable "Data_in"
    Ewrite: Low CS ' Enable the eeprom
    Shiftout SI,SCK,MSBFIRST,[EWEN] ' Send WRITE ENABLE command
    High CS ' Disable the eeprom, to execute the command
    Low CS ' Re-enable the eeprom
    Shiftout SI,SCK,MSBFIRST,[EWR,Addr.highbyte,Addr.lowbyte,Data_In]' Send address and data
    High CS ' Disable the eeprom
    Pause 5 ' Allow the eeprom to allocate the byte
    Return

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    You've caught me in a very busy week... so just briefly try this (I haven't fully checked all your code) and see if there's an improvement...

    For the write...

    For CounterA=0 to 98 step 2
    Addr=CounterA
    Data_In =Temp.LowByte ' Data_In is data TO EPPROM
    Gosub EWrite
    Addr=Addr+1
    Data_In =Temp.HighByte ' Data_In is data TO EPPROM
    Gosub EWrite
    Temp =Temp + 1000 ' Add 1000 to the previous value
    Next CounterA

    For the read...

    For CounterA=0 To 98 STEP 2 ' Create a loop of 50 WORD VALUES
    Addr=CounterA
    Gosub ERead ' Read the byte from the Eeprom
    Temp2.LowByte= Data_Out
    AddR=AddR+1
    Gosub ERead
    Temp2.HighByte = Data_Out
    Next CounterA

    1. It's dangerous to manually advance the counter variable in a for-next loop, because when it steps, it'll step from your new altered value.

    2. Doesn't counting "From 0 to 100 step 2" produce 51 steps (not 50)? Since 0 is the first step, and 100 is the 51st step?

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    For reference, see also the reply I gave atomski today.

  6. #6
    TONIGALEA's Avatar
    TONIGALEA Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks you for all your Mel,
    i am begining to see the light now. by just adding addrs-1 this solves the problem become i am increment addr twice as you pointer out .
    This is the working code if someone else needs it .


    Biggest Regards
    Toni
    DEFINE LOADER_USED 1 ' uses a bootloader
    Include "Modedefs.Bas"


    Define OSC 20
    clear

    ' Setup Hardware for uart
    DEFINE HSER_BAUD 19200
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 24h
    DEFINE HSER_CLROERR 1


    DEFINE LCD_DREG PORTD ' Use Portd for LCD Data
    DEFINE LCD_DBIT 4 ' Use Upper(4) 4 bits of Port
    ' PORTD-4 thru PORTD-7 connects to
    ' LCD DB4 thru LCD DB-7 respectively
    DEFINE LCD_RSREG PORTE ' PORTE for RegisterSelect (RS) bit
    DEFINE LCD_RSBIT 0 ' PORTE-0 pin for LCD's RS line
    DEFINE LCD_EREG PORTE ' PORTE for Enable (E) bit
    DEFINE LCD_EBIT 1 ' PORTE-1 pin for LCD's E line

    DEFINE LCD_BITS 4 ' Using 4-bit bus
    DEFINE LCD_LINES 2 ' Using 2 line Display
    DEFINE LCD_COMMANDUS 2000 ' Command Delay (uS)
    DEFINE LCD_DATAUS 50 ' Data Delay (uS)

    ' ** Define LCD Control Constants **

    I CON 254 ' Control Byte
    Clr CON 1 ' Clear the display
    Line1 CON 128 ' Point to beginning of line 1
    Line2 CON 192 ' Point to beginning of line 2


    ' Software Defines
    ' ----------------
    ' ** Declare the Variables **
    Temp VAR word ' General Purpose
    Temp2 VAR word ' General Purpose
    Data_Out VAR BYTE ' Data read from the Eeprom
    Data_In VAR BYTE ' Data written to the Eeprom
    CounterA var word
    ' ** Define the Pin assignments **
    CS Var PortC.4 ' Chip select pin
    SCK Var PortC.3 ' Clock pin
    SI Var PortC.5 ' Data in pin
    SO Var PortC.5 ' Data out pin

    ' ** Declare the Variables **
    Addr var Word ' Memory address within the eeprom (0-511)


    ' ** Define the eeprom's op-codes **
    WRSR Con 1 ' Write to STATUS REGISTER op-code
    EWR Con 2 ' WRITE op-code
    ERD Con 3 ' READ op-code
    WRDI Con 4 ' DISABLE WRITES op-code
    RDSR Con 5 ' Read the STATUS REGISTER op-code
    EWEN Con 6 ' ENABLE WRITES op-code

    '
    ' Initialise Hardware
    ' -------------------
    ADCON1=7
    TRISA=%00000000
    TRISB=%00000000
    TRISC=%10000000
    TRISD=%00000000


    Pause 1000
    lcdout I,Clr:Pause 30
    lcdout I,Line1," 25LC640 TEST "
    lcdout I,Line2+2,"..Power On.. !!"
    Pause 1000 '
    Data_Out=0: Data_In =0: Temp =0

    'WRITE SUB
    '==========
    For CounterA=0 to 98 step 2
    Addr=CounterA
    Data_In =Temp.LowByte ' Data_In is data TO EPPROM
    Gosub EWrite
    Addr=Addr+1
    Data_In =Temp.HighByte ' Data_In is data TO EPPROM
    Gosub EWrite
    Temp =Temp + 1000 ' Add 1000 to the previous value
    Next CounterA



    Again:
    lcdout I,Clr ' Clear the LCD
    For CounterA=0 To 98 STEP 2 ' Create a loop of 50 WORD VALUES
    Addr=CounterA
    Gosub ERead ' Read the byte from the Eeprom
    Temp2.LowByte= Data_Out
    AddR=AddR+1
    Gosub ERead
    Temp2.HighByte = Data_Out


    lcdout I,Line1," Eprom ADDR:" ,DEC AddR-1
    lcdout I,Line2+2,DEC Temp2
    HSerout ["Eeprom Address = ",DEC Addr-1," Eeprom Value = ",DEC Temp2,10,13]
    ' Display the WORD values from the eeprom
    Pause 1000
    Next CounterA
    Goto Again ' Do it forever
    End

    ' Read a single byte from the Eeprom
    ' The address is held in the variable "ADDR"
    ' The byte read is returned in the variable "Data_Out"
    Eread: Low CS ' Enable the eeprom
    Shiftout SI,SCK,MSBFIRST,[ERD,Addr.highbyte,Addr.lowbyte]' Send READ COMMAND and address
    Shiftin SO,SCK,MSBPRE,[Data_Out] ' Read data
    High CS ' Disable the eeprom
    Return

    ' Write a single byte to the Eeprom
    ' The address is held in the variable "ADDR"
    ' The byte to be written is held in the variable "Data_in"
    Ewrite: Low CS ' Enable the eeprom
    Shiftout SI,SCK,MSBFIRST,[EWEN] ' Send WRITE ENABLE command
    High CS ' Disable the eeprom, to execute the command
    Low CS ' Re-enable the eeprom
    Shiftout SI,SCK,MSBFIRST,[EWR,Addr.highbyte,Addr.lowbyte,Data_In]' Send address and data
    High CS ' Disable the eeprom
    Pause 5 ' Allow the eeprom to allocate the byte
    Return

Similar Threads

  1. Minimizing code space
    By Tobias in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th May 2009, 07:25
  2. SEROUT WORD variable problem
    By Tobias in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 19th April 2009, 11:20
  3. DS2760 Thermocouple Kit from Parallax in PicBasicPro
    By seanharmon in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th July 2008, 23:19
  4. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  5. calculation problem
    By nicolelawsc in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2006, 15:23

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts