[求助]用C语言如何编写MC9S12DG128的EEPROM檫除,写入程序

- UID
- 104673
- 性别
- 男
|

/*******************************************************************
* EEPROM program command subroutine
* Description : Copy from RAM area to EEPROM area
* : Input RAM,EEPROM start address and no. of word to be
* : programmed
* Example : Copy 4 words from RAM(0x1000) to EEPROM (0x600)
* input : eram_addi=0x1000, eeprom_addi=0x600, no_of word=0x4
* modify : eeprom content (0x600 to 0x608)= (0x1000 to 0x1008)
*********************************************************************/
void eeprom_program_cmd(int eram_addi, int eeprom_addi, int no_of_word)
{
int *ram_add,*eeprom_add,count=0;
ram_add=(int*)eram_addi;
eeprom_add=(int*)eeprom_addi;
eclkdiv = 0x49;
DisableInterrupts;
estat=pviol+accerr; //movb #$30,$115 ; clear previous command error
while(count!=no_of_word)
{
*eeprom_add=*ram_add; //; get data and put it to EEPROM
ecmd=prog; //; initiate PROGRAM COMMAND
estat=cbeif; //; begin command
if(((estat&pviol)==pviol)||((estat&accerr)==accerr))
printf0("EEprom programming error\n\r"); //display eree_program_error
else
while((estat&ccif)==0x00); // wait for command to complete
count++;
eeprom_add++;
ram_add++;
}
EnableInterrupts;
}
/*******************************************************************
* EEPROM erase command subroutine
* Description : Erase EEPROM long word (4 bytes)
* : Input EEPROM start address and no. of word to be
* : erased
* Example : Erase 4 long words from EEPROM (0x600)
* input : eeprom_addi=0x600, no_of word=0x4
* modify : eeprom content (0x600-0x610)=0xff
*********************************************************************/
void eeprom_erase_cmd(int eeprom_addi, int no_of_long_word)
{
int *eeprom_add,count=0;
eeprom_add=(int*)eeprom_addi;
eclkdiv = 0x49;
DisableInterrupts;
estat=pviol+accerr; //movb #$30,$115 ; clear previous command error
while(count!= no_of_long_word)
{
*eeprom_add=0; // to erase the data at EEPROM
ecmd=erase; //; initiate ERASE COMMAND
estat=cbeif; //; begin command
if(((estat&pviol)==pviol)||((estat&accerr)==accerr))
{
printf0("EEprom programming error in \n\r"); //display eree_program_error
hex_asc((byte)(eeprom_addi>>8));
hex_asc((byte)(eeprom_addi));
printf0("\n\r"); //display lf
}
else
while((estat&ccif)==0x00); // wait for command to complete
count++;
eeprom_add+=2; //int pointer increment
eeprom_addi+=4; //eeprom_addi increment by 4 (long)
tx_char0('.');
}
EnableInterrupts;
}
/*******************************************************************
* EEPROM program subroutine
* Description : User input RAM area and EEPROM area to be copied
* : Input RAM,EEPROM start address and no. of word to be
* : programmed
* Example : Copy 4 words from RAM(0x1000) to EEPROM (0x600)
* input : eram_addi=0x1000, eeprom_addi=0x600, no_of word=0x4
* modify : eeprom content
*********************************************************************/
void eeprom_program()
{
unsigned int *eeprom_addi,add0,*eram_addi,add1;
byte status=DISABLE,ebyte;
eeprom_addi=&add0;
eram_addi=&add1;
printf0("RAM start address (0x1000-0x12ff)= \r");
if(input_word(eram_addi)==OK)
{
if((*eram_addi>=0x1000)&&(*eram_addi<0x1300))
status=ENABLE;
else
{
printf0("\nOut of range!\r");
status=DISABLE;
}
}
else
status=DISABLE;
printf0("\n\r");
if (status==ENABLE)
{
printf0("EEPROM start address to be programmed ($400 default)= \r");
if(input_word(eeprom_addi)==OK)
{
if((*eeprom_addi>=0x400)&&(*eeprom_addi<0x1000))
status=ENABLE;
else
{
printf0("\nOut of range!\r");
status=DISABLE;
}
}
else
status=DISABLE;
printf0("\n\r");
}
if (status==ENABLE)
{
printf0("No. of word to be programmed (Dec) = \r");
if(input_dec(&ebyte)==OK)
{
if (((ebyte)==DOT)||((ebyte)==ESC))
status=DISABLE;
else
status=ENABLE;
printf0("\n\r");
}
}
if (status==ENABLE)
eeprom_program_cmd(*eram_addi, *eeprom_addi, (unsigned int) ebyte);
}
/*******************************************************************
* EEPROM erase subroutine
* Description : Erase EEPROM long word (4 bytes)
* : User input EEPROM start address and no. of word to be
* : erased
* Example : Erase 4 long words from EEPROM (0x600)
* input : eeprom_addi=0x600, no_of word=0x4
* modify : eeprom content (0x600-0x610)=0xff
*********************************************************************/
void eeprom_erase()
{
unsigned int *address,add;
byte status=DISABLE,ebyte;
address=&add;
printf0("EEPROM start address to be Erase (hex) (0x400-0xfff) = \r");
if(input_word(address)==OK)
{
if((*address>=0x400)&&(*address<0x1000))
status=ENABLE;
else
{
printf0("\nOut of range!\r");
status=DISABLE;
}
}
else
status=DISABLE;
printf0("\n\r");
if (status==ENABLE)
{
printf0("No. of long word (4 bytes) to be erased (Dec) = \r");
if(input_dec(&ebyte)==OK)
{
if (((ebyte)==DOT)||((ebyte)==ESC))
status=DISABLE;
else
status=ENABLE;
printf0("\n\r");
}
}
if (status==ENABLE)
eeprom_erase_cmd(*address, (unsigned int) ebyte);
} |
|
|
|
|
|