中级会员
- 积分
- 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 |= 0x50; //设置时钟分频,外部晶振为16Mhz
}
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(uchar k)
{
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,0x00,des_addr); //0X00
Write_Command(0x02,k,k);
//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 */
uchar read1[8]={1,2,4,8,16,32,64,128};
DDRB=0XFF;
ReadEeprom(read,0x0800,0x08);
k=read[1];
for(;;) {
if(k>=7)k=0;
k++;
PORTB=read1[k];
delay(50000);
delay(50000);
delay(50000);
Write_D_Flash(k);
}
}
做的流水灯 但是掉电后每次都从初始位置开始 可能是没有写进去 但是读写又正常 就是不能断电存储 求解 谢谢 |
|