View previous topic :: View next topic |
Author |
Message |
cypherff
Joined: 23 Apr 2009 Posts: 4 Location: Montreal,QC,Canada
|
PIC 16F690 reading and writing to EEPROM |
Posted: Thu Apr 23, 2009 5:44 pm |
|
|
As a project I'm making a digital safe that prompts the user to enter a 6 digit password with the the use of an LCD, and numpad.
I added a feature that allows the user to create a new password, as long as he/she remembers the old one. The safe can now be only unlocked with the new password.
The only problem is that every time I power down and power back up, my PIC re-inializes my variables to the default password that I placed in the C+ code.
I would like to know if anyone knows how to write to the EEPROM of the PIC 16F690 so that after powering up again, it retains the new code.
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
cypherff
Joined: 23 Apr 2009 Posts: 4 Location: Montreal,QC,Canada
|
EEPROM addresses for the 16F690 |
Posted: Sun Apr 26, 2009 2:45 pm |
|
|
Thanks for the example codes on how do read and wirte to the EEPROM.
I've read the datasheet of the 16f690 and if im not mistaken, each address can only hold a byte.
My "password" variable is an int32. I could use a function to break it down into 4 bytes then write to 4 different adresses.
In the example you sent me, the user was writing to address 10, I was wondering if you knew 3 more addresses I could write to.
I was also wondering what the line #rom 0x2100 = {0, 1, 2, 3, 4} meant
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 26, 2009 3:41 pm |
|
|
Use this FAQ article as a guide. To write to internal eeprom, just remove
the "ext" from all the function names in this example:
http://www.ccsinfo.com/faq.php?page=write_eeprom_not_byte
If you need detail instructions on how to convert that example to
work with 'int32' variables, read this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=38270&start=1
Quote: | I was also wondering what the line #rom 0x2100 = {0, 1, 2, 3, 4} meant |
It inserts eeprom data into the HEX file at compile-time. The eeprom
will be programmed with those values when you program the PIC
(with your ICD-U40, or ICD2, or other programmer).
The address of 0x2100 is specific to the PIC 16-series devices. |
|
|
cypherff
Joined: 23 Apr 2009 Posts: 4 Location: Montreal,QC,Canada
|
Any address in mind? |
Posted: Sun Apr 26, 2009 7:28 pm |
|
|
In the example you sent me it specified that "n is an offset into the eeprom" and that it was a long int. In guessing that it was some long address the user had given "n" at the start of the code that is missing from the example.
Your link also stated that if I wanted to add another in32, I would need to increment the chosen address by 4. I only have one int 32 to store in the eeprom, if you know of one such address it would be a great help.
You've already helped me a lot. I greatly appreciate it.
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 26, 2009 7:44 pm |
|
|
Quote: |
I only have one int 32 to store in the eeprom, if you know of one such
address it would be a great help. |
Then put it at eeprom address 0. It will use addresses 0 to 3. |
|
|
cypherff
Joined: 23 Apr 2009 Posts: 4 Location: Montreal,QC,Canada
|
Initialization problems |
Posted: Sun Apr 26, 2009 11:47 pm |
|
|
I was able to Read and Write new values of the "password" variable although it did not store it once I powered down and back up. Is it even possible to retain it? Maybe the PIC automatically sets my in32 password to 0 at power up.
Here is my code that reads and writes to the EEPROM. Let me know if I made any mistakes.
Code: | void write_int32_EEPROM(int32 password)
{
int i;
long int y=0;
for (i = 0; i < 4; i++)
write_eeprom(i + y, *((int8*)&password + i) ) ;
}
int32 read_int32_EEPROM(void)
{
int i;
int32 password;
long int y=0;
for (i = 0; i < 4; i++)
*((int8*)&password + i) = read_eeprom(i + y);
return(password);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 26, 2009 11:57 pm |
|
|
You have left out the address parameter in each routine. You have
taken away the general nature of the routines and hard-coded them
to use only address 0. Apparently that's what you want to do.
But if 'y' is always 0 there is no reason to have it in the routines.
i + 0 still = i. You might as well just use 'i' alone. You don't even
need to declare 'y' in each routine.
And then, because these are dedicated routines, the names of the
routines should be changed to something like 'write_password' and
'read_password'. |
|
|
|