sayzer
- 20th May 2008, 11:57
<font color="#000080"><i>' Omer YILDIZ , Istanbul/TURKEY
' May-2008
' Sayzer @ www.picbasic.co.uk/forum
' This code is to show how to use the page write feature of I2C eeproms.
' Page write feature allows us to write an entire page at one shot,
' and takes one write cycle to complete.
' This code will accept 128-byte packet from RS232 port @9600bps,
' and write the entire packet into 24C512 external eeprom at one shot.
' Incoming 128-bytes packets must have at least 15mS intervals.
' Page size is different from eeprom to eeprom.
' For example: Page size for 24C512 is 128 bytes while
' it is 32 bytes for 24LC64.
' That means, we can write 128-bytes into the eeprom
' at once and it takes only approx 5 - 10 ms.
' If we had to do this one byte at a time, we would have to wait
' approx. 1 second to complete.
' 1 second may not sound that long but,
' think about 64,000 byte waiting to be written !!
' PIC18F452
' Set your fuse settings up here.
</i><b>DEFINE </b></font>OSC <font color="#FF0000"><b>20
</b></font><font color="#000080"><i>'Thanks to DT
</i><b>INCLUDE </b></font><font color="#008000"><b>"DT_INTS-18.bas" </b></font><font color="#000080"><i>' Base Interrupt System
</i><b>INCLUDE </b></font><font color="#008000"><b>"ReEnterPBP-18.bas" </b></font><font color="#000080"><i>' Include if using PBP interrupts
'Thanks to Steve.
</i><b>DEFINE </b></font>HSER_RCSTA <font color="#FF0000"><b>90</b></font>h <font color="#000080"><i>' Enable serial port & continuous receive
</i><b>DEFINE </b></font>HSER_TXSTA <font color="#FF0000"><b>24</b></font>h <font color="#000080"><i>' Enable transmit, BRGH = 1
</i><b>DEFINE </b></font>HSER_SPBRG <font color="#FF0000"><b>129 </b></font><font color="#000080"><i>' 9600 Baud @ 0,16%
</i><b>DEFINE </b></font>HSER_CLROERR <font color="#FF0000"><b>1 </b></font><font color="#000080"><i>' Clear overflow automatically
'Thanks to MicroChip.
</i></font>ADCON1 = <font color="#FF0000"><b>7
</b></font>TrisA = <font color="#FF0000"><b>%000000
</b></font>TrisB = <font color="#FF0000"><b>%00000000
</b></font>TrisC = <font color="#FF0000"><b>%10000000 </b></font><font color="#000080"><i>'RC7 => RS232 RX (input pin).
</i></font>TrisD = <font color="#FF0000"><b>%00000000
</b></font>TrisE = <font color="#FF0000"><b>%000
</b></font>PortA = <font color="#FF0000"><b>0
</b></font>PortB = <font color="#FF0000"><b>0
</b></font>PortC = <font color="#FF0000"><b>0
</b></font>PortD = <font color="#FF0000"><b>0
</b></font>PortE = <font color="#FF0000"><b>0
</b></font>I2C_SCL <font color="#000080"><b>VAR </b></font>PortC.<font color="#FF0000"><b>3
</b></font>I2C_SDA <font color="#000080"><b>VAR </b></font>PortC.<font color="#FF0000"><b>4
</b></font>AdrW <font color="#000080"><b>VAR WORD </b><i>' Address variable for write operation.
</i></font>AdrR <font color="#000080"><b>VAR WORD </b><i>' Address variable for read operation.
</i></font>Mem <font color="#000080"><b>VAR BYTE</b></font>[<font color="#FF0000"><b>1350</b></font>] <font color="#000080"><i>' Create an array as large as possible.
</i></font>RXFlag <font color="#000080"><b>VAR BIT </b><i>' Just a flag.
</i></font>Writetime <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>15 </b></font><font color="#000080"><i>' This is the write cycle time for external eeprom. (mS)
</i></font>PageSize <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>128 </b></font><font color="#000080"><i>' Page size for 24C512 : 128 bytes; for 24LC64 : 32 bytes.
'====This is DT's famous and useful Int routine. =========
</i><b>ASM
</b></font><font color="#008000">INT_LIST macro </font><font color="#000080"><i>; IntSource, Label, Type, ResetFlag?
</i></font><font color="#008000">INT_Handler RX_INT, _ReadPort, PBP, yes
endm
INT_CREATE </font><font color="#000080"><i>; Creates the interrupt processor
</i><b>ENDASM
</b></font><font color="#008000">@ INT_ENABLE RX_INT </font><font color="#000080"><i>; Enable RX232 RX interrupt
</i></font>Begin: <font color="#000080"><i>' Initial adr values are zero.
</i></font>RXFlag = <font color="#FF0000"><b>0
</b></font>AdrW = <font color="#FF0000"><b>0
</b></font>AdrR = <font color="#FF0000"><b>0
</b></font>Start:
<font color="#000080"><b>I2CREAD </b></font>I2C_SDA, I2C_SCL,<font color="#FF0000"><b>$A1</b></font>,AdrR, [<font color="#000080"><b>STR </b></font>Mem\PageSize]
<font color="#000080"><i>' Read one page from EEPROM and store into mem array.
</i></font>AdrR = AdrR + PageSize <font color="#000080"><i>'First page: 0-127
'Second page: 0 +128 = 128 (128 - 255)
'Third page: 128 +128 = 256 (256 - 383)
'....etc..
' Do your stuff ....
</i><b>GOTO </b></font>Start
ReadPort:
<font color="#000080"><b>HSERIN </b></font>[<font color="#000080"><b>STR </b></font>Mem\PageSize]
<font color="#000080"><i>' Store 128-byte (0-127) incoming packet into Mem array.
</i><b>I2CWRITE </b></font>I2C_SDA, I2C_SCL,<font color="#FF0000"><b>$A0</b></font>,AdrW,[<font color="#000080"><b>STR </b></font>Mem\PageSize]
<font color="#000080"><i>' Starting from addresss location = Adr,
' ...write 128-byte mem elements into EEPROM at one shot.
'...Adr is the start address of the page in line.
</i><b>PAUSE </b></font>Writetime <font color="#000080"><i>' For 24C512, 15ms is more then good enough.
' For LC series, 5-6 ms should be ok.
</i></font>AdrW = AdrW + PageSize
<font color="#000080"><i>' For the next page write, increment EEPROM address
' ...with the size of one page.
</i></font>RXFlag = <font color="#FF0000"><b>1 </b></font><font color="#000080"><i>' Set a flag for use in main loop.
</i></font><font color="#008000">@ INT_RETURN
</font>
' May-2008
' Sayzer @ www.picbasic.co.uk/forum
' This code is to show how to use the page write feature of I2C eeproms.
' Page write feature allows us to write an entire page at one shot,
' and takes one write cycle to complete.
' This code will accept 128-byte packet from RS232 port @9600bps,
' and write the entire packet into 24C512 external eeprom at one shot.
' Incoming 128-bytes packets must have at least 15mS intervals.
' Page size is different from eeprom to eeprom.
' For example: Page size for 24C512 is 128 bytes while
' it is 32 bytes for 24LC64.
' That means, we can write 128-bytes into the eeprom
' at once and it takes only approx 5 - 10 ms.
' If we had to do this one byte at a time, we would have to wait
' approx. 1 second to complete.
' 1 second may not sound that long but,
' think about 64,000 byte waiting to be written !!
' PIC18F452
' Set your fuse settings up here.
</i><b>DEFINE </b></font>OSC <font color="#FF0000"><b>20
</b></font><font color="#000080"><i>'Thanks to DT
</i><b>INCLUDE </b></font><font color="#008000"><b>"DT_INTS-18.bas" </b></font><font color="#000080"><i>' Base Interrupt System
</i><b>INCLUDE </b></font><font color="#008000"><b>"ReEnterPBP-18.bas" </b></font><font color="#000080"><i>' Include if using PBP interrupts
'Thanks to Steve.
</i><b>DEFINE </b></font>HSER_RCSTA <font color="#FF0000"><b>90</b></font>h <font color="#000080"><i>' Enable serial port & continuous receive
</i><b>DEFINE </b></font>HSER_TXSTA <font color="#FF0000"><b>24</b></font>h <font color="#000080"><i>' Enable transmit, BRGH = 1
</i><b>DEFINE </b></font>HSER_SPBRG <font color="#FF0000"><b>129 </b></font><font color="#000080"><i>' 9600 Baud @ 0,16%
</i><b>DEFINE </b></font>HSER_CLROERR <font color="#FF0000"><b>1 </b></font><font color="#000080"><i>' Clear overflow automatically
'Thanks to MicroChip.
</i></font>ADCON1 = <font color="#FF0000"><b>7
</b></font>TrisA = <font color="#FF0000"><b>%000000
</b></font>TrisB = <font color="#FF0000"><b>%00000000
</b></font>TrisC = <font color="#FF0000"><b>%10000000 </b></font><font color="#000080"><i>'RC7 => RS232 RX (input pin).
</i></font>TrisD = <font color="#FF0000"><b>%00000000
</b></font>TrisE = <font color="#FF0000"><b>%000
</b></font>PortA = <font color="#FF0000"><b>0
</b></font>PortB = <font color="#FF0000"><b>0
</b></font>PortC = <font color="#FF0000"><b>0
</b></font>PortD = <font color="#FF0000"><b>0
</b></font>PortE = <font color="#FF0000"><b>0
</b></font>I2C_SCL <font color="#000080"><b>VAR </b></font>PortC.<font color="#FF0000"><b>3
</b></font>I2C_SDA <font color="#000080"><b>VAR </b></font>PortC.<font color="#FF0000"><b>4
</b></font>AdrW <font color="#000080"><b>VAR WORD </b><i>' Address variable for write operation.
</i></font>AdrR <font color="#000080"><b>VAR WORD </b><i>' Address variable for read operation.
</i></font>Mem <font color="#000080"><b>VAR BYTE</b></font>[<font color="#FF0000"><b>1350</b></font>] <font color="#000080"><i>' Create an array as large as possible.
</i></font>RXFlag <font color="#000080"><b>VAR BIT </b><i>' Just a flag.
</i></font>Writetime <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>15 </b></font><font color="#000080"><i>' This is the write cycle time for external eeprom. (mS)
</i></font>PageSize <font color="#000080"><b>CON </b></font><font color="#FF0000"><b>128 </b></font><font color="#000080"><i>' Page size for 24C512 : 128 bytes; for 24LC64 : 32 bytes.
'====This is DT's famous and useful Int routine. =========
</i><b>ASM
</b></font><font color="#008000">INT_LIST macro </font><font color="#000080"><i>; IntSource, Label, Type, ResetFlag?
</i></font><font color="#008000">INT_Handler RX_INT, _ReadPort, PBP, yes
endm
INT_CREATE </font><font color="#000080"><i>; Creates the interrupt processor
</i><b>ENDASM
</b></font><font color="#008000">@ INT_ENABLE RX_INT </font><font color="#000080"><i>; Enable RX232 RX interrupt
</i></font>Begin: <font color="#000080"><i>' Initial adr values are zero.
</i></font>RXFlag = <font color="#FF0000"><b>0
</b></font>AdrW = <font color="#FF0000"><b>0
</b></font>AdrR = <font color="#FF0000"><b>0
</b></font>Start:
<font color="#000080"><b>I2CREAD </b></font>I2C_SDA, I2C_SCL,<font color="#FF0000"><b>$A1</b></font>,AdrR, [<font color="#000080"><b>STR </b></font>Mem\PageSize]
<font color="#000080"><i>' Read one page from EEPROM and store into mem array.
</i></font>AdrR = AdrR + PageSize <font color="#000080"><i>'First page: 0-127
'Second page: 0 +128 = 128 (128 - 255)
'Third page: 128 +128 = 256 (256 - 383)
'....etc..
' Do your stuff ....
</i><b>GOTO </b></font>Start
ReadPort:
<font color="#000080"><b>HSERIN </b></font>[<font color="#000080"><b>STR </b></font>Mem\PageSize]
<font color="#000080"><i>' Store 128-byte (0-127) incoming packet into Mem array.
</i><b>I2CWRITE </b></font>I2C_SDA, I2C_SCL,<font color="#FF0000"><b>$A0</b></font>,AdrW,[<font color="#000080"><b>STR </b></font>Mem\PageSize]
<font color="#000080"><i>' Starting from addresss location = Adr,
' ...write 128-byte mem elements into EEPROM at one shot.
'...Adr is the start address of the page in line.
</i><b>PAUSE </b></font>Writetime <font color="#000080"><i>' For 24C512, 15ms is more then good enough.
' For LC series, 5-6 ms should be ok.
</i></font>AdrW = AdrW + PageSize
<font color="#000080"><i>' For the next page write, increment EEPROM address
' ...with the size of one page.
</i></font>RXFlag = <font color="#FF0000"><b>1 </b></font><font color="#000080"><i>' Set a flag for use in main loop.
</i></font><font color="#008000">@ INT_RETURN
</font>