PDA

View Full Version : can a pic remember a variable?



peterdeco1
- 9th January 2005, 11:08
Hello Everyone. I have written a program that works perfectly - until you turn the power off! I need the pic to "remember" a variable and load it upon power-up. I'm using a 16F72 without eeprom. In the main program the variable is Y and I use statements such as "let Y = 036". In my program, Y is never larger than 36. When you power off & power on the circuit, Y goes to a high number (255?) and the circuit does what it should if Y is 36 or greater. If Y = 001 or 005, I need it to be this number upon power-up. Does anyone have a solution without changing the pic to a different device? Thank you.

mister_e
- 9th January 2005, 11:40
use WRITECODE and READCODE
http://www.picbasic.co.uk/forum/showthread.php?threadid=137&highlight=readcode

Bruce
- 9th January 2005, 17:59
> When you power off & power on the circuit, Y goes to a high
> number (255?)

GPRs (general purpose registers) are not initialized by a power-on reset, so there's no telling what value a GPR (or your variable) will hold on power-up. This is volatile memory, and will lose whatever value it holds when power is removed.

> If Y = 001 or 005, I need it to be this number upon power-up

Your only option is some type of non-volatile memory.

As Steve mentioned, you can use WRITECODE/READCODE, but be careful using this. You can wear out FLASH program code memory pretty fast. Look in your datasheet for the typical erase/write cycle specifications for FLASH program memory. It's very limited.

If your variable changes only "1" time each second, and you're writing this new value to FLASH program code space, you'll have 86,400 writes in a single 24 hour period. You can wear out program code memory or EEPROM pretty fast depending on how often your variable changes.

Ramtron offers a line of high endurance FRAM memory ICs. These offer around 10 billion read/write cycles, and NoDelay write times, but you'll still want to limit your write frequency to some order of magnitude that doesn't wear out your non-volatile memory during the expected life of the product/project.

peterdeco1
- 11th January 2005, 08:18
Thank you Steve. Thank you Bruce. Writecode doesn't work on the 'F72 I'm using - it generates many errors. If I compile it with an 'F872, it compiles fine but I am trying to avoid using a different pic. The datasheet says the 'F72 does not have eeprom. Is this why Writecode doesn't compile? The pic is addressing a voice recorder chip ISD5008. If a person records sequential messages, it counts the number of messages recorded and will playback only that number and then returns to the first message. I don't believe write/rewrite cycles is an issue for wearing out the pic. Right now as a temporary fix, I'm leaving the circuit powered-up and the power switch simply cuts off the speaker. However, if I do need to use Writecode, I have a couple of questions: is the 'F72 capable of Writecode (no eeprom)? and if I have to change pics to an 'F872, what are valid addresses I can use? For example, I am using about 1/2 of the program memory in the pic. What is a valid address for Writecode that is at the bottom where there is alot of empty space? Thank you. - Peter

mister_e
- 11th January 2005, 15:50
WRITECODE don't use the internal EEPROM. It use you code space memory. If you want to access to a valid memory space, Use MPLAB to start a new project with project wizard, compile it. Go to 'View' menu, get the 'PROGRAM MEMORY', scroll it down untill you'll get free code space. I suggest to use some close to the end. For an F72, where the last memory address is $07FF, use something like $07F0 can do something... i guess. I've never play with this statement at this point.

I'm also playing with those ISD chipcorder too(ISD4002 for me). IMO it's a good idea to save the message count but you can also do it when the power comes up. I'm also at this pont. I think we can do some 'message cueing' and test the interrupt message comming out of ISD chip. Increment count until you will get 'OVF' bit enable.

Bruce
- 11th January 2005, 16:16
Hi Peter,

Data EEPROM memory and FLASH program memory are both EEPROM, and unfortunately not all PICs have the ability to write to their own FLASH program memory space.

Most of the 16Fxxx & 18Fxxx parts allow writing to program FLASH. The best way to find out is by looking over the datasheet.

If the datasheet for the PIC you're using doesn't show EEPROM control registers like EECON1, EECON2, etc.., then you can't use WRITECODE or WRITE, and attempting to use these commands returns the error you received.

WRITECODE is used to write to FLASH "EEPROM" program code space. WRITE is used to write to Data EEPROM.

> and if I have to change pics to an 'F872, what are valid
> addresses I can use?

Pretty much anywhere as long as you're not going to over-write your own program, and if you're going to use WRITECODE, then I highly recommend you read this app note by Melanie: http://www.picbasic.co.uk/forum/showthread.php?s=&threadid=137

mister_e
- 11th January 2005, 16:39
That's point out again Bruce. Since i've never never use that WRITECODE it answer to some of my assumption then.

Peter, if you have any extra i/o and you dont want to change PIC, you can also use a i/o to monitor your supply line. If there's any failure, use an little 3volt battery backup to supply only the PIC. When you'll get any power failure drop the PIC into SLEEP mode an wait for power comes up. That could be a easy solution here.

peterdeco1
- 14th January 2005, 10:28
Hi Bruce. Hi Steve. Thanks for your help but I cannot compile an 'F72 using WRITECODE. Just generates errors. However, I did have success using READ and WRITE and changing the pic to an 'F872. Everytime variable Y changes, the following line is: WRITE 3,Y. Upon power-up, at the top of my program is: READ 3,Y. It works perfectly - even if you remove the pic & reinstall it the next day! Now, all I have to do if figure out where is 3. I chose location 3 at random. I know...I know...READ THE DATASHEET! That's the first thing Melanie would say. By the way, where is Melanie? I haven't seen any posts by her in a while. - Thanks again. - Peter

mister_e
- 14th January 2005, 15:11
hi Peter,

Now, all I have to do if figure out where is 3. I chose location 3 at random.

whatever the location you choose, it refer to a specific location of the INTERNAL EEPROM. So, 0 is the first location of the EEPROM. It's a separate location of your codespace. If you'r using MPLAB, you can read PROGRAM MEMORY, EEPROM.

You can maybe consider EEPROM as an other internal register... only to figure out.

peterdeco1
- 15th January 2005, 10:53
Thanks Steve. I have the 16F872 datasheet but still have questions. How many eeprom locations does the 16F872 have? I can't find the answer in the datasheet. Thank you. - Peter

NavMicroSystems
- 15th January 2005, 22:55
Peter,

according to the datasheet the the 16F872 has 64Bytes of EEPROM, means 64 "locations"

Decimal 0 to 63

HEX $00 to $3F

peterdeco1
- 18th January 2005, 09:50
Thank you Ralph. Now I understand that I can make the pic "remember" 64 things if necessary. By the way, I just read your post about overclocking a pic. We have been doing it at work for 2 years. We run a 4MHZ pic with an 8MHZ resonator and never had any problems. The circuit is low-volume - only about 5 or 10 pieces per week, so that's not to say there will never be a problem. Thank you. - Peter