Beim c8051f120 Daten ins Flash schreiben / lesen

Gast #1604404
Lesenswert?

Bei Nutzung des Flash als EEPROM muss immer eine ganze Page gelöscht 
werden, einzlne Bytes gehen nicht. Geschrieben werden können Sie schon.

Denke das hilft!

/*********************************************************************** 
***
  ucFlashRead
************************************************************************ 
**/

unsigned char ucFlashRead (unsigned int addr)
{
   bit EA_SAVE = EA;                   // Preserve EA
   char code * data pread;             // FLASH read pointer
   unsigned char byte;

   WDT_RESET;

   EA = 0;                             // Disable interrupts

   pread = (char code *) addr;

   byte = *pread;                      // Read the byte

   EA = EA_SAVE;                       // Restore interrupts

   return byte;
}


/*********************************************************************** 
***
  vFlashWrite
************************************************************************ 
**/

void vFlashWrite (unsigned int addr, char byte)
{
   bit EA_SAVE = EA;                   // Preserve EA
   char xdata * data pwrite;           // FLASH write pointer

   WDT_RESET;

   EA = 0;                             // Disable interrupts

   VDM0CN = 0xA0;                      // Enable VDD monitor and high 
threshold

   RSTSRC = 0x02;                      // Enable VDD monitor as a reset 
source

   pwrite = (char xdata *) addr;

   FLKEY  = 0xA5;                      // Key Sequence 1
   FLKEY  = 0xF1;                      // Key Sequence 2
   PSCTL |= 0x01;                      // PSWE = 1 which enables writes

   VDM0CN = 0xA0;                      // Enable VDD monitor and high 
threshold

   RSTSRC = 0x02;                      // Enable VDD monitor as a reset 
source

   *pwrite = byte;                     // Write the byte

   PSCTL &= ~0x01;                     // PSWE = 0 which disable writes

   EA = EA_SAVE;                       // Restore interrupts
}


/*********************************************************************** 
***
  vFlashPageErase
************************************************************************ 
**/

void vFlashPageErase (unsigned int addr)
{
   bit EA_SAVE = EA;                   // Preserve EA
   char xdata * data pwrite;           // FLASH write pointer

   WDT_RESET;

   EA = 0;                             // Disable interrupts

   VDM0CN = 0xA0;                      // Enable VDD monitor and high 
threshold

   RSTSRC = 0x02;                      // Enable VDD monitor as a reset 
source

   pwrite = (char xdata *) addr;

   FLKEY  = 0xA5;                      // Key Sequence 1
   FLKEY  = 0xF1;                      // Key Sequence 2
   PSCTL |= 0x03;                      // PSWE = 1; PSEE = 1


   VDM0CN = 0xA0;                      // Enable VDD monitor and high 
threshold

   RSTSRC = 0x02;                      // Enable VDD monitor as a reset 
source
   *pwrite = 0;                        // Initiate page erase

   PSCTL &= ~0x03;                     // PSWE = 0; PSEE = 0

   EA = EA_SAVE;                       // Restore interrupts
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren