中级会员
- 积分
- 302
- 威望
- 239
- 贡献
- 27
- 兑换币
- 0
- 注册时间
- 2010-12-3
- 在线时间
- 18 小时
|
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#define D_Addr 0x00
#define Program_Dflash 0x11
typedef unsigned char UINT8;
typedef unsigned int UINT16;
typedef unsigned char uchar;
typedef unsigned int uint;
unsigned char read[8];
uchar k;
void delay(uint j)
{
uchar i;
for(;j>0;j--)
for(i=20;i>0;i--);
}
void Write_Command(uchar index,uint fcmd,uint addr)
{
FCCOBIX = index;
//set the conmand mode and parameter
FCCOB = (fcmd<<8) | addr; //set the address and the data
}
void Pre_Start()
{
if(FCLKDIV_FDIVLD ==0)
FCLKDIV |= 0x0f; //设置时钟分频,外部晶振为8Mhz
}
void Start_Command()
{
FSTAT_CCIF = 1; //To start the command. clear the FSTAT_CCIF begin to write the lauch command
}
void Wait_Complete_Command()
{
while(FSTAT_CCIF == 0);
//if the command is finish FSTAT_CCIF will be zero
}
void Write_D_Flash()
{
uint des_addr = D_Addr;
Pre_Start();
if(FSTAT_CCIF)
{
if(FSTAT_ACCERR)
{
FSTAT_ACCERR = 1;
delay(100); //似乎此处必须加延时
}
if(FSTAT_FPVIOL)
{
FSTAT_FPVIOL = 1;
delay(100);
}
Write_Command(0x00,Program_Dflash,0x10);
Write_Command(0x01,0x01,des_addr); //0X00
Write_Command(0x02,1,2);
Write_Command(0x03,4,8);
Write_Command(0x04,16,32);
Write_Command(0x05,64,128);
Start_Command();
Wait_Complete_Command();
}
}
void ReadEeprom( uchar * srcAddr, uint destAddr, UINT16 size)
{
EPAGE=0X00;
/* current location */
while(size != 0)
{
/* copy to destination */
*(UINT16 *)srcAddr=*(UINT16 *)destAddr;
/* next address */
destAddr=destAddr+1;
/* next address */
srcAddr=srcAddr+1;
/* one byte less */
size--;
}
}
void main(void)
{
/* put your own code here */
DDRB=0XFF;
Write_D_Flash();
ReadEeprom(read,0x0900,0x08);
for(;;)
{
for(k=0;k<8;k++) {
PORTB=read[k];
delay(50000);
delay(50000);
}
} /* wait forever */
/* please make sure that you never leave this function */
}
这是写的程序 调试时可用 但是一旦把读操作注释掉 只执行写入时 数据就全没了 就解答 谢谢 |
|