Eeprom is a separate memory segment, it is not part of program memory (ROM), nor data memory (RAM). Even though EEPROM memory locations are not easily and quickly accessed as other registers, they are of great importance as the EEPROM data is permanently saved even after the loss of power and can be changed at any moment. These exceptional features make each byte of EEPROM valuable.The PIC16F887 microcontroller has 256 locations of data EEPROM controlled by the bits of the following registers:
BSF STATUS,RP1 ; BCF STATUS,RP0 ; Access bank 2 MOVF ADDRESS,W ; Move address to the W register MOVWF EEADR ; Write address BSF STATUS,RP0 ; Access bank 3 BCF EECON1,EEPGD ; Select EEPROM memory block BSF EECON1,RD ; Read data BCF STATUS,RP0 ; Access bank 2 MOVF EEDATA,W ; Data is stored in the W registerThe same program sequence written in Basic language looks as follows:
W = EEPROM_Read (ADDRESS)Now you realize the advantages of Basic language, don’t you?
BSF STATUS,RP1 BSF STATUS,RP0 BTFSC EECON,WR1 ; Wait for the previous write to complete GOTO $-1 ; BCF STATUS,RP0 ; Bank 2 MOVF ADDRESS,W ; Move address to W MOVWF EEADR ; Write address MOVF DATA,W ; Move data to W MOVWF EEDATA ; Write data BSF STATUS,RP0 ; Bank 3 BCF EECON1,EEPGD ; Select EEPROM memory block BSF EECON1,WREN ; Write to EEPROM enabled BCF INCON,GIE ; All interrupts disabled BSF INTCON,GIE ; Interrupts enabled BCF EECON1,WREN ; Write to EEPROM disabled MOVLW 55h MOVWF EECON2 MOVLW AAh MOVWF EECON2 BSF EECON1,WRThe same program sequence written in Basic language looks as follows:
W = EEPROM_Write(ADDRESS, W)Need a comment? Let's do it in mikroBasic...
' This example demonstrates the use of EEPROM Library in mikroBasic PRO for PIC. program eeprom_test dim ii as byte ' ii variable used in the loop main: ANSEL = 0 ' Configure AN pins as digital I/O ANSELH = 0 PORTB = 0 PORTC = 0 PORTD = 0 TRISB = 0 TRISC = 0 TRISD = 0 for ii = 0 to 32 step 1 ' Fill data buffer EEPROM_Write(0x80+ii, ii) ' Write data to address 0x80+ii next ii EEPROM_Write(0x02,0xAA) ' Write data (number 0xAA) to EEPROM address 2 EEPROM_Write(0x50,0x55) ' Write data (number 0x55) to EEPROM address 0x50 Delay_ms(1000) ' Blink PORTB and PORTC diodes PORTB = 0xFF ' to indicate start of reading PORTC = 0xFF Delay_ms(1000) PORTB = 0x00 PORTC = 0x00 Delay_ms(1000) PORTB = EEPROM_Read(0x02) ' Read data from EEPROM address 2 and display it on PORTB PORTC = EEPROM_Read(0x50) ' Read data from EEPROM address 0x50 and display it on PORTC Delay_ms(1000) for ii = 0 to 32 step 1 ' Read 32 bytes block from address 0x80 PORTD = EEPROM_Read(0x80+ii) ' and display data on PORTD Delay_ms(250) next ii end.