This is a brief tutorial on how to write to EEPROM in the PIC 16 series and i'll limit this tutorial on how to write to EEPROM of PIC16F877a .
as an intro to the subject think of the EEPROM inside the PIC as a hardware storage element that saves constant values that you could use later ( in case of our project it's the tracks that we want to save inside) the following things should be considered while doing so :
-make a lookup table for the tracks and make sure it is addressed correctly .
-the (writing to EEPROM) asm file is seperated from the main project asm file ,which means that you create 2 asm files ,one for writing tracks to the EEPROM and the other is the original game and all of it playable code .
-read the datasheet very well to understand that there are 2 important SFR that you'll deal with while writing/reading to/from EEPROM :
1-the EEDATA SFR which contains the data that will be written to the EEPROM in case of writing to the EEPROM and will contain the data to be read when reading from it .
2-the EEADR which contains which address in the EEPROM we will be writing to,or reading from through our code .in the case of 16F877a we have (256 locations available)
now that we know these important SFR ,there's a standard extra lines of instructions is required (which we don't have to understand of even think of ) which are the following :
bcf EECON1,EEPGD ; points to data memory
bsf EECON1,WREN ; enable write
movwf EECON2 ; init write sequence
movlw 0xAA ; using 2 instructions
bsf EECON1,WR ; write data
you use this sequence after you already put the correct value of both the address of the location in EEPROM to be used (again the address in EEADR) and the correct data itself from the lookup table (again put data in EEDATA) .
one more step which am not sure if it's a necessity but it worked with me (the main idea of it is to hold the program flow until we're sure that the data was perfectly written to the EEPROM ) :
and finally disable writing to EEPROM using : bcf EECON1,WREN ; disables write
-just remember to adjust both EEADR and EEDATA each time you want to write new values to EEPROM (you could use increment procedure for both , find your way through it )
-As for reading from the EEPROM it's an easy similar sequence just remember again to adjust the value of EEADR each time you read a certain value , and the data that u had just read is located in the SFR EEDATA . this is a brief sequence :
BCF EECON1,EEPGD ; Point to Data
BSF EECON1,RD ; EE Read
BTFSC EECON1,RD ;read confirmation
BCF STATUS,RP0 ; Bank 2
MOVF EEDATA,W ; W = EEDATA
-be always sharp and remind yourself of what BANK you're using ,because you'll be moving back and forth in the memory banks . and when you're done with EEPROM reading remember to get back to Bank 0 .
i guess this is the main procedure for both processes (writing and reading) comment if you have any problems and i'll try to help ,and correct me if i made any mistake in this tutorial .
good luck to all of you guys ...............