'Code by Shaun Wilson November 01/99
'This is code writes data reads data to 25LC640 64K EEPROM and
'displays it in the debug screen. It will give you in idea of 
'how to read and write data to the EEPROM.  To use it in your 
'specific app just insert the subroutines into you code


'----------------Constants--------------------------------------
WRSR_EERPOM		con   $01   	'write status register
WRITE_EEPROM  		con   $02   	'write data to memory beginning at selected address
READ_EEPROM		con   $03   	'read data from memory beginning at selected adress 
WRDI_EEPROM	  	con   $04  	'Reset the write enable latch
RDSR_EEPROM		con   $05 	'read status register
WREN_EEPROM 		con   $06   	'set the write enable latch
CS			con	0	'chip select 
Serdata			con	1	'serial data in/out	
SerClk			con	2	'Serial clock input

'----------------Varibles---------------------------------------

EEPROM_data		var	byte		'data
Status_register		var	byte		'read status byte
EEPROM_busy		var	Status_register.lowbit	'first bit of the read status
address			var	word		'

'----------------Program--------------------------------

address = 0			'address to start writing data to 
EEPROM_data = $AA		'data to write to EEPROM

main:
debug " Writing ", hex EEPROM_data, " to location ",hex address dig 3,hex address dig 2, hex address dig 1, hex address dig 0, " of the EEPROM", cr
gosub writeEEPROM				'subroutine to write data to EEPROM
if address > 20 then readEEPROM	'only right to the first 20 locations 
address=address+1				'counter to increment the memory address
goto main

'--------------Subroutines-----------------------------


WriteEEPROM:
	gosub EEPROMreadstatus					'check to see if the EEPROM is busy
	if EEPROM_Busy =1 then WriteEEPROM			'if its busy loop until its not
	low CS							'tell EEPROM a transmission is coming 
	shiftout SerData,SerClk,msbfirst,[WREN_EEPROM]  	'tell the EEPROM you want to
									'set the write enable latch
	high CS							'tell EEPROM its done transmitting
	low CS							'tell EEPROM a transmission is coming 
	'Tell the EEPROM you want to write to it, send the 16bit address to write to, 
	'and send a byte of data to write to that location
	shiftout SerData,SerClk,msbfirst,[Write_EEPROM,Address\16,EEPROM_data\8]	
	high CS							'tell EEPROM its done transmitting
return

EEPROMreadstatus:
	low CS								'tell EEPROM a transmission is coming 								
	'send the command to EEPROM that a device wants to read the status register
	shiftout SerData,SerClk,msbfirst,[RDSR_EEPROM]	
	'return the byte containing the status of the read register
      shiftin SerData,SerClk,msbpre,[Status_register\8]											
	EEPROM_busy = Status_register
      high CS								'tell EEPROM its done transmitting								
return	

ReadEEPROM:
	address =0							'set address back to 0
	pause 10
	for address = 0 to 30					'read 30 locations of the EEPROM
	low CS							'tell EEPROM a transmission is coming 
	'send command to EEPROM that a device wants to read data from a memory location
	Shiftout SerData,SerClk,MSBFIRST,[READ_EEPROM,Address\16]
	'returns a byte of data from the EEPROM
	Shiftin SerData,SerClk,MSBPRE,[EEPROM_data\8]
	high CS							'tell EEPROM its done transmitting			
	debug " The data in location ", hex address dig 3,hex address dig 2, hex address dig 1, hex address dig 0, " of the EEPROM is ",hex EEPROM_data, cr
	next 
	gosub done
return

done: 
	debug " Done "
	pause 1000
goto done

    Source: geocities.com/siliconvalley/Orchard/6633

               ( geocities.com/siliconvalley/Orchard)                   ( geocities.com/siliconvalley)