f_write.c

来自「这是单片机C51典型应用设计代码」· C语言 代码 · 共 87 行

C
87
字号
#include <ADUC812.H>
#include "f_adi.h"

/*---------------------------------------------------------
---------------------------------------------------------*/
unsigned long flash_write (
  const void *buffer,      /* Buffer to save */
  unsigned long len,       /* Buffer length */
  unsigned long address)   /* FLASH address to write to */
{
const unsigned char *s = buffer;
unsigned int i;
unsigned int addr;

/*-------------------------------------
If the address is too big, exit.
--------------------------------------*/
if (address >= ADI_EEMEM_SIZE)
  return (0UL);

addr = (unsigned int) address;

/*-------------------------------------
--------------------------------------*/
for (i = 0; i < len; i++)
  {

/*-------------------------------------
Read the page.
--------------------------------------*/
  if ((i == 0) || (addr & 0x03) == 0)
    {
    EADRL = addr >> 2;
    ECON = ADI_EE_READ_PAGE;
    }

/*-------------------------------------
Update the data.
--------------------------------------*/
  switch ((addr++) & 0x03)
    {
    case 0:
      EDATA1 = s[i];
      break;

    case 1:
      EDATA2 = s[i];
      break;

    case 2:
      EDATA3 = s[i];
      break;

    case 3:
      EDATA4 = s[i];

    WRITE_EEPROM:
      ECON = ADI_EE_ERASE_PAGE;
      ECON = ADI_EE_WRITE_PAGE;
      break;
    }

/*-------------------------------------
Update the address.
--------------------------------------*/
  if (addr >= ADI_EEMEM_SIZE)
    break;
  }

/*-------------------------------------
Write the final page IFF address is not
at the start of a new page.
--------------------------------------*/
  if (addr & 0x03)
    {
    addr = ADI_EEMEM_SIZE;
    goto WRITE_EEPROM;
    }

/*-------------------------------------
-------------------------------------*/
return (i);
}

/*---------------------------------------------------------
---------------------------------------------------------*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?