这个程序是按照张教主的改了一点点. 但是不能用啊.我是用在MC9S08SG8上的. 一运行,程序就会死机,总线频率发生频繁变化.后重启. 我主要是拿来用,在底层操作也不熟.请张教主指点.请大家帮我一下.
看一看这个程序哪里出了问题的. /* This is a linker parameter file for the mc9s08sg8 */
NAMES END /* CodeWarrior will pass all the needed files to the linker by command line. But here you may add your own files too. */
SEGMENTS /* Here all RAM/ROM areas of the device are listed. Used in PLACEMENT below. */ EEPROM = READ_ONLY 0xE000 TO 0xE3FF; Z_RAM = READ_WRITE 0x0080 TO 0x00FF; RAM = READ_WRITE 0x0100 TO 0x027F; ROM = READ_ONLY 0xE400 TO 0xFFAD; /* INTVECTS = READ_ONLY 0xFFC0 TO 0xFFFF; Reserved for Interrupt Vectors */ END
PLACEMENT /* Here all predefined and user segments are placed into the SEGMENTS defined above. */ DEFAULT_RAM /* non-zero page variables */ INTO RAM;
_PRESTART, /* startup code */ STARTUP, /* startup data structures */ ROM_VAR, /* constant variables */ STRINGS, /* string literals */ VIRTUAL_TABLE_SEGMENT, /* C++ virtual table segment */ DEFAULT_ROM, COPY /* copy down information: how to initialize variables */ INTO ROM;
_DATA_ZEROPAGE, /* zero page variables */ MY_ZEROPAGE INTO Z_RAM; END
STACKSIZE 0x50
VECTOR 0 _Startup /* Reset vector: this is the default entry point for an application. */
#include"DSD.h" #include"MC9S08SG8.h"
#define SIZE_FUNC_RAM 50 //擦写在RAM中的空间.
//函数类型声明 void WriteEE(byte*, byte*, byte); //写一串数据字节到E2 void EraseEE(byte*); //擦除E2数据页
#pragma CONST_SEG EEPROM const byte EE_Data[1023]; //保留一页Flash空间作为E2模拟 //============================================================== // 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] = "12345678"; //测试时被写入E2的数据
//============================================================== // Following data are declared in the common data area // (address >= 0x100) //============================================================== #pragma DATA_SEG DEFALUT //default data segment
//MCU初始化子程序 void MCU_init(void) { SOPT1 = 0x00; SOPT2 = 0x00; ICSC1_CLKS = 0; ICSC1_IREFS = 1; ICSC1_RDIV = 0; ICSC2_BDIV = 2; ICSTRM = 0xB5; //自调整 0x96 // 配置时钟频率 FCDIV_PRDIV8 = 0; FCDIV_DIV = 19; //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 _asm NOP; //wait at least 4 nop _asm NOP; _asm NOP; _asm 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 Disable_Interrupts; //禁止任何中断,很重要!!! ((byte (*)(byte))codeBuff)(0x20); //编程一个字节 在前面加 (void)就没有了报警 -C1420 Enable_Interrupts; 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 Disable_Interrupts; ((byte (*)(byte))codeBuff)(0x40); //do page erase在前面加 (void)就没有了报警 -C1420 Enable_Interrupts; }
//主程序入口 void main(void) { MCU_init(); Enable_Interrupts; EraseEE((byte*)EE_Data); WriteEE((byte*)EE_Data, testData, 8); for(;;) { _asm NOP _asm NOP __RESET_WATCHDOG(); // feeds the dog } }
|