|
|
View previous topic :: View next topic |
Author |
Message |
d00dajo
Joined: 20 Jul 2004 Posts: 34
|
Writing Configuration Bits in PIC18F |
Posted: Thu Nov 23, 2006 11:29 am |
|
|
Hi all,
I'm struggling with writing configuration bits using the table write. Reading the datasheet for the PIC18F4680 it seems to be a rather simple thing to do, but just cant get it right, probably something I'm just leaving out of the equation.
Code: |
val = 0x1A;
TBLPTRU = 0x30;
TBLPTRH = 0x00;
TBLPTRL = 0x02; //Adress CONFIG2L
TABLAT = val; //Load TABLAT with value for CONFIG2L
#asm
TBLWT* ;Table Write
#endasm
bit_set(EECON1,7); //Flash memory select
bit_set(EECON1,6); //Configuration memory select
disable_interrupts(GLOBAL);
bit_set(EECON1,2); //Write Enable.
EECON2 = 0x55;
EECON2 = 0xAA;
bit_set(EECON1,1); //WR = 1
#asm
nop;
#endasm
enable_interrupts(GLOBAL);
restart_wdt();
if(EECON1bits.WRERR)
DEBUGLED = 1;
|
|
|
|
Ttelmah Guest
|
|
Posted: Thu Nov 23, 2006 3:42 pm |
|
|
First comment. Why not just use the write_configuration_memory function?....
Next comment. The memory writes are performed a block at a time (64 bytes). You cannot write a single byte. Look at section 6.5.1 in the data sheet. You need to read the entire block, update just the byte you require, erase the entire block, and then write the whole block back.
Best Wishes |
|
|
d00dajo
Joined: 20 Jul 2004 Posts: 34
|
Re |
Posted: Fri Nov 24, 2006 1:45 am |
|
|
Hi Ttelmah,
Yes, I've actually tried the write_configuration_memory and that it does work. But, it is a bit strange since it writes "count number of bytes from the start of configuration word". So I cant write "only" the config2l with that routine, even though there should be no limitations by the uC. Also, it starts to write at adress 0x300000 which does not exist. Conifuration word begin at 0x300001 in PIC18F. Probably this is ok, but I dont like to use functions that I can't reproduce "manually".
No, actually Configuration memory does not work as the rest of the Flash memory, the programming procedure is similar but not exactly the same. Unlike Flashmemory, the configuration memory is read and written to 1 byte at a time (flash is written to 64 bytes at a time). The Erase Control Bit has no effect in configuration memory space since each byte is automatically erased before being written. (See AN247 at MicroChip, page 5)
I have compared the code generated by write_configuration_memory with my code, and I cant really find a big difference. Except that when writing config location 0x300002 , the function writes first 0x300000, 0x300001 and 0x300002. I have tried the same approach with no success.
Also, writing to position 0x300000 which is not described in the datasheet makes me a bit uncomfortable.
//Daniel. |
|
|
Ttelmah Guest
|
|
Posted: Fri Nov 24, 2006 6:03 am |
|
|
The code memory, has a _smaller_ block size, but otherwise behaves the same as the main memory (with the exception that it automatically erases for those bits that support erase). You need to write the entire block (not just one byte). The block size is two bytes. So you need to either read the whole config register (which loads the existing contents into the temporary buffer), or update both bytes in your operation.
You also reset the watchdog after you finish, but if this is enabled, it is _vital_ that this is cleared before you start the write.
Best Wishes |
|
|
|
|
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
|