??? 10/12/06 03:48 Read: times |
#126263 - IAP code example... Responding to: ???'s previous message |
Hello Arvind,
I am cutting and pasting some code snippets which I have used to store and recover passwords and other parameters. You will have to adapt this to the P89V51RD2, if there are any changes, mostly they may be minor. If I remember correctly, the orgiginal sources are from Flash Magic website and Philips application notes. Flash Magic website has wealth of information on IAP. The code may not be complete. Place each of them in different souces files. ;=========================================================================== ; 05/06/2002 - Modular 'C' SOURCE CODE for various products ;=========================================================================== ; MODULE : This is the flash asm module ; FILENAME : flash.a51 ; PROGRAMMER : John D. Maniraj ; HISTORY : 05/07/2004 ; DISCRIPTION : This code will read and write to flash ; HARDWARE : Worked on 06/07/2004 ; uCONTROLLER : 89C51RD2BN ;=========================================================================== $NOMOD51 ; module name NAME FLASHA ; segments in this library ?PR?_WriteFlashByte?FLASHA SEGMENT CODE ?PR?_ReadFlashByte?FLASHA SEGMENT CODE ; function names and global variables PUBLIC _WriteFlashByte PUBLIC _ReadFlashByte ; SFRs and sbits required DPH DATA 083H DPL DATA 082H AUXR1 DATA 0A2H EA BIT 0AFH CMOD DATA 0D9H ACC DATA 0E0H IE DATA 0A8H ;============================================================================ ;write flash byte ;============================================================================ RSEG ?PR?_WriteFlashByte?FLASHA _WriteFlashByte: PUSH IE ; save interrupts CLR EA ; disable interrupts MOV A,CMOD MOV R2,A ; store copy of CMOD JNB ACC.6,?IAPTAG11 ; if watchdog enabled then disable ANL CMOD,#0BFH ?IAPTAG11: ORL AUXR1,#020H ; enable bootrom MOV R0,#20 ; osc frequency 20MHz MOV R1,#02H MOV DPH,R4 ; address to program MOV DPL,R5 MOV A,R7 ; data to write CALL 0FFF0H ; call iap routine MOV R7,A ; id in accumulator ANL AUXR1,#0DFH ; disable bootrom MOV CMOD,R2 ; restore CMOD (restore watchdog state) POP IE ; restore interrupts to initial state RET ;============================================================================ ;read flash byte ;============================================================================ RSEG ?PR?_ReadFlashByte?FLASHA _ReadFlashByte: PUSH IE ; disable interrupts CLR EA MOV A,CMOD MOV R2,A ; store copy of CMOD JNB ACC.6,?IAPTAG12 ; if watchdog enabled then disable ANL CMOD,#0BFH ?IAPTAG12: ORL AUXR1,#020H ; enable bootrom MOV R0,#20 ; osc frequency 20MHz MOV R1,#03H MOV DPH,R6 ; address to program MOV DPL,R7 CALL 0FFF0H ; call iap routine MOV R7,A ; id in accumulator ANL AUXR1,#0DFH ; disable bootrom MOV CMOD,R2 ; restore CMOD (restore watchdog state) POP IE ; restore interrupts to initial state RET END ;============================================================================ //=========================================================================== // 05/06/2002 - Modular 'C' SOURCE CODE for various products //=========================================================================== // MODULE : This is the flash module // FILENAME : flash.c // PROGRAMMER : John D. Maniraj // HISTORY : 05/07/2004 // DISCRIPTION : This code will read and write to flash // HARDWARE : // uCONTROLLER : 89C51RD2BN,89C669 //=========================================================================== #define _FLASH_ // this module //=========================================================================== extern Byte WriteFlashByte(Byte FlashData, Word FlashAddress); extern Byte ReadFlashByte(Word FlashAddress); //=========================================================================== //Function prototypes //=========================================================================== void WriteFlashWord(Word FiveDigitNumber, Word FlashAddress,Byte Segment); Word ReadFlashWord(Word FlashAddress,Byte Segment); //=========================================================================== void WriteFlashWord(Word FiveDigitNumber, Word FlashAddress) { static volatile Byte data HighByte; static volatile Byte data LowByte; LowByte=FiveDigitNumber;HighByte=FiveDigitNumber>>8; WriteFlashByte(LowByte,FlashAddress); WriteFlashByte(HighByte,FlashAddress+1); } //=========================================================================== Word ReadFlashWord(Word FlashAddress) { static volatile Byte data HighByte; static volatile Byte data LowByte; LowByte=ReadFlashByte(FlashAddress); HighByte=ReadFlashByte(FlashAddress+1); return((HighByte*256)+LowByte); } //=========================================================================== Hope this helps. Regads, John. PS: When I place it here, the assembly code's neat formating is gone, I will try to post it again. |