  | 
	  | 
		 
	 
	
		| View previous topic :: View next topic   | 
	 
	
	
		| Author | 
		Message | 
	 
	
		
			SL1978 Guest
 
 
 
 
  
			
			
			
			
			
			
			
			
			
			
  
		  | 
		
			
				| 18f452 EEPROM not working | 
			 
			
				 Posted: Sun Aug 11, 2002 7:22 pm     | 
				     | 
			 
			
				
  | 
			 
			
				I program my 18f452 with PCH 3.06. I had downloaded The suggested solution form mark and use in my program. Unfortunately it still doesn't work. The EEDATA always hold the last written value when we try to read back the eeprom contain. 
 
Attached is a code of mine, can you kindly consult me where is the problem?
 
 
Thanks & regards, 
 
Law
 
 
///my program///
 
 
#include <18f452.h> 
 
#fuses xt,noprotect,nowdt,put,brownout,nolvp 
 
#use delay(clock=4000000) 
 
#use rs232(xmit=PIN_c6,rcv=PIN_c7,ERRORS)
 
 
//--------------------------- old version ------------------------------------- 
 
#define EEADR 0xFA9 
 
#define EEDATA 0xFA8 
 
#define EECON1 0xFA6 
 
#define RD 0 
 
#define WR 1 
 
#define WREN 2 
 
#define WRERR 3 
 
#define FREE 4 
 
#define CFGS 6 
 
#define EEPGD 7 
 
 
#define EECON2 0xFA7 
 
#define PIR2 0xFA1 
 
#define EEIF 4 
 
 
void writeeeprom(int address, int data) 
 
{ 
 
 
bit_clear(*PIR2, EEIF); // Make sure that the int flag is cleared 
 
(int)*EEADR = address; // Load our address 
 
(int)*EEDATA = data; // Load our data 
 
bit_clear(*EECON1, EEPGD); // Point to data memory 
 
bit_set(*EECON1, WREN); // Enable writes 
 
 
 
// Microchip recommends disabling ints here 
 
disable_interrupts(GLOBAL); 
 
(int)*EECON2 = 0x55; // Write 0x55 
 
(int)*EECON2 = 0xAA; // Write 0xAA 
 
bit_set(*EECON1, WR); // Set WR to begin write 
 
 
// Ok to turn back on ints 
 
enable_interrupts(GLOBAL); 
 
 
// Wait for the write to complete 
 
while (!bit_test(*PIR2, EEIF)); 
 
bit_clear(*EECON1, WREN); // disable writes 
 
bit_clear(*PIR2, EEIF); 
 
} 
 
 
int readeeprom(int address) 
 
{ 
 
 
(int)*EEADR = address; // Load our address 
 
bit_clear(*EECON1, EEPGD); // Point to data memory 
 
bit_set(*EECON1, RD); // EEPROM Read 
 
(int)*EEADR = address; // Load our address 
 
bit_clear(*EECON1, EEPGD); // Point to data memory 
 
bit_set(*EECON1, RD); // EEPROM Read 
 
return(*EEDATA); // Return with our value 
 
} 
 
 
 
main() 
 
{ int8 ctr, ctr2, ctr3,i;
 
 
SET_UART_SPEED(19200); 
 
for(ctr = 100; ctr<=110; ctr++) 
 
{ 
 
ctr2 = ctr - 100 + 1; 
 
writeeeprom(ctr, ctr2); 
 
}
 
 
ctr = 100; 
 
while(1) 
 
{ 
 
putc(13); 
 
ctr3 = readeeprom(ctr); 
 
printf("addr = \%u eep = \%u", ctr,ctr3); 
 
if(ctr<110) 
 
ctr++; 
 
else 
 
ctr=100; 
 
delay_ms(600); 
 
} 
 
} 
 
___________________________
 
This message was ported from CCS's old forum
 
	Original Post ID: 6317 | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Peter H Anderson Guest
 
 
 
 
  
			
			
			
			
			
			
			
			
			
			
  
		  | 
		
			
				| Re: 18f452 EEPROM not working | 
			 
			
				 Posted: Sun Aug 11, 2002 10:28 pm     | 
				     | 
			 
			
				
  | 
			 
			
				I struggled with this for some time and discovered the cfgs bit must be cleared.
 
 
Attached are sample routines which I have recently tested.  Note that I use a header file which defines all SFRs and bit within the SFRs.  However, you should be able to read this.
 
 
Note that when I wrote this CCS had not defined the EE interrupt for the PIC18F.  Thus, I am simply waiting 10 ms for the programming.
 
 
********************
 
 
byte read_data_eeprom(byte adr)
 
{
 
   
 
   cfgs = 0;		// this is critical
 
   EEADR=adr;
 
   eepgd = 0;		// select data EEPROM
 
   rd=1;	// set the read bit
 
   return(EEDATA);
 
}
 
 
void write_data_eeprom(byte adr, byte d)
 
{
 
   byte gie_flag;
 
   cfgs = 0;
 
   eepgd = 0;		// select data EEPROM
 
 
   EEADR = adr;
 
   EEDATA = d;
 
 
#ifdef EE_INT
 
 
   eeif = 0;
 
   eeie = 1;
 
   peie = 1;
 
   ee_int_occ = FALSE;
 
 
#endif
 
 
   if (gie)	// if gie is enabled, turn it off
 
   {
 
	   gie_flag = TRUE;
 
	   while(gie)
 
	   {
 
		   gie = 0;
 
	   }
 
   }
 
   else
 
   {
 
	   gie_flag = FALSE;
 
   }
 
 
   wren = 1;		// write enable
 
   EECON2 = 0x55;	// protection sequence
 
   EECON2 = 0xaa;
 
 
   wr = 1;		// begin programming sequence
 
 
#ifdef EE_INT
 
   gie = 1;
 
   while(!ee_int_occ)
 
   {
 
   }
 
   eeie = 0;
 
   while(gie)
 
   {
 
	   gie = 0;
 
   }
 
#else
 
   delay_ms(10);
 
#endif
 
 
   wren = 0;		// disable write enable
 
   if (gie_flag)
 
   {
 
	   gie = 1;
 
   }
 
   else
 
   {
 
	   while(gie)
 
	   {
 
		   gie = 0;
 
	   }
 
   }
 
}
 
 
Peter H Anderson,  <a href="http://www.phanderson.com," TARGET="_blank">http://www.phanderson.com,</a> pha(at)phanderson.com
 
 
:=I program my 18f452 with PCH 3.06. I had downloaded The suggested solution form mark and use in my program. Unfortunately it still doesn't work. The EEDATA always hold the last written value when we try to read back the eeprom contain. 
 
:=Attached is a code of mine, can you kindly consult me where is the problem?
 
:=
 
:=Thanks & regards, 
 
:=Law
 
:=
 
:=///my program///
 
:=
 
:=#include <18f452.h> 
 
:=#fuses xt,noprotect,nowdt,put,brownout,nolvp 
 
:=#use delay(clock=4000000) 
 
:=#use rs232(xmit=PIN_c6,rcv=PIN_c7,ERRORS)
 
:=
 
:=//--------------------------- old version ------------------------------------- 
 
:=#define EEADR 0xFA9 
 
:=#define EEDATA 0xFA8 
 
:=#define EECON1 0xFA6 
 
:=#define RD 0 
 
:=#define WR 1 
 
:=#define WREN 2 
 
:=#define WRERR 3 
 
:=#define FREE 4 
 
:=#define CFGS 6 
 
:=#define EEPGD 7 
 
:=
 
:=#define EECON2 0xFA7 
 
:=#define PIR2 0xFA1 
 
:=#define EEIF 4 
 
:=
 
:=void writeeeprom(int address, int data) 
 
:={ 
 
:=
 
:=bit_clear(*PIR2, EEIF); // Make sure that the int flag is cleared 
 
:=(int)*EEADR = address; // Load our address 
 
:=(int)*EEDATA = data; // Load our data 
 
:=bit_clear(*EECON1, EEPGD); // Point to data memory 
 
:=bit_set(*EECON1, WREN); // Enable writes 
 
:=
 
:=
 
:=// Microchip recommends disabling ints here 
 
:=disable_interrupts(GLOBAL); 
 
:=(int)*EECON2 = 0x55; // Write 0x55 
 
:=(int)*EECON2 = 0xAA; // Write 0xAA 
 
:=bit_set(*EECON1, WR); // Set WR to begin write 
 
:=
 
:=// Ok to turn back on ints 
 
:=enable_interrupts(GLOBAL); 
 
:=
 
:=// Wait for the write to complete 
 
:=while (!bit_test(*PIR2, EEIF)); 
 
:=bit_clear(*EECON1, WREN); // disable writes 
 
:=bit_clear(*PIR2, EEIF); 
 
:=} 
 
:=
 
:=int readeeprom(int address) 
 
:={ 
 
:=
 
:=(int)*EEADR = address; // Load our address 
 
:=bit_clear(*EECON1, EEPGD); // Point to data memory 
 
:=bit_set(*EECON1, RD); // EEPROM Read 
 
:=(int)*EEADR = address; // Load our address 
 
:=bit_clear(*EECON1, EEPGD); // Point to data memory 
 
:=bit_set(*EECON1, RD); // EEPROM Read 
 
:=return(*EEDATA); // Return with our value 
 
:=} 
 
:=
 
:=
 
:=main() 
 
:={ int8 ctr, ctr2, ctr3,i;
 
:=
 
:=SET_UART_SPEED(19200); 
 
:=for(ctr = 100; ctr<=110; ctr++) 
 
:={ 
 
:=ctr2 = ctr - 100 + 1; 
 
:=writeeeprom(ctr, ctr2); 
 
:=}
 
:=
 
:=ctr = 100; 
 
:=while(1) 
 
:={ 
 
:=putc(13); 
 
:=ctr3 = readeeprom(ctr); 
 
:=printf("addr = \%u eep = \%u", ctr,ctr3); 
 
:=if(ctr<110) 
 
:=ctr++; 
 
:=else 
 
:=ctr=100; 
 
:=delay_ms(600); 
 
:=} 
 
:=} 
 
___________________________
 
This message was ported from CCS's old forum
 
	Original Post ID: 6321 | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Mark
 
 
  Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA 
			
			 
			 
			 
			
			
			
			
			
			
  
		  | 
		
			
				| Re: 18f452 EEPROM not working | 
			 
			
				 Posted: Tue Aug 13, 2002 1:58 pm     | 
				     | 
			 
			
				
  | 
			 
			
				I compiled this with 3.085 and everything seems okay (running on an ice2000).  Here is the lst file.  Note that I commented out the printf since I was not connected to a PC.  Instead, I read the EEPROM and checked the value of ctr3 after each read.
 
 
0000:  GOTO   0084
 
....................  ///my program///  
 
....................   
 
.................... #include <18f452.h>  
 
....................  //////// Standard Header file for the PIC18F452 device ////////////////  
 
.................... #device PIC18F452  
 
.................... #list  
 
....................  
 
.................... #fuses xt,noprotect,nowdt,put,brownout,nolvp   
 
.................... #use delay(clock=4000000)   
 
*
 
0054:  CLRF   FEA
 
0056:  MOVLW  0B
 
0058:  MOVWF  FE9
 
005A:  MOVF   FEF,W
 
005C:  BTFSC  FD8.2
 
005E:  GOTO   0080
 
0062:  MOVLW  01
 
0064:  MOVWF  01
 
0066:  CLRF   00
 
0068:  DECFSZ 00,F
 
006A:  BRA    0068
 
006C:  DECFSZ 01,F
 
006E:  BRA    0066
 
0070:  MOVLW  4A
 
0072:  MOVWF  00
 
0074:  DECFSZ 00,F
 
0076:  BRA    0074
 
0078:  NOP   
 
007A:  NOP   
 
007C:  DECFSZ FEF,F
 
007E:  BRA    0062
 
0080:  GOTO   0102 (RETURN)
 
.................... #use rs232(xmit=PIN_c6,rcv=PIN_c7,ERRORS)  
 
....................   
 
.................... //--------------------------- old version -------------------------------------   
 
.................... #define EEADR 0xFA9   
 
.................... #define EEDATA 0xFA8   
 
.................... #define EECON1 0xFA6   
 
.................... #define RD 0   
 
.................... #define WR 1   
 
.................... #define WREN 2   
 
.................... #define WRERR 3   
 
.................... #define FREE 4   
 
.................... #define CFGS 6   
 
.................... #define EEPGD 7   
 
....................   
 
.................... #define EECON2 0xFA7   
 
.................... #define PIR2 0xFA1   
 
.................... #define EEIF 4   
 
....................   
 
.................... void writeeeprom(int address, int data)   
 
.................... {   
 
....................   
 
.................... bit_clear(*PIR2, EEIF); // Make sure that the int flag is cleared   
 
*
 
0004:  BCF    FA1.4
 
.................... (int)*EEADR = address; // Load our address   
 
0006:  MOVFF  0A,FA9
 
.................... (int)*EEDATA = data; // Load our data   
 
000A:  MOVFF  0B,FA8
 
.................... bit_clear(*EECON1, EEPGD); // Point to data memory   
 
000E:  BCF    FA6.7
 
.................... bit_set(*EECON1, WREN); // Enable writes   
 
0010:  BSF    FA6.2
 
....................   
 
....................   
 
.................... // Microchip recommends disabling ints here   
 
.................... disable_interrupts(GLOBAL);   
 
0012:  BCF    FF2.6
 
0014:  BCF    FF2.7
 
0016:  BTFSC  FF2.7
 
0018:  GOTO   0014
 
.................... (int)*EECON2 = 0x55; // Write 0x55   
 
001C:  MOVLW  55
 
001E:  MOVWF  FA7
 
.................... (int)*EECON2 = 0xAA; // Write 0xAA   
 
0020:  MOVLW  AA
 
0022:  MOVWF  FA7
 
.................... bit_set(*EECON1, WR); // Set WR to begin write   
 
0024:  BSF    FA6.1
 
....................   
 
.................... // Ok to turn back on ints   
 
.................... enable_interrupts(GLOBAL);   
 
0026:  MOVLW  C0
 
0028:  IORWF  FF2,F
 
....................   
 
.................... // Wait for the write to complete   
 
.................... while (!bit_test(*PIR2, EEIF));   
 
002A:  BTFSS  FA1.4
 
002C:  GOTO   002A
 
0030:  GOTO   0034
 
.................... bit_clear(*EECON1, WREN); // disable writes   
 
0034:  BCF    FA6.2
 
.................... bit_clear(*PIR2, EEIF);   
 
0036:  BCF    FA1.4
 
0038:  GOTO   00C2 (RETURN)
 
.................... }   
 
....................   
 
.................... int readeeprom(int address)   
 
.................... {   
 
....................   
 
.................... (int)*EEADR = address; // Load our address   
 
003C:  MOVFF  0A,FA9
 
.................... bit_clear(*EECON1, EEPGD); // Point to data memory   
 
0040:  BCF    FA6.7
 
.................... bit_set(*EECON1, RD); // EEPROM Read   
 
0042:  BSF    FA6.0
 
.................... (int)*EEADR = address; // Load our address   
 
0044:  MOVFF  0A,FA9
 
.................... bit_clear(*EECON1, EEPGD); // Point to data memory   
 
0048:  BCF    FA6.7
 
.................... bit_set(*EECON1, RD); // EEPROM Read   
 
004A:  BSF    FA6.0
 
.................... return(*EEDATA); // Return with our value   
 
004C:  MOVF   FA8,W
 
004E:  MOVWF  01
 
0050:  GOTO   00DE (RETURN)
 
.................... }   
 
....................   
 
....................   
 
.................... main()   
 
.................... { int8 ctr, ctr2, ctr3,i;  
 
*
 
0084:  CLRF   FEA
 
0086:  CLRF   FE9
 
0088:  CLRF   FC1
 
008A:  CLRF   05
 
008C:  MOVLW  19
 
008E:  MOVWF  FAF
 
0090:  MOVLW  26
 
0092:  MOVWF  FAC
 
0094:  MOVLW  90
 
0096:  MOVWF  FAB
 
....................   
 
.................... SET_UART_SPEED(19200);   
 
0098:  MOVLW  0C
 
009A:  MOVWF  FAF
 
009C:  MOVLW  26
 
009E:  MOVWF  FAC
 
.................... for(ctr = 100; ctr<=110; ctr++)   
 
00A0:  MOVLW  64
 
00A2:  MOVWF  06
 
00A4:  MOVF   06,W
 
00A6:  SUBLW  6E
 
00A8:  BTFSS  FD8.0
 
00AA:  GOTO   00C8
 
.................... {   
 
.................... ctr2 = ctr - 100 + 1;   
 
00AE:  MOVLW  64
 
00B0:  SUBWF  06,W
 
00B2:  ADDLW  01
 
00B4:  MOVWF  07
 
.................... writeeeprom(ctr, ctr2);   
 
00B6:  MOVFF  06,0A
 
00BA:  MOVFF  07,0B
 
00BE:  GOTO   0004
 
.................... }  
 
00C2:  INCF   06,F
 
00C4:  GOTO   00A4
 
....................   
 
.................... ctr = 100;   
 
00C8:  MOVLW  64
 
00CA:  MOVWF  06
 
.................... while(1)   
 
.................... {   
 
.................... putc(13);   
 
00CC:  MOVLW  0D
 
00CE:  BTFSS  F9E.4
 
00D0:  GOTO   00CE
 
00D4:  MOVWF  FAD
 
.................... ctr3 = readeeprom(ctr);   
 
00D6:  MOVFF  06,0A
 
00DA:  GOTO   003C
 
00DE:  MOVFF  01,08
 
.................... //printf("addr = \%u eep = \%u", ctr,ctr3);   
 
.................... if(ctr<110)   
 
00E2:  MOVF   06,W
 
00E4:  SUBLW  6D
 
00E6:  BTFSS  FD8.0
 
00E8:  GOTO   00F2
 
.................... ctr++;   
 
00EC:  INCF   06,F
 
.................... else   
 
00EE:  GOTO   00F6
 
.................... ctr=100;   
 
00F2:  MOVLW  64
 
00F4:  MOVWF  06
 
.................... delay_ms(600);   
 
00F6:  MOVLW  03
 
00F8:  MOVWF  0A
 
00FA:  MOVLW  C8
 
00FC:  MOVWF  0B
 
00FE:  GOTO   0054
 
0102:  DECFSZ 0A,F
 
0104:  BRA    00FA
 
.................... }   
 
0106:  GOTO   00CC
 
.................... }   
 
....................   
 
010A:  SLEEP 
 
....................   
 
....................  
 
 
Mark
 
:=I program my 18f452 with PCH 3.06. I had downloaded The suggested solution form mark and use in my program. Unfortunately it still doesn't work. The EEDATA always hold the last written value when we try to read back the eeprom contain. 
 
:=Attached is a code of mine, can you kindly consult me where is the problem?
 
:=
 
:=Thanks & regards, 
 
:=Law
 
:=
 
:=///my program///
 
:=
 
:=#include <18f452.h> 
 
:=#fuses xt,noprotect,nowdt,put,brownout,nolvp 
 
:=#use delay(clock=4000000) 
 
:=#use rs232(xmit=PIN_c6,rcv=PIN_c7,ERRORS)
 
:=
 
:=//--------------------------- old version ------------------------------------- 
 
:=#define EEADR 0xFA9 
 
:=#define EEDATA 0xFA8 
 
:=#define EECON1 0xFA6 
 
:=#define RD 0 
 
:=#define WR 1 
 
:=#define WREN 2 
 
:=#define WRERR 3 
 
:=#define FREE 4 
 
:=#define CFGS 6 
 
:=#define EEPGD 7 
 
:=
 
:=#define EECON2 0xFA7 
 
:=#define PIR2 0xFA1 
 
:=#define EEIF 4 
 
:=
 
:=void writeeeprom(int address, int data) 
 
:={ 
 
:=
 
:=bit_clear(*PIR2, EEIF); // Make sure that the int flag is cleared 
 
:=(int)*EEADR = address; // Load our address 
 
:=(int)*EEDATA = data; // Load our data 
 
:=bit_clear(*EECON1, EEPGD); // Point to data memory 
 
:=bit_set(*EECON1, WREN); // Enable writes 
 
:=
 
:=
 
:=// Microchip recommends disabling ints here 
 
:=disable_interrupts(GLOBAL); 
 
:=(int)*EECON2 = 0x55; // Write 0x55 
 
:=(int)*EECON2 = 0xAA; // Write 0xAA 
 
:=bit_set(*EECON1, WR); // Set WR to begin write 
 
:=
 
:=// Ok to turn back on ints 
 
:=enable_interrupts(GLOBAL); 
 
:=
 
:=// Wait for the write to complete 
 
:=while (!bit_test(*PIR2, EEIF)); 
 
:=bit_clear(*EECON1, WREN); // disable writes 
 
:=bit_clear(*PIR2, EEIF); 
 
:=} 
 
:=
 
:=int readeeprom(int address) 
 
:={ 
 
:=
 
:=(int)*EEADR = address; // Load our address 
 
:=bit_clear(*EECON1, EEPGD); // Point to data memory 
 
:=bit_set(*EECON1, RD); // EEPROM Read 
 
:=(int)*EEADR = address; // Load our address 
 
:=bit_clear(*EECON1, EEPGD); // Point to data memory 
 
:=bit_set(*EECON1, RD); // EEPROM Read 
 
:=return(*EEDATA); // Return with our value 
 
:=} 
 
:=
 
:=
 
:=main() 
 
:={ int8 ctr, ctr2, ctr3,i;
 
:=
 
:=SET_UART_SPEED(19200); 
 
:=for(ctr = 100; ctr<=110; ctr++) 
 
:={ 
 
:=ctr2 = ctr - 100 + 1; 
 
:=writeeeprom(ctr, ctr2); 
 
:=}
 
:=
 
:=ctr = 100; 
 
:=while(1) 
 
:={ 
 
:=putc(13); 
 
:=ctr3 = readeeprom(ctr); 
 
:=printf("addr = \%u eep = \%u", ctr,ctr3); 
 
:=if(ctr<110) 
 
:=ctr++; 
 
:=else 
 
:=ctr=100; 
 
:=delay_ms(600); 
 
:=} 
 
:=} 
 
___________________________
 
This message was ported from CCS's old forum
 
	Original Post ID: 6348 | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Jay Guest
 
 
 
 
  
			
			
			
			
			
			
			
			
			
			
  
		  | 
		
			
				| Re: 18f452 EEPROM not working | 
			 
			
				 Posted: Wed Oct 16, 2002 1:50 am     | 
				     | 
			 
			
				
  | 
			 
			
				I had the same problem: writing nor reading to and from the EEPROM did work. (18f452)
 
 
Now I 'manually' clear the CFGS bit in EECON1 (FA6h)
 
#ASM
 
  bcf EECON1,6	// solve bug
 
#ENDASM
 
 
And it works! 
 
 
BTW: I'm using v3.089 and I think this bug is solved in later updates, but my support has run out.
 
 
Thanks Peter!
 
___________________________
 
This message was ported from CCS's old forum
 
	Original Post ID: 7870 | 
			 
		  | 
	 
	
		  | 
	 
	
		 | 
	 
 
  
	 
	    
	   | 
	
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
  
		 |