| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| tienchuan 
 
 
 Joined: 25 Aug 2009
 Posts: 175
 
 
 
			      
 
 | 
			
				| Help a code example for read write data eeprom on PIC18F |  
				|  Posted: Sun May 26, 2013 9:00 pm |   |  
				| 
 |  
				| 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
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun May 26, 2013 9:25 pm |   |  
				| 
 |  
				| 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
 
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Sun May 26, 2013 10:00 pm |   |  
				| 
 |  
				|  	  | 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
 
 
 
			    
 
 |  | 
	
		|  | 
	
		| tienchuan 
 
 
 Joined: 25 Aug 2009
 Posts: 175
 
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Sun May 26, 2013 10:34 pm |   |  
				| 
 |  
				| 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
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun May 26, 2013 10:37 pm |   |  
				| 
 |  
				| 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
 
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Sun May 26, 2013 11:00 pm |   |  
				| 
 |  
				| 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
  _________________
 Begin Begin Begin !!!
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon May 27, 2013 5:03 am |   |  
				| 
 |  
				| 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
 
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Tue May 28, 2013 6:02 am |   |  
				| 
 |  
				|  	  | 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
   Thanks u.
 _________________
 Begin Begin Begin !!!
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue May 28, 2013 7:52 am |   |  
				| 
 |  
				| 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
 |  | 
	
		|  | 
	
		|  |