|
|
View previous topic :: View next topic |
Author |
Message |
evaradharaj
Joined: 15 Jan 2009 Posts: 60
|
Pic16f676 internal EEPROM reading and writing problem |
Posted: Sat Nov 26, 2011 3:11 am |
|
|
Hi,
I am using PIC16f676 for my project. My project is just switching 4 devices. If powercut happens then, the appliances should retain the old condition as it is before powercut when the board is ON.
So, I decided to use internal EEPROM of the pic controller. I have attached the code below. But some reading and writing problem in EEPROM happens in real time. It is not retaining the old condition as it is before powercut.
Please suggest me how to read and write using internal EEPROM of pic16f676.
Note: The code is working fine in proteus simulation. but in real time only the problem is coming.
#include <16F676.H>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOBROWNOUT, NOPUT
#use delay(clock = 4000000)
void main()
{
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int flag4 = 0;
flag1 = read_eeprom(20);
flag2 = read_eeprom(21);
flag3 = read_eeprom(22);
flag4 = read_eeprom(23);
set_tris_a(0xFF);
set_tris_c(0x01);
output_c(0x00);
while(1)
{
if(input(pin_a0))
{
while(input(pin_a0));
if(flag1 == 0)
flag1 = 0xFF;
else
flag1 = 0;
}
else if(input(pin_a1))
{
while(input(pin_a1));
if(flag2 == 0)
flag2 = 0xFF;
else
flag2 = 0;
}
else if(input(pin_a2))
{
while(input(pin_a2));
if(flag3 == 0)
flag3 = 0xFF;
else
flag3 = 0;
}
else if(input(pin_c0))
{
while(input(pin_c0));
if(flag4 == 0)
flag4 = 0xFF;
else
flag4 = 0;
}
if(flag1 == 0xFF)
{
output_low(pin_c1);
//delay_ms(33);
}
else if(flag1 == 0)
{
output_high(pin_c1);
//delay_ms(33);
}
if(flag2 == 0xFF)
{
output_low(pin_c2);
//delay_ms(33);
}
else if(flag2 == 0)
{
output_high(pin_c2);
//delay_ms(33);
}
if(flag3 == 0xFF)
{
output_low(pin_c3);
//delay_ms(33);
}
else if(flag3 == 0)
{
output_high(pin_c3);
//delay_ms(33);
}
if(flag4 == 0xFF)
{
output_low(pin_c4);
//delay_ms(33);
}
else if(flag4 == 0)
{
output_high(pin_c4);
//delay_ms(33);
}
write_eeprom(20,0);
write_eeprom(21,0);
write_eeprom(22,0);
write_eeprom(23,0);
}
// TODO: USER CODE!!
} _________________ embedding innovation in engineers |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sat Nov 26, 2011 3:45 am |
|
|
First, learn to use the code buttons, and indent. The code is basically unreadable as posted....
What you seem to be trying to do, think again.
Problem is EEPROM life. _every_ time you write to the EEPROM, you use one of it's lives. The EEPROM on the 676, has a warranted lifespan, of just 10000 write/erase cycles (at normal temperatures).
If none of your inputs are true, the chip will loop writing the EEPROM as fast as possible, and will _kill_ the EEPROM in between 20 and 200seconds.
This almost certainly has already happened to your chip.....
As written the code does nothing, reading '0' from the EEPROM when it starts, and continually writing '0' to the EEPROM as it loops. You should be writing the flag1 to flag4 values....
Do a bit of searching here. There are two basic approaches to this:
1) Don't write to the EEPROM, unless something changes. If your changes are quite infrequent, then this solves the problem.
2) Only write when the power is removed!. For this you need some external circuitry to detect power failure (or a PIC with this built in). You ensure you have enough capacitance on the supply rail, that this will stay high enough to write to the EEPROM for the 8mSec needed to write four values. When the power goes off, you write the values then, and only then....
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|