⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eeprom.c

📁 矿工定位系统单端
💻 C
字号:
//eeprom.c - code recommendation for C header file
/***********************************************************************
MODULE:    EEPROM
VERSION:   1.00
CONTAINS:  Routines for accessing the EEPROM on the Philips
           P89LPC932
COPYRIGHT: Embedded Systems Academy, Inc. - www.esacademy.com
LICENSE:   May be freely used in commercial and non-commercial code
           without royalties provided this copyright notice remains
           in this file and unaltered
WARNING:   IF THIS FILE IS REGENERATED BY CODE ARCHITECT ANY CHANGES
           MADE WILL BE LOST. WHERE POSSIBLE USE ONLY CODE ARCHITECT
           TO CHANGE THE CONTENTS OF THIS FILE
GENERATED: On "Mar 14 2004" at "11:02:25" by Code Architect 2.03
***********************************************************************/

// SFR description needs to be included
#include<REG922.h>
#include "eeprom.h"

// flag that indicates if the EEPROM is busy or not
static bit meeprombusy;

/***********************************************************************
DESC:    Initializes the EEPROM. Enables EEPROM interrupt
RETURNS: Nothing
CAUTION: Set EA to 1 after calling to enable all interrupts
************************************************************************/
void eeprom_init
  (
  void
  )
{
  // initially eeprom is not busy
  meeprombusy = 0;

  // set isr priority to 0
  IP1 &= 0x7F;
  IP0H &= 0x7F;
  // enable eeprom interrupt
  EIEE = 1;
}

/***********************************************************************
DESC:    Reads a location in the EEPROM.
         If either global interrupts or the EEPROM interrupt is disabled
         then the function will return when the operation is complete.
         If global interrupts and the EEPROM interrupt are enabled, the
         function will return immediately and an interrupt will occur
         when the operation is complete.
RETURNS: The 8-bit value read if interrupts are disabled, otherwise
         0x00 will be returned.
CAUTION: eeprom_init must be called first
************************************************************************/
unsigned char eeprom_read
  (
  unsigned int address     // 9-bit address to read (0x000 - 0x1FF)
  )
{
  // wait for previous operation to complete
  while (meeprombusy);

  // eeprom now busy
  meeprombusy = 1;

  // store bit 8 of address
  // byte operation, clear interrupt flag
  DEECON = (address >> 8) & 0x01;
  // store bits 0-7 of address
  DEEADR = address & 0xFF;

  // if not using interrupt then poll for end of operation
  if (!EA || !EIEE)
  {
    // wait for operation to complete
    while (!(DEECON & 0x80));
    // eeprom no longer busy
    meeprombusy = 0;
    // return value
    return DEEDAT;
  }

  return 0x00;
}

/***********************************************************************
DESC:    Writes to a location in the EEPROM.
         If either global interrupts or the EEPROM interrupt is disabled
         then the function will return when the operation is complete.
         If global interrupts and the EEPROM interrupt are enabled, the
         function will return immediately and an interrupt will occur
         when the operation is complete.
RETURNS: Nothing
CAUTION: eeprom_init must be called first
************************************************************************/
void eeprom_write
  (
  unsigned int address,    // 9-bit address to write to (0x000 - 0x1FF)
  unsigned char value      // value to write
  )
{
  bit eacopy;

  // wait for previous operation to complete
  while (meeprombusy);

  // eeprom now busy
  meeprombusy = 1;

  // store bit 8 of address
  // byte operation, clear interrupt flag
  DEECON = (address >> 8) & 0x01;
  // disable interrupts
  eacopy = EA;
  EA = 0;
  // store value to write
  DEEDAT = value;
  // store bits 0-7 of address
  DEEADR = address & 0xFF;
  // restore interrupts
  EA = eacopy;

  // if not using interrupt then poll for end of operation
  if (!EA || !EIEE)
  {
    // wait for operation to complete
    while (!(DEECON & 0x80));
    // eeprom no longer busy
    meeprombusy = 0;
  }
}

/***********************************************************************
DESC:    Writes a value to every location in a 64-byte row in
         the EEPROM.
         If either global interrupts or the EEPROM interrupt is disabled
         then the function will return when the operation is complete.
         If global interrupts and the EEPROM interrupt are enabled, the
         function will return immediately and an interrupt will occur
         when the operation is complete.
RETURNS: Nothing
CAUTION: eeprom_init must be called first
************************************************************************/
void eeprom_fillrow
  (
   unsigned int address,    // 9-bit starting address of row
                            // (64-byte aligned)
   unsigned char value      // value to fill row with
  )
{
  bit eacopy;

  // wait for previous operation to complete
  while (meeprombusy);

  // eeprom now busy
  meeprombusy = 1;

  // store bit 8 of address
  // row fill operation, clear interrupt flag
  DEECON = ((address >> 8) & 0x01) | 0x20;
  // disable interrupts
  eacopy = EA;
  EA = 0;
  // store fill pattern
  DEEDAT = value;
  // store bits 0-7 of address (note bits 0-5 are ignored by device)
  DEEADR = address & 0xFF;
  // restore interrupts
  EA = eacopy;

  // if not using interrupt then poll for end of operation
  if (!EA || !EIEE)
  {
    // wait for operation to complete
    while (!(DEECON & 0x80));
    // eeprom no longer busy
    meeprombusy = 0;
  }
}

/***********************************************************************
DESC:    Writes a value to every location in the EEPROM.
         If either global interrupts or the EEPROM interrupt is disabled
         then the function will return when the operation is complete.
         If global interrupts and the EEPROM interrupt are enabled, the
         function will return immediately and an interrupt will occur
         when the operation is complete.
RETURNS: Nothing
CAUTION: eeprom_init must be called first
************************************************************************/
void eeprom_fill
  (
  unsigned char value      // value to fill EEPROM with
  )
{
  bit eacopy;

  // wait for previous operation to complete
  while (meeprombusy);

  // eeprom now busy
  meeprombusy = 1;

  // bit 8 of address = 1
  // block fill operation, clear interrupt flag
  DEECON = 0x31;
  // disable interrupts
  eacopy = EA;
  EA = 0;
  // store fill pattern
  DEEDAT = value;
  // store anything to address register - value ignored by device
  DEEADR = 0x00;
  // restore interrupts
  EA = eacopy;

  // if not using interrupt then poll for end of operation
  if (!EA || !EIEE)
  {
    // wait for operation to complete
    while (!(DEECON & 0x80));
    // eeprom no longer busy
    meeprombusy = 0;
  }
}

/***********************************************************************
DESC:    Erases a 64-byte row in the EEPROM.
         If either global interrupts or the EEPROM interrupt is disabled
         then the function will return when the operation is complete.
         If global interrupts and the EEPROM interrupt are enabled, the
         function will return immediately and an interrupt will occur
         when the operation is complete.
         Equivalent to eeprom_fillrow(address, 0x00);
RETURNS: Nothing
CAUTION: eeprom_init must be called first
************************************************************************/
void eeprom_eraserow
  (
  unsigned int address      // 9-bit starting address of row
                            // (64-byte aligned)
  )
{
  eeprom_fillrow(address, 0x00);
}

/***********************************************************************
DESC:    Erases the EEPROM.
         If either global interrupts or the EEPROM interrupt is disabled
         then the function will return when the operation is complete.
         If global interrupts and the EEPROM interrupt are enabled, the
         function will return immediately and an interrupt will occur
         when the operation is complete.
         Equivalent to eeprom_fill(0x00);
RETURNS: Nothing
CAUTION: eeprom_init must be called first
************************************************************************/
void eeprom_erase
  (
  void
  )
{
  eeprom_fill(0x00);
}

/***********************************************************************
DESC:    EEPROM Interrupt Service Routine
         Called when an EEPROM operation has completed
         Uses register bank 1
RETURNS: Nothing
CAUTION: eeprom_init must be called first
************************************************************************/
void eeprom_isr
  (
  void
  ) interrupt 14 using 1
{
  // clear EEIF flag
  DEECON &= ~0x80;
  // eeprom no longer busy
  meeprombusy = 0;
}

⌨️ 快捷键说明

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