CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Use of eeprom in ccs PIC16F916

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Daniel7



Joined: 22 Nov 2018
Posts: 3

View user's profile Send private message

Use of eeprom in ccs PIC16F916
PostPosted: Thu Nov 22, 2018 12:36 pm     Reply with quote

Hi,
I'm trying to program a PIC16F916 using CCS and i'm having problems with the instructions for use the eeprom.
Reading the ccs manual I thought the following instructions work by themselves
read_eeprom(adress)
write_eeprom (address,value)

But when I try to use them it simply doesn't work, in fact when i add these instructions to my code, the entire program stops working.
Is there any kind of configuration that i need to do before i can use them?
The way i'm using them is "a=read_eeprom(0);" and "write_eeprom(0,a);", where "a" is an int variable i'm using without problem in other parts of the code.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Use of eeprom in ccs PIC16F916
PostPosted: Thu Nov 22, 2018 1:06 pm     Reply with quote

Daniel7 wrote:
But when I try to use them it simply doesn't work, in fact when i add these instructions to my code, the entire program stops working.

How do you know it stops working ? Post a short program that
demonstrates this.

Also post your compiler version.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Nov 22, 2018 1:35 pm     Reply with quote

and (I hope) you are not using the write very often.

You have to understand that EEPROM has a very limited write life.
If you wrote a program that looped calling write_eeprom, it could kill the EEPROM in under a minute. Once damaged, a write might well hang....
Daniel7



Joined: 22 Nov 2018
Posts: 3

View user's profile Send private message

Re: Use of eeprom in ccs PIC16F916
PostPosted: Thu Nov 22, 2018 2:09 pm     Reply with quote

Quote:
Also post your compiler version.


About the version, is it that i'm using "pcw compiler" what you want?

Quote:

How do you know it stops working ? Post a short program that
demonstrates this.


I'm simulating the program with altium. Here is most of the program (it's no very big)

Code:

#include <16f916.h>
#fuses NOPROTECT,NOCPD,NOWDT,XT,BROWNOUT
#use delay (clock=4000000)
#use rs232 (BAUD=9600,XMIT=PIN_C6,RCV=PIN_C7)

#byte port_a=5
#byte port_b=6
#byte port_c=7

int s;
int a=0;
int reset=1;
int g;

#int_rda
Modo_leds()
{s=getc();
if(a<5)
{a++;
reset=2;
}
if(a>=5&reset==1)
{a=0;
}
reset=1;
write_eeprom(0,a);
}

main()
{
set_tris_a(0b00000000);
set_tris_b(0b00000000);
set_tris_c(0b00000001);
 
enable_interrupts (int_rda);
enable_interrupts (global);

a=read_eeprom(1);


After this in the loop i'm doing different out series using the a, b and c ports depending on the value of "a" variable
When i compile the program without using the eeprom instructions it works perfectly, but when i use them, the out ports just stay always the same way, no matter how many times i use the interruption.

Quote:
and (I hope) you are not using the write very often.

I'm trying to use it only when the external interruption, i think that should work for quite a few of them, but correct me if you think it doesn't
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Nov 22, 2018 2:50 pm     Reply with quote

OK.

Huge issue.
Writing the eeprom, inside the RDA interrupt.

Reason. An EEPROM write takes typically 5mSec. INT_RDA at 9600Baud, can occur every 1.04mSec. Now if you are sending with a terminal program for example, and type a character<enter>, the 'enter' will result in three characters being sent one immediately after the other. The UART buffer is just two two characters long, so if this happens it will overflow, since it is hung writing the EEPROM. If it overflows, since you do not have 'ERRORS' in your RS232 setup, the UART will then be permanently hung.....

Repeat five times. When setting up a hardware UART, I _must_ always have 'ERRORS' on the UART setup, unless I'm handling errors myself.

Then move the write out of the interrupt handler. Set a flag, and do the write in your main. Modify your sending method, to ensure that there is always a delay to allow the write to happen in your transmitted data.

Then rethink what you are doing. Currently a byte could be being written every time a character is received. This could kill the EEPROM in just over 10 seconds. You need to only write to the EEPROM at very long intervals. Perhaps at the moment when the chip is switched off, or once a day at the most. Otherwise it _will_ die....
Daniel7



Joined: 22 Nov 2018
Posts: 3

View user's profile Send private message

PostPosted: Thu Nov 22, 2018 4:25 pm     Reply with quote

For now i've configured the timer0 when external interruption is over, and then write the eeprom in the timer interruption. At least it's working this way.

Now I have to rethink what i'm doing to extend the life of the eeprom.

Thank you very much, Ttelmah and PCM programmer, for your help. Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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