Jump to content
Sign in to follow this  
Rodrigo Valladares P

16f648, Eeprom And Bank Selection

Recommended Posts

hi, (my first post, hope not posted before :D )

 

i need to write eeprom to a 16f648a, apparently there is not a specific routine for that device, so i translate and adapt some found in the web. the datasheet is easy to translate to a c + asm mix, but i'm not shure about the bank selection done in assembler or c, the compiler take care about this and correct it after routine exit?, or what is the better form of doing that?, or any 16f648 specific write/read eeprom code to implement?

 

thanks

 

my adaptation, i can't remember from where i borrowed

void write_flash( unsigned char addr, unsigned char data )
{
unsigned char aux;
   //Write flash
   eeadr = addr;
   eedata = data;

   set_bit( status, RP0 );
   set_bit( eecon1, WREN );

asm
{
lp_disint:
	bcf _intcon, GIE 
	btfsc _intcon, GIE
	goto lp_disint
};
       
   eecon2 = 0x55;
   eecon2 = 0xAA;
   
   set_bit( eecon1, WR );
   
   while(eecon1.WR)
;
   
   clear_bit( eecon1, WREN );
   clear_bit( status, RP0 );
   
delay_us(15);

aux = read_flash(addr);

if (aux != data)
{

	lcd_clear_display();
	lcd_char('x');
	lcd_3_digit(addr);
	lcd_char(' ');
	lcd_char('>');
	lcd_3_digit(data);  // lo original
	lcd_char(' ');
	lcd_char('<');
	lcd_3_digit(aux); // lo leido

	for(;;);

}
}


unsigned char read_flash( unsigned char addr )
{
unsigned char aux;

   set_bit( status, RP0 );

   //Read flash
   eeadr = addr;
   set_bit( eecon1, RD );
   
   aux = eedata;
   
   clear_bit( status, RP0 );
   
   return aux;
   // asm movf _eedata, W
}

Share this post


Link to post
Share on other sites

Rodrigo Valladares P,

 

Leave the bank switching to the compiler and linker, doing it manually will cause problems. Linker tracks the current bank selected through the code, so it added bank switching where necessary. Adding bank switching yourself causes problems with this process - so leave it to the linker :D

 

Regards

Dave

Share this post


Link to post
Share on other sites
Rodrigo Valladares P,

 

Leave the bank switching to the compiler and linker, doing it manually will cause problems. Linker tracks the current bank selected through the code, so it added bank switching where necessary. Adding bank switching yourself causes problems with this process - so leave it to the linker :D

 

Regards

Dave

 

 

I am working on a similar problem...I need to write to the EEPROM in a 16F676. I leave the bank switch to the linker, and it adds and extra bsf instruction in the middle of the required sequence for writing to the EEPROM as defined by Microchip. I have included the disassembly for the code, it's just inline assembly:

 

asm

689: {

690: bsf _status,RP0 ;Bank 1

04A 1283 BCF 0x3, 0x5

04B 1683 BSF 0x3, 0x5

691: bsf _eecon1,WREN ;Enable write

04C 1683 BSF 0x3, 0x5

04D 151C BSF 0x1c, 0x2

692: bcf _intcon,GIE ;Disable INTs

04E 1283 BCF 0x3, 0x5

04F 138B BCF 0xb, 0x7

693: movlw 55h ;Unlock write

050 3055 MOVLW 0x55

694: movwf _eecon2 ;

051 1683 BSF 0x3, 0x5

052 009D MOVWF 0x1d

695: movlw AAh ;

053 30AA MOVLW 0xaa

696: movwf _eecon2 ;

054 009D MOVWF 0x1d

697: bsf _eecon1,WR ;Start the write

055 149C BSF 0x1c, 0x1

698: bsf _intcon,GIE ;Enable INTS

056 1283 BCF 0x3, 0x5

057 178B BSF 0xb, 0x7

699: }

 

I tried turning off optimization for both the linker and the compiler but neither worked in getting rid of this instruction. Also, when I turn off optimization for the linker it makes my code very close to overflowing the ROM. Is there any way to get this to go away, and if so can you let me know about it.

 

Thanks,

Dave

Share this post


Link to post
Share on other sites

In case my last post was hard to understand, which it kind of is when I go back and read it myself. I put in the required sequence to write to EEPROM in the 16F676.

eecon2 = 0x55 ;

eecon2 = 0xAA ;

 

or

 

_asm

{

movlw 0x55

movwf _eecon2

movlw 0xAA

movwf _eecon2

}

 

this is what I get in the disassembly:

 

693: movlw 55h ;Unlock write

050 3055 MOVLW 0x55

694: movwf _eecon2 ;

051 1683 BSF 0x3, 0x5

052 009D MOVWF 0x1d

695: movlw AAh ;

053 30AA MOVLW 0xaa

696: movwf _eecon2 ;

054 009D MOVWF 0x1d

 

I did not write that BSF instruction.

 

First I tried using C, but when I looked at the disassembly I noticed the extra bank switching instruction bsf 0x3, 0x5. So, I used inline assembly with the two movlw's and two movfw's, and it still added the same bsf instruction right in the middle. Like I said, I turned off the optimization on both the linker(I had read in another post that was the source of automatic bank switching) and the compiler and neither of them caused that instruction to go away. We really need to write to the EEPROM for this project, help would be greatly appreciated.

 

Thanks again,

Dave

Share this post


Link to post
Share on other sites

OK, we decided to try something here. We added an extra instruction before the required sequence. We added a movwf _eecon2 right before the movlw 0x55, and that made the bsf move out of right in the middle of the required sequence to right before. I hope this helps anyone else that has a similar issue.

 

Thanks,

Dave

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...