EEPROM 功能使用方法 EEPROM 功能使用方法 1 适用产品: 1.1 SM39R16A2/ SM39R12A2/ SM39R08A2 1.2 SM39R4051/ SM39R2051 1.3 SM39R04G1/ SM39R02G1 2 EEPROM 功能概述: 2.1 使用 code flash 仿真为 Internal EEPROM,在程序执行时,可将 code flash 作为 data flash 储存 数据使用。 2.2 EEPROM command 包括:Chip erase、page erase 及 byte program。 2.3 EEPROM 必须避免寻址在主程序(user code)区域;而为保护 ISP code,在 ISP 保护区(ISP block N)所保护的位置,EEPROM command 将视为无效。 Start addr: $0000 EEPROM must avoid User code Memory space EEPROM valid... EEPROM in-valid ISP code End addr: $3FFF 2.4 MCU 空片及 erase 后 code flash 皆为 FFh。Flash 每一 page=256 byte。 2.5 Page erase 及 byte program 时间纪录: EEPROM 执行 command 时间,为系统内振,与 user 设定的系统晶振频率无关 page erase (ms) byte program(ms) about 28~35ms about 0.065~0.075 Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0238 1 Ver A 2011/09 EEPROM 功能使用方法 2.6 EEPROM 相关的特殊缓存器 Special Function Register (SFR) 2.6.1 EERPOM register – TAKEY, IFCON, ISPFAH, ISPFAL, ISPFD and ISPFC Mnemoni c Description Bit 7 Direct Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 RESET ISP function TAKEY IFCON ISPFAH ISPFAL ISPFD ISPFC Time Access Key register Interface Control register ISP Flash Address - High register ISP Flash Address - Low register ISP Flash Data register ISP Flash Control register F7h TAKEY [7:0] 8Fh - CDPR - - 00H - - ISPE 00H E1h ISPFAH [7:0] FFH E2h ISPFAL [7:0] FFH E3h ISPFD [7:0] FFH E4h EMF1 EMF2 EMF3 EMF4 - ISPF.2 6 ISPF.1 ISPF.0 00H Address: F7H Mnemonic: TAKEY 7 - 5 4 3 TAKEY [7:0] 2 1 0 Reset 00H ISP 致能位(ISPE)预设为只读属性,为避免程序错误导致 ISP 误动作,使用者必须依序写入三笔数据到(55h, AAh, 5Ah)TAKEY,才可将 ISP 致能位(ISPE)改变为可写入属性,程序参考如下: MOV TAKEY, #055h MOV TAKEY, #0AAh MOV TAKEY, #05Ah ; enable ISPE write attribute ORL IFCON, #001H ; enable ISP function 注意:以上程序,中间不可有任何程序执行,包括不可有程序中断(Interrupt)或设定仿真器中断(break point) 干扰其操作流程,必须依序且完整的连续写入,否则该功能将无法致能。完整程序可参考 Assembly 或 C 语言范例程序。 Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0238 2 Ver A 2011/09 EEPROM 功能使用方法 Mnemonic: IFCON 7 6 ITS CDPR 5 F12K 4 F8K 3 ALEC[1] 2 ALEC[0] 1 EMEN Address: 8FH 0 Reset ISPE 00H ISP 致能位(ISPE)预设为只读属性,为避免程序错误导致 ISP 误动作,使用者必须依序写入三笔数据到(55h, AAh, 5Ah)TAKEY,才可将 ISP 致能位(ISPE)改变为可写入属性: ISPE: = 1, ISP致能,ISP缓存器(ISPFAH, ISPFAL, ISPFD , ISPFC)设为可写入。 = 0, ISP禁能,ISP缓存器(ISPFAH, ISPFAL, ISPFD and ISPFC)为只读 (预设)。 程序范例,ISP byte program #22H 到 program flash 位置$1005H,如下: MOV TAKEY, #055h MOV TAKEY, #0AAh MOV TAKEY, #05Ah ; enable ISPE write attribute ORL IFCON, #001H ; enable ISP function MOV ISPFAH, #010H ; set flash address-high, 10H MOV ISPFAL, #005H ; set flash address-low, 05H MOV ISPFD, #022H ; set flash data to be programmed, data = 22H MOV ISPFC, #000H ; start to program #22H to the flash address $1005H Mnemonic: ISPFAH 7 6 ISPFAH7 ISPFAH6 5 ISPFAH5 4 ISPFAH4 3 ISPFAH3 2 ISPFAH2 1 ISPFAH1 Address: E1H 0 Reset ISPFAH0 FFH ISPFAH [7:0]: ISP 共提供 16 位寻址,此为高位 8~15 位置。 Mnemonic: ISPFAL 7 6 ISPFAL7 ISPFAL6 5 ISPFAL5 4 ISPFAL4 3 ISPFAL3 2 ISPFAL2 1 ISPFAL1 Address: E2H 0 Reset ISPFAL0 FFH 1 ISPFD1 Address: E3H 0 Reset ISPFD0 FFH ISPFAL [7:0]: ISP 共提供 16 位寻址,此为低位 0~7 的位置。 Mnemonic: ISPFD 7 6 ISPFD7 ISPFD6 5 ISPFD5 4 ISPFD4 3 ISPFD3 2 ISPFD2 Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0238 3 Ver A 2011/09 EEPROM 功能使用方法 ISPFD [7:0]: ISP 数据缓存器。 Mnemonic: ISPFC Address: E4H 7 6 5 4 3 2 1 0 Reset EMF1 EMF2 EMF3 EMF4 ISPF[2] ISPF[1] ISPF[0] 00H EMF1: Entry mechanism (1) flag, clear by reset. (Read only) ISP 进入记录旗标(1),只读,可由芯片复位清除 EMF2: Entry mechanism (2) flag, clear by reset. (Read only) ISP 进入记录旗标(2),只读,可由芯片复位清除 EMF3: Entry mechanism (3) flag, clear by reset. (Read only) ISP 进入记录旗标(3),只读,可由芯片复位清除 EMF4: Entry mechanism (4) flag, clear by reset. (Read only) ISP 进入记录旗标(4),只读,可由芯片复位清除 ISPF [2:0]: ISP function select bit. ISP 功能选择元位,提供七组功能 ISPF[2:0] ISP function 000 Byte program 001 Chip protect 010 Page erase 011 Chip erase 100 Write option 101 Read option 110 Erase option Finish Flag*(注一) 111 注一:只有Write option或Erase option后,才须执行Finish flag指令以示完成,否则写入 值皆视为无效。EEPROM不须执行Finish Flag。 2.7 EEPROM Assembly 语言范例程序: Description Main program 此范例展示有 EEP_Byte_Program、EEP_Chip_Protect、EEP_Page_Erase、EEP_Byte_Read、EEP_Enable 及 EEP_Disable 基本功能,提供使用者自行开发 ISP code 或 EEPROM 应用参考。 //========================================================================= // // S Y N C M O S T E C H N O L O G Y // //========================================================================= #include "SM39R16A2.h" //ISPFC.ISPF[2:0] #define d_COMMAND_BYTE_PROGRAM #define d_COMMAND_CHIP_PROTECT #define d_COMMAND_PAGE_ERASE //#define d_COMMAND_CHIP_ERASE 0 1 2 3 dat equ 30h addh equ 31h addl equ 32h org 0000h //Start Start: //test1====================== mov addh, #0x01 Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0238 4 Ver A 2011/09 EEPROM 功能使用方法 mov addl, #0x00 mov dat, #0x10 call EEP_Byte_Program call EEP_Byte_Read mov P0, dat //test2====================== mov addh, #0x02 mov addl, #0x00 mov dat, #0x20 call EEP_Byte_Program call EEP_Byte_Read mov P1, dat //=========================== jmp $ EEP_Byte_Program: call EEP_Enable mov ISPFD, dat mov ISPFAH, addh mov ISPFAL, addl mov ISPFC, #d_COMMAND_BYTE_PROGRAM call EEP_Disable ret EEP_Chip_Protect: call EEP_Enable mov ISPFC, #d_COMMAND_CHIP_PROTECT; call EEP_Disable ret EEP_Page_Erase://256-Byte call EEP_Enable mov ISPFAH, addh mov ISPFC, #d_COMMAND_PAGE_ERASE call EEP_Disable ret EEP_Byte_Read: clr A mov dph, addh mov dpl, addl movc A, @a+dptr mov dat, A ret EEP_Enable: MOV A, IFCON JB 0xE0.0, EEP_Enable_ret mov TAKEY, #0x55 mov TAKEY, #0xAA mov TAKEY, #0x5A orl IFCON, #0x01 // ISPE=1, Enable ISP function MOV A, IFCON JNB 0xE0.0, EEP_Enable EEP_Enable_ret: ret EEP_Disable: MOV A, IFCON JNB 0xE0.0, EEP_Disable_ret mov TAKEY, #0x55 mov TAKEY, #0xAA Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0238 5 Ver A 2011/09 EEPROM 功能使用方法 mov TAKEY, anl IFCON, MOV A, JB 0xE0.0, EEP_Disable_ret: ret end #0x5A #0xFE IFCON EEP_Disable // ISPE=1, Enable ISP function 2.8 EEPROM C 语言范例程序: Description EEPROM program 此范例展示有 EEP_Byte_Program、EEP_Chip_Protect、EEP_Page_Erase、EEP_Byte_Read、EEP_Enable 及 EEP_Disable 基本功能,提供使用者自行开发 ISP code 或 EEPROM 应用参考。 //==================================================================== // // S Y N C M O S T E C H N O L O G Y // //==================================================================== #include <source\SM39R04G1.h> #include <absacc.h> /* Include Macro Definitions */ /*ISPFC.ISPF[2:0]*/ #define d_Command_Byte_Program #define d_Command_Chip_Protect #define d_Command_Page_Erase #define d_Command_Chip_Erase 0 1 2 3 //==================================================================== #define d_DATALEN 128 // idata limit:1~128 unsigned char idata buf[d_DATALEN]; // idata limit:1~128 void EEPROM_Enable(void) { TAKEY = 0x55; TAKEY = 0xAA; TAKEY = 0x5A; IFCON |= 0x01; } void EEPROM_Disable(void) { TAKEY = 0x55; TAKEY = 0xAA; TAKEY = 0x5A; IFCON &= 0xFE; } // ISPE=1, Enable ISP function // ISPE=0, Disable ISP function void EEPROM_Byte_Program(unsigned int Addr, unsigned char Data) { EEPROM_Enable(); ISPFD = Data; ISPFAH = (Addr / 256); ISPFAL = (Addr % 256); ISPFC = d_Command_Byte_Program; EEPROM_Disable(); } Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0238 6 Ver A 2011/09 EEPROM 功能使用方法 void EEPROM_Page_Erase(unsigned int Addr) { EEPROM_Enable(); ISPFAH = (unsigned char)(Addr >>8); ISPFC = d_Command_Page_Erase; EEPROM_Disable(); } /* void EEPROM_Page_Erase(unsigned int Addr) { EEPROM_Enable(); ISPFAH = (unsigned char)(Addr >>8); ISPFC = d_Command_Chip_Erase; EEPROM_Disable(); } */ // erase 256-Byte // erase while chip /* void EEPROM_Chip_Protect(void) { EEPROM_Enable(); ISPFC = d_Command_Chip_Protect; EEPROM_Disable(); } */ void EEPROM Sector Program(unsigned int Addr start, unsigned int Addr end, unsigned char Data) { unsigned int i; for(i=Addr_start; i<=Addr_end; i++) { EEPROM_Byte_Program(i, Data); } /* for(i=0; i<(Addr_end - Addr_start)+1; i++) { EEPROM_Byte_Program(Addr_start+i, Data); } */ } unsigned char EEPROM_Byte_Read(unsigned int Addr) { /* unsigned char temp; unsigned char code *pAddr; pAddr = Addr; // Set address temp = (*pAddr); // Read data return temp; */ return CBYTE[Addr]; //The range of valid index values for this macro is 0-65535. } /* unsigned int EEPROM_Word_Read(unsigned int Addr) { return CWORD[Addr]; //The range of valid index values for this macro is 0-32767. Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0238 7 Ver A 2011/09 EEPROM 功能使用方法 } */ void EEPROM_Byte_Modify(unsigned int Addr, unsigned char Data) { unsigned char i; for(i=0; i<d_DATALEN; i++) buf[i] = EEPROM_Byte_Read((Addr&0xFF00)+i); // page read EEPROM_Page_Erase(Addr); // erase page buf[(unsigned char)Addr] = Data; // byte modify for(i=0; i<d_DATALEN; i++) EEPROM_Byte_Program((Addr&0xFF00)+i, buf[i]);// page program Main.c } #include <source\SM39R04G1.h> #include <source\EEPROM.h> #define test_len 100 #define test_start_addr 0x1000 void main(void) { unsigned int i; unsigned char buf[test_len]; EEPROM_Page_Erase(test_start_addr); EEPROM_Sector_Program(test_start_addr, test_start_addr+test_len-1, 0x55); for(i=0; i<test_len; i++) { buf[i]= EEPROM_Byte_Read(test_start_addr+i); } for(i=0; i<test_len; i++) { EEPROM_Byte_Modify(test_start_addr+i, i); } for(i=0; i<test_len; i++) { buf[i]= EEPROM_Byte_Read(test_start_addr+i); } while(1) { } } 新茂国际科技 希望能为客户减少开发的时间及辛勞,针对所有特殊功能应在C语言程序开发,皆提供 “Codzard 范例程序产生器"可于 新茂网站首页>下载专区> 软件下载 内下载此软件,如有任何建议,请來信告知,谢谢! 销售客服 电子信箱: [email protected] Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0238 8 Ver A 2011/09 EEPROM 功能使用方法 技术支持 电子信箱: [email protected] Specifications subject to change without notice, contact your sales representatives for the most recent information. ISSFA-0238 9 Ver A 2011/09