CAN'T WRITE EEPROM ON P16F628A. PLS HELP - Page 1
Forum section: electronics / general

author: quaternion00
author_rank: Contributor
timestamp: February 03, 2019, 02:11:41 am
content: I can't write to EEPROM of 16f628a. the code is in assembly. I hope you guys can help.thanks in advance.

author: Ian.M
author_rank: Super Contributor
timestamp: February 03, 2019, 05:03:46 am
content: In future, when posting code please useSMF BBCode code tags, or attach it as a text file.There are two non-obvious things wrong with that code, There is no delay after startup or wait for a user button press before starting to write to EEPROM, and there is nothing to stop the processor at the end of the program. (END is a MPASM directive, it doesn't generate code.) It has 2048 instruction words so if running at 1 MIPS from a 4MHz crystal, it will blow through the unprogrammed instruction locations in about 2 ms, then execution will wrap back to the start. As writing to EEPROM typically takes 4ms, worst case 8ms there's a less than 33% chance you'll cut power after the write has finished, before it repeats it, and even lower odds at higher crystal frequencies. If you interrupt a write that's in progress the usual result is failure to program, either unprogramed 0xFF or corrupted data.Additionally, the programmer typially briefly powers the chip and releases /MCLR during the process of identifying it. If you don't have a short delay after powerup (1/4 sec will do), or wait for user intervention on a non-ICSP pin, its fairly certain that it will run your code as far as the EEPROM write when you try to read the EEPROM contents, and interrupt the write by raising /MCLR to Vpp before it can complete, leaving the EEPROM location unprogrammed or corrupted.

author: quaternion00
author_rank: Contributor
timestamp: February 03, 2019, 05:25:29 am
content: Thanks for the reply,Sir. Is the EELOOP not enough to ensure the program waits until the write is done?

author: Ian.M
author_rank: Super Contributor
timestamp: February 03, 2019, 05:49:27 am
content: That loop waits till the write is done but you need to stop the execution continuing past the END directive and wrapping back to the start.The simplest fix is adding an endless loop before the end directive.Code:halt: GOTO haltAs the min EEPROM write endurance is only 100K write cycles (typ. 1M) and, as is, it takes about 6ms to write and wrap back to the start at 4MHz clock speed, your code could have damaged that EEPROM location in as little as ten minutes of power-on time. After 100 minutes, its even odds that location has failed. The times are shorter at higher clock speeds, dropping to 7 1/2 minutes and 73 minutes respectively at 20MHz. Therefore you should either change the address or use a brand new PIC once you fix your code. (If you know how to edit EEPROM memory in the IPE or 3rd party programmer software, you could also test the location and if its OK carry on using the same PIC and address.)Don't forget to add a delay after startup or enable /RBPU and wait for a pin on port B (not RB6,7 the ICSP pins) to go low before starting the write to avoid the programmer corrupting the EEPROM when you attempt to read it.

author: quaternion00
author_rank: Contributor
timestamp: February 03, 2019, 12:06:53 pm
content: I added halt before the END and it worked. I checked the EEPROM if it's damage, glad that its not.I'm wondering how the mikroC eeprom library write to EEPROM, now i know. It's exciting to know the assembly language behindlibrary codes . Thanks for your time Sir.