Hello I would like ask you.
I have problem with program eeprom.
I am using CodeWarrior, HC9S12DP256 fbus = 8MHz, fosc=16MHz. Are values right?
There is program.
EEPROM = READ_ONLY 0x0400 TO 0x0EFF;
MYPROM = READ_WRITE 0x0F00 TO 0xFEF;
int Initi(void){
DisableInterrupts;
ECLKDIV_PRDIV8 = 0;
ECLKDIV_PRDIV8 = 1;
ECLKDIV = 0x4a; //EDIV[5:0] 10 PRDVI8 = 1 EnableInterrupts;
return 0;
}
int erase_sector(int *ptr){ /*ptr (unsigned int *far)0x3ff40*/
DisableInterrupts;
ESTAT = ESTAT_ACCERR_MASK | ESTAT_PVIOL_MASK; /* clear error flags */
if(ESTAT_CBEIF == 1){ /*buffer full*/
*ptr = 0x00;
ECMD = SECTOR_ERASE; /*sector erase*/
ESTAT = ESTAT_CBEIF_MASK; /***/
if(ESTAT & (ESTAT_ACCERR_MASK | ESTAT_PVIOL_MASK)){
EnableInterrupts;
return 1;
}
while(!(ESTAT & ESTAT_CCIF_MASK));
EnableInterrupts;
return 0; /*ok*/
}else{
EnableInterrupts;
return 1; /*bad*/
}
}
int Writebyte(unsigned char* address, unsigned char data){
DisableInterrupts;
ESTAT = ESTAT_ACCERR_MASK | ESTAT_PVIOL_MASK;
if(ESTAT_CBEIF == 1){
*address = data;
ECMD = PROG;
ESTAT = ESTAT_CBEIF_MASK;
if((ESTAT & (ESTAT_ACCERR_MASK | ESTAT_PVIOL_MASK)) != 0){
EnableInterrupts;
return 0; /*bad*/
}
while(ESTAT_CCIF != 1);
EnableInterrupts;
return 1; /*OK*/
}else{
EnableInterrupts;
return 0; /*bad*/
}
}
int Writeword(unsigned int* address, unsigned int data){
unsigned int a =0;
a = Writebyte((unsigned char*)address++, (unsigned char)(data>>8));
a = Writebyte((unsigned char*)address, (unsigned char)data & 0xFF);
return 1;
}
Thank you very much
Added p/n to subject
Solved! Go to Solution.
So what problem do you have with EEPROM? Famous "doesn't work"?
I see two bugs in your code.
1) ECLKDIV is write once register and this piece of code may lead to damage of EEPROM module:
ECLKDIV_PRDIV8 = 0;
ECLKDIV_PRDIV8 = 1;
ECLKDIV = 0x4a; //EDIV[5:0] 10 PRDVI8 = 1 EnableInterrupts;
2) Programming misaligned words or chars is illegal operation:
int Writebyte(unsigned char* address, unsigned char data){
DisableInterrupts;
ESTAT = ESTAT_ACCERR_MASK | ESTAT_PVIOL_MASK;
if(ESTAT_CBEIF == 1){
*address = data;
...
So what problem do you have with EEPROM? Famous "doesn't work"?
I see two bugs in your code.
1) ECLKDIV is write once register and this piece of code may lead to damage of EEPROM module:
ECLKDIV_PRDIV8 = 0;
ECLKDIV_PRDIV8 = 1;
ECLKDIV = 0x4a; //EDIV[5:0] 10 PRDVI8 = 1 EnableInterrupts;
2) Programming misaligned words or chars is illegal operation:
int Writebyte(unsigned char* address, unsigned char data){
DisableInterrupts;
ESTAT = ESTAT_ACCERR_MASK | ESTAT_PVIOL_MASK;
if(ESTAT_CBEIF == 1){
*address = data;
...
Thank you very much.
Yes, erase don´t work and program too.
Is it now right?
1)
ECLKDIV = value_ediv | PRDIV8;
2)
int Writebyte(unsigned short* address, unsigned char data){
DisableInterrupts;
ESTAT = ESTAT_ACCERR_MASK | ESTAT_PVIOL_MASK;
if(ESTAT_CBEIF == 1){
*address = data;
Really thank you very much
if( ((unsigned short)address % 2) == 0) { *address = data; } else { /* error */ }