微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > C8051F单片机读写串行EEPROM程序

C8051F单片机读写串行EEPROM程序

时间:11-24 来源:互联网 点击:

return data_in;
}

//------------------------------------------------------------------------------
// Routine: i2c_start
// Inputs: none
// Outputs: none
// Purpose: Sends I2C Start Trasfer - State "B"
//------------------------------------------------------------------------------
void i2c_start (void)
{
while (BUS_BUSY); // Wait until we are clear to write
BUS_START = TRUE; // Perform I2C start
while (!BUS_INT); // Wait until start sent
BUS_START = FALSE; // Reset I2C start
BUS_INT = 0; // Clear SI
}

//------------------------------------------------------------------------------
// Routine: repeated_i2c_start_and_write
// Inputs: none
// Outputs: none
// Purpose: Sends I2C Start Trasfer - State "B"
//------------------------------------------------------------------------------
void repeated_i2c_start_and_write (unsigned char output_data)
{
BUS_START = TRUE; // Perform I2C start
SMB0DAT = output_data; // Put data into buffer
while (!BUS_INT); // Wait unitl we are done with send
BUS_INT = 0; // Clear SI
BUS_START = FALSE; // Reset I2C start
while (!BUS_INT); // Wait unitl we are done with reset
BUS_INT = 0; // Clear SI
}

//------------------------------------------------------------------------------
// Routine: i2c_stop_and_write
// Inputs: output byte
// Outputs: none
// Purpose: Sends I2C Stop Trasfer - State "C" (also sends last byte)
//------------------------------------------------------------------------------
void i2c_stop_and_write (unsigned char output_data)
{
BUS_STOP = TRUE; // Perform I2C stop
SMB0DAT = output_data; // Put data into buffer
while (!BUS_INT); // Wait unitl we are done with send
BUS_INT = 0; // Clear SI
}

//------------------------------------------------------------------------------
// Routine: i2c_stop_and_read
// Inputs: none
// Outputs: input byte
// Purpose: Sends I2C Stop Trasfer - State "C" (also reads last byte)
//------------------------------------------------------------------------------
unsigned char i2c_stop_and_read (void)
{
unsigned char input_data;

BUS_STOP = TRUE; // Perform I2C stop
while (!BUS_INT); // Wait until we have data to read
input_data = SMB0DAT; // Read the data
BUS_INT = 0; // Clear SI

return input_data;
}

//------------------------------------------------------------------------------
// Routine: i2c_write
// Inputs: output byte
// Outputs: none
// Purpose: Writes data over the I2C bus
//------------------------------------------------------------------------------
void i2c_write (unsigned char output_data)
{
SMB0DAT = output_data; // Put data into buffer
while (!BUS_INT); // Wait unitl we are done with send
BUS_INT = 0; // Clear SI
}

//------------------------------------------------------------------------------
// Routine: i2c_read
// Inputs: none
// Outputs: input byte
// Purpose: Reads data from the I2C bus
//------------------------------------------------------------------------------
unsigned char i2c_read (void)
{
unsigned char input_data;

while (!BUS_INT); // Wait until we have data to read
input_data = SMB0DAT; // Read the data
BUS_INT = 0; // Clear SI

return input_data;
}

////////////////////////////////////////////////////////////////////////////////
// Routine: delay_time
// Inputs: counter value to stop delaying
// Outputs: none
// Purpose: To pause execution for pre-determined time
////////////////////////////////////////////////////////////////////////////////
void delay_time (unsigned int time_end)
{
unsigned int index;
for (index = 0; index < time_end; index++);
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top