PDA

View Full Version : WRITE routine for EEPROM writing



glaugle
- 14th July 2006, 00:56
I tried emailing PBP tech support and I got referred to this forum. Please see my question below:
Greetings,

I am using the PicBasic PRO version 2.46 inside of MicroCode Studio version 2.3.0.0. I use MPASM to assemble. I am developing code for the PIC18F4620 device.

I have noticed in the PBP Help that interrupts should be disabled when using the WRITE command for EEPROM writes. But, I cannot afford to do this, because I am relying on the interrupt service routine to keep pulling characters out of the RCREG whenever they are received. At 19200BPS, I get a new character every 469uS, but the EEPROM write command should take about 4mS. However, I also noticed in the PIC datasheet that the interrupts only need to be turned off for 5 critical instructions to occur prior to actually programming the EEPROM. So, I'd like to modify your WRITE command to do this. Otherwise, I'm going to have to write my own routine in assembly language, and I don't want to bother with that. I want to add 2 lines to your WRITE command, which will disable the GIE, then re-enable it, as shown in the PIC18F4620 datasheet section 7, Example 7-2.

Can you tell me how I can do this? Do I have access to the source code inside of your pre-defined commands? If not, then can you make this change (or have you already) in an updated version?

Any help on this would be greatly appreciated. If you need to know my license information, then I'm sure I can find that, but I don't know where it is at the moment.

Darrel Taylor
- 14th July 2006, 03:29
Hi glaugle,

We don't work for meLabs, but we still like to help out.

You might take a look at this...

EEPROM Variables (EE_Vars.pbp)
http://www.picbasic.co.uk/forum/showthread.php?t=2444

Even if you don't use the whole thing, look at the Write2EE: subroutine at the bottom of the EE_Vars.PBP file.

It makes a pre-defined number of attempts to write to EEPROM, and reads it back each time. If it doesn't read the same thing back, it tries again, otherwise it succeded.

If it doesn't succeed after so many attempts, it turns off the interrupts and does it one last time.

I've tried it with several different interrupt programs, and have never had it actually turn-off the interrupts. It always gets it written within a few tries.

HTH,
  Darrel

glaugle
- 15th July 2006, 01:43
Thanks for your replies everyone, but I've gone ahead and just done it my way. All I really wanted was to disable interrupts during the 5 critical instructions that occur when setting up an EEPROM write, and then I wanted to re-enable them immediately after setting the WR bit, so that interrupts would be active during the 4mS EEPROM write-time. This is okay, and it is described clearly in the PIC datasheet. But, since I never did get access to the guts of the PBP "WRITE" routine, I just wrote my own routine. I called it "WRITE2", and it does what I need. I tested it on my bench for many thousands of EEPROM writes, and it never fails. This is much preferred over the re-try scheme, because the re-try scheme would introduce a lot of non-deterministic timings into my program, which I cannot afford. I am using this for motor control, so timing is critical. Thanks for the effort though.

Cheers.
GREG

Darrel Taylor
- 15th July 2006, 08:03
Thanks for your replies everyoneBy "everyone", I assume you mean me. Possibly a good guess since I'm the only one that replied.
All I really wanted was to disable interrupts during the 5 critical instructions Then, Duh!, disable them...
INTCON.7 = 0
But, since I never did get access to the guts of the PBP "WRITE" routine, Double Duh!, look in your PBP folder for PBPPIC18.lib, and then search for "WRITE"
I just wrote my own routine. I called it "WRITE2", and it does what I need. Of course, we will never see the code.
I tested it on my bench for many thousands of EEPROM writes, and it never fails. This is much preferred over the re-try scheme, because the re-try scheme would introduce a lot of non-deterministic timings into my program, non-deterministic timings, this must have some profound meaning, but just cant find it in the dictionary.
Thanks for the effort thoughOh sure, happy to not help, any time you want.

Bruce
- 15th July 2006, 18:17
At any rate; you're better off making your own version to begin with instead
of modifying a PBP library function.

If you attempt to install most PBP patches, they'll fail if you've modified the
library, and installing the latest PBP upgrade overwrites the library again, so
your approach was probably still the best one in the end anyhow..;o}

As Darrel pointed out, the library is there, and easy enough to modify. Just be
sure you know what you're doing, and make a backup copy before tinkering
with the original library file.

glaugle
- 17th July 2006, 17:00
Darrel, okay, I see that you are the only one who replied to me on this post, but I was originally referred to a different discussion group by PBP Help, and some people over there tried to give some advice too. As for your first "Duh", yes thank you, I knew how to do that, but I couldn't find out where. Your "Double-Duh" response answered that question for me. Thank you. If you read my original question, that is what I was looking for. I just needed to know where to find the libraries. As for my new routine, it's nothing special, and the reason I didn't post it is that anything I write is company-confidential, no matter how small or simple. As for "non-deterministic", it basically means that the code has the ability to do different things on different runs. For example, if I used a retry scheme for the EEPROM, then on one run, it may be able to write the EEPROM on the first try, but on the next run, it may not do it until the second try. Well, then the second run would be skewed by about 4mS longer than the first. This makes digital filters and controls very hard to manage.

Anyway, you have helped me. In the future, if I need to hack a PBP library, now I know where they hide them. And, as Bruce pointed out, I'm probably better off leaving them alone, since an upgrade to PBP would wipe out my changes, in case I forget what I've done.