本人最近写一个程序,在MC08HC908SR12中将flash中的一个数据块进行擦写。
源程序如下:
#include "Cpu.h" #include "Events.h" /* Include shared modules, which are used for whole project */ #include "PE_Types.h" #include "PE_Error.h" #include "PE_Const.h" #include "IO_Map.h" #define N 200
void DoEarseFlash(void); void DoWriteFlash(void); /*------------MOVE PROGRAM FROM FLASH TO RAM------------*/ void move(void(*p)(),byte size) { byte *w; byte i=0; w=(byte *)0x0100; for(i=0;i<size;i++) *(w+i)=*((byte*)(p)+i); } /*------------------------------------------------------*/
/*---------MOVE DoEraseFlash form FLASH to RAM-----------*/ void EraseFlash(void) { void(*a)(); move(DoEarseFlash,0x3E); a=(byte *)0x0100; (*a)(); } /*-------------------------------------------------------*/
/*---------MOVE DoWriteFlash form FLASH to RAM-----------*/ void WriteFlash(void) { void(*t)(); move(DoWriteFlash,0x50); t=(byte *)0x0100; (*t)(); } /*-------------------------------------------------------*/ /*-------------DoEraseFlash:擦除指定flash区------------*/ void DoEarseFlash(void) { unsigned char i; FLBPR=0x01; //设置flash保护区 FLCR1=0b00000010; //1->ERASE,0->MASS(页擦除) i=FLBPR; //读FLBPR *((volatile unsigned char *)0xC000)=0x99;//向被擦写的单元写任意字符 for(i=0;i<N;i++); //延时10us FLCR1=0b00001010; //1->HVEN (加高压) for(i=0;i<N;i++); //延时时间必须>1.6ms FLCR1=0b00001000; //0->Erase for(i=0;i<N;i++); //10us FLCR1=0b00000000; //0->HVEN(取消高压) for(i=0;i<N;i++); //延时10us }
/*-----------DoWriteFlash:实际执行的写入函数-----------*/ void DoWriteFlash(unsigned int addr) { unsigned char i; FLBPR=0x01; FLCR1=0b00000001; //1->GM,编程状态 i=FLBPR; //读FLBPR *((volatile unsigned char *)(addr))=0x88;//0x88->C000,选中flash行 for(i=0;i<N;i++); //10us FLCR1=0b00001001; //1->HVEN for(i=0;i<N;i++); //10us *((volatile unsigned char *)addr)=0x88;//将数据写入相应的flash地址 FLCR1=0b00001000; //0->GM for(i=0;i<N;i++); //10us FLCR1=0b00000000; //0->HVEN for(i=0;i<N;i++); //10us }
void main(void) { /** * Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/ PE_low_level_init(); FLBPR=0x01; EraseFlash(); DoEarseFlash(); WriteFlash(); DoWriteFlash(0xC000); FLBPR=0;
问题:
1.我的FLBPR不能修改,即FLBPR(FE09)=0x00不能将它置为1。而FLCR1(FE08)可以访问并且修改!
2.似乎从c000-c080的flash代码段没有被擦写,不知道我的程序是否有错,欢迎大家指正,谢谢!
[此贴子已经被作者于2005-11-30 22:16:37编辑过] |