|
|
View previous topic :: View next topic |
Author |
Message |
vino182k6
Joined: 27 Jun 2012 Posts: 6
|
PIC24F Program Memory Write in ASM |
Posted: Thu Oct 04, 2012 6:24 am |
|
|
HI All
am working on a Project in PIC24FV32KA304 with CCS PCD Compiler Version 4.130.
I am trying to implement Program memory Write in CCS with ASM Coding.
Tried all Combinations but still am getting error after PM Write sequence (WRERR bit set in NVMCON register)
given the simple code for just writing some constant data in a Particular address in Program Memory.
Please guide where am wrong or any other Idea to do this.
Can anyone tell me what is wrong with this code.?? am trying it for the Whole day. and am not getting any clue.
Thanks
Vino
Code: |
//Fuse Settings
#include <24F32KA304.h>
#device ICD=TRUE
#FUSES WPOSTS1 //Watch Dog Timer PostScalar 1:1
#FUSES FRC //Internal Fast RC Oscillator
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES NOWINDIS //Watch Dog Timer in Window mode
#FUSES WDT_NOSL //Watch Dog Timer, disabled during SLEEP
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPUT //No Power Up Timer
#FUSES DEBUG //Debug mode for use with ICD
#FUSES ICSP3 //ICD uses PGC3/PGD3 pins
#FUSES NODSBOR //BOR disabled in Deep Sleep
#FUSES NODSWDT //Deep Sleep Watchdog Timer disabled
#use delay(clock=8000000)
#use i2c(I2C2,slave,SCL=PIN_B3,SDA=PIN_B2,ADDRESS=0XBC)
//Variables Used in this Function
long NVMCON,NVMKEY,PSVPAG,TBLPAG,W0,W1,W2,W3,W4;
int1 NVMCON_WR;
#locate NVMCON = 0X0760
#bit NVMCON_WR = 0X0760.15
#locate NVMKEY = 0X0766
#locate TBLPAG = 0X0032
#locate W0 = 0X0000
#locate W1 = 0X0002
#locate W2 = 0X0004
#locate W3 = 0X0006
#locate W4 = 0X0008
/// The Function "Write_PM_Word" is called from Main Program through an I2C Interrupt
void Write_PM_Word(void)
{
TBLPAG = 0X0000;
W0 = 0X07C0; //PM Address to write 0X0007C0
W1 = 0X1AA5; //Constant word L to write into that Addr
W2 = 0X0000; //Constant word M to write into that Addr
bit_set(CORCON,3); //Set CPU in High Priority Mode
#asm
tblwtl W1,[W0] //Write to the PM Write Latch lower Word
tblwth W2,[W0]
MOV #0X4008,W3 //set the Write Mode in NVMCON
MOV W3,NVMCON
MOV #0X0055,W3
MOV W3,NVMKEY
MOV #0X00AA,W4
MOV W4,NVMKEY
BSET NVMCON,#15 //Set the WR bit to start Write
#endasm
while(NVMCON_WR);
bit_clear(CORCON,3); //Reset CPU from High Priority Mode
}
|
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Oct 04, 2012 8:04 am |
|
|
I don't think that #0X4008 corresponds to a valid write mode for this processor.
According to the datasheet, CORCON bit 3 can't be set. You'll either want to set IPL to level 7 or use a disi instruction as suggested in the datasheet. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1353
|
|
Posted: Thu Oct 04, 2012 11:45 am |
|
|
Something else to consider. On the PIC24 parts, there are sometimes some timing considerations when implementing unlock sequences. Try changing your unlock sequence from:
Code: |
MOV #0X0055,W3
MOV W3,NVMKEY
MOV #0X00AA,W4
MOV W4,NVMKEY
|
To:
Code: |
MOV #0X0055,W3
MOV #0X00AA,W4
MOV W3,NVMKEY
MOV W4,NVMKEY
|
I know that seems like a small change, but it can sometimes make a difference.
As FvM suggested, I would also look at the DISI instuction. It is what the CCS functions use and is pretty handy for this type of scenario. |
|
|
vino182k6
Joined: 27 Jun 2012 Posts: 6
|
|
Posted: Thu Oct 04, 2012 9:33 pm |
|
|
Hi FvM /Jeremiah
Thanks for your valid time.
After FvM's suggestion, I checked again in the datasheet. I found 2 different definitions for 1 word write mode selection in the NVMCON register. (One under program memory and another under eeprom memory). I followed
register description under eeprom memory (Microchip -ds39995c-page 64). Sorry, I don't know how to add the image.
Thats the problem. Now I modified it, working good. (to #4004 from # 4008).
Between, how/where to use "Disi #5" instruction in my code. A bit confused. Should use inside the #asm or in c ?
Thank you so much for your help. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Oct 05, 2012 1:59 am |
|
|
DISI #5 is an ASM instruction. |
|
|
|
|
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
|