


'**************************************************************************************
'   24FC512 (24XX512*) is a 64K x 8 (512 Kbit
'   the page-write 24XX512    128-byte Page Write mode available
'   the page-write 24XX515    64-byte Page Write mode available
'   capability of the 24LC512 serial memory.  This program
'   will transfer 64 bytes of data to the memory before
'   a pause is needed to complete the write.
'         MagMax con 40 'Max Buffer Size
'         BUTTON  button PORTA
'         usart
'         eeprom externe
'
'
'**************************************************************************************
 
 DEFINE OSC 20                 '20Mhz Oscillator was used
 DEFINE HSER_RCSTA       90h   ' Set receive register to receiver enabled
 DEFINE HSER_TXSTA       24h   ' Set transmit register to transmitter enabled
 DEFINE HSER_BAUD        9600  ' Set baud rate
     
 SCL var PORTA.0               ' Clock pin
 SDA var PORTA.1               ' Data pin
 button  PORTA.2               '
     var PORTA.3               ' Serial output pin 

 'ADCON1 = 7                   ' Set PORTA and PORTE to digital
 'Initialize PORTA             ' 
 TRISA = %1111000              '                    

'**************************************************************************************
   '[MagMax con 40'     Max Buffer Size  
  addr        VAR     WORD                         ' Memory Address
  data_array  VAR     BYTE                         '
  MagMax      VAR     BYTE[40]                     ' Data array with location for checksum
  i           VAR     BYTE                         ' Loop counter

     
'****************************** button ********************************************************
    SO      con     0                               ' Define serial output pin
    B       con     5                               ' Define Button input pin
    N2400   con     4                               ' Set serial mode
    B0      var     byte

    B0 = 0				            ' Zero Button Working Buffer
loop:   Button B,1,10,5,B0,0,notp                   ' Check Button (Skip if Not Pressed)
        Serout SO,N2400,["Press",13,10]             ' Indicate Button Pressed
notp:   Serout SO,N2400,[#B0,13,10]                 ' Show Working Variable
	Pause 100			            ' Visual Pause
        Goto loop                                   ' Foreve
'******************************* I2CWrite *******************************************************
 For addr = 0 To 65535 Step 64                      ' Store $00 to all locations (for testing)
 I2CWrite sda,scl,$A0,addr,[STR MagMax\64]          ' Send a 64-byte page
 HSerout ["."]				            ' Indicate progress           
 Pause 10	                                    ' Pause 10mS to let the write complete
 Next addr

'********************************* I2CRead *****************************************************

' Spot-check the data on the terminal screen
 i = 0                                              '
 Hserout [10,13, 10,13, "checking", 10,13]          '

 For addr = 0 to 65535  Step 257	'?	    ' read every ? th address
 i = i + 1
 I2CRead sda, scl, $A0, addr, [data_array[0]]       ' Read a single byte from memory
 Hserout [HEX2 data_array[0], " "]		    ' To terminal with hex value 
 If (i & $0007) = 0 Then		            ' Break it into organized lines
 Hserout [10,13]			            ' 8 locations to a line
 Endif                                              '

 Next addr				            ' Go get the next test

 End


'**************************************************************************************
' Read and write hardware USART
 B1      var     byte



' Echo received characters in infinite loop

  loop:Gosub charin                       ' Get a character from serial input, if any
  If B1 = 0 Then loop                     ' No character yet
  Gosub charout                           ' Send character to serial output
  Goto loop                               ' Do it forever

' Subroutine to get a character from USART receiver   RECEPTION

  CHARIN: B1 = 0                          ' Preset to no character received
  If PIR1.5 = 1 Then                      ' If receive flag then...
  B1 = RCREG                              ' ...get received character to B1
  Endif
  CIRET:  Return                          ' Go back to caller

' Subroutine to send a character to USART transmitter  TRANSMISSION

  CHAROUT: If PIR1.4 = 0 Then charout     ' Wait for transmit register empty
  TXREG = B1                              ' Send character to transmit register
  Return                                  ' Go back to caller




        
 
