volatile uchar PrgOfRam[18]; #pragma CODE_SEG NON_BANKED void Flash_Init(void) { //不启用分页机制 while(FSTAT_ACCERR == 1 || FSTAT_PVIOL == 1) ; //错误标志位访问 if((FCLKDIV & 0x80) == 0) //FDIVLD是否被写过 FCLKDIV |= 0x28; // 40分频 // while(FCLKDIV_FDIVLD == 0); // 等待时钟设置成功 FCNFG = 0x00; //禁止中断 选择block0 FSTAT |= 0x20; //对保护区域进行擦写 // PPAGE = 0x3E; //页号为3E
} //================================================= //#pragma CODE_SEG NON_BANKED void Flash_Erase(uint addr) { uint *p; p = (uint *)addr; *p = 0xFFFF; while(FSTAT_ACCERR == 1 || FSTAT_PVIOL == 1) ; //错误标志位访问 FCMD = 0x40 ;//页擦除 FSTAT |= 0x80;//启动命令执行 while(FSTAT_CCIF == 0) ;//等待完成 //asm("JSR PrgOfRam"); } //=================================================== //#pragma CODE_SEG NON_BANKED void Flash_Write(uint addr,uint *dat,uchar lenth) { uint i = 0; uint *p; p = (uint *)addr; for(i=0;i<lenth;i++) { *p = *dat; while(FSTAT_ACCERR == 1 || FSTAT_PVIOL == 1) ; //错误标志位访问 FCMD = 0x20;//写一个字命令 FSTAT |= 0x80;//启动命令执行 while(FSTAT_CCIF == 0) ;//等待完成 // asm("JSR PrgOfRam"); p++;dat++; } } #pragma CODE_SEG DEFAULT //===================================================== void main(void) { // put your own code here // EnableInterrupts; const uchar P[18] = {0x18,0x0B,0x80,0x01,0x05,0xF6,0x01,0x05, 0x87,0x87,0xC4,0x40,0x8C,0x00,0x00, 0x27,0xF4,0x3D};
volatile uchar LED[5] ={0,1,2,3,4}; uchar i; for(i=0;i<18;i++) { PrgOfRam = P; } Flash_Init(); Flash_Erase(0x4000); Flash_Write(0x4000,(uint *)LED,5); for(;;) {} // wait forever //please make sure that you never leave this function } //====================================================== 这里是源程序,NON_BANKED和block0是在同一页中吗?4000地址后都是FF,数没写进去。是怎么回事?本人菜鸟,多多指教!多谢!!! |