void main(void)
{
Init();
//EEPROM power off for long time and dint operate, the first 8 bit cannot be read.
//So have to read twice.
Master = ByteRead(0x1FF0)<<8;
Master|= ByteRead(0x1FF1);
//Restore master pin again
Master = ByteRead(0x1FF0)<<8;
Master|= ByteRead(0x1FF1);
//Other program here...
}
//**************************************************************
void ByteWrite(unsigned int address, unsigned char ByteData)
{
static unsigned char status;
//WriteEnable(); // Set WEL bit for write (Comment out: Stack over flow)
CS=0; // Bring CS low (active)
dummy = SPI_OUTPUT(0x06); // Output WREN command
CS=1;
CS=0; // Bring CS high (inactive)
dummy = SPI_OUTPUT (0x02); // Output WRITE command
dummy = SPI_OUTPUT ((address>>8)&0xFF); // Output MSB of address
dummy = SPI_OUTPUT (address&0xFF); // Output LSB of address
dummy = SPI_OUTPUT (ByteData); // Write byte of data
CS=1; // Bring CS high (inactive)
// WIP_Poll(); // Perform WIP polling
Delay_Routine(720); // WIP polling (EEPROM error, change to delay 5ms)
}
//**************************************************************
char ByteRead(unsigned int address)
{
static unsigned char spi_received;
CS=0; // Bring CS low (active)
dummy = SPI_OUTPUT(0x03); // Output READ command
dummy = SPI_OUTPUT((address>>8)&0xFF); // Output MSB of address
dummy = SPI_OUTPUT(address&0xFF); // Output LSB of address
spi_received = SPI_OUTPUT(0x00); // The byte transmitted here is a don't care.
CS=1; // Bring CS high (inactive)
return spi_received; // Return byte of data
}
//**************************************************************
char SPI_OUTPUT (char spi_byte)
{
SSPBUF = spi_byte; // Transfer data to SSPBUF
do{}
while(!BF); // Wait until the transmission is complete.
return SSPBUF; // The data received should be valid.
}
//**************************************************************
void WIP_Poll(void)
{
static unsigned char status; // Variable to store Status Register
do{
status = ReadStatusReg(); // Read status register
}while(status & 0x01); // Loop wile WIP (bit 0) is 1
}