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 CCS Technical Support

Help a code example for read write data eeprom on PIC18F

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



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

Help a code example for read write data eeprom on PIC18F
PostPosted: Sun May 26, 2013 9:00 pm     Reply with quote

Dear all
I'll try to write and read a byte into internal eeprom on PIC18F4680
The read data result is 00
There are 2 slave function on my project
1. Write eeprom
Code:

         if (check_link_pc == 1)
         {
            gettime_ds1307 ();
            delay_ms (1) ;
            printf ("Run time: %02X:%02X:%02X\r\n", hour, min,sec) ;
            delay_ms(10);
           
            //-------------------------------------
               write_eeprom(0xF00000,0xaa);
               delay_ms(10);
               write_eeprom(0xF00005,0xbb);
               delay_ms(10);
            }


2. Read eeprom
Code:

         if (check_restore_pc == 1)
         {
               
         printf("Sending....\n");

         unsigned int8 temp_a=read_eeprom(0xF00000);
         delay_ms(10);
         printf("%d",temp_a);
         
         unsigned int8 temp_b=read_eeprom(0xF00005);
         delay_ms(10);
         printf("%d",temp_b);
         check_restore_pc=0;         
         }
      }     


I used CCS C ver 4.140
and further, i want to manager address of eeprom by variable int16 to perform for() function to read data eeprom as fast.
Pls show me way to fix it.
Thanks all.
_________________
Begin Begin Begin !!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun May 26, 2013 9:25 pm     Reply with quote

Download the CCS manual and look
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Look at the examples of code on page 62 (page 74 in the Acrobat
reader). Does it show 0xF00000 as the address ? No. It shows
values starting at 0.

Also, where does the manual say you need to add a 10 ms delay
after the write_eeprom() function ? It doesn't say that.
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Sun May 26, 2013 10:00 pm     Reply with quote

PCM programmer wrote:
Download the CCS manual and look
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Look at the examples of code on page 62 (page 74 in the Acrobat
reader). Does it show 0xF00000 as the address ? No. It shows
values starting at 0.

Also, where does the manual say you need to add a 10 ms delay
after the write_eeprom() function ? It doesn't say that.

Thank you.
I change my code following on this example but the result is no change, is still 00.
Code:

if (check_link_pc == 1)
         {
            gettime_ds1307 ();
            delay_ms (1);
            printf ("Run time: %02X: %2X: %02X\r\n", hour, min, sec) ;
            delay_ms (10) ;
           
            write_eeprom (0x0, 0x0A) ;
            //delay_ms (10) ;
            write_eeprom (0x5, 0x0b) ;
            //delay_ms (10) ;           
            printf ("Saved EEPROM") ;
            //----------------------------------------
            check_link_pc = 0;
         }

         //-----------------------------------------------------------------------------------
         // GOI LAI DU LIEU KHI MAT KET NOI CHO PC
         //-----------------------------------------------------------------------------------
         if (check_restore_pc == 1)
         {
            printf ("Sending....\n") ;
            temp_a = read_eeprom (0x0) ;               
            temp_b = read_eeprom (0x5) ;
           
            //printf (" %d", temp_a) ;
            putc(temp_a);
            //printf (" %d", temp_b) ;
            putc(temp_b);
            check_restore_pc = 0;
         }
      }

Please show me way to fix it.
Thank you.
_________________
Begin Begin Begin !!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun May 26, 2013 10:23 pm     Reply with quote

Run a simple eeprom test program like this:
http://www.ccsinfo.com/forum/viewtopic.php?t=17590&start=1
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Sun May 26, 2013 10:34 pm     Reply with quote

PCM programmer wrote:
Run a simple eeprom test program like this:
http://www.ccsinfo.com/forum/viewtopic.php?t=17590&start=1

thanks PCM
But the result is still 00
The result printf on this terminal screen:
Code:


Wrote 0x55 to eeprom address 0
Read 00

Wrote 0xAA to eeprom address 0
Read 00

Done

_________________
Begin Begin Begin !!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun May 26, 2013 10:37 pm     Reply with quote

Are you testing this in real hardware or is this a simulation ?

I just installed vs. 4.140 and ran the program on an 18F4580 (it's in the
same PIC family as an 18F4680) and it worked perfectly. I ran it on
a PicDem2-Plus board (non-Rohs version, red board).
Quote:

Start

Wrote 0x55 to eeprom address 0
Read 55

Wrote 0xAA to eeprom address 0
Read AA

Done


Here is the test program:
Code:

#include <18F4580.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20M)
#use rs232(baud = 9600, xmit=PIN_C6, rcv = PIN_C7, ERRORS)
//===========================================

void main()
{
printf("Start\n\r\n\r");

write_eeprom(0, 0x55);
printf("Wrote 0x55 to eeprom address 0\n\r");
printf("Read %X\n\r", read_eeprom(0));
printf("\n\r");

write_eeprom(0, 0xAA);
printf("Wrote 0xAA to eeprom address 0\n\r");
printf("Read %X\n\r", read_eeprom(0));

printf("\n\rDone\n\r");

while(1);
}
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Sun May 26, 2013 11:00 pm     Reply with quote

Thanks PCM
I tested on real hardware, I think have a problem with eeprom?
I tried replace new pic and result is ok.
Thanks great Smile
_________________
Begin Begin Begin !!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Mon May 27, 2013 5:03 am     Reply with quote

One obvious comment, is "what sets 'check_link_pc' back to zero"?.
If it remains '1' for any time at all, the EEPROM will be destroyed. Every time you write to the EEPROM cell, you use a 'life'. Since a write takes typically 4mSec, and the cell write life is warranted as 100K cycles, you could potentially kill the EEPROM in just 400 seconds.....

Best Wishes
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Tue May 28, 2013 6:02 am     Reply with quote

Ttelmah wrote:
One obvious comment, is "what sets 'check_link_pc' back to zero"?.
If it remains '1' for any time at all, the EEPROM will be destroyed. Every time you write to the EEPROM cell, you use a 'life'. Since a write takes typically 4mSec, and the cell write life is warranted as 100K cycles, you could potentially kill the EEPROM in just 400 seconds.....

Best Wishes

Thanks u,Ttelmah
I'm writing a first code to test internal eeprom.
Normal, check_link_pc flag is zero, when it controlled by PC, it set to one.
Main Programme will perform task and clear it, so that I think I only work when PC request,normal it don't work as you think Smile
Thanks u.
_________________
Begin Begin Begin !!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Tue May 28, 2013 7:52 am     Reply with quote

Seriously, that is dangerous. The code doing the writing, should clear the flag as soon as the write is done. Otherwise it only takes a little error, for the EEPROM to be killed. Think about it like having a the return spring on the throttle on your car. When you let go, it closes _itself_.

Best Wishes
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