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

📄 isr_i2c.c

📁 基于EEPROM AT24C64的数据存储
💻 C
📖 第 1 页 / 共 2 页
字号:
//*-----------------------------------------------------------------------------
//* 函数名称       : I2CRxPrepareACK (I2Cdesc *I2C_pt)
//* Object         : I2C
//* 输入参数       : <I2C_pt> =  为I2C的描述指针,其结构定义在source.h中
//* 输出变量       : 无
//* 功能           :为I2C的接收应答时序的前的准备
//*-----------------------------------------------------------------------------
void I2CRxPrepareACK (I2Cdesc *I2C_pt)
//* Begin
{
    //* set SCL line
    SCL_SET();

    if(I2C_pt->mode == STOP)
        //* change interrupt handler to
        I2C_pt->I2CTCHandler = I2CTxPrepareSTOP;

    else
        //* change interrupt handler to I2CRxDoACK
        I2C_pt->I2CTCHandler = I2CRxDoACK;

//* End
}


//*-----------------------------------------------------------------------------
//* 函数名称       : I2CTxWaitACK (I2Cdesc *I2C_pt)
//* Object         : I2C
//* 输入参数       : <I2C_pt> =  为I2C的描述指针,其结构定义在source.h中
//* 输出变量       : 无
//* 功能           :为I2C的等待芯片的应答
//*-----------------------------------------------------------------------------
void I2CTxWaitACK (I2Cdesc *I2C_pt){
    if(SDA_VAL() == 0){	    //* if ACK (SDA = 0)...
		switch(I2C_pt->mode){
	    	case STOP :
            SCL_CLR();		//* clear SCL line
	          I2C_pt->I2CTCHandler = I2CTxPrepareSTOP;		//* change interrupt handler to
				    break;
		    case DEVICE_READ :
				    SCL_CLR();		//* clear SCL line
		        I2C_pt->I2CTCHandler = I2CDeviceRead;			//* change interrupt handler to
        		break;
		    case DATA_READ :
		        I2C_pt->I2CTCHandler = I2CRxReceiveByte;		//* change interrupt handler to
				    break;
		    default :
				    SCL_CLR();		//* clear SCL line
				    //* change interrupt handler to
				    I2C_pt->I2CTCHandler = I2CTxSendByte;
				break;
		}
    }
    else{
        I2C_pt->nb_ACK--;							//Decrease the counter. 
		    if(I2C_pt->nb_ACK == 0)
	    	    I2C_pt->I2CTCHandler = I2CError;	//Timeout, change interrupt handler to I2C
    }
}


//*-----------------------------------------------------------------------------
//* 函数名称       : I2CRxDoACK (I2Cdesc *I2C_pt)
//* Object         : I2C
//* 输入参数       : <I2C_pt> =  为I2C的描述指针,其结构定义在source.h中
//* 输出变量       : 无
//* 功能           :为主动发送应答给I2C设备
//*-----------------------------------------------------------------------------
void I2CRxDoACK (I2Cdesc *I2C_pt)
//* Begin
{
    //* clear SCL line
    SCL_CLR();

    //* change interrupt handler to
    I2C_pt->I2CTCHandler = I2CRxReceiveByte;

//* End
}


//*-----------------------------------------------------------------------------
//* 函数名称       : I2CTxSetSCL (I2Cdesc *I2C_pt)
//* Object         : I2C
//* 输入参数       : <I2C_pt> =  为I2C的描述指针,其结构定义在source.h中
//* 输出变量       : 无
//* 功能           :为在发送数据时把I2C的SCL线设置为高电平
//*-----------------------------------------------------------------------------
void I2CTxSetSCL (I2Cdesc *I2C_pt){
    SCL_SET();		    //* set SCL line
    //* change interrupt handler to I2CTxSendBit
    I2C_pt->I2CTCHandler = I2CTxSendBit;
}


//*-----------------------------------------------------------------------------
//* 函数名称       : I2CTxClearSCL (I2Cdesc *I2C_pt)
//* Object         : I2C
//* 输入参数       : <I2C_pt> =  为I2C的描述指针,其结构定义在source.h中
//* 输出变量       : 无
//* 功能           :为在发送数据时把SCL线设置为低电平
//*-----------------------------------------------------------------------------
void I2CTxClearSCL (I2Cdesc *I2C_pt)
//* Begin
{
    //* Clear SCL line

  	SCL_CLR();

    if(I2C_pt->countBit == 0)
    	//* set SDA line as an input

    //* change interrupt handler to I2CTxSendBit
    I2C_pt->I2CTCHandler = I2CTxSendBit;

//* End
}


//*-----------------------------------------------------------------------------
//* 函数名称       : I2CRxSetSCL (I2Cdesc *I2C_pt)
//* Object         : I2C
//* 输入参数       : <I2C_pt> =  为I2C的描述指针,其结构定义在source.h中
//* 输出变量       : 无
//* 功能           :为在接收数据时把SCL线设置为高电平
//*-----------------------------------------------------------------------------
void I2CRxSetSCL (I2Cdesc *I2C_pt)
//* Begin
{
    //* set SCL line
    SCL_SET();

    //* change interrupt handler to I2CRxReceiveBit
    I2C_pt->I2CTCHandler = I2CRxReceiveBit;

//* End
}


//*-----------------------------------------------------------------------------
//* 函数名称       : I2CRxClearSCL (I2Cdesc *I2C_pt)
//* Object         : I2C
//* 输入参数       : <I2C_pt> =  为I2C的描述指针,其结构定义在source.h中
//* 输出变量       : 无
//* 功能           :为在接收数据时把SCL线设置为低电平
//*-----------------------------------------------------------------------------
void I2CRxClearSCL (I2Cdesc *I2C_pt)
//* Begin
{
    //* Clear SCL line
    SCL_CLR();

    if(I2C_pt->countBit == 0)
      {
	    //* change interrupt handler to I2CI2CRxPrepareACK
  	    I2C_pt->I2CTCHandler = I2CRxPrepareACK;

            //* set SDA line as an output

	    //* clear SDA line
 	    SDA_CLR();
      }
    else
	//* change interrupt handler to I2CRx
	I2C_pt->I2CTCHandler = I2CRxReceiveBit;

//* End
}


//*-----------------------------------------------------------------------------
//* 函数名称       : I2CRxReceiveByte (I2Cdesc *I2C_pt)
//* Object         : I2C
//* 输入参数       : <I2C_pt> =  为I2C的描述指针,其结构定义在source.h中
//* 输出变量       : 无
//* 功能           :为在从I2C中接收一个字节的数据
//*-----------------------------------------------------------------------------
unsigned int ttmp=0;
void I2CRxReceiveByte (I2Cdesc *I2C_pt)
//* Begin
{
	//* set SDA line as an input
	//* clear SCL line
	SCL_CLR();

	//* initialize countBit
	I2C_pt->countBit = 8;
	*I2C_pt->RxEnd=0;
	ttmp=0;
    I2C_pt->nbI2CByte--;
    if(I2C_pt->nbI2CByte == 0)
        //* next mode
        I2C_pt->mode = STOP;

	//* change interrupt handler to I2C
	I2C_pt->I2CTCHandler = I2CRxReceiveBit;

//* End
}


//*-----------------------------------------------------------------------------
//* 函数名称       : I2CRxReceiveBit (I2Cdesc *I2C_pt)
//* Object         : I2C
//* 输入参数       : <I2C_pt> =  为I2C的描述指针,其结构定义在source.h中
//* 输出变量       : 无
//* 功能           :为在从I2C中接收一个比特的数据
//*-----------------------------------------------------------------------------
void I2CRxReceiveBit (I2Cdesc *I2C_pt)
//* Begin
{
	unsigned char bit_val;
 	
     //* set SCL line
    SCL_SET();

	//* decrement countBit
	I2C_pt->countBit--;

	//* get bit
	if(SDA_VAL()){
	    bit_val = 1;
		//printk("r1 ");
	}
	else{
	    bit_val = 0;
		//printk("r0 ");
	}
	//* if bit counter > 0
	if(I2C_pt->countBit > 0){
	    //* update working byte
	    *I2C_pt->RxEnd |= (bit_val << I2C_pt->countBit);
		ttmp |= (bit_val << I2C_pt->countBit);
	}
	else{
	     *(I2C_pt->RxEnd++) |= (bit_val << I2C_pt->countBit) ;
		 ttmp |= (bit_val << I2C_pt->countBit);
		 //printk("\r\n ");
	}

    //* change interrupt handler to I2CRxClearSCL
    I2C_pt->I2CTCHandler = I2CRxClearSCL;

//* End
}


//*-----------------------------------------------------------------------------
//* 函数名称       : Timer_I2C (I2Cdesc *I2C_pt)
//* Object         : I2C
//* 输入参数       : 无
//* 输出变量       : 无
//* 功能           :为I2C的中断定时器,I2C的一切函数都由此中断来驱动
//*-----------------------------------------------------------------------------
void Timer_I2C()    interrupt T0_INT {//timer 1 interrupt.
	if(!I2c_timer_cnt){
    	(*(I2c.I2CTCHandler))(&I2c);	//call automate state handler
	}
	else{
		I2c_timer_cnt--;
	}
}

//*-----------------------------------------------------------------------------
//* 函数名称       : I2C_lineOpen (I2Cdesc *I2C_pt, unsigned int us)
//* Object         : I2C
//* 输入参数       : <I2C_pt> =  为I2C的描述指针,其结构定义在source.h中
//*                :us 为I2C中断的时中断时间长度,此参数表示I2C的SCL的频率
//* 输出变量       : 无
//* 功能           :初始化I2C的设备和定时器
//*-----------------------------------------------------------------------------
void I2C_lineOpen (I2Cdesc *I2C_pt, unsigned int us){
	/*****Set Up Paramter ***/
    int tmp;
	I2C_pt->deviceAddress = 0;
	/*****Set Up Line****/
    I2C_pt->state = OK;
    SDA_SET();;		//* set SDA line high
    SCL_SET();     //* set SCL line high
	/*****Setup Interrupt **/
	/*****Setup Timer **/
	I2C_pt->Timer_Gap_us=us;
	Init_I2c_Timer(us); 
	Start_I2c_Timer(us);  
}

⌨️ 快捷键说明

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