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 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 C language looks as follows:
W = EEPROM_Read(ADDRESS);The advantages of C language becomes more obvious, don’t they?
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 BSF EECON1,WREN ; Write to EEPROM enabled BCF INCON,GIE ; All interrupts disabled ;Required Sentence MOVLW 55h MOVWF EECON2 MOVLW AAh MOVWF EECON2 BSF EECON1,WR BSF INTCON,GIE ; Interrupts enabled BCF EECON1,WREN ; Write to EEPROM disabledThe same program sequence written in C language looks as follows:
W = EEPROM_Write(ADDRESS, W);Need a comment? Let's do it in mikroC...
// This example demonstrates the use of EEPROM Library in mikroC PRO for PIC. char ii; // Loop variable void 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; ii < 32; ii++) // Fill data buffer EEPROM_Write(0x80+ii, ii); // Write data to address 0x80+ii EEPROM_Write(0x02,0xAA); // Write some data to EEPROM address 2 EEPROM_Write(0x50,0x55); // Write some data 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; ii < 32; ii++) { // Read 32 bytes block from address 0x80 PORTD = EEPROM_Read(0x80+ii); // and display data on PORTD Delay_ms(250); } }At first glance, it is sufficient to turn the power on to make the microcontroller operate. At first glance, it is sufficient to turn the power off to make it stop operating. Only at first glance... In reality, start and end of operation are critical phases of which a special signal called RESET takes care.