
// EEPROM VARIABLES
xdata unsigned short int EEWU_indexTimer          _at_ 0x00;
xdata unsigned short int EEWU_deflectorTimer      _at_ 0x02;
xdata unsigned short int EEWU_doorTimer           _at_ 0x04;
xdata unsigned char      EEWU_roomRelayDelay      _at_ 0x06;
xdata unsigned char      EEWU_boxRelayOn          _at_ 0x07;
xdata unsigned char      EEWU_fuseSimulatorOn     _at_ 0x08;
xdata unsigned char      EEWU_itemCountPulseHi    _at_ 0x09;
xdata unsigned short int EEWU_itemCountPulseLow   _at_ 0x0A;
xdata unsigned short int EEWU_doorOpensMode       _at_ 0x0C;


/******************************************************************************
* NAME          : EEWU_eepromAccessEnable
* DESCRIPTION   : This Function enables onchip EEPROM access of the processor
* INPUT         : Nothing
* OUTPUT        : Nothing
******************************************************************************/
void EEWU_eepromAccessEnable(void)
{
    WMCON |= EEMEN_; //EEPROM access enable
}

/******************************************************************************
* NAME          : eepromWriteEnable
* DESCRIPTION   : This Function enables on chip EEPROM write of the processor
* INPUT         : Nothing
* OUTPUT        : Nothing
******************************************************************************/
static void eepromWriteEnable(void)
{
    WMCON |= EEMWE_; //Write enable
}

/******************************************************************************
* NAME          : eepromWriteDisable
* DESCRIPTION   : This Function disables on chip EEPROM write of the processor
* INPUT         : Nothing
* OUTPUT        : Nothing
******************************************************************************/
static void eepromWriteDisable(void)
{
    WMCON &= ~EEMWE_; //Write disable
}

/******************************************************************************
* NAME          : eepromReadyToWrite
* DESCRIPTION   : This Function shows if EEPROM is ready to write
* INPUT         : Nothing
* OUTPUT        : bit 1 if EEPROM is ready to write
******************************************************************************/
static unsigned char eepromReadyToWrite(void)
{
    return(WMCON & EERDY_);
}

/******************************************************************************
* NAME          : EEWU_writeByte
* DESCRIPTION   : This Function writes a byte to EEPROM.
* INPUT         : char* Address of byte to be stored and unsigned char to write 
* OUTPUT        : Nothing
******************************************************************************/
void EEWU_writeByte(unsigned char* EE_ptr, // out
                    unsigned char value8)
{
    eepromWriteEnable();
    *EE_ptr = value8;
    while(eepromReadyToWrite() == 0)
    {
    }                           // wait till byte is written (about 2.5 msec)
    eepromWriteDisable();
}

/******************************************************************************
* NAME          : EEWU_writeShortInt
* DESCRIPTION   : This Function writes short int to  EEPROM upper byte first
* INPUT         : int* Address of byte to be stored,unsigned short to write 
* OUTPUT        : Nothing
******************************************************************************/
void EEWU_writeShortInt(unsigned short int* EE_ptr2, // out
                        unsigned short int value16)
{
    unsigned char tempVar;
    unsigned char cntr;
    unsigned char* charPtr = (unsigned char*)EE_ptr2;
    tempVar = value16 & 0x0ff; // lower byte
    for(cntr = 0; cntr < 2; cntr++, charPtr++)
    {
        EEWU_writeByte(charPtr,tempVar);
        tempVar = value16 >> 8; // higher byte
    }
}
