Internal Hardware Resources of PIC Microcontroller 16F84 (VIII)[Copy link]
12 How to use E2PROM In the PIC16F84 microcontroller, in addition to the data memory composed of SRAM that can be directly addressed, there is also an E2PROM data memory that can be electrically erased and written. The E2PROM has a total of 64 bytes, and its address is 00~3FH unit. Since E2PROM has the characteristics of online rewriting and can still retain data after power failure, it can provide convenience for users' special applications. The E2PROM of 16F84 is readable and writable within the entire VDD operating voltage range during normal operation. Under typical circumstances, it can be rewritten 1 million times, and the data retention period is greater than 40 years. The E2PROM of the PIC16F84 microcontroller is not mapped in the register group space, so they cannot be directly addressed by instructions like SRAM general registers, but need to be indirectly addressed through special registers. Therefore, the following four special registers are added to the 16F84 microcontroller, namely EECON1, EECON2, EEDATA, and EEADR, which are specifically used for on-chip operations on E2PROM. In this special register, EEDATA stores 8-bit read/write data, and EEADR stores the address of the E2PROM storage unit being accessed. EECON1 is a control register with only the lower five bits. The upper three bits do not exist and are read as "0". See the table below for details. D7 D6 D5 D4 D3 D2 D1 D0 - - - EEIF WRERR WREN WR RD The control bits RD and WR are used to start read and write operations respectively. These two bits can be set to 1 by software to start read and write operations, but cannot be cleared by software to prevent improper software operations from causing write failures. When the read and write operations are completed, they are automatically cleared by hardware, indicating that no read or write operations are being performed on the E2PROM at this moment. When the WREN bit is set to 1, write operations are allowed, and this bit is cleared to 0 when power is on. In normal operation, once there is a MCLR or WDT reset, the WRERR bit is set to 1, indicating that the write operation is terminated. When the write operation is completed, EEIF is set to 1 (needs to be cleared by software); when the write operation is not completed or has not yet started, EEIF is "0". EECON2 is only a logical register, not a physical register, and will always be zero when read. It only works during a write operation. (1) E2PROM read operation To perform an E2PROM read operation, the following steps need to be performed: 1) Put the unit address of the E2PROM into EEADR. 2) Set RD (D0 bit of EECON) = 1. 3) Read the EEDATA register. Program segment example, read the E2PROM memory data at 25H: … BCF STATUS, RP0; select Bank0 MOVLW 25H MOVWF EEADR; address 25H → EEADR BSF STATUS, RP0; select Bank1 BSF EECON1, RD; start the read operation BCF STATUS, RP0; select Bank0 MOVF EEDATA, W; read the E2PROM data … into the W register (2) E2PROM write operation To perform an E2PROM write operation, perform the following steps: 1) put the E2PROM unit address into EEADR; 2) put the write data into EEDATA; 3) execute a control program segment. For example: to write data 99H to unit 25H of E2PROM, the following program needs to be executed: … BCF STATUS, RP0; send Bank0 MOVLW 25H MOVWF EEADR; address → EEADR MOVLW 99H MOVWF EEDATA; write data → EEDATA BSF STATUS, RP0; select Bank1 BSF EECON1, WREN; write operation function is allowed 1 BCF INTCON, GIE; turn off the general interrupt 2 MOVLW 0x55 3 MOVWF EECON2 4 MOVLW 0xAA 5 MOVWF EECON2; operate EECON2 6 BSF EECON1, WR; start the write operation 7 BSF INTCON, GIE; open the general interrupt ... Note: statements 2 to 6 in the above program must be strictly executed, otherwise the write operation of E2PROM cannot be started. Items 1 to 7 are the operations we recommend users to perform, that is, to turn off all interrupts in the E2PROM write operation sequence to prevent the sequence from being interrupted. In addition, WREN (D2 bit of EECON1) is set to ensure that E2PROM will not be accidentally written, so in normal times, the user program should keep WREN=0 to prohibit write operations. Only when E2PROM needs to be written should WREN=1 be set, and it will be restored to 0 after the writing is completed. Only after the user sets WREN=1 can WREN=1 be set to start the write operation. The WREN bit is automatically cleared after power-on reset. The E2PROM write operation takes about 10ms to complete. The user program can determine whether the E2PROM write operation is completed by querying the status of the WR bit (when WR=0, it means the operation is completed), or using the E2PROM write completion interrupt. If you want to use the interrupt, you should first set EEIF (D6 of INTCON) to 1 to open the interrupt. The E2PROM write completion interrupt flag bit EEIF can only be cleared by software.