/**********************EEPROM.C***********************/ /********作者:sly *************版本:1.0.0********/
#include <hidef.h> /* for EnableInterrupts macro */ #include "derivative.h" /* include peripheral declarations */
#pragma DATA_SEG MY_ZEROPAGE unsigned char RAM_CODE[60],pdata; unsigned int faddr;
unsigned char Page_Erase(void) { static unsigned char * paddr; if (FSTAT&0x10){ //Check to see if FACCERR is set FSTAT = FSTAT | 0x10; //write a 1 to FACCERR to clear } paddr=(unsigned char *)faddr; *paddr=0xff; FCMD="0x40"; FSTAT = FSTAT | 0x80; //Put FCBEF at 1. _asm NOP; //Wait 4 cycles _asm NOP; _asm NOP; _asm NOP; if (FSTAT&0x30){ //check to see if FACCERR or FVIOL are set return 0xFF; //if so, error. } while ((FSTAT&0x40)==0){ } //else wait for command to complete return 0x00; }
unsigned char Program_Byte(void) { static unsigned char * paddr; if (FSTAT&0x10){ //Check to see if FACCERR is set FSTAT = FSTAT | 0x10; //write a 1 to FACCERR to clear } paddr=(unsigned char *)faddr; *paddr=pdata; FCMD="0x20"; FSTAT = FSTAT | 0x80; //Put FCBEF at 1. _asm NOP; //Wait 4 cycles _asm NOP; _asm NOP; _asm NOP; if (FSTAT&0x30){ //check to see if FACCERR or FVIOL are set return 0xFF; //if so, error. } while ((FSTAT&0x40)==0){ } //else wait for command to comple return 0x00; }
void write_eeprom(unsigned char *data,unsigned char length,unsigned int first_addr) { static unsigned char i; static void (*funcPtr)(); static unsigned char *src,*dest; funcPtr =Program_Byte; i=60; src= (unsigned char *) funcPtr; dest= (unsigned char *)&RAM_CODE[0]; do { *dest++ = *src++; } while(--i); faddr="first"_addr; for(i=0; i< length; i++) { pdata=*data++; asm (jsr RAM_CODE ) ; faddr++; } }
void erase(void) {
static unsigned char i; static void (*funcPtr)(); static unsigned char *src,*dest; funcPtr =Page_Erase; src= (unsigned char *) funcPtr; dest= (unsigned char *)&RAM_CODE[0]; i="60"; do{ *dest++ = *src++; } while(--i); faddr="0xf800"; asm (jsr RAM_CODE ) ; // do page erase }
在对flash编程时注意要点:
1先擦除,再写入。
2擦除只有也擦除,所以修改一个存储单元的值时,要将所有的单元读入ram中,修改后,将所有单元重新写入。
3写flash的时钟要确保在150k~200kHz之间。
以上函数经过freescale codewarrior 5.1版本测试通过。 |
用户377235 2013-7-11 11:08