SFCF DATA 0B1H ; Flash Configuration SFCM DATA 0B2H ; Flash Command SFAL DATA 0B3H ; Flash Address Low SFAH DATA 0B4H ; Flash Address High SFDT DATA 0B5H ; Flash Data SFST DATA 0B6H ; Flash Status ;************************************************************************ ;* MCU IAP Commands * ;************************************************************************ SFCM_SE EQU 0BH ; Sector-Erase IAP cmd SFCM_VB EQU 0CH ; Byte-Verify IAP cmd SFCM_PB EQU 0EH ; Byte-Program IAP cmd ;************************************************************************ ORIG_DATA EQU R6 ;************************************************************************ ;* MAIN PROGRAM * ;************************************************************************ org 00000h ljmp main org 2000h main: mov PSW, #0 ; select register bank 0 start: anl SFCF, #10111111b ; BFh--> force IAPEN=0 ; orl SFCF, #10000000b ; VIS=1 ; mov SFDT, #0 ; Enter any value other than 55h mov dptr, #1f00h ;load destination addr. lcall sector_erase ; erase the bytes before write to them ;===================================================================== mov a,#00h mov dptr,#1f20h loop: mov ORIG_DATA, a lcall byte_pgm ; write a byte from ACC to dest address lcall verify_byte ; read back the byte written to destination xrl a, ORIG_DATA ; compare with original data jnz ERROR ; jump to ERROR if verification fails Stop: ajmp Stop ; end of code execution ;************************************************************************ ;* IAP SUBROUTINES * ;* 1. SECTOR-ERASE * ;* 2. BYTE-PROGRAM * ;* 3. BYTE-VERIFY * ;************************************************************************ ;=================================================================== ; Subroutine of Sector Erase ;=================================================================== sector_erase: orl SFCF, #40h ; enable IAP mov SFAH, dph ; load high order address byte mov SFAL, dpl ; load low order address byte mov SFCM, #SFCM_SE ; issue sector erase command busy: mov a, SFST jb acc.2, busy ; wait until flash is NOT busy. safety: anl SFCF, #10111111b ; disable IAP mov SFDT, #0 ; any value other than 55h ret ;===================================================================== ; Subroutine of Byte Program ;===================================================================== byte_pgm: orl SFCF, #40h ; enable IAP mov SFAH, dph ; set address to write a byte mov SFAL, dpl mov SFDT, ORIG_DATA ; store data to be programmed mov SFCM, #SFCM_PB ; issue program-byte cmd done?: mov a, SFST jb acc.2, done? ; wait until byte-programming is done. ljmp safety ;===================================================================== ; Subroutine of Byte Verify ;===================================================================== verify_byte: orl SFCF, #40h ; enable IAP mov SFAH, dph ; address from where to read a byte mov SFAL, dpl mov SFCM, #SFCM_VB ; issue verify_byte cmd nop mov a, SFDT ; data is stored in ACC ljmp safety ;===================================================================== ERROR: mov p1, #55h ;indicating IAP failed. EXIT: sjmp $ END