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

PIC18F252 EEPROM read/write
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
TD
Guest







PIC18F252 EEPROM read/write
PostPosted: Sun Oct 23, 2005 9:00 pm     Reply with quote

Hello all, My application requires to write a program to write data ( 4 characters) to the eeprom based on the input from RS232. This is going to be run on the pic microcontroller once to store the values in the eeprom. I am using PIC18F252.

say i want to store 1234 in memory. i enter #AB1234
Code:

do
{
  if(( '#' == buffer[0]) && ( 'A' == buffer[1] ) && ( 'B' == buffer[2] ) )
   {
    C1 = buffer[3];
    C2 = buffer[4];
    C3 = buffer[5];
    C4 = buffer[6];

    write_eeprom(20,CO1);
    write_eeprom(21,CO2);
    write_eeprom(22,CO3);
    write_eeprom(23,CO4);
  }
}
while(true);


and this data has to be stored permanently in the EEPROM. My question is what address in EEPROM do i chose ( i wrote 20-23 to show some values)

I want to download a second program to the pic without erasing the EEPROM data( I am using picstartplus programmer and any directions on this would be helpful) Now this program reads the eeprom data once every powerup for processing

Code:

   v1 = read_eeprom(20) - 0x30;
   v2 = read_eeprom(21) - 0x30;
   v3 = read_eeprom(22) - 0x30;
   v4 = read_eeprom(23) - 0x30;



i am reading from the address locations 20-23. Please let me any suggestions on the whole approach and things i am missing here since I am new to this field.
Ttelmah
Guest







PostPosted: Mon Oct 24, 2005 2:57 am     Reply with quote

For the EEPROM address, whatever you want.
The 'limit' on the value, is the size of the EEPROM on the chip (look at the data sheet), and addresses start at '0'. Nothing else uses the EEPROM, except your code, so provided you are careful to ensure your code allways uses the same addresses, and objects do not overlap, you can put the data anywhere. If you are going to use more than one value stored, then consider using a 'virtual' address, with a define, so something like:
#define TEXT_MESSAGE (20)

and then storing the message to the locations starting at this point.
Be 'wary' of the code you show though. As written, it'll keep writing values to the EEPROM continuously, once the buffer value is seen. This will wear out the EEPROM quite quickly (Each write will take about 2mSec, and the typical EEPROM has a 'life' of about 100K cycles). It could wear the EEPROM out in potentially only about 100 seconds - generally it'll probably last longer because the value oes not change, but it shows an important 'caveat'...

Best Wishes
TD
Guest







eeprom read/write
PostPosted: Mon Oct 24, 2005 1:33 pm     Reply with quote

thanks for your reply.

Ttelmah wrote:

and then storing the message to the locations starting at this point.
Be 'wary' of the code you show though. As written, it'll keep writing values to the EEPROM continuously, once the buffer value is seen. This will wear out the EEPROM quite quickly (Each write will take about 2mSec, and the typical EEPROM has a 'life' of about 100K cycles). It could wear the EEPROM out in potentially only about 100 seconds - generally it'll probably last longer because the value oes not change, but it shows an important 'caveat'...

in my application the buffer string will be entered once so as to write to the eeprom. It will be read from eeprom every power up. hence 100K cycles should be sufficient.

The problem I am facing is preserving the contents of EEPROM when I am reprogramming the pic using picstartplus programmer. As someone suggested I unchecked the Program Data EEPROM but I think I am making a mistake.

Okay this is what I follow. After enabling the programmer > Erase Flash Device > Program for downloading the 1st program

I wrote to the eeprom and checked it as well and it seems to be fine.

Code:

   // would have received the characters to store in data eeprom from   
   // RS 232 input but here i am just showing some character
    write_eeprom(20,'1');
    write_eeprom(21,'5');
    write_eeprom(22,'2');
    write_eeprom(23,'5');

    v1 = read_eeprom(20) - 0x30;
    v2 = read_eeprom(21) - 0x30;
    v3 = read_eeprom(22)- 0x30;
    v4 = read_eeprom(23) - 0x30;

    if( 1 == v1 && 5 == v2 && 2 == v3 && 5 == v4)
       Output_high(Pin_C5);
    else
       Output_high(Pin_C3);
   


Now when I want to reprogram the pic without erasing the eeprom. I compile/run the new code and then Enabled the programmer. Now do I have to click on erase flash device to erase the previos program memory/ config bits ? And then click on Program/ verify and unchecked DATA EEPROM. But still it seems to clear the eeprom data memory. Any suggestions would be very helpful. thanks much
TD
Guest







PostPosted: Mon Oct 24, 2005 1:59 pm     Reply with quote

Quote:

Now do I have to click on erase flash device to erase the previos program memory/ config bits ?


I guess I should not do this while reprogramming the PIC so as to preserve the EEPROM contents ?
Ttelmah
Guest







PostPosted: Mon Oct 24, 2005 2:38 pm     Reply with quote

Dead right.
The 'erase', triggers a _full_ erase. It is essential in some circumstances (the only way of turning off most of the 'protection' fuses), but the programmer is smart enough to know that if you ask it to program the main memory, it has to erase just this first. It should automatically erase what is needed.

Best Wishes
TD
Guest







PostPosted: Tue Oct 25, 2005 3:33 pm     Reply with quote

thanks again. But I am having trouble reprogramming the pic without erasingthe data eeprom.

I downloaded the code (1st program) and eeprom is updated with some values. Now I run the new code and then Enabled the programmer. And when I click on Program/ verify and unchecked data EEPROM and then clicked on Program, I always get a Failure. Is it because I have not erased the previous program memory/ this is the not the right way to do it ? Any suggestions would be very helpful. thanks much
Ttelmah
Guest







PostPosted: Wed Oct 26, 2005 3:02 am     Reply with quote

I think you need to post which programmer is involved. There are so many slight 'variations' in how they work....
On the one I use, there is a seperate 'erase' button, which triggers a full erase, and an option in the programming menu, for 'block erase before programming', which you enable to have it clear the area being programmed.

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Oct 26, 2005 3:11 am     Reply with quote

TD wrote:
thanks again. But I am having trouble reprogramming the pic without erasingthe data eeprom.

I downloaded the code (1st program) and eeprom is updated with some values. Now I run the new code and then Enabled the programmer. And when I click on Program/ verify and unchecked data EEPROM and then clicked on Program, I always get a Failure. Is it because I have not erased the previous program memory/ this is the not the right way to do it ? Any suggestions would be very helpful. thanks much
Which #fuse settings are you using? As Ttelmah already indicated some fuse settings may cause troubles, for example the PROTECT fuse makes it impossible to write to the flash memory (duh, that's why it's called 'protect'). These fuses can only be reset by executing a 'full erase'.
Guest








PostPosted: Wed Oct 26, 2005 9:48 pm     Reply with quote

ckielstra wrote:
TD wrote:
thanks again. But I am having trouble reprogramming the pic without erasingthe data eeprom.

I downloaded the code (1st program) and eeprom is updated with some values. Now I run the new code and then Enabled the programmer. And when I click on Program/ verify and unchecked data EEPROM and then clicked on Program, I always get a Failure. Is it because I have not erased the previous program memory/ this is the not the right way to do it ? Any suggestions would be very helpful. thanks much
Which #fuse settings are you using? As Ttelmah already indicated some fuse settings may cause troubles, for example the PROTECT fuse makes it impossible to write to the flash memory (duh, that's why it's called 'protect'). These fuses can only be reset by executing a 'full erase'.


Okay these are my fuse settings,
#fuses HS,PROTECT,CPD,CPB, PUT,NOWDT,BORV27,NOLVP

I will try doing the necessary changes on them (I know I did not do research on the fuse settings since I am modifying the program written by another person.....lame reason) And I do not have access to the program now but will post the the version details tomorrow. But regards the PROTECT fuse, I ran the 1st program on the PIC and am able to write/read the entered data from the EEPROM. thanks in advance...
TD
Guest







PostPosted: Thu Oct 27, 2005 12:02 am     Reply with quote

Okay I was just glancing through the fuses and found this,
CPD Data EEPROM Code Protected
NOCPD No EE protection
Ttelmah
Guest







PostPosted: Thu Oct 27, 2005 2:22 am     Reply with quote

The big problem though is the 'PROTECT' fuse.
Once this is set, it can only be cleared by a full chip erase...
Basically if the code needs protection, you should be thinking again about how everything is implemented. Remember that if somebody has access to the .hex file, the fuse does no good at all. The way to retain the EEPROM, if the protect fuse is set, and the EEPROM is unprotected, is to first read the EEPROM, then erase the whole chip, and program the new code, and return the EEPROM contents, verify, then program just the protect fuse. Some programmers will allow you to 'script' this sort of sequence.
For development, leave the code protection off, till you have stable code.

Best Wishes
TD
Guest







PostPosted: Thu Oct 27, 2005 5:21 pm     Reply with quote

Thanks a lot. That definitely makes sense. I am going to have to try them out tomorrow.
TD
Guest







still having trouble downloading
PostPosted: Fri Oct 28, 2005 12:15 pm     Reply with quote

Hello, I am still having trouble downloading the program ( it always says failure when i try to program it without erasing the EEPROM). I am using PIC C Compiler IDE v 3.215 PCH v 3.217. I have MPLAB 5.70 with PICStartplus programmer v2.60

Okay this is my first program where I write 2 values to eeprom and then read it succcessfully as well ( I had Starting and Success on my hyperterminal)

Code:


#include <18f252.h>
#use delay(clock = 20000000)
#fuses HS,NOPROTECT,NOCPD,NOCPB,NOWDT,NOBROWNOUT,NOLVP
#use rs232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7)
BYTE v1, v2, v3, v4;

void main()
{
   enable_interrupts(global);
   enable_interrupts(int_rda);

   printf("Starting \r\n");

   write_eeprom(20,'1');
   write_eeprom(21,'5');
   v1 = read_eeprom(20) - 0x30;
   v2 = read_eeprom(21) - 0x30;
   
    if( 1 == v1 && 5 == v2 )  printf("Success \r\n");   
    else  printf(" Failure \r\n");
   
}


And I wrote a test program which i want to download to the PIC without erasing the data EEPROM contents. I just commented the write_eeprom from the previous program and I Click on
PICStartPlus > Enable programmer
PICStartPlus > Program (Unchecked Data EEPROM) and then hit program


Code:

  // same code as above except  I removed write_eeprom so that I can
      read the EEPROM and test if it was retained


But it gives me failure when I program....
TD
Guest







still having trouble downloading
PostPosted: Fri Oct 28, 2005 12:16 pm     Reply with quote

Hello, I am still having trouble downloading the program ( it always says failure when i try to program it without erasing the EEPROM). I am using PIC C Compiler IDE v 3.215 PCH v 3.217. I have MPLAB 5.70 with PICStartplus programmer v2.60

Okay this is my first program where I write 2 values to eeprom and then read it succcessfully as well ( I had Starting and Success on my hyperterminal)

Code:


#include <18f252.h>
#use delay(clock = 20000000)
#fuses HS,NOPROTECT,NOCPD,NOCPB,NOWDT,NOBROWNOUT,NOLVP
#use rs232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7)
BYTE v1, v2, v3, v4;

void main()
{
   enable_interrupts(global);
   enable_interrupts(int_rda);

   printf("Starting \r\n");

   write_eeprom(20,'1');
   write_eeprom(21,'5');
   v1 = read_eeprom(20) - 0x30;
   v2 = read_eeprom(21) - 0x30;
   
    if( 1 == v1 && 5 == v2 )  printf("Success \r\n");   
    else  printf(" Failure \r\n");
   
}


And I wrote a test program which i want to download to the PIC without erasing the data EEPROM contents. I just commented the write_eeprom from the previous program and I Click on
PICStartPlus > Enable programmer
PICStartPlus > Program (Unchecked Data EEPROM) and then hit program


Code:

  // same code as above except  I removed write_eeprom so that I can
      read the EEPROM and test if it was retained


But it gives me failure when I program....
TD
Guest







PostPosted: Fri Oct 28, 2005 12:22 pm     Reply with quote

Sorry for double posting.

In the above post when I said failure, I meant I cannot program the PIC and not the output when I hook up the device... This is the Error Log

    Device Type: 18F252
    Program Memory Errors
    Address Good Bad
    010A: 5037 5035
    010C: 0F17 0F15
    011E: C037 C035
    0124: 2437 2435
    0128: 6E37 6E35
    012C: 5C37 5C35
    0132: F037 F035
    0158: 6A37 6A17
    015A: 6A38 6A18
    015C: 0EC0 0E00
    015E: 12F2 0210
    0160: 8A9D 0A18
    0162: 6A3E 6A1A
    0164: 503E 401C
    0166: DF97 4A15
    016A: 6E00 0E00
    016C: 5000 1000
    016E: A89E 889C
    0170: D7FE 4244
    0172: 6EAD 4004
    0174: 0E0B 0E00
    0176: 5C3E 0804
    0178: E1F5 6000
    017A: 0103 0000
    017C: 915D 801C
    017E: 0E14 0614
    0182: 9CA6 0C02
    0184: 9EA6 1C04
    0186: 80A6 80A4
    0188: 50A8 0000
    018A: B0F2 0000
    018C: 8983 0801
    Any additional errors are not displayed.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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