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

📄 i2c.c

📁 基于MST-G240128点阵液晶的 状态机机制 人机界面四级滚屏菜单 源代码 ,带时间片机制模拟操作系统
💻 C
字号:
/****************************************************************************
**
** 文件名:   I2C.c
** 功能:     C8051F040的I2C控制器驱动;
** 说明:    
** 作者:     李立学(上海交通大学)
** 版权申明: 可以拷贝,可以修改,但必须保留作者信息
** 创建日期:2005.11.28
** 修改日期:
****************************************************************************/

#include "LZK.H"


//------------------------------------------------------------------------------
// I2C Bus (SMBus) register bit definitions
//------------------------------------------------------------------------------
sbit I2C_BUSY = SMB0CN ^ 7;  /* SMBUS 0 BUSY                    */
sbit I2C_EN   = SMB0CN ^ 6;  /* SMBUS 0 ENABLE                  */
sbit I2C_STA  = SMB0CN ^ 5;  /* SMBUS 0 START FLAG              */
sbit I2C_STO  = SMB0CN ^ 4;  /* SMBUS 0 STOP FLAG               */
sbit I2C_SI   = SMB0CN ^ 3;  /* SMBUS 0 INTERRUPT PENDING FLAG  */
sbit I2C_AA   = SMB0CN ^ 2;  /* SMBUS 0 ASSERT/ACKNOWLEDGE FLAG */
sbit I2C_FTE  = SMB0CN ^ 1;  /* SMBUS 0 FREE TIMER ENABLE       */
sbit I2C_TOE  = SMB0CN ^ 0;  /* SMBUS 0 TIMEOUT ENABLE          */


/****************************************************************************
** 函数名称: i2c_Init()
** 功能描述: i2c接口初始化
** 入口参数: 
** 出口参数: 无
** 全局变量: 无
** 调用模块: 无
** 说明:
****************************************************************************/
void i2c_Init(void)

{    

	SFRPAGE = 0x00;   // Page 0. 

	SMB0CN  = 0x44;	// SMBus Control Register
   // Bit7: BUSY: Busy Status Flag.
   //  0/1: SMBus0 is free / Busy.
   // Bit6: ENSMB: SMBus Enable.
   //  0/1: SMBus0 disabled / Enabled.
   // Bit5: STA: SMBus Start Flag.
   //  0/1: No START / a START condition is transmitted.
   // Bit4: STO: SMBus Stop Flag.
   //  0/1: No STOP / a STOP condition is transmitted.
   // Bit3: SI: SMBus Serial Interrupt Flag.
   //  This bit is set by hardware when one of 27 possible SMBus0 states is entered. 
   // Bit2: AA: SMBus Assert Acknowledge Flag.
   //  0/1: A "not acknowledge" (high / Low level on SDA)
	// Bit1: FTE: SMBus Free Timer Enable Bit
   //  0/1: No timeout when SCL is high / timeout.
   // Bit0: TOE: SMBus Timeout Enable Bit
   //  0/1: No timeout when SCL is low  / timeout.


	SMB0ADR = 0xF3;	// SMBus Address Register

	SMB0CR  = 235;	   // SMBus Clock Rate Register. 400kBPS / 2.5uS. 

}


/****************************************************************************
** 函数名称: i2c_Init()
** 功能描述:  使能i2c接口,并清除i2c数据发送标记
** 入口参数: 
** 出口参数: 无
** 全局变量: 无
** 调用模块: 无
** 说明:
****************************************************************************/
void i2c_start(void)
{
	while( I2C_BUSY );					// Wait until we are clear to write
	I2C_STA = TRUE;						// Perform I2C start
	while( !I2C_SI );						// Wait until start sent
	I2C_STA = FALSE;						// Reset I2C start
	I2C_SI = 0;						  	   // Clear SI
}


/****************************************************************************
** 函数名称: i2c_write()
** 功能描述: 向i2c接口写一个字节数据
** 入口参数: 
** 出口参数: 无
** 全局变量: 无
** 调用模块: 无
** 说明:
****************************************************************************/
void i2c_write(unsigned char ucDATA)
{
	SMB0DAT = ucDATA;					// Put data into buffer
	while( !I2C_SI );					// Wait unitl we are done with send
	I2C_SI = 0;							// Clear SI
}


/****************************************************************************
** 函数名称: i2c_read()
** 功能描述: 从i2c接口读取一个字节数据
** 入口参数: 
** 出口参数: 无
** 全局变量: 无
** 调用模块: 无
** 说明:
****************************************************************************/
unsigned char i2c_read(void)
{
	unsigned char ucDATA;

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

	return ucDATA;
}


/****************************************************************************
** 函数名称: repeated_i2c_start_and_write()
** 功能描述: 
** 入口参数: 
** 出口参数: 无
** 全局变量: 无
** 调用模块: 无
** 说明:
****************************************************************************/
void repeated_i2c_start_and_write(unsigned char ucDATA)
{
	I2C_STA = TRUE;					// Perform I2C start
	SMB0DAT = ucDATA;					// Put data into buffer
	while( !I2C_SI );					// Wait unitl we are done with send
	I2C_SI = 0;							// Clear SI
	I2C_STA = FALSE;					// Reset I2C start
	while( !I2C_SI );					// Wait unitl we are done with reset
	I2C_SI = 0;							// Clear SI
}


/****************************************************************************
** 函数名称: i2c_stop_and_write()
** 功能描述: i2c接口初始化
** 入口参数: 
** 出口参数: 无
** 全局变量: 无
** 调用模块: 无
** 说明:
****************************************************************************/
void i2c_stop_and_write(unsigned char ucDATA)
{
	I2C_STO = TRUE;					// Perform I2C stop
	SMB0DAT = ucDATA;					// Put data into buffer
	while( !I2C_SI );					// Wait unitl we are done with send
	I2C_SI = 0;							// Clear SI
}


/****************************************************************************
** 函数名称: i2c_stop_and_read()
** 功能描述: 
** 入口参数: 
** 出口参数: 无
** 全局变量: 无
** 调用模块: 无
** 说明:
****************************************************************************/
unsigned char i2c_stop_and_read(void)
{
	unsigned char ucDATA;

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

	return ucDATA;
}


/****************************************************************************
** 函数名称: i2c_write_byte()
** 功能描述: 对指定地址的i2c从器件写一个字节数据
** 入口参数: 
** 出口参数: 无
** 全局变量: 无
** 调用模块: 无
** 说明:
****************************************************************************/
void i2c_write_byte(unsigned char ucDATA, unsigned int uiADDR)
{
   INTUC Addr;
	
	i2c_start();						      	// Send start signal
   i2c_write(0xA0);              			// Send identifier I2C address
   
   Addr.uiVAR = uiADDR;
   i2c_write( Addr.ucVAR[1] );            // Send High 8-bit address out.
   i2c_write( Addr.ucVAR[0] );            // Send Low  8-bit address out.
   i2c_stop_and_write(ucDATA);      		// Send DATA out.
   DmS(1); 
}


/****************************************************************************
** 函数名称: I2C_WriteOneByte()
** 功能描述: 对指定地址的I2C从器件读入一个字节数据
** 入口参数: 
** 出口参数: 无
** 全局变量: 无
** 调用模块: 无
** 说明:
****************************************************************************/
unsigned char i2c_read_byte(unsigned int uiADDR)
{
   INTUC Addr.
   unsigned char ucTMP;

	i2c_start();						     	   // Send start signal
  	i2c_write(0xA0);              			// Send identifer I2C address

   Addr.uiVAR = uiADDR;
   i2c_write( Addr.ucVAR[1] );            // Send High 8-bit address out.
	repeated_i2c_start_and_write( Addr.ucVAR[0] );					

   i2c_write(0xA1);              			// Send identifer I2C address
   ucTMP = i2c_stop_and_read();			   // Read byte, send stop signal

   return ucTMP;                 
}

⌨️ 快捷键说明

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