把张教主的FLASH写EEPROM的移到MC9S08QG8怎么不成,哪出错了? //MC9S08QG8
#include <hidef.h> /* for EnableInterrupts macro */ #include "derivative.h" /* include peripheral declarations */
//自己定义一个汇编NOP指令的宏
#define NOP() asm(nop) #define SIZE_FUNC_RAM 50 //擦写在RAM中的空间.
//函数类型声明
void WriteEE(byte*, byte*, byte); //写一串数据字节到E2 void EraseEE(byte*); //擦除E2数据页
//定义一些数据段 //============================================================== // Following data are declared as EEPROM (Flash emulation) // Refer to PRM file for address mapping //============================================================== // PRM文件定义---是EEPROM定义.*/ // ROM = READ_ONLY 0XE000 TO 0XF5FF; // EPROM = READ_ONLY 0xF600 TO 0xFDFF; // ROM1 = READ_ONLY 0xFE00 TO 0xFFCF; // END //PLACEMENT /* Here all predefined and user segments are placed into the SEGMENTS defined above. */ // EEPROM INTO EPROM;
#pragma CONST_SEG EEPROM const byte EE_Data[2048]; //保留一页Flash空间作为E2模拟 //#pragma CONST_SEG DEFALUT //default data segment //============================================================== // Following data are declared in the direct addressing area // for fast access (address < 0x100) //============================================================== #pragma DATA_SEG SHORT MY_ZEROPAGE //direct addressing data segment byte testData[8]={'1', '2', '3', '4', '5', '6', '7', '8'}; //测试时被写入E2的数据
//============================================================== // Following data are declared in the common data area // (address >= 0x100) //==============================================================
#pragma DATA_SEG DEFALUT //default data segment
void MCU_init(void); //初始化MCU
//主程序入口
void main(void) {
MCU_init();
EnableInterrupts; EraseEE((byte*)EE_Data); WriteEE((byte*)EE_Data, testData, 8); for(;;) { __RESET_WATCHDOG(); // feeds the dog
}
}
//MCU初始化子程序
void MCU_init(void) { // 配置时钟频率 FCDIV = 0x27; //set FCLK base on 8MHz Fbus 200K }
//下面这段代码是专门为启动Flash编程命令然后查询编程结束标志
//在执行之前必须被拷贝到RAM区(任意地址),然后从RAM中运行
//============================================================================= byte ExecEePrgCmd(byte cmd) { FCMD = cmd; //set command FSTAT_FCBEF = 1; //command launched and FCBEF cleared NOP(); //wait at least 4 nop NOP(); NOP(); NOP(); if (FSTAT_FPVIOL || FSTAT_FACCERR) { //exit if encounter any error return(0); //return with error flag } while(!FSTAT_FCCF) { //wait for FCCF=1 __RESET_WATCHDOG(); } return(1); //return with success }
//定义上面一段代码的长度,编译后不超过50字节 //写一串数据到模拟E2区,大部分代码是在Flash区内运行的,只是上面关键的一小段代码必须拷贝到RAM区才能运行
void WriteEE(byte* eeAddr, byte* datBuff, byte byteCount) { byte i; byte *srcPtr; byte codeBuff[SIZE_FUNC_RAM]; //buffer size is slightly bigger than the length of ExecEePrgCmd function
//这里示范的是将关键代码拷贝到局部变量(堆栈)区。用户可以将代码拷贝到静态数据区,一样使用
//Copy ExecEePrgCmd code into RAM srcPtr = (byte*)ExecEePrgCmd; //function entry in Flash for (i=0;i<SIZE_FUNC_RAM;i++) { //do byte copy codeBuff = srcPtr; }
FSTAT_FACCERR = 1; //clear any pending Flash error flag
while (byteCount) { *eeAddr++ = *datBuff++; //latch data byte and memory address DisableInterrupts; //禁止任何中断,很重要!!! ((byte (*)(byte))codeBuff)(0x20); //编程一个字节 在前面加 (void)就没有了报警 -C1420 EnableInterrupts; byteCount--; } }
//擦除E2页面,原理基本同上//============================================================================= // Erase a page of EEPROM (Flash emulated) //============================================================================= void EraseEE(byte* eeAddr) { byte i; byte *srcPtr; byte codeBuff[SIZE_FUNC_RAM]; //buffer size is slightly bigger than the length of ExecEePrgCmd function
//Copy ExecEePrgCmd code into RAM srcPtr = (byte*)ExecEePrgCmd; //function entry in Flash for (i=0;i<SIZE_FUNC_RAM;i++) { //do byte copy codeBuff = srcPtr; }
FSTAT_FACCERR = 1; //clear any pending Flash error flag
*eeAddr = 0; //latch memory address DisableInterrupts; ((byte (*)(byte))codeBuff)(0x40); //do page erase在前面加 (void)就没有了报警 -C1420 EnableInterrupts; }
|