|
楼主 |
发表于 2011-8-29 14:31:31
|
显示全部楼层
void EEPROM_ByteWrite(unsigned long addr, unsigned char dat)
{
unsigned char SMB_SINGLEBYTE_OUT; // Global holder for single byte writes.
while (SMB_BUSY); // Wait for SMBus to be free.
SMB_BUSY = 1; // Claim SMBus (set to busy)
// Set SMBus ISR parameters
TARGET = EEPROM_ADDR; // Set target slave address
SMB_RW = WRITE; // Mark next transfer as a write
SMB_SENDWORDADDR = 1; // Send Word Address after Slave Address
SMB_RANDOMREAD = 0; // Do not send a START signal after the word address
SMB_ACKPOLL = 1; // Enable Acknowledge Polling (The ISR
// will automatically restart the
// transfer if the slave does not
// acknoledge its address.
// Specify the Outgoing Data
WORD_ADDR_order=0;
//ADDR_D0=0;
//ADDR_D0=addr&0x01;
//ADDR_D0 |=(addr>>16)&0x01;
// if(addr>0xffff)
if(addr & 0xffff0000)
{
SMB_RW |= 0x02;
}
else {SMB_RW |= 0x00;} // ADDR_D0=0x02;
// else ADDR_D0=0x00;
WORD_ADDR_H = (addr>>8)&0xff; // Set the target address in the
// EEPROM's internal memory space
WORD_ADDR_L = addr&0xff;
SMB_SINGLEBYTE_OUT = dat; // Store <dat> (local variable) in a
// global variable so the ISR can read
// it after this function exits
pSMB_DATA_OUT = &SMB_SINGLEBYTE_OUT;// The outgoing data pointer points to the <dat> variable
SMB_DATA_LEN = 1; // Specify to ISR that the next transfer
// will contain one data byte
STA = 1; // Initiate SMBus Transfer
}
//-----------------------------------------------------------------------------
// EEPROM_WriteArray ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) unsigned char dest_addr - beginning address to write to in the EEPROM
// range is full range of character: 0 to 255
//
// 2) unsigned char* src_addr - pointer to the array of data to be written
// range is full range of character: 0 to 255
//
// 3) unsigned char len - length of the array to be written to the EEPROM
// range is full range of character: 0 to 255
//
// Writes <len> data bytes to the EEPROM slave specified by the <EEPROM_ADDR>
// constant.
//
void EEPROM_WriteArray(unsigned long dest_addr, unsigned char* src_addr,
unsigned char len)
{
unsigned char i;
//// unsigned char* pData = (unsigned char*) src_addr;
unsigned char* pData;
pData = src_addr;
WP = 0; // allows normal write operations
PCA0CPH4 = 0x00; // 喂狗
for( i = 0; i < len; i++ )
{
EEPROM_ByteWrite(dest_addr++, *pData++);
}
// WP = 1; // write operations to the memory are inhibited
}
//-----------------------------------------------------------------------------
// EEPROM_ReadArray ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) unsigned char* dest_addr - pointer to the array that will be filled
// with the data from the EEPROM
// range is full range of character: 0 to 255
//
// 2) unsigned char src_addr - beginning address to read data from the EEPROM
// range is full range of character: 0 to 255
//
// 3) unsigned char len - length of the array to be read from the EEPROM
// range is full range of character: 0 to 255
//
// Reads up to 256 data bytes from the EEPROM slave specified by the
// <EEPROM_ADDR> constant.
//
void EEPROM_ReadArray (unsigned char* dest_addr, unsigned long src_addr,
unsigned char len)
{
while (SMB_BUSY); // Wait for SMBus to be free.
SMB_BUSY = 1; // Claim SMBus (set to busy)
// Set SMBus ISR parameters
TARGET = EEPROM_ADDR; // Set target slave address
SMB_RW = WRITE; // A random read starts as a write
// then changes to a read after
// the repeated start is sent. The
// ISR handles this switchover if
// the <SMB_RANDOMREAD> bit is set.
SMB_SENDWORDADDR = 1; // Send Word Address after Slave Address
SMB_RANDOMREAD = 1; // Send a START after the word address
SMB_ACKPOLL = 1; // Enable Acknowledge Polling
// Specify the Incoming Data
WORD_ADDR_order=0;
if(src_addr & 0xffff0000)
{
SMB_RW |= 0x02;
}
else {SMB_RW |= 0x00;}
//ADDR_D0=0;
//ADDR_D0=src_addr&0x01;
// ADDR_D0 |=(src_addr>>16)&0x01;
// if(src_addr>0xffff)
// ADDR_D0=0x02;
//else ADDR_D0=0x00;
WORD_ADDR_H = (src_addr>>8)&0xff; // Set the target address in the
// EEPROM's internal memory space
WORD_ADDR_L = src_addr&0xff;
// Set the the incoming data pointer
pSMB_DATA_IN = (unsigned char*) dest_addr;
SMB_DATA_LEN = len; // Specify to ISR that the next transfer will contain <len> data bytes
STA = 1; // Initiate SMBus Transfer
while(SMB_BUSY); // Wait until data is read
} |
|